from __future__ import annotations

import unittest

from mais_humana.institutional_decisions import (
    PLATFORM_ALIASES,
    build_human_decision_matrix,
    classify_development_action_for_humans,
    list_institutional_decisions,
    summarize_decisions,
)


class InstitutionalDecisionHumanTests(unittest.TestCase):
    def test_six_decisions_are_available_for_human_translation(self) -> None:
        decisions = list_institutional_decisions()
        self.assertEqual(len(decisions), 6)
        self.assertEqual(
            [decision.decision_id for decision in decisions],
            [
                "docs_full_operational_platform",
                "mcp_required_cross_platform_acceptance",
                "ten_year_institutional_retention",
                "live_sensitive_data_purge_forbidden",
                "canonical_platform_names",
                "development_execution_for_gpt_codex",
            ],
        )
        self.assertTrue(all(decision.affected_profiles for decision in decisions))
        self.assertTrue(all(decision.expected_mcp_evidence for decision in decisions))

    def test_summary_exposes_profiles_evidence_and_blockers(self) -> None:
        summary = summarize_decisions()
        self.assertEqual(summary["decisions"], 6)
        self.assertGreaterEqual(summary["profile_count"], 5)
        self.assertIn("sourceRecordsHash", summary["expected_mcp_evidence_fields"])
        self.assertIn("segredo_exposto", summary["blockers"])
        self.assertGreaterEqual(summary["ready_criteria_count"], 18)

    def test_matrix_keeps_biblioteca_privada_outside_platforms(self) -> None:
        matrix = build_human_decision_matrix()
        self.assertTrue(matrix["ok"])
        self.assertEqual(matrix["summary"]["decisions"], 6)
        aliases = matrix["platform_aliases"]
        biblioteca = [alias for alias in aliases if alias["current_name"] == "tudo-para-ia-biblioteca-privada"][0]
        self.assertFalse(biblioteca["is_platform"])
        self.assertEqual(biblioteca["owner_platform_id"], "tudo-para-ia-integracoes-platform")
        self.assertIn("Mais Humana traduz impacto", matrix["human_boundary"])

    def test_platform_aliases_document_legacy_platform_suffixes(self) -> None:
        legacy = [alias for alias in PLATFORM_ALIASES if alias.alias_status == "legacy_alias"]
        self.assertGreaterEqual(len(legacy), 3)
        self.assertTrue(all(alias.canonical_name.endswith("-platform") for alias in legacy))

    def test_development_action_assessment_is_broad_but_blocks_secret_and_external_effect(self) -> None:
        allowed = classify_development_action_for_humans("simular usuario tenant compra e painel")
        self.assertTrue(allowed.allowed)
        self.assertEqual(allowed.decision, "development_simulation_allowed")
        self.assertIn("truthState", allowed.required_controls)

        external = classify_development_action_for_humans("send real message to customer")
        self.assertFalse(external.allowed)
        self.assertEqual(external.decision, "explicit_authorization_required")

        secret = classify_development_action_for_humans("mostrar token secret")
        self.assertFalse(secret.allowed)
        self.assertEqual(secret.decision, "blocked_secret_exposure")


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