from __future__ import annotations

import unittest

from pydantic import ValidationError

from govoplan_core.privacy.schemas import (
    RETENTION_POLICY_FIELD_KEYS,
    PrivacyRetentionPolicyItem,
    PrivacyRetentionPolicyPatchItem,
    default_allow_lower_level_limits,
    normalize_allow_lower_level_limits,
)


class PrivacySchemaContractTests(unittest.TestCase):
    def test_default_allow_lower_level_limits_cover_every_retention_field(self) -> None:
        self.assertEqual({key: True for key in RETENTION_POLICY_FIELD_KEYS}, default_allow_lower_level_limits())

    def test_policy_item_fills_missing_lower_level_limits(self) -> None:
        item = PrivacyRetentionPolicyItem.model_validate({"allow_lower_level_limits": {"audit_detail_level": False}})

        self.assertFalse(item.allow_lower_level_limits["audit_detail_level"])
        self.assertTrue(item.allow_lower_level_limits["store_raw_campaign_json"])

    def test_policy_patch_keeps_lower_level_limits_sparse(self) -> None:
        patch = PrivacyRetentionPolicyPatchItem.model_validate({"allow_lower_level_limits": {"audit_detail_level": False}})

        self.assertEqual({"audit_detail_level": False}, patch.allow_lower_level_limits)
        self.assertIsNone(normalize_allow_lower_level_limits("", fill_defaults=False))

    def test_unknown_lower_level_limit_field_is_rejected(self) -> None:
        with self.assertRaises(ValidationError):
            PrivacyRetentionPolicyItem.model_validate({"allow_lower_level_limits": {"unknown": True}})


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