from datetime import datetime

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

from app.core.database import Base


class BossResult(Base):
    __tablename__ = "boss_results"
    __table_args__ = (
        UniqueConstraint("run_id", "boss_battle_id", name="uq_boss_results_run_boss"),
    )

    id: Mapped[int] = mapped_column(primary_key=True)
    run_id: Mapped[int] = mapped_column(
        ForeignKey("nuzlocke_runs.id", ondelete="CASCADE"), index=True
    )
    boss_battle_id: Mapped[int] = mapped_column(
        ForeignKey("boss_battles.id"), index=True
    )
    result: Mapped[str] = mapped_column(String(10))  # won, lost
    attempts: Mapped[int] = mapped_column(SmallInteger, default=1)
    completed_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))

    run: Mapped["NuzlockeRun"] = relationship(back_populates="boss_results")
    boss_battle: Mapped["BossBattle"] = relationship()

    def __repr__(self) -> str:
        return f"<BossResult(id={self.id}, run_id={self.run_id}, boss_battle_id={self.boss_battle_id}, result='{self.result}')>"
