"""Training Management schemas — Phase 9.6."""
from __future__ import annotations

from datetime import date, datetime
from uuid import UUID

from pydantic import BaseModel, Field

from app.models.types import AssessmentKind, GoalStatus, ProgramStatus, WorkoutStatus
from app.schemas.common import ORMBase


class TrainingProgramCreate(BaseModel):
    sports_center_id: UUID
    code: str = Field(max_length=50)
    name: str = Field(max_length=255)
    description: str | None = None
    sport_id: UUID | None = None
    coach_id: UUID | None = None
    status: ProgramStatus = ProgramStatus.DRAFT
    duration_weeks: int | None = None
    metadata_json: dict | None = None


class TrainingProgramUpdate(BaseModel):
    name: str | None = Field(default=None, max_length=255)
    description: str | None = None
    sport_id: UUID | None = None
    coach_id: UUID | None = None
    status: ProgramStatus | None = None
    duration_weeks: int | None = None
    metadata_json: dict | None = None
    version: int | None = None


class TrainingProgramRead(ORMBase):
    id: UUID
    tenant_id: UUID
    sports_center_id: UUID
    code: str
    name: str
    description: str | None
    sport_id: UUID | None
    coach_id: UUID | None
    status: ProgramStatus
    duration_weeks: int | None
    metadata_json: dict | None
    version: int
    is_deleted: bool


class ExerciseCreate(BaseModel):
    sports_center_id: UUID
    code: str = Field(max_length=50)
    name: str = Field(max_length=255)
    description: str | None = None
    muscle_group: str | None = Field(default=None, max_length=100)
    equipment_ref: str | None = Field(default=None, max_length=100)
    default_sets: int | None = None
    default_reps: int | None = None
    default_duration_seconds: int | None = None
    metadata_json: dict | None = None


class ExerciseUpdate(BaseModel):
    name: str | None = Field(default=None, max_length=255)
    description: str | None = None
    muscle_group: str | None = Field(default=None, max_length=100)
    equipment_ref: str | None = Field(default=None, max_length=100)
    default_sets: int | None = None
    default_reps: int | None = None
    default_duration_seconds: int | None = None
    metadata_json: dict | None = None


class ExerciseRead(ORMBase):
    id: UUID
    tenant_id: UUID
    sports_center_id: UUID
    code: str
    name: str
    description: str | None
    muscle_group: str | None
    equipment_ref: str | None
    default_sets: int | None
    default_reps: int | None
    default_duration_seconds: int | None
    metadata_json: dict | None
    is_deleted: bool


class WorkoutPlanCreate(BaseModel):
    sports_center_id: UUID
    program_id: UUID | None = None
    code: str = Field(max_length=50)
    name: str = Field(max_length=255)
    description: str | None = None
    coach_id: UUID | None = None
    status: WorkoutStatus = WorkoutStatus.DRAFT
    exercises: list | None = None
    metadata_json: dict | None = None


class WorkoutPlanUpdate(BaseModel):
    name: str | None = Field(default=None, max_length=255)
    description: str | None = None
    coach_id: UUID | None = None
    status: WorkoutStatus | None = None
    exercises: list | None = None
    metadata_json: dict | None = None
    version: int | None = None


class WorkoutPlanRead(ORMBase):
    id: UUID
    tenant_id: UUID
    sports_center_id: UUID
    program_id: UUID | None
    code: str
    name: str
    description: str | None
    coach_id: UUID | None
    status: WorkoutStatus
    exercises: list | None
    metadata_json: dict | None
    version: int
    is_deleted: bool


class WorkoutAssignmentCreate(BaseModel):
    sports_center_id: UUID
    workout_plan_id: UUID
    member_id: UUID
    coach_id: UUID | None = None
    starts_on: date | None = None
    ends_on: date | None = None
    status: WorkoutStatus = WorkoutStatus.ACTIVE
    notes: str | None = None


class WorkoutAssignmentRead(ORMBase):
    id: UUID
    tenant_id: UUID
    sports_center_id: UUID
    workout_plan_id: UUID
    member_id: UUID
    coach_id: UUID | None
    starts_on: date | None
    ends_on: date | None
    status: WorkoutStatus
    notes: str | None
    is_deleted: bool


