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] = Field(default=None, primary_key=True)
    id: int
    name: str
    origin_country: Optional[str] = None
    logo_path: Optional[str] = None
    movies: List["Movie"] = Relationship(
        back_populates="production_companies", link_model=ProductionCompanyMovieLink
    )

    def __rich_repr__(self):
        yield self.name


class Collection(SQLModel, table=True):
    local_id: Optional[int] = Field(default=None, primary_key=True)
    id: int
    name: str
    poster_path: Optional[str] = None
    backdrop_path: Optional[str] = None
    movies: List["Movie"] = Relationship(back_populates="collection")

    def __rich_repr__(self):
        yield self.name


class GenreMovieLink(SQLModel, table=True):
    genre_id: Optional[int] = Field(
        default=None, foreign_key="genre.local_id", primary_key=True
    )
    movie_id: Optional[int] = Field(
        default=None, foreign_key="movie.local_id", primary_key=True
    )


class Genre(SQLModel, table=True):
    local_id: Optional[int] = Field(default=None, primary_key=True)
    id: int
    name: str
    movies: List["Movie"] = Relationship(
        back_populates="genres", link_model=GenreMovieLink
    )

    def __rich_repr__(self):
        yield self.name


class Movie(SQLModel, table=True):
    local_id: Optional[int] = Field(default=None, primary_key=True)
    adult: Optional[bool] = None
    backdrop_path: Optional[str] = None
    collection_id: Optional[int] = Field(
        default=None, foreign_key="collection.local_id"
    )
    collection: Optional[Collection] = Relationship(back_populates="movies")
    budget: Optional[int] = None
    genres: List[Genre] = Relationship(
        back_populates="movies", link_model=GenreMovieLink
    )
    homepage: Optional[str] = None
    id: int
    imdb_id: Optional[str] = None
    original_language: Optional[str] = None
    original_title: Optional[str] = None
    overview: Optional[str] = None
    popularity: Optional[float] = None
    poster_path: Optional[str] = None
    production_companies: List[ProductionCompany] = Relationship(
        back_populates="movies", link_model=ProductionCompanyMovieLink
    )
    production_countries: List[ProductionCountry] = Relationship(
        back_populates="movies", link_model=ProductionCountryMovieLink
    )
    release_date: Optional[date] = Field(None, index=True)
    revenue: Optional[int] = None
    runtime: Optional[int] = None
    spoken_languages: List[SpokenLanguage] = Relationship(
        back_populates="movies", link_model=SpokenLanguageMovieLink
    )
    status: Optional[str] = None
    tagline: Optional[str] = None
    title: str = Field(..., index=True)
    video: Optional[bool] = None
    vote_average: Optional[float] = None
    vote_count: Optional[int] = None

    def __rich_repr__(self):
        yield self.title
        yield "overview", self.overview
        yield "release_date", self.release_date
        yield "runtime", f"{self.runtime} min"
        yield "genres", self.genres
        yield "collection", self.collection
        yield "spoken_languages", self.spoken_languages
        yield "revenue", f"{self.revenue / 1e6:.1f}M"

    def __rich_console__(
        self, console: Console, options: ConsoleOptions
    ) -> RenderResult:

        yield Text(f"{self.title}", justify="center", style="bold magenta")

        release_date_str = self.release_date.strftime("%b %d, %Y")
        yield Text(f"Released: {release_date_str}")
        yield Text(f"Runtime: {self.runtime} min")

        genres_str = ", ".join([g.name for g in self.genres])
        yield Text(f"Genres: {genres_str}")

        return

