"""
Pydantic schemas for public API.

This module defines request and response schemas for public API endpoints.
"""

from datetime import datetime
from typing import Optional, Any

from pydantic import BaseModel, Field, ConfigDict


class PublicMatchInfo(BaseModel):
    """Schema for match information in public API."""
    id: int = Field(..., description="Match ID")
    home_team: str = Field(..., description="Home team name")
    away_team: str = Field(..., description="Away team name")
    date: str = Field(..., description="Match date and time (ISO 8601)")
    league: str = Field(..., description="League name")
    status: str = Field(..., description="Match status")


class PublicPredictionResponse(BaseModel):
    """Schema for prediction response in public API."""
    id: int = Field(..., description="Prediction ID")
    match_id: int = Field(..., description="Match ID")
    match: PublicMatchInfo = 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: str = Field(..., description="Timestamp when prediction was created")


class PublicMatchResponse(BaseModel):
    """Schema for match response in public API."""
    id: int = Field(..., description="Match ID")
    home_team: str = Field(..., description="Home team name")
    away_team: str = Field(..., description="Away team name")
    date: str = Field(..., description="Match date and time (ISO 8601)")
    league: str = Field(..., description="League name")
    status: str = Field(..., description="Match status")


class SuccessMeta(BaseModel):
    """Schema for success response metadata."""
    total: Optional[int] = Field(None, description="Total number of items")
    limit: Optional[int] = Field(None, description="Number of items returned")
    offset: Optional[int] = Field(None, description="Number of items skipped")
    timestamp: str = Field(..., description="ISO 8601 timestamp of response")
    version: str = Field(default="v1", description="API version")


class SuccessResponse(BaseModel):
    """Schema for standardized success response."""
    data: Any = Field(..., description="Response data")
    meta: SuccessMeta = Field(..., description="Response metadata")


class ErrorResponse(BaseModel):
    """Schema for error response."""
    error: dict = Field(..., description="Error details")
    meta: dict = Field(..., description="Response metadata")


class ErrorDetail(BaseModel):
    """Schema for error detail."""
    code: str = Field(..., description="Error code")
    message: str = Field(..., description="Human-readable error message")
    details: Optional[dict] = Field(None, description="Additional error details")


class ApiKeyResponse(BaseModel):
    """Schema for API key response (includes plain key only on creation)."""
    id: int = Field(..., description="API key ID")
    user_id: int = Field(..., description="User ID")
    key_prefix: str = Field(..., description="First 8 characters of API key")
    is_active: bool = Field(..., description="Whether the key is active")
    rate_limit: int = Field(..., description="Rate limit per minute")
    last_used_at: Optional[str] = Field(None, description="Last usage timestamp")
    created_at: str = Field(..., description="Creation timestamp")
    api_key: Optional[str] = Field(None, description="Plain API key (only on creation)")


class ApiStatsResponse(BaseModel):
    """Schema for API usage statistics."""
    total_requests: int = Field(..., description="Total API requests made")
    requests_this_month: int = Field(..., description="Requests in current month")
    rate_limit: int = Field(..., description="Rate limit per minute")
    requests_remaining: int = Field(..., description="Requests remaining in rate limit window")
