"""Flexible, normalized credit system (section 7). A `Credit` links a `Person` to any media entity (movie, series, episode) with a department/job/character, instead of duplicating cast/crew as text blobs on the media tables. """ from __future__ import annotations import uuid from sqlalchemy import ForeignKey, Index, Integer, String from sqlalchemy.orm import Mapped, mapped_column, relationship from app.models.base import Base, TimestampMixin, UUIDPrimaryKeyMixin from app.models.person import Person from app.models.provider import Provider class Credit(Base, UUIDPrimaryKeyMixin, TimestampMixin): __tablename__ = "credits" __table_args__ = (Index("ix_credits_entity", "entity_type", "entity_id"),) person_id: Mapped[uuid.UUID] = mapped_column( ForeignKey("people.id", ondelete="CASCADE"), nullable=False ) # Polymorphic target: "movie", "series", "episode". entity_type: Mapped[str] = mapped_column(String(30), nullable=False) entity_id: Mapped[uuid.UUID] = mapped_column(nullable=False) # "Acting", "Directing", "Writing", "Production", "Camera", "Sound", # "Editing", "Art", "Costume & Make-Up", "Crew", "Creator", ... department: Mapped[str] = mapped_column(String(100), nullable=False) job: Mapped[str | None] = mapped_column(String(150), nullable=True) character: Mapped[str | None] = mapped_column(String(300), nullable=True) billing_order: Mapped[int | None] = mapped_column(Integer, nullable=True) is_guest: Mapped[bool] = mapped_column(default=False, nullable=False) episode_count: Mapped[int | None] = mapped_column(Integer, nullable=True) provider_id: Mapped[uuid.UUID | None] = mapped_column( ForeignKey("providers.id", ondelete="SET NULL"), nullable=True ) person: Mapped[Person] = relationship() provider: Mapped[Provider | None] = relationship() __all__ = ["Credit"]