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

from uuid import UUID

from sqlalchemy import select

from app.models.training import (
    Assessment,
    BodyComposition,
    Exercise,
    Measurement,
    NutritionPlan,
    PerformanceRecord,
    ProgressRecord,
    TrainingGoal,
    TrainingProgram,
    WorkoutAssignment,
    WorkoutPlan,
)
from app.repositories.base import TenantBaseRepository


class TrainingProgramRepository(TenantBaseRepository[TrainingProgram]):
    model = TrainingProgram


class ExerciseRepository(TenantBaseRepository[Exercise]):
    model = Exercise


class WorkoutPlanRepository(TenantBaseRepository[WorkoutPlan]):
    model = WorkoutPlan


class WorkoutAssignmentRepository(TenantBaseRepository[WorkoutAssignment]):
    model = WorkoutAssignment

    async def list_by_member(
        self, tenant_id: UUID, member_id: UUID, *, offset: int = 0, limit: int = 20
    ):
        stmt = (
            select(self.model)
            .where(
                self.model.tenant_id == tenant_id,
                self.model.member_id == member_id,
                self.model.is_deleted.is_(False),
            )
            .offset(offset)
            .limit(limit)
        )
        return (await self.session.execute(stmt)).scalars().all()


class TrainingGoalRepository(TenantBaseRepository[TrainingGoal]):
    model = TrainingGoal

    async def list_by_member(
        self, tenant_id: UUID, member_id: UUID, *, offset: int = 0, limit: int = 20
    ):
        stmt = (
            select(self.model)
            .where(
                self.model.tenant_id == tenant_id,
                self.model.member_id == member_id,
                self.model.is_deleted.is_(False),
            )
            .offset(offset)
            .limit(limit)
        )
        return (await self.session.execute(stmt)).scalars().all()


class MeasurementRepository(TenantBaseRepository[Measurement]):
    model = Measurement

    async def list_by_member(
        self, tenant_id: UUID, member_id: UUID, *, offset: int = 0, limit: int = 20
    ):
        stmt = (
            select(self.model)
            .where(
                self.model.tenant_id == tenant_id,
                self.model.member_id == member_id,
                self.model.is_deleted.is_(False),
            )
            .order_by(self.model.measured_at.desc())
            .offset(offset)
            .limit(limit)
        )
        return (await self.session.execute(stmt)).scalars().all()


class ProgressRecordRepository(TenantBaseRepository[ProgressRecord]):
    model = ProgressRecord


class NutritionPlanRepository(TenantBaseRepository[NutritionPlan]):
    model = NutritionPlan


class AssessmentRepository(TenantBaseRepository[Assessment]):
    model = Assessment


class PerformanceRecordRepository(TenantBaseRepository[PerformanceRecord]):
    model = PerformanceRecord


class BodyCompositionRepository(TenantBaseRepository[BodyComposition]):
    model = BodyComposition
