from datetime import datetime

from pydantic import BaseModel


class AnswerSubmission(BaseModel):
    question_id: int
    user_answer: str


class AttemptSubmit(BaseModel):
    answers: list[AnswerSubmission]


class AnswerDetail(BaseModel):
    question_id: int
    question_text: str
    question_type: str
    options: list[str] | None = None
    user_answer: str
    correct_answer: str
    is_correct: bool
    explanation: str | None

    class Config:
        from_attributes = True


class AttemptResponse(BaseModel):
    id: int
    quiz_id: int
    score: int
    total_questions: int
    percentage: float
    started_at: datetime
    completed_at: datetime | None
    course_id: int | None = None
    allow_review: bool = True

    class Config:
        from_attributes = True


class AttemptDetail(AttemptResponse):
    answers: list[AnswerDetail] = []


# Stats schemas
class QuizStats(BaseModel):
    quiz_id: int
    quiz_title: str
    attempts_count: int
    best_score: float
    latest_score: float
    average_score: float


class DashboardStats(BaseModel):
    total_documents: int
    total_quizzes: int
    total_attempts: int
    average_score: float
    quiz_stats: list[QuizStats] = []
