from __future__ import annotations

from fastapi import APIRouter, HTTPException, Request

from app.models import (
    ExerciseTemplateCreate,
    MeasurementEntryCreate,
    ProfileUpdate,
    RoutineCreate,
    WeightEntryCreate,
    WorkoutCreate,
)

router = APIRouter(prefix='/api')


@router.get('/health')
def health() -> dict[str, str]:
    return {'status': 'ok'}


@router.get('/dashboard/summary')
def dashboard_summary(request: Request) -> dict:
    return request.app.state.store.dashboard_summary()


@router.put('/profile/{user_id}')
def update_profile(user_id: str, payload: ProfileUpdate, request: Request) -> dict:
    try:
        return request.app.state.store.update_profile(user_id, payload.model_dump())
    except KeyError as exc:
        raise HTTPException(status_code=404, detail='Unknown user') from exc


@router.post('/profile/{user_id}/weight', status_code=201)
def add_weight_entry(user_id: str, payload: WeightEntryCreate, request: Request) -> dict:
    try:
        return request.app.state.store.add_weight_entry(user_id, payload.model_dump())
    except KeyError as exc:
        raise HTTPException(status_code=404, detail='Unknown user') from exc


@router.get('/history/{user_id}/weights')
def weight_history(user_id: str, request: Request) -> list[dict]:
    try:
        return request.app.state.store.weight_history(user_id)
    except KeyError as exc:
        raise HTTPException(status_code=404, detail='Unknown user') from exc


@router.post('/profile/{user_id}/measurements', status_code=201)
def add_measurement_entry(user_id: str, payload: MeasurementEntryCreate, request: Request) -> dict:
    try:
        return request.app.state.store.add_measurement_entry(user_id, payload.model_dump())
    except KeyError as exc:
        raise HTTPException(status_code=404, detail='Unknown user') from exc


@router.get('/history/{user_id}/measurements')
def measurement_history(user_id: str, request: Request) -> list[dict]:
    try:
        return request.app.state.store.measurement_history(user_id)
    except KeyError as exc:
        raise HTTPException(status_code=404, detail='Unknown user') from exc


@router.post('/exercises/templates', status_code=201)
def create_exercise_template(payload: ExerciseTemplateCreate, request: Request) -> dict:
    return request.app.state.store.add_exercise_template(payload.model_dump())


@router.get('/exercises/templates')
def list_exercise_templates(request: Request) -> list[dict]:
    return request.app.state.store.list_exercise_templates()


@router.post('/routines', status_code=201)
def create_routine(payload: RoutineCreate, request: Request) -> dict:
    try:
        return request.app.state.store.add_routine(payload.model_dump())
    except KeyError as exc:
        raise HTTPException(status_code=404, detail='Unknown user') from exc


@router.get('/routines')
def list_routines(request: Request) -> list[dict]:
    return request.app.state.store.list_routines()


@router.post('/workouts', status_code=201)
def add_workout(payload: WorkoutCreate, request: Request) -> dict:
    try:
        return request.app.state.store.add_workout(payload.model_dump())
    except KeyError as exc:
        raise HTTPException(status_code=404, detail='Unknown user') from exc


@router.get('/history/{user_id}/workouts')
def workout_history(user_id: str, request: Request) -> list[dict]:
    try:
        return request.app.state.store.workout_history(user_id)
    except KeyError as exc:
        raise HTTPException(status_code=404, detail='Unknown user') from exc


@router.get('/progression/{user_id}/exercises')
def exercise_progression(user_id: str, request: Request) -> list[dict]:
    try:
        return request.app.state.store.exercise_progression(user_id)
    except KeyError as exc:
        raise HTTPException(status_code=404, detail='Unknown user') from exc
