from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, Enum
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
from database import Base
import enum


class EpisodeStatus(str, enum.Enum):
    PLANNING = "planning"
    IN_PROGRESS = "in_progress"
    ON_HOLD = "on_hold"
    COMPLETED = "completed"
    CANCELLED = "cancelled"


class Episode(Base):
    __tablename__ = "episodes"

    id = Column(Integer, primary_key=True, index=True)
    project_id = Column(Integer, ForeignKey("projects.id"), nullable=False, index=True)
    name = Column(String, nullable=False, index=True)
    description = Column(String)
    episode_number = Column(Integer, nullable=False)
    status = Column(Enum(EpisodeStatus), nullable=False, default=EpisodeStatus.PLANNING)
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now())

    # Relationships
    project = relationship("Project", back_populates="episodes")
    shots = relationship("Shot", back_populates="episode", cascade="all, delete-orphan")
    tasks = relationship("Task", back_populates="episode", cascade="all, delete-orphan")

    def __repr__(self):
        return f"<Episode(id={self.id}, name='{self.name}', episode_number={self.episode_number})>"