sqlmodel.Relationship sqlmodel.Field rich.text.Text
[((297, 374), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'foreign_key': '"""spoken_language.local_id"""', 'primary_key': '(True)'}), "(default=None, foreign_key='spoken_language.local_id', primary_key=True)\n", (302, 374), False, 'from sqlmodel import Field, Relationship, SQLModel\n'), ((419, 486), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'foreign_key': '"""movie.local_id"""', 'primary_key': '(True)'}), "(default=None, foreign_key='movie.local_id', primary_key=True)\n", (424, 486), False, 'from sqlmodel import Field, Relationship, SQLModel\n'), ((615, 652), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'primary_key': '(True)'}), '(default=None, primary_key=True)\n', (620, 652), False, 'from sqlmodel import Field, Relationship, SQLModel\n'), ((770, 858), 'sqlmodel.Relationship', 'Relationship', ([], {'back_populates': '"""spoken_languages"""', 'link_model': 'SpokenLanguageMovieLink'}), "(back_populates='spoken_languages', link_model=\n    SpokenLanguageMovieLink)\n", (782, 858), False, 'from sqlmodel import Field, Relationship, SQLModel\n'), ((1023, 1108), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'foreign_key': '"""production_country.local_id"""', 'primary_key': '(True)'}), "(default=None, foreign_key='production_country.local_id', primary_key=True\n    )\n", (1028, 1108), False, 'from sqlmodel import Field, Relationship, SQLModel\n'), ((1148, 1215), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'foreign_key': '"""movie.local_id"""', 'primary_key': '(True)'}), "(default=None, foreign_key='movie.local_id', primary_key=True)\n", (1153, 1215), False, 'from sqlmodel import Field, Relationship, SQLModel\n'), ((1350, 1387), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'primary_key': '(True)'}), '(default=None, primary_key=True)\n', (1355, 1387), False, 'from sqlmodel import Field, Relationship, SQLModel\n'), ((1467, 1562), 'sqlmodel.Relationship', 'Relationship', ([], {'back_populates': '"""production_countries"""', 'link_model': 'ProductionCountryMovieLink'}), "(back_populates='production_countries', link_model=\n    ProductionCountryMovieLink)\n", (1479, 1562), False, 'from sqlmodel import Field, Relationship, SQLModel\n'), ((1727, 1812), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'foreign_key': '"""production_company.local_id"""', 'primary_key': '(True)'}), "(default=None, foreign_key='production_company.local_id', primary_key=True\n    )\n", (1732, 1812), False, 'from sqlmodel import Field, Relationship, SQLModel\n'), ((1852, 1919), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'foreign_key': '"""movie.local_id"""', 'primary_key': '(True)'}), "(default=None, foreign_key='movie.local_id', primary_key=True)\n", (1857, 1919), False, 'from sqlmodel import Field, Relationship, SQLModel\n'), ((2054, 2091), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'primary_key': '(True)'}), '(default=None, primary_key=True)\n', (2059, 2091), False, 'from sqlmodel import Field, Relationship, SQLModel\n'), ((2223, 2318), 'sqlmodel.Relationship', 'Relationship', ([], {'back_populates': '"""production_companies"""', 'link_model': 'ProductionCompanyMovieLink'}), "(back_populates='production_companies', link_model=\n    ProductionCompanyMovieLink)\n", (2235, 2318), False, 'from sqlmodel import Field, Relationship, SQLModel\n'), ((2454, 2491), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'primary_key': '(True)'}), '(default=None, primary_key=True)\n', (2459, 2491), False, 'from sqlmodel import Field, Relationship, SQLModel\n'), ((2624, 2665), 'sqlmodel.Relationship', 'Relationship', ([], {'back_populates': '"""collection"""'}), "(back_populates='collection')\n", (2636, 2665), False, 'from sqlmodel import Field, Relationship, SQLModel\n'), ((2796, 2863), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'foreign_key': '"""genre.local_id"""', 'primary_key': '(True)'}), "(default=None, foreign_key='genre.local_id', primary_key=True)\n", (2801, 2863), False, 'from sqlmodel import Field, Relationship, SQLModel\n'), ((2908, 2975), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'foreign_key': '"""movie.local_id"""', 'primary_key': '(True)'}), "(default=None, foreign_key='movie.local_id', primary_key=True)\n", (2913, 2975), False, 'from sqlmodel import Field, Relationship, SQLModel\n'), ((3057, 3094), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'primary_key': '(True)'}), '(default=None, primary_key=True)\n', (3062, 3094), False, 'from sqlmodel import Field, Relationship, SQLModel\n'), ((3149, 3213), 'sqlmodel.Relationship', 'Relationship', ([], {'back_populates': '"""genres"""', 'link_model': 'GenreMovieLink'}), "(back_populates='genres', link_model=GenreMovieLink)\n", (3161, 3213), False, 'from sqlmodel import Field, Relationship, SQLModel\n'), ((3349, 3386), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'primary_key': '(True)'}), '(default=None, primary_key=True)\n', (3354, 3386), False, 'from sqlmodel import Field, Relationship, SQLModel\n'), ((3495, 3549), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'foreign_key': '"""collection.local_id"""'}), "(default=None, foreign_key='collection.local_id')\n", (3500, 3549), False, 'from sqlmodel import Field, Relationship, SQLModel\n'), ((3603, 3640), 'sqlmodel.Relationship', 'Relationship', ([], {'back_populates': '"""movies"""'}), "(back_populates='movies')\n", (3615, 3640), False, 'from sqlmodel import Field, Relationship, SQLModel\n'), ((3700, 3764), 'sqlmodel.Relationship', 'Relationship', ([], {'back_populates': '"""movies"""', 'link_model': 'GenreMovieLink'}), "(back_populates='movies', link_model=GenreMovieLink)\n", (3712, 3764), False, 'from sqlmodel import Field, Relationship, SQLModel\n'), ((4109, 4185), 'sqlmodel.Relationship', 'Relationship', ([], {'back_populates': '"""movies"""', 'link_model': 'ProductionCompanyMovieLink'}), "(back_populates='movies', link_model=ProductionCompanyMovieLink)\n", (4121, 4185), False, 'from sqlmodel import Field, Relationship, SQLModel\n'), ((4252, 4328), 'sqlmodel.Relationship', 'Relationship', ([], {'back_populates': '"""movies"""', 'link_model': 'ProductionCountryMovieLink'}), "(back_populates='movies', link_model=ProductionCountryMovieLink)\n", (4264, 4328), False, 'from sqlmodel import Field, Relationship, SQLModel\n'), ((4378, 4401), 'sqlmodel.Field', 'Field', (['None'], {'index': '(True)'}), '(None, index=True)\n', (4383, 4401), False, 'from sqlmodel import Field, Relationship, SQLModel\n'), ((4515, 4588), 'sqlmodel.Relationship', 'Relationship', ([], {'back_populates': '"""movies"""', 'link_model': 'SpokenLanguageMovieLink'}), "(back_populates='movies', link_model=SpokenLanguageMovieLink)\n", (4527, 4588), False, 'from sqlmodel import Field, Relationship, SQLModel\n'), ((4687, 4709), 'sqlmodel.Field', 'Field', (['...'], {'index': '(True)'}), '(..., index=True)\n', (4692, 4709), False, 'from sqlmodel import Field, Relationship, SQLModel\n'), ((5322, 5383), 'rich.text.Text', 'Text', (['f"""{self.title}"""'], {'justify': '"""center"""', 'style': '"""bold magenta"""'}), "(f'{self.title}', justify='center', style='bold magenta')\n", (5326, 5383), False, 'from rich.text import Text\n'), ((5466, 5503), 'rich.text.Text', 'Text', (['f"""Released: {release_date_str}"""'], {}), "(f'Released: {release_date_str}')\n", (5470, 5503), False, 'from rich.text import Text\n'), ((5518, 5554), 'rich.text.Text', 'Text', (['f"""Runtime: {self.runtime} min"""'], {}), "(f'Runtime: {self.runtime} min')\n", (5522, 5554), False, 'from rich.text import Text\n'), ((5632, 5661), 'rich.text.Text', 'Text', (['f"""Genres: {genres_str}"""'], {}), "(f'Genres: {genres_str}')\n", (5636, 5661), False, 'from rich.text import Text\n')]