from __future__ import annotations

import json
import unittest

from mais_humana.cli import main
from mais_humana.mcp_admin_route_acceptance import (
    AdminRouteAcceptanceStatus,
    acceptance_csv,
    acceptance_markdown,
    build_admin_route_acceptance_report,
    compact_acceptance_payload,
    iter_admin_route_acceptance_cases,
    run_admin_route_acceptance,
    summarize_admin_route_acceptance,
)
from tests.helpers import make_tmp


class McpAdminRouteAcceptanceTests(unittest.TestCase):
    def test_generated_catalog_has_expected_operations_and_volume(self) -> None:
        cases = iter_admin_route_acceptance_cases()
        self.assertGreaterEqual(len(cases), 900)
        operations = {case.operation for case in cases}
        self.assertTrue({"consulta", "diagnostico", "acao", "auditoria", "explicacao"}.issubset(operations))
        self.assertTrue(all(case.source_payload_hash for case in cases[:20]))
        self.assertTrue(all(case.source_records_hash for case in cases[:20]))

    def test_summary_reports_real_readiness_and_blockers(self) -> None:
        summary = summarize_admin_route_acceptance()
        self.assertGreaterEqual(summary.total_cases, 900)
        self.assertEqual(summary.status, AdminRouteAcceptanceStatus.BLOCKED)
        self.assertGreaterEqual(summary.ready_cases, 500)
        self.assertGreater(summary.partial_cases, 0)
        self.assertGreater(summary.blocked_cases, 0)
        self.assertGreater(summary.ready_ratio, 0.5)
        self.assertIn("consulta", summary.operations)

    def test_filtered_report_and_renderers_are_compact(self) -> None:
        report = build_admin_route_acceptance_report(platform_id="business", operation="consulta", limit=5)
        self.assertEqual(report.status, AdminRouteAcceptanceStatus.READY)
        self.assertLessEqual(len(report.returned_cases), 5)
        payload = compact_acceptance_payload(report)
        self.assertEqual(payload["status"], "ready")
        self.assertEqual(payload["returnedCases"], len(report.returned_cases))
        self.assertIn("route_id,platform_id,profile_id", acceptance_csv(report))
        self.assertIn("MCP Admin Route Acceptance", acceptance_markdown(report))

    def test_run_acceptance_writes_project_and_central_artifacts(self) -> None:
        root = make_tmp()
        project = root / "tudo-para-ia-mais-humana"
        central = root / "central" / "15_repo_tudo-para-ia-mais-humana-platform"
        report, records = run_admin_route_acceptance(project_root=project, central_platform_folder=central, limit=8)
        self.assertEqual(report.status, AdminRouteAcceptanceStatus.BLOCKED)
        self.assertTrue((project / "dados" / "mcp-admin-route-acceptance.json").exists())
        self.assertTrue((project / "dados" / "mcp-admin-route-acceptance-compacto.json").exists())
        self.assertTrue((project / "matrizes" / "mcp-admin-route-acceptance.csv").exists())
        self.assertTrue((project / "ecossistema" / "MCP-ADMIN-ROUTE-ACCEPTANCE.md").exists())
        self.assertTrue((central / "reports" / "EXECUTADO__mcp-admin-route-acceptance.md").exists())
        self.assertTrue((central / "indexes" / "mcp-admin-route-acceptance-index.md").exists())
        self.assertGreaterEqual(len(records), 6)

    def test_cli_acceptance_writes_json_payload(self) -> None:
        root = make_tmp()
        project = root / "project"
        code = main(["mcp-admin-route-acceptance", "--project-root", str(project), "--limit", "3"])
        self.assertEqual(code, 0)
        payload = json.loads((project / "dados" / "mcp-admin-route-acceptance.json").read_text(encoding="utf-8"))
        self.assertEqual(payload["status"], "blocked")
        self.assertEqual(len(payload["returned_cases"]), 3)


if __name__ == "__main__":
    unittest.main()
