from datetime import datetime
from typing import Self
from pydantic import BaseModel, Field, model_validator

from app.models.game import GameType
from app.models.challenge import ProofType
from app.schemas.user import UserPublic


class GameBase(BaseModel):
    title: str = Field(..., min_length=1, max_length=100)
    download_url: str = Field(..., min_length=1)
    genre: str | None = Field(None, max_length=50)


class GameCreate(GameBase):
    cover_url: str | None = None

    # Тип игры
    game_type: GameType = GameType.CHALLENGES

    # Поля для типа "Прохождение"
    playthrough_points: int | None = Field(None, ge=1)
    playthrough_description: str | None = None
    playthrough_proof_type: ProofType | None = None
    playthrough_proof_hint: str | None = None

    @model_validator(mode='after')
    def validate_playthrough_fields(self) -> Self:
        if self.game_type == GameType.PLAYTHROUGH:
            if self.playthrough_points is None:
                raise ValueError('playthrough_points обязателен для типа "Прохождение"')
            if self.playthrough_description is None:
                raise ValueError('playthrough_description обязателен для типа "Прохождение"')
            if self.playthrough_proof_type is None:
                raise ValueError('playthrough_proof_type обязателен для типа "Прохождение"')
        return self


class GameUpdate(BaseModel):
    title: str | None = Field(None, min_length=1, max_length=100)
    download_url: str | None = None
    genre: str | None = None

    # Тип игры
    game_type: GameType | None = None

    # Поля для типа "Прохождение"
    playthrough_points: int | None = Field(None, ge=1)
    playthrough_description: str | None = None
    playthrough_proof_type: ProofType | None = None
    playthrough_proof_hint: str | None = None


class GameShort(BaseModel):
    id: int
    title: str
    cover_url: str | None = None
    download_url: str
    game_type: str = "challenges"

    class Config:
        from_attributes = True


class GameResponse(GameBase):
    id: int
    cover_url: str | None = None
    status: str = "pending"
    proposed_by: UserPublic | None = None
    approved_by: UserPublic | None = None
    challenges_count: int = 0
    created_at: datetime

    # Тип игры
    game_type: str = "challenges"

    # Поля для типа "Прохождение"
    playthrough_points: int | None = None
    playthrough_description: str | None = None
    playthrough_proof_type: str | None = None
    playthrough_proof_hint: str | None = None

    class Config:
        from_attributes = True


class PlaythroughInfo(BaseModel):
    """Информация о прохождении для игр типа playthrough"""
    description: str | None = None
    points: int | None = None
    proof_type: str | None = None
    proof_hint: str | None = None
