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

from datetime import datetime, timezone

import pytest

from app.events.types import SportsCenterEventType
from app.tests.conftest import TENANT_A, tenant_headers


async def _seed(client, headers):
    center = await client.post(
        "/api/v1/sports-centers",
        json={"code": "tr96", "name": "Training Center", "status": "active"},
        headers=headers,
    )
    assert center.status_code == 201
    center_id = center.json()["id"]
    member = await client.post(
        "/api/v1/members",
        json={"sports_center_id": center_id, "display_name": "Train Member", "status": "active"},
        headers=headers,
    )
    assert member.status_code == 201
    return center_id, member.json()["id"]


@pytest.mark.asyncio
async def test_training_happy_path(client):
    headers = tenant_headers(TENANT_A)
    center_id, member_id = await _seed(client, headers)

    program = await client.post(
        "/api/v1/training-programs",
        json={"sports_center_id": center_id, "code": "prog1", "name": "Strength"},
        headers=headers,
    )
    assert program.status_code == 201, program.text

    exercise = await client.post(
        "/api/v1/exercises",
        json={"sports_center_id": center_id, "code": "sq", "name": "Squat"},
        headers=headers,
    )
    assert exercise.status_code == 201, exercise.text

    plan = await client.post(
        "/api/v1/workout-plans",
        json={
            "sports_center_id": center_id,
            "code": "wp1",
            "name": "Week 1",
            "program_id": program.json()["id"],
        },
        headers=headers,
    )
    assert plan.status_code == 201, plan.text

    assignment = await client.post(
        "/api/v1/workout-assignments",
        json={
            "sports_center_id": center_id,
            "workout_plan_id": plan.json()["id"],
            "member_id": member_id,
        },
        headers=headers,
    )
    assert assignment.status_code == 201, assignment.text

    goal = await client.post(
        "/api/v1/training-goals",
        json={
            "sports_center_id": center_id,
            "member_id": member_id,
            "title": "Lose 5kg",
            "target_value": 5,
            "unit": "kg",
        },
        headers=headers,
    )
    assert goal.status_code == 201, goal.text

    measurement = await client.post(
        "/api/v1/measurements",
        json={
            "sports_center_id": center_id,
            "member_id": member_id,
            "metric_code": "weight",
            "metric_name": "Weight",
            "value": 80.5,
            "unit": "kg",
            "measured_at": datetime.now(timezone.utc).isoformat(),
        },
        headers=headers,
    )
    assert measurement.status_code == 201, measurement.text


@pytest.mark.asyncio
async def test_capabilities_phase_96(client):
    resp = await client.get("/capabilities")
    data = resp.json()
    assert data["phase"] == "9.6"
    assert data["features"]["training_management"] is True
    assert data["features"]["booking_engine"] is True
    assert data["features"]["attendance_engine"] is True
