"""Movies — full-length adult films (paradisehill primary, mirrory z innych tube'ów).

Movies vs Scenes:
- Scena: 5-30min, single performance, jeden moment w czasie. Kanon: TPDB/StashDB.
- Movie: 60-180min, multi-chapter, full DVD/release. Kanon: paradisehill + mirrory.

Performers/Studios/Tags reusable — ten sam ludzie/studia mogą występować w obu.
"""
from __future__ import annotations

import uuid
from datetime import date, datetime

from sqlalchemy import (
    Date,
    DateTime,
    Float,
    ForeignKey,
    Integer,
    String,
    Text,
    UniqueConstraint,
    func,
)
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import Mapped, mapped_column

from app.models.base import Base, TimestampMixin, UUIDPKMixin


class Movie(UUIDPKMixin, TimestampMixin, Base):
    __tablename__ = "movies"

    title: Mapped[str] = mapped_column(String(512), nullable=False)
    title_normalized: Mapped[str] = mapped_column(String(512), nullable=False, index=True)
    slug: Mapped[str | None] = mapped_column(String(512), index=True)
    release_year: Mapped[int | None] = mapped_column(Integer, index=True)
    release_date: Mapped[date | None] = mapped_column(Date, index=True)
    studio_id: Mapped[uuid.UUID | None] = mapped_column(
        UUID(as_uuid=True), ForeignKey("studios.id", ondelete="SET NULL"), index=True
    )
    director: Mapped[str | None] = mapped_column(String(256))
    country: Mapped[str | None] = mapped_column(String(64))
    duration_sec: Mapped[int | None] = mapped_column(Integer)
    description: Mapped[str | None] = mapped_column(Text)
    poster_url: Mapped[str | None] = mapped_column(String(2048))
    backdrop_url: Mapped[str | None] = mapped_column(String(2048))
    rating: Mapped[float | None] = mapped_column(Float)


class MovieExternalRef(Base):
    __tablename__ = "movie_external_refs"

    source_id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True), ForeignKey("sources.id", ondelete="CASCADE"), primary_key=True
    )
    external_id: Mapped[str] = mapped_column(String(256), primary_key=True)
    movie_id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True), ForeignKey("movies.id", ondelete="CASCADE"), nullable=False, index=True
    )
    confidence: Mapped[float] = mapped_column(Float, nullable=False, default=1.0, server_default="1.0")
    url: Mapped[str | None] = mapped_column(String(1024))
    first_seen: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), server_default=func.now(), nullable=False
    )
    last_seen: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), server_default=func.now(), nullable=False
    )


class MoviePerformer(Base):
    __tablename__ = "movie_performers"

    movie_id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True), ForeignKey("movies.id", ondelete="CASCADE"), primary_key=True
    )
    performer_id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True), ForeignKey("performers.id", ondelete="CASCADE"), primary_key=True
    )
    role: Mapped[str | None] = mapped_column(String(64))
    position: Mapped[int | None] = mapped_column(Integer)
    as_alias: Mapped[str | None] = mapped_column(String(256))


class MovieTag(Base):
    __tablename__ = "movie_tags"

    movie_id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True), ForeignKey("movies.id", ondelete="CASCADE"), primary_key=True
    )
    tag_id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True), ForeignKey("tags.id", ondelete="CASCADE"), primary_key=True
    )
    source_id: Mapped[uuid.UUID | None] = mapped_column(
        UUID(as_uuid=True), ForeignKey("sources.id", ondelete="SET NULL")
    )


class MovieChapter(UUIDPKMixin, Base):
    """Optional chapter markers dla filmów (paradisehill czasem dzieli na sceny)."""

    __tablename__ = "movie_chapters"
    __table_args__ = (
        UniqueConstraint("movie_id", "chapter_index", name="uq_movie_chapters_movie_id_chapter_index"),
    )

    movie_id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True), ForeignKey("movies.id", ondelete="CASCADE"), nullable=False, index=True
    )
    chapter_index: Mapped[int] = mapped_column(Integer, nullable=False)
    title: Mapped[str | None] = mapped_column(String(512))
    start_sec: Mapped[int | None] = mapped_column(Integer)
    end_sec: Mapped[int | None] = mapped_column(Integer)
    scene_id: Mapped[uuid.UUID | None] = mapped_column(
        UUID(as_uuid=True), ForeignKey("scenes.id", ondelete="SET NULL")
    )
