"""Coach & Staff Management tests — Phase 9.3."""
from __future__ import annotations

import pytest

from app.events.publisher import get_event_publisher
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": "st93", "name": "Staff Center", "status": "active"},
        headers=headers,
    )
    assert center.status_code == 201
    center_id = center.json()["id"]
    coach = await client.post(
        "/api/v1/coaches",
        json={
            "sports_center_id": center_id,
            "code": "c1",
            "display_name": "Coach Ali",
            "staff_kind": "coach",
        },
        headers=headers,
    )
    assert coach.status_code == 201, coach.text
    return center_id, coach.json()["id"]


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

    cert = await client.post(
        "/api/v1/staff-certificates",
        json={
            "sports_center_id": center_id,
            "coach_id": coach_id,
            "name": "CPR",
            "issuer": "Red Cross",
        },
        headers=headers,
    )
    assert cert.status_code == 201, cert.text

    skill = await client.post(
        "/api/v1/staff-skills",
        json={
            "sports_center_id": center_id,
            "coach_id": coach_id,
            "skill_code": "yoga",
            "skill_name": "Yoga",
            "level": 3,
        },
        headers=headers,
    )
    assert skill.status_code == 201, skill.text

    events = get_event_publisher().published
    assert any(e.event_type == SportsCenterEventType.STAFF_CERTIFICATE_CREATED for e in events)
    assert any(e.event_type == SportsCenterEventType.STAFF_SKILL_CREATED for e in events)


@pytest.mark.asyncio
async def test_capabilities_phase_93(client):
    resp = await client.get("/capabilities")
    assert resp.status_code == 200
    data = resp.json()
    assert data["version"] == "0.9.7.0"
