from __future__ import annotations

import unittest

from govoplan_core.core.modules import DeleteVetoIssue, ModuleManifest
from govoplan_core.core.registry import PlatformRegistry


class DeleteVetoContractTests(unittest.TestCase):
    def test_structured_provider_result_is_module_attributed(self) -> None:
        registry = PlatformRegistry()
        registry.register(ModuleManifest(
            id="records",
            name="Records",
            version="test",
            delete_veto_providers={
                "tenant": (
                    lambda _session, _tenant_id, _resource_id: DeleteVetoIssue(
                        severity="warning",
                        code="records_retained",
                        message="Records will remain archived.",
                    ),
                ),
            },
        ))

        issues = registry.collect_delete_veto_issues("tenant", object(), "tenant-1", "tenant-1")

        self.assertEqual(1, len(issues))
        self.assertEqual("warning", issues[0].severity)
        self.assertEqual("records_retained", issues[0].code)
        self.assertEqual("records", issues[0].module_id)
        self.assertEqual({"resource_type": "tenant", "resource_id": "tenant-1"}, issues[0].details)

    def test_exception_based_provider_remains_blocking_veto(self) -> None:
        def veto(_session: object, _tenant_id: str, _resource_id: str) -> None:
            raise RuntimeError("Tenant owns external records.")

        registry = PlatformRegistry()
        registry.register(ModuleManifest(
            id="external_records",
            name="External Records",
            version="test",
            delete_veto_providers={"tenant": (veto,)},
        ))

        issues = registry.collect_delete_veto_issues("tenant", object(), "tenant-1", "tenant-1")

        self.assertEqual(1, len(issues))
        self.assertEqual("blocker", issues[0].severity)
        self.assertEqual("module_veto", issues[0].code)
        self.assertEqual("external_records", issues[0].module_id)
        self.assertEqual("Tenant owns external records.", issues[0].message)


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