"""Production companies, studios, networks, and distributors. Modeled as a single `Company` table distinguished by `company_type` per the spec (section 8), rather than a separate Network entity — a TV network and a studio share the same shape (name, logo, country, parent company).""" from __future__ import annotations import uuid from sqlalchemy import ForeignKey, String, Text from sqlalchemy.orm import Mapped, mapped_column, relationship from app.models.base import Base, TimestampMixin, UUIDPrimaryKeyMixin from app.models.enums import CompanyType class Company(Base, UUIDPrimaryKeyMixin, TimestampMixin): __tablename__ = "companies" name: Mapped[str] = mapped_column(String(300), nullable=False, index=True) company_type: Mapped[CompanyType] = mapped_column( default=CompanyType.PRODUCTION_COMPANY, nullable=False ) description: Mapped[str | None] = mapped_column(Text, nullable=True) country_code: Mapped[str | None] = mapped_column( ForeignKey("countries.code"), nullable=True ) headquarters: Mapped[str | None] = mapped_column(String(300), nullable=True) homepage: Mapped[str | None] = mapped_column(String(500), nullable=True) parent_company_id: Mapped[uuid.UUID | None] = mapped_column( ForeignKey("companies.id", ondelete="SET NULL"), nullable=True ) parent_company: Mapped[Company | None] = relationship(remote_side="Company.id") class MovieCompany(Base): __tablename__ = "movie_companies" movie_id: Mapped[uuid.UUID] = mapped_column( ForeignKey("movies.id", ondelete="CASCADE"), primary_key=True ) company_id: Mapped[uuid.UUID] = mapped_column( ForeignKey("companies.id", ondelete="CASCADE"), primary_key=True ) role: Mapped[str | None] = mapped_column(String(50), nullable=True) class SeriesCompany(Base): __tablename__ = "series_companies" series_id: Mapped[uuid.UUID] = mapped_column( ForeignKey("series.id", ondelete="CASCADE"), primary_key=True ) company_id: Mapped[uuid.UUID] = mapped_column( ForeignKey("companies.id", ondelete="CASCADE"), primary_key=True ) role: Mapped[str | None] = mapped_column(String(50), nullable=True) __all__ = ["Company", "MovieCompany", "SeriesCompany"]