from datetime import datetime
from uuid import UUID

from pydantic import BaseModel, Field


class ActivityMetricsResponse(BaseModel):
    model_config = {"from_attributes": True}

    tss: float | None = None
    normalized_power: float | None = None
    intensity_factor: float | None = None
    variability_index: float | None = None
    avg_power: float | None = None
    max_power: int | None = None
    avg_hr: int | None = None
    max_hr: int | None = None
    avg_cadence: int | None = None
    avg_speed: float | None = None
    calories: int | None = None


class IntervalResponse(BaseModel):
    model_config = {"from_attributes": True}

    id: UUID
    start_ts: datetime
    end_ts: datetime
    interval_type: str
    avg_power: float | None = None
    avg_hr: int | None = None
    duration: int | None = None


class ActivityResponse(BaseModel):
    model_config = {"from_attributes": True}

    id: UUID
    rider_id: UUID
    name: str | None = None
    activity_type: str
    date: datetime
    duration: int
    distance: float | None = None
    elevation_gain: float | None = None
    metrics: ActivityMetricsResponse | None = None
    intervals: list[IntervalResponse] = []
    exercise_sets: list[dict] | None = None
    training_plan_id: UUID | None = None
    plan_week: int | None = None
    plan_day: str | None = None


class ActivityListResponse(BaseModel):
    items: list[ActivityResponse]
    total: int


class DataPointResponse(BaseModel):
    model_config = {"from_attributes": True}

    timestamp: datetime
    power: int | None = None
    heart_rate: int | None = None
    cadence: int | None = None
    speed: float | None = None
    latitude: float | None = None
    longitude: float | None = None
    altitude: float | None = None
    temperature: int | None = None


class ZoneItem(BaseModel):
    zone: int
    name: str
    seconds: int
    percentage: float


class PowerZoneItem(ZoneItem):
    min_watts: int
    max_watts: int | None = None


class HrZoneItem(ZoneItem):
    min_bpm: int
    max_bpm: int | None = None


class ZonesResponse(BaseModel):
    power_zones: list[PowerZoneItem] = []
    hr_zones: list[HrZoneItem] = []


class PowerCurveResponse(BaseModel):
    curve: dict[int, int]  # {duration_seconds: max_power}
