"""
Analytics Pydantic schemas for TCO (Total Cost of Ownership) API responses.
"""

from typing import List, Optional, Dict, Any
from pydantic import BaseModel, Field


class TCOResponse(BaseModel):
    """Response schema for a single TCO category breakdown."""
    category_id: int = Field(..., description="Cost category ID")
    category_code: str = Field(..., description="Category code (e.g., 'FUEL', 'MAINTENANCE')")
    category_name: str = Field(..., description="Human-readable category name")
    amount: float = Field(..., description="Total amount in original currency")
    currency: str = Field(..., description="Original currency code (e.g., 'EUR', 'HUF')")
    amount_huf: float = Field(..., description="Amount converted to HUF")
    percentage: float = Field(..., description="Percentage of total cost (0-100)")

    class Config:
        from_attributes = True


class TCOSummaryStats(BaseModel):
    """Statistics part of the TCO summary."""
    total_cost: float = Field(..., description="Total cost in HUF")
    cost_per_km: Optional[float] = Field(None, description="Cost per kilometer (HUF/km)")
    total_transactions: int = Field(..., description="Number of cost transactions")
    date_range: Optional[Dict[str, str]] = Field(None, description="Start and end dates if filtered")


class TCOSummaryResponse(BaseModel):
    """Complete TCO summary for a vehicle."""
    vehicle_id: int = Field(..., description="Vehicle ID")
    user_tco: List[TCOResponse] = Field(..., description="TCO breakdown for the current user/organization")
    lifetime_tco: List[TCOResponse] = Field(..., description="Lifetime TCO across all owners (anonymized)")
    benchmark_tco: List[TCOResponse] = Field(..., description="Benchmark TCO for similar vehicles")
    stats: TCOSummaryStats = Field(..., description="Aggregated statistics")

    class Config:
        from_attributes = True


class TCOErrorResponse(BaseModel):
    """Error response for TCO endpoints."""
    detail: str = Field(..., description="Error description")
    vehicle_id: Optional[int] = Field(None, description="Related vehicle ID if applicable")


class DashboardMonthlyCost(BaseModel):
    """Monthly cost data for dashboard charts."""
    month: str = Field(..., description="Month abbreviation (e.g., 'Jan', 'Feb')")
    maintenance: float = Field(..., description="Maintenance costs")
    fuel: float = Field(..., description="Fuel costs")
    insurance: float = Field(..., description="Insurance costs")
    total: float = Field(..., description="Total monthly cost")
    
    class Config:
        from_attributes = True


class DashboardFuelEfficiency(BaseModel):
    """Fuel efficiency trend data."""
    month: str = Field(..., description="Month abbreviation")
    efficiency: float = Field(..., description="Fuel efficiency in km per liter")
    
    class Config:
        from_attributes = True


class DashboardCostPerKm(BaseModel):
    """Cost per km trend data."""
    month: str = Field(..., description="Month abbreviation")
    cost: float = Field(..., description="Cost per kilometer")
    
    class Config:
        from_attributes = True


class DashboardFunFacts(BaseModel):
    """Fun facts for dashboard."""
    total_km_driven: float = Field(..., description="Total kilometers driven")
    total_trees_saved: int = Field(..., description="Total trees saved (eco metric)")
    total_co2_saved: float = Field(..., description="Total CO2 saved in tons")
    total_money_saved: float = Field(..., description="Total money saved in EUR")
    moon_trips: int = Field(..., description="Number of moon trips equivalent")
    earth_circuits: int = Field(..., description="Number of Earth circuits equivalent")
    
    class Config:
        from_attributes = True


class DashboardBusinessMetrics(BaseModel):
    """Business metrics for fleet management."""
    fleet_size: int = Field(..., description="Number of vehicles in fleet")
    average_vehicle_age: float = Field(..., description="Average vehicle age in years")
    total_monthly_cost: float = Field(..., description="Total monthly cost for fleet")
    average_cost_per_km: float = Field(..., description="Average cost per kilometer")
    utilization_rate: float = Field(..., description="Fleet utilization rate in percentage")
    downtime_hours: int = Field(..., description="Total downtime hours per month")
    
    class Config:
        from_attributes = True


class DashboardResponse(BaseModel):
    """Complete dashboard data response."""
    monthly_costs: List[DashboardMonthlyCost] = Field(..., description="Monthly cost breakdown")
    fuel_efficiency_trends: List[DashboardFuelEfficiency] = Field(..., description="Fuel efficiency trends")
    cost_per_km_trends: List[DashboardCostPerKm] = Field(..., description="Cost per km trends")
    fun_facts: DashboardFunFacts = Field(..., description="Fun facts and eco metrics")
    business_metrics: DashboardBusinessMetrics = Field(..., description="Business metrics")
    
    class Config:
        from_attributes = True