"""
Pydantic schemas for predictions.

This module defines request and response schemas for prediction-related operations.
"""

from datetime import datetime
from typing import Optional, Any

from pydantic import BaseModel, Field, ConfigDict


class PredictionBase(BaseModel):
    """Base schema for prediction data."""
    match_id: int = Field(..., description="Foreign key to matches table")
    energy_score: str = Field(..., min_length=1, max_length=50, description="Energy score for the prediction")
    confidence: str = Field(..., min_length=1, max_length=50, description="Confidence level of the prediction")
    predicted_winner: str = Field(..., min_length=1, max_length=255, description="Predicted winner team name")
    created_at: datetime = Field(..., description="Timestamp when prediction was created")


class PredictionCreate(PredictionBase):
    """Schema for creating a new prediction."""
    pass


class PredictionUpdate(BaseModel):
    """Schema for updating a prediction."""
    energy_score: Optional[str] = Field(None, min_length=1, max_length=50)
    confidence: Optional[str] = Field(None, min_length=1, max_length=50)
    predicted_winner: Optional[str] = Field(None, min_length=1, max_length=255)


class MatchInfo(BaseModel):
    """Schema for match information included in prediction response."""
    id: int = Field(..., description="Match ID")
    home_team: str = Field(..., description="Home team name")
    away_team: str = Field(..., description="Away team name")
    date: datetime = Field(..., description="Match date and time")
    league: str = Field(..., description="League name")
    status: str = Field(..., description="Match status")


class PredictionResponse(BaseModel):
    """Schema for prediction response with match details."""
    id: int = Field(..., description="Primary key")
    match_id: int = Field(..., description="Foreign key to matches table")
    match: MatchInfo = Field(..., description="Match details")
    energy_score: str = Field(..., description="Energy score for the prediction")
    confidence: str = Field(..., description="Confidence level of the prediction")
    predicted_winner: str = Field(..., description="Predicted winner team name")
    created_at: datetime = Field(..., description="Timestamp when prediction was created")
    model_config = ConfigDict(from_attributes=False)


class PredictionListMeta(BaseModel):
    """Schema for pagination metadata."""
    total: int = Field(..., description="Total number of predictions matching filters")
    limit: int = Field(..., description="Number of predictions returned")
    offset: int = Field(..., description="Number of predictions skipped")
    timestamp: str = Field(..., description="ISO 8601 timestamp of response")
    version: str = Field(default="v1", description="API version")


class PredictionListResponse(BaseModel):
    """Schema for a list of predictions with standardized metadata."""
    data: list[PredictionResponse] = Field(..., description="List of predictions")
    meta: PredictionListMeta = Field(..., description="Pagination and metadata")


class PredictionStatsResponse(BaseModel):
    """Schema for prediction statistics."""
    total_predictions: int = Field(..., description="Total number of predictions")
    predictions_by_confidence: dict = Field(..., description="Breakdown by confidence level")
    predictions_by_energy_score: dict = Field(..., description="Breakdown by energy score")
    avg_confidence: float = Field(..., description="Average confidence level")
    unique_matches_predicted: int = Field(..., description="Number of unique matches with predictions")