class TrainingGoalCreate(BaseModel):
    sports_center_id: UUID
    member_id: UUID
    program_id: UUID | None = None
    title: str = Field(max_length=255)
    description: str | None = None
    target_value: float | None = None
    current_value: float | None = None
    unit: str | None = Field(default=None, max_length=50)
    target_date: date | None = None
    status: GoalStatus = GoalStatus.ACTIVE


class TrainingGoalRead(ORMBase):
    id: UUID
    tenant_id: UUID
    sports_center_id: UUID
    member_id: UUID
    program_id: UUID | None
    title: str
    description: str | None
    target_value: float | None
    current_value: float | None
    unit: str | None
    target_date: date | None
    status: GoalStatus
    is_deleted: bool


class MeasurementCreate(BaseModel):
    sports_center_id: UUID
    member_id: UUID
    metric_code: str = Field(max_length=50)
    metric_name: str = Field(max_length=255)
    value: float
    unit: str | None = Field(default=None, max_length=50)
    measured_at: datetime
    recorded_by_coach_id: UUID | None = None
    notes: str | None = None


class MeasurementRead(ORMBase):
    id: UUID
    tenant_id: UUID
    sports_center_id: UUID
    member_id: UUID
    metric_code: str
    metric_name: str
    value: float
    unit: str | None
    measured_at: datetime
    recorded_by_coach_id: UUID | None
    notes: str | None
    is_deleted: bool


class ProgressRecordCreate(BaseModel):
    sports_center_id: UUID
    member_id: UUID
    goal_id: UUID | None = None
    workout_assignment_id: UUID | None = None
    recorded_at: datetime
    summary: str | None = None
    metrics: dict | None = None


class ProgressRecordRead(ORMBase):
    id: UUID
    tenant_id: UUID
    sports_center_id: UUID
    member_id: UUID
    goal_id: UUID | None
    workout_assignment_id: UUID | None
    recorded_at: datetime
    summary: str | None
    metrics: dict | None
    is_deleted: bool


class NutritionPlanCreate(BaseModel):
    sports_center_id: UUID
    member_id: UUID
    coach_id: UUID | None = None
    title: str = Field(max_length=255)
    description: str | None = None
    starts_on: date | None = None
    ends_on: date | None = None
    daily_targets: dict | None = None
    is_active: bool = True


class NutritionPlanRead(ORMBase):
    id: UUID
    tenant_id: UUID
    sports_center_id: UUID
    member_id: UUID
    coach_id: UUID | None
    title: str
    description: str | None
    starts_on: date | None
    ends_on: date | None
    daily_targets: dict | None
    is_active: bool
    is_deleted: bool


class AssessmentCreate(BaseModel):
    sports_center_id: UUID
    member_id: UUID
    coach_id: UUID | None = None
    assessment_kind: AssessmentKind
    title: str = Field(max_length=255)
    assessed_at: datetime
    score: float | None = None
    results: dict | None = None
    notes: str | None = None


class AssessmentRead(ORMBase):
    id: UUID
    tenant_id: UUID
    sports_center_id: UUID
    member_id: UUID
    coach_id: UUID | None
    assessment_kind: AssessmentKind
    title: str
    assessed_at: datetime
    score: float | None
    results: dict | None
    notes: str | None
    is_deleted: bool


class PerformanceRecordCreate(BaseModel):
    sports_center_id: UUID
    member_id: UUID
    exercise_id: UUID | None = None
    recorded_at: datetime
    value: float
    unit: str | None = Field(default=None, max_length=50)
    notes: str | None = None


class PerformanceRecordRead(ORMBase):
    id: UUID
    tenant_id: UUID
    sports_center_id: UUID
    member_id: UUID
    exercise_id: UUID | None
    recorded_at: datetime
    value: float
    unit: str | None
    notes: str | None
    is_deleted: bool


class BodyCompositionCreate(BaseModel):
    sports_center_id: UUID
    member_id: UUID
    measured_at: datetime
    weight_kg: float | None = None
    body_fat_percent: float | None = None
    muscle_mass_kg: float | None = None
    bmi: float | None = None
    metrics: dict | None = None
    notes: str | None = None


class BodyCompositionRead(ORMBase):
    id: UUID
    tenant_id: UUID
    sports_center_id: UUID
    member_id: UUID
    measured_at: datetime
    weight_kg: float | None
    body_fat_percent: float | None
    muscle_mass_kg: float | None
    bmi: float | None
    metrics: dict | None
    notes: str | None
    is_deleted: bool
