from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, Text, Enum as SQLEnum
from sqlalchemy.orm import relationship
from datetime import datetime
import enum
from config.database import Base

class MediaType(enum.Enum):
    IMAGE = "image"
    VIDEO = "video"

class Album(Base):
    __tablename__ = "albums"
    
    id = Column(Integer, primary_key=True, index=True)
    title = Column(String, nullable=False)
    description = Column(Text)
    creator_id = Column(Integer, ForeignKey("users.id"), nullable=False)
    event_id = Column(Integer, ForeignKey("events.id"), nullable=True)
    cover_image = Column(String)
    created_at = Column(DateTime, default=datetime.utcnow)
    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    
    # Relationships
    creator = relationship("User", back_populates="albums")
    event = relationship("Event", back_populates="albums")
    media = relationship("Media", back_populates="album", cascade="all, delete-orphan")

class Media(Base):
    __tablename__ = "media"
    
    id = Column(Integer, primary_key=True, index=True)
    album_id = Column(Integer, ForeignKey("albums.id"), nullable=False)
    file_path = Column(String, nullable=False)
    thumbnail_path = Column(String)
    media_type = Column(SQLEnum(MediaType), nullable=False)
    caption = Column(Text)
    file_size = Column(Integer)  # in bytes
    width = Column(Integer)
    height = Column(Integer)
    duration = Column(Integer)  # in seconds for videos
    likes_count = Column(Integer, default=0)
    created_at = Column(DateTime, default=datetime.utcnow)
    
    # Relationships
    album = relationship("Album", back_populates="media")
    likes = relationship("MediaLike", back_populates="media", cascade="all, delete-orphan")

class MediaLike(Base):
    __tablename__ = "media_likes"
    
    id = Column(Integer, primary_key=True, index=True)
    media_id = Column(Integer, ForeignKey("media.id"), nullable=False)
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False)
    created_at = Column(DateTime, default=datetime.utcnow)
    
    # Relationships
    media = relationship("Media", back_populates="likes")
    user = relationship("User", back_populates="media_likes")
