# grantflow/tests/test_integration_pilot_export_readiness.py

from fastapi.testclient import TestClient

from grantflow.api.app import app
from grantflow.tests.integration_helpers import wait_for_terminal_status

client = TestClient(app)


def test_status_export_payload_exposes_evaluation_rfq_submission_package_readiness():
    gen = client.post(
        "/generate/from-preset",
        json={
            "preset_key": "un_agencies_katch_evaluation_kyrgyzstan",
            "preset_type": "auto",
            "llm_mode": False,
            "hitl_enabled": False,
        },
    )
    assert gen.status_code == 200
    job_id = gen.json()["job_id"]
    status = wait_for_terminal_status(client, job_id)
    assert status["status"] == "done"

    export_payload = client.get(f"/status/{job_id}/export-payload")
    assert export_payload.status_code == 200
    payload = export_payload.json()["payload"]
    submission_readiness = payload.get("submission_package_readiness") or {}
    assert submission_readiness["readiness_status"] in {"ready", "partial", "weak", "missing"}
    assert float(submission_readiness["completeness_score"]) >= 0.0
    assert "submission_package_counts" in submission_readiness
    assert "attachment_manifest_counts" in submission_readiness
    assert "compliance_matrix_counts" in submission_readiness
    export_contract = payload.get("export_contract") or {}
    assert export_contract["template_key"] == "evaluation_rfq"
    assert isinstance(export_contract.get("submission_readiness_summary"), dict)


def test_demo_endpoint_chain_exposes_quality_metrics_and_export_readiness():
    gen = client.post(
        "/generate/from-preset",
        json={
            "preset_key": "un_agencies_katch_evaluation_kyrgyzstan",
            "preset_type": "auto",
            "llm_mode": False,
            "hitl_enabled": False,
        },
    )
    assert gen.status_code == 200
    job_id = gen.json()["job_id"]
    status = wait_for_terminal_status(client, job_id)
    assert status["status"] == "done"

    quality = client.get(f"/status/{job_id}/quality")
    assert quality.status_code == 200
    quality_body = quality.json()
    assert isinstance(quality_body.get("grounding_trust_summary"), dict)
    export_contract = quality_body.get("export_contract") or {}
    assert isinstance(export_contract, dict)
    assert isinstance(export_contract.get("submission_readiness_summary"), dict)

    metrics = client.get(f"/status/{job_id}/metrics")
    assert metrics.status_code == 200
    metrics_body = metrics.json()
    assert isinstance(metrics_body.get("grounding_trust_summary"), dict)
    assert metrics_body.get("terminal_status") == "done"

    export_payload = client.get(f"/status/{job_id}/export-payload")
    assert export_payload.status_code == 200
    payload_root = export_payload.json().get("payload") or {}
    submission_readiness = payload_root.get("submission_package_readiness") or {}
    assert submission_readiness.get("readiness_status") in {"ready", "partial", "weak", "missing"}
    assert submission_readiness.get("top_gap") is not None
