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

from app.core.database import Base


class BossBattle(Base):
    __tablename__ = "boss_battles"
    __table_args__ = (
        UniqueConstraint("version_group_id", "order", name="uq_boss_battles_version_group_order"),
    )

    id: Mapped[int] = mapped_column(primary_key=True)
    version_group_id: Mapped[int] = mapped_column(
        ForeignKey("version_groups.id"), index=True
    )
    name: Mapped[str] = mapped_column(String(100))
    boss_type: Mapped[str] = mapped_column(String(20))  # gym_leader, elite_four, champion, rival, evil_team, other
    specialty_type: Mapped[str | None] = mapped_column(String(20), default=None)  # pokemon type specialty (e.g. rock, water)
    badge_name: Mapped[str | None] = mapped_column(String(100))
    badge_image_url: Mapped[str | None] = mapped_column(String(500))
    level_cap: Mapped[int] = mapped_column(SmallInteger)
    order: Mapped[int] = mapped_column(SmallInteger)
    after_route_id: Mapped[int | None] = mapped_column(
        ForeignKey("routes.id"), index=True, default=None
    )
    location: Mapped[str] = mapped_column(String(200))
    section: Mapped[str | None] = mapped_column(String(100), default=None)
    sprite_url: Mapped[str | None] = mapped_column(String(500))

    version_group: Mapped["VersionGroup"] = relationship(
        back_populates="boss_battles"
    )
    after_route: Mapped["Route | None"] = relationship()
    pokemon: Mapped[list["BossPokemon"]] = relationship(
        back_populates="boss_battle", cascade="all, delete-orphan"
    )

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