# grantflow/tests/test_integration_quality_metrics.py

from fastapi.testclient import TestClient


from grantflow.api import runtime_state

from grantflow.api.app import app

client = TestClient(app)


def test_metrics_endpoint_derives_timeline_metrics_from_events():
    job_id = "test-job-metrics-1"
    runtime_state.JOB_STORE.set(
        job_id,
        {
            "status": "done",
            "hitl_enabled": True,
            "job_events": [
                {
                    "event_id": "e1",
                    "ts": "2026-02-24T10:00:00+00:00",
                    "type": "status_changed",
                    "from_status": None,
                    "to_status": "accepted",
                    "status": "accepted",
                },
                {
                    "event_id": "e2",
                    "ts": "2026-02-24T10:00:10+00:00",
                    "type": "status_changed",
                    "from_status": "accepted",
                    "to_status": "running",
                    "status": "running",
                },
                {
                    "event_id": "e3",
                    "ts": "2026-02-24T10:01:00+00:00",
                    "type": "status_changed",
                    "from_status": "running",
                    "to_status": "pending_hitl",
                    "status": "pending_hitl",
                },
                {
                    "event_id": "e4",
                    "ts": "2026-02-24T10:05:00+00:00",
                    "type": "resume_requested",
                    "status": "accepted",
                    "resuming_from": "mel",
                },
                {
                    "event_id": "e5",
                    "ts": "2026-02-24T10:05:05+00:00",
                    "type": "status_changed",
                    "from_status": "pending_hitl",
                    "to_status": "accepted",
                    "status": "accepted",
                },
                {
                    "event_id": "e6",
                    "ts": "2026-02-24T10:05:06+00:00",
                    "type": "status_changed",
                    "from_status": "accepted",
                    "to_status": "running",
                    "status": "running",
                },
                {
                    "event_id": "e7",
                    "ts": "2026-02-24T10:06:00+00:00",
                    "type": "status_changed",
                    "from_status": "running",
                    "to_status": "done",
                    "status": "done",
                },
            ],
        },
    )

    response = client.get(f"/status/{job_id}/metrics")
    assert response.status_code == 200
    body = response.json()
    assert body["job_id"] == job_id
    assert body["status"] == "done"
    assert body["event_count"] == 7
    assert body["status_change_count"] == 6
    assert body["pause_count"] == 1
    assert body["resume_count"] == 1
    assert body["created_at"] == "2026-02-24T10:00:00+00:00"
    assert body["started_at"] == "2026-02-24T10:00:10+00:00"
    assert body["first_pending_hitl_at"] == "2026-02-24T10:01:00+00:00"
    assert body["terminal_at"] == "2026-02-24T10:06:00+00:00"
    assert body["terminal_status"] == "done"
    assert body["time_to_first_draft_seconds"] == 60.0
    assert body["time_to_terminal_seconds"] == 360.0
    assert body["time_in_pending_hitl_seconds"] == 245.0
    assert body["retrieval_expected"] is True
    assert body["grounding_risk_level"] == "unknown"
    assert body["citation_count"] == 0
    assert body["fallback_namespace_citation_count"] == 0
    assert body["strategy_reference_citation_count"] == 0
    assert body["retrieval_grounded_citation_count"] == 0
    assert body["non_retrieval_citation_count"] == 0
    assert body.get("retrieval_grounded_citation_rate") is None
    assert body.get("non_retrieval_citation_rate") is None
