"""
Pydantic schemas for energy scores.

This module defines request and response schemas for energy score operations.
"""

from datetime import datetime
from typing import Optional, List
from pydantic import BaseModel, Field, ConfigDict


class EnergyScoreBase(BaseModel):
    """Base schema for energy score data."""
    match_id: int = Field(..., description="ID of the match")
    team_id: int = Field(..., description="ID of the team")
    score: float = Field(..., ge=0.0, le=100.0, description="Final energy score (0-100)")
    confidence: float = Field(default=0.0, ge=0.0, le=1.0, description="Confidence level (0-1)")
    sources_used: List[str] = Field(default_factory=list, description="List of sources used")


class EnergyScoreCreate(EnergyScoreBase):
    """Schema for creating a new energy score."""
    twitter_score: Optional[float] = Field(None, description="Energy score from Twitter component")
    reddit_score: Optional[float] = Field(None, description="Energy score from Reddit component")
    rss_score: Optional[float] = Field(None, description="Energy score from RSS component")
    temporal_factor: Optional[float] = Field(None, description="Temporal weighting factor applied")
    twitter_weight: Optional[float] = Field(None, description="Adjusted weight for Twitter")
    reddit_weight: Optional[float] = Field(None, description="Adjusted weight for Reddit")
    rss_weight: Optional[float] = Field(None, description="Adjusted weight for RSS")


class EnergyScoreUpdate(BaseModel):
    """Schema for updating an energy score."""
    score: Optional[float] = Field(None, ge=0.0, le=100.0, description="Final energy score (0-100)")
    confidence: Optional[float] = Field(None, ge=0.0, le=1.0, description="Confidence level (0-1)")
    sources_used: Optional[List[str]] = Field(None, description="List of sources used")
    twitter_score: Optional[float] = Field(None, description="Energy score from Twitter component")
    reddit_score: Optional[float] = Field(None, description="Energy score from Reddit component")
    rss_score: Optional[float] = Field(None, description="Energy score from RSS component")
    temporal_factor: Optional[float] = Field(None, description="Temporal weighting factor applied")
    twitter_weight: Optional[float] = Field(None, description="Adjusted weight for Twitter")
    reddit_weight: Optional[float] = Field(None, description="Adjusted weight for Reddit")
    rss_weight: Optional[float] = Field(None, description="Adjusted weight for RSS")


class EnergyScoreResponse(EnergyScoreCreate):
    """Schema for energy score response."""
    id: int = Field(..., description="Primary key")
    created_at: datetime = Field(..., description="Timestamp when energy score was calculated")
    updated_at: datetime = Field(..., description="Timestamp when energy score was last updated")
    model_config = ConfigDict(from_attributes=True)


class EnergyScoreCalculationRequest(BaseModel):
    """Schema for requesting energy score calculation."""
    match_id: int = Field(..., description="ID of the match")
    team_id: int = Field(..., description="ID of the team")
    twitter_sentiments: Optional[List[dict]] = Field(None, description="List of Twitter sentiment scores")
    reddit_sentiments: Optional[List[dict]] = Field(None, description="List of Reddit sentiment scores")
    rss_sentiments: Optional[List[dict]] = Field(None, description="List of RSS sentiment scores")
    tweets_with_timestamps: Optional[List[dict]] = Field(None, description="List of tweets with timestamps")


class EnergyScoreCalculationResponse(BaseModel):
    """Schema for energy score calculation response."""
    score: float = Field(..., ge=0.0, le=100.0, description="Final energy score (0-100)")
    confidence: float = Field(..., ge=0.0, le=1.0, description="Confidence level (0-1)")
    sources_used: List[str] = Field(..., description="List of sources used")
    twitter_score: Optional[float] = Field(None, description="Energy score from Twitter component")
    reddit_score: Optional[float] = Field(None, description="Energy score from Reddit component")
    rss_score: Optional[float] = Field(None, description="Energy score from RSS component")
    temporal_factor: Optional[float] = Field(None, description="Temporal weighting factor applied")


class EnergyScoreListResponse(BaseModel):
    """Schema for a list of energy scores."""
    data: List[EnergyScoreResponse] = Field(..., description="List of energy scores")
    count: int = Field(..., description="Total number of energy scores")
    meta: dict = Field(default_factory=dict, description="Additional metadata")


class EnergyScoreQueryParams(BaseModel):
    """Schema for energy score query parameters."""
    match_id: Optional[int] = Field(None, description="Filter by match ID")
    team_id: Optional[int] = Field(None, description="Filter by team ID")
    min_score: Optional[float] = Field(None, ge=0.0, le=100.0, description="Filter by minimum score")
    max_score: Optional[float] = Field(None, ge=0.0, le=100.0, description="Filter by maximum score")
    min_confidence: Optional[float] = Field(None, ge=0.0, le=1.0, description="Filter by minimum confidence")
    source: Optional[str] = Field(None, description="Filter by source used")
    limit: int = Field(default=10, ge=1, le=100, description="Maximum number of results")
    offset: int = Field(default=0, ge=0, description="Offset for pagination")
