from datetime import datetime
from enum import Enum
from sqlalchemy import String, DateTime, ForeignKey, Text, Integer
from sqlalchemy.orm import Mapped, mapped_column, relationship

from app.core.database import Base


class GameStatus(str, Enum):
    PENDING = "pending"      # Предложена участником, ждёт модерации
    APPROVED = "approved"    # Одобрена организатором
    REJECTED = "rejected"    # Отклонена


class GameType(str, Enum):
    PLAYTHROUGH = "playthrough"  # Прохождение игры
    CHALLENGES = "challenges"    # Челленджи


class Game(Base):
    __tablename__ = "games"

    id: Mapped[int] = mapped_column(primary_key=True)
    marathon_id: Mapped[int] = mapped_column(ForeignKey("marathons.id", ondelete="CASCADE"), index=True)
    title: Mapped[str] = mapped_column(String(100), nullable=False)
    cover_path: Mapped[str | None] = mapped_column(String(500), nullable=True)
    download_url: Mapped[str] = mapped_column(Text, nullable=False)
    genre: Mapped[str | None] = mapped_column(String(50), nullable=True)
    status: Mapped[str] = mapped_column(String(20), default=GameStatus.PENDING.value)
    proposed_by_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="SET NULL"), nullable=True)
    approved_by_id: Mapped[int | None] = mapped_column(ForeignKey("users.id", ondelete="SET NULL"), nullable=True)
    created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)

    # Тип игры
    game_type: Mapped[str] = mapped_column(String(20), default=GameType.CHALLENGES.value, nullable=False)

    # Поля для типа "Прохождение" (заполняются только для playthrough)
    playthrough_points: Mapped[int | None] = mapped_column(Integer, nullable=True)
    playthrough_description: Mapped[str | None] = mapped_column(Text, nullable=True)
    playthrough_proof_type: Mapped[str | None] = mapped_column(String(20), nullable=True)  # screenshot, video, steam
    playthrough_proof_hint: Mapped[str | None] = mapped_column(Text, nullable=True)

    # Relationships
    marathon: Mapped["Marathon"] = relationship("Marathon", back_populates="games")
    proposed_by: Mapped["User"] = relationship(
        "User",
        back_populates="proposed_games",
        foreign_keys=[proposed_by_id]
    )
    approved_by: Mapped["User | None"] = relationship(
        "User",
        back_populates="approved_games",
        foreign_keys=[approved_by_id]
    )
    challenges: Mapped[list["Challenge"]] = relationship(
        "Challenge",
        back_populates="game",
        cascade="all, delete-orphan"
    )
    # Assignments для прохождений (playthrough)
    playthrough_assignments: Mapped[list["Assignment"]] = relationship(
        "Assignment",
        back_populates="game",
        foreign_keys="Assignment.game_id"
    )

    @property
    def is_approved(self) -> bool:
        return self.status == GameStatus.APPROVED.value

    @property
    def is_pending(self) -> bool:
        return self.status == GameStatus.PENDING.value

    @property
    def is_playthrough(self) -> bool:
        return self.game_type == GameType.PLAYTHROUGH.value

    @property
    def is_challenges(self) -> bool:
        return self.game_type == GameType.CHALLENGES.value
