from datetime import date
from typing import List, Optional

from rich.console import Console, ConsoleOptions, RenderResult
from rich.text import Text
from sqlmodel import Field, Relationship, SQLModel


class SpokenLanguageMovieLink(SQLModel, table=True):
    spoken_language_id: Optional[int] = Field(
        default=None, foreign_key="spoken_language.local_id", primary_key=True
    )
    movie_id: Optional[int] = Field(
        default=None, foreign_key="movie.local_id", primary_key=True
    )


class SpokenLanguage(SQLModel, table=True):
    __tablename__ = "spoken_language"
    local_id: Optional[int] = Field(default=None, primary_key=True)
    english_name: Optional[str] = None
    iso_639_1: Optional[str] = None
    name: str
    movies: List["Movie"] = Relationship(
        back_populates="spoken_languages", link_model=SpokenLanguageMovieLink
    )

    def __rich_repr__(self):
        yield self.name


class ProductionCountryMovieLink(SQLModel, table=True):
    production_country_id: Optional[int] = Field(
        default=None, foreign_key="production_country.local_id", primary_key=True
    )
    movie_id: Optional[int] = Field(
        default=None, foreign_key="movie.local_id", primary_key=True
    )


class ProductionCountry(SQLModel, table=True):
    __tablename__ = "production_country"
    local_id: Optional[int] = Field(default=None, primary_key=True)
    iso_3166_1: Optional[str] = None
    name: str
    movies: List["Movie"] = Relationship(
        back_populates="production_countries", link_model=ProductionCountryMovieLink
    )

    def __rich_repr__(self):
        yield self.name


class ProductionCompanyMovieLink(SQLModel, table=True):
    production_company_id: Optional[int] = Field(
        default=None, foreign_key="production_company.local_id", primary_key=True
    )
    movie_id: Optional[int] = Field(
        default=None, foreign_key="movie.local_id", primary_key=True
    )


class ProductionCompany(SQLModel, table=True):
    __tablename__ = "production_company"
    local_id: Optional[int] = 