from sqlalchemy import ForeignKey, SmallInteger, String
from sqlalchemy.orm import Mapped, mapped_column, relationship

from app.core.database import Base


class Game(Base):
    __tablename__ = "games"

    id: Mapped[int] = mapped_column(primary_key=True)
    name: Mapped[str] = mapped_column(String(100))
    slug: Mapped[str] = mapped_column(String(100), unique=True)
    generation: Mapped[int] = mapped_column(SmallInteger)
    region: Mapped[str] = mapped_column(String(50))
    category: Mapped[str | None] = mapped_column(String(20))  # original, remake, enhanced, sequel, spinoff
    box_art_url: Mapped[str | None] = mapped_column(String(500))
    release_year: Mapped[int | None] = mapped_column(SmallInteger)
    color: Mapped[str | None] = mapped_column(String(7))  # Hex color e.g. #FF0000
    version_group_id: Mapped[int | None] = mapped_column(
        ForeignKey("version_groups.id"), index=True
    )

    version_group: Mapped["VersionGroup | None"] = relationship(
        back_populates="games"
    )
    runs: Mapped[list["NuzlockeRun"]] = relationship(back_populates="game")

    def __repr__(self) -> str:
        return f"<Game(id={self.id}, name='{self.name}')>"
