from __future__ import annotations

import unittest
from pathlib import Path

from mais_humana.contract import build_contract, contract_markdown
from mais_humana.models import ReportBundle
from mais_humana.snapshots import EcosystemSnapshot, PlatformScoreSnapshot, diff_snapshots, snapshot_delta_markdown, write_snapshot, load_snapshot
from mais_humana.status_pages import orders_index_markdown
from tests.helpers import make_tmp


class ContractSnapshotsStatusTests(unittest.TestCase):
    def test_contract_exports_core_surface_metadata(self) -> None:
        bundle = ReportBundle(
            output_root="out",
            generated_files=(),
            platform_count=14,
            profile_count=13,
            matrix_cells=182,
            total_code_lines_analyzed=10000,
            warnings=(),
        )
        contract = build_contract(bundle, ())
        self.assertEqual(contract.contract_version, "mais-humana.contract.v1")
        self.assertGreaterEqual(len(contract.surfaces), 5)
        markdown = contract_markdown(contract)
        self.assertIn("Contrato publico", markdown)
        self.assertIn("human_matrix", markdown)

    def test_snapshot_roundtrip_and_diff(self) -> None:
        tmp = make_tmp()
        path = tmp / "snapshot.json"
        before = EcosystemSnapshot(
            generated_at="before",
            platforms=(PlatformScoreSnapshot("identity", 50, 10, 2, ()),),
        )
        after = EcosystemSnapshot(
            generated_at="after",
            platforms=(PlatformScoreSnapshot("identity", 70, 12, 5, ()), PlatformScoreSnapshot("business", 60, 20, 4, ())),
        )
        write_snapshot(path, before)
        loaded = load_snapshot(path)
        self.assertIsNotNone(loaded)
        deltas = diff_snapshots(loaded, after)
        self.assertEqual(len(deltas), 2)
        text = snapshot_delta_markdown(deltas)
        self.assertIn("identity", text)
        self.assertIn("business", text)

    def test_status_order_index_handles_empty_orders(self) -> None:
        text = orders_index_markdown(())
        self.assertIn("Indice de ordens", text)


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