from datetime import datetime
from pydantic import BaseModel, Field

from app.models.challenge import ChallengeType, Difficulty, ProofType, ChallengeStatus
from app.schemas.game import GameShort


class ProposedByUser(BaseModel):
    """Minimal user info for proposed challenges"""
    id: int
    nickname: str

    class Config:
        from_attributes = True


class ChallengeBase(BaseModel):
    title: str = Field(..., min_length=1, max_length=100)
    description: str = Field(..., min_length=1)
    type: ChallengeType
    difficulty: Difficulty
    points: int = Field(..., ge=1, le=500)
    estimated_time: int | None = Field(None, ge=1)  # minutes
    proof_type: ProofType
    proof_hint: str | None = None


class ChallengeCreate(ChallengeBase):
    pass


class ChallengeUpdate(BaseModel):
    title: str | None = Field(None, min_length=1, max_length=100)
    description: str | None = None
    type: ChallengeType | None = None
    difficulty: Difficulty | None = None
    points: int | None = Field(None, ge=1, le=500)
    estimated_time: int | None = None
    proof_type: ProofType | None = None
    proof_hint: str | None = None


class ChallengeResponse(ChallengeBase):
    id: int
    game: GameShort
    is_generated: bool
    created_at: datetime
    status: str = "approved"
    proposed_by: ProposedByUser | None = None

    class Config:
        from_attributes = True


class ChallengePropose(ChallengeBase):
    """Schema for proposing a challenge by a participant"""
    pass


class ChallengeGenerated(BaseModel):
    """Schema for GPT-generated challenges"""
    title: str
    description: str
    type: str
    difficulty: str
    points: int
    estimated_time: int | None = None
    proof_type: str
    proof_hint: str | None = None


class ChallengePreview(BaseModel):
    """Schema for challenge preview (with game info)"""
    game_id: int
    game_title: str
    title: str
    description: str
    type: str
    difficulty: str
    points: int
    estimated_time: int | None = None
    proof_type: str
    proof_hint: str | None = None


class ChallengesPreviewResponse(BaseModel):
    """Response with generated challenges for preview"""
    challenges: list[ChallengePreview]


class ChallengeSaveItem(BaseModel):
    """Single challenge to save"""
    game_id: int
    title: str
    description: str
    type: str
    difficulty: str
    points: int
    estimated_time: int | None = None
    proof_type: str
    proof_hint: str | None = None


class ChallengesSaveRequest(BaseModel):
    """Request to save previewed challenges"""
    challenges: list[ChallengeSaveItem]


class ChallengesGenerateRequest(BaseModel):
    """Request to generate challenges for specific games"""
    game_ids: list[int] | None = None  # If None, generate for all approved games without challenges
