"""Step 23 — interview analytics."""

from __future__ import annotations

from fastapi.testclient import TestClient

from app.config import get_settings
from app.main import app

client = TestClient(app)
HEADERS = {"X-Internal-Token": get_settings().internal_api_token}


def test_analytics_from_inline_sessions() -> None:
    res = client.post(
        "/api/v1/interview-analytics/analyze",
        headers=HEADERS,
        json={
            "include_persisted_sessions": False,
            "sessions": [
                {
                    "interview_id": "a1",
                    "role_title": "Backend Engineer",
                    "status": "completed",
                    "turn_count": 6,
                    "planned_question_count": 5,
                    "asked_question_count": 5,
                    "scores": [0.8, 0.7, 0.6, 0.9],
                    "question_stats": [
                        {
                            "question_id": "q1",
                            "question_text": "Caching?",
                            "score": 0.8,
                            "kind": "planned",
                        },
                        {
                            "question_id": "fu-1",
                            "score": 0.6,
                            "kind": "follow_up",
                        },
                    ],
                    "evaluation_degraded_count": 0,
                },
                {
                    "interview_id": "a2",
                    "status": "active",
                    "turn_count": 2,
                    "scores": [0.3],
                    "question_stats": [
                        {"question_id": "q1", "score": 0.3, "kind": "planned"}
                    ],
                    "evaluation_degraded_count": 1,
                },
            ],
        },
    )
    assert res.status_code == 200
    body = res.json()
    assert body["session_count"] == 2
    assert body["completion_rate"] == 0.5
    assert body["score_distribution"]["count"] == 5
    assert body["score_distribution"]["mean"] is not None
    assert body["question_effectiveness"]
    assert body["evaluation_quality"]["quality_band"] in {"high", "medium", "low"}
    assert body["summary"]


def test_analytics_empty() -> None:
    res = client.post(
        "/api/v1/interview-analytics/analyze",
        headers=HEADERS,
        json={"include_persisted_sessions": False, "sessions": []},
    )
    assert res.status_code == 200
    body = res.json()
    assert body["session_count"] == 0
    assert body["completion_rate"] == 0.0
