"""Linki do odtwarzania movies — analog do PlaybackSource ale z movie_id zamiast scene_id.

Origin patterns (przykłady):
  - 'paradisehill'              — primary, full URL
  - 'psyplay:streamporn'        — psyplay-themed sites: streamporn/pandamovies/mangoporn
  - 'wp_movies:speedporn'       — generic wp themes: speedporn/eroticmv/...
"""
from __future__ import annotations

import uuid
from datetime import datetime

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

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


class MoviePlaybackSource(UUIDPKMixin, TimestampMixin, Base):
    __tablename__ = "movie_playback_sources"
    __table_args__ = (
        UniqueConstraint("origin", "page_url", name="uq_movie_playback_sources_origin_page_url"),
    )

    movie_id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True),
        ForeignKey("movies.id", ondelete="CASCADE"),
        nullable=False,
        index=True,
    )

    origin: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
    page_url: Mapped[str] = mapped_column(String(2048), nullable=False)
    embed_url: Mapped[str | None] = mapped_column(String(2048))
    stream_url: Mapped[str | None] = mapped_column(String(2048))
    quality: Mapped[str | None] = mapped_column(String(16))
    duration_sec: Mapped[int | None] = mapped_column(Integer)
    thumbnail_url: Mapped[str | None] = mapped_column(String(2048))
    animated_thumbnail_url: Mapped[str | None] = mapped_column(String(2048))

    last_seen_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), server_default=func.now(), nullable=False
    )

    dead_at: Mapped[datetime | None] = mapped_column(
        DateTime(timezone=True), nullable=True, index=True
    )
    dead_reason: Mapped[str | None] = mapped_column(String(512), nullable=True)
