# #region TestEnforceDictionary [C:3] [TYPE Module] [SEMANTICS test, dictionary, enforce, regex]
# @BRIEF Validate _enforce_dictionary regex and non-regex enforcement logic.
# @RELATION BINDS_TO -> [TranslationUtils]

from src.plugins.translate._utils import _enforce_dictionary


class TestEnforceDictionaryNonRegex:
    """Verify non-regex enforcement still works."""

    # region test_enforce_basic_replacement [C:2] [TYPE Function]
    def test_enforce_basic_replacement(self):
        """LLM left source term untranslated — enforcement should fix it."""
        source_text = "hello world"
        per_lang = {"es": "hello mundo"}  # "hello" wasn't translated to "hola"
        matches = [
            {"source_term": "hello", "target_term": "hola", "is_regex": False},
        ]
        _enforce_dictionary(source_text, per_lang, matches, "batch_1", "row_1")
        assert "hola" in per_lang["es"]
        assert "hello" not in per_lang["es"]
    # endregion test_enforce_basic_replacement

    # region test_enforce_target_already_present [C:2] [TYPE Function]
    def test_enforce_target_already_present(self):
        """If target term is already in the translation, skip replacement."""
        source_text = "hello world"
        per_lang = {"es": "hola mundo"}
        matches = [
            {"source_term": "hello", "target_term": "hola", "is_regex": False},
        ]
        _enforce_dictionary(source_text, per_lang, matches, "batch_1", "row_1")
        assert per_lang["es"] == "hola mundo"  # unchanged
    # endregion test_enforce_target_already_present

    # region test_enforce_source_not_in_text [C:2] [TYPE Function]
    def test_enforce_source_not_in_text(self):
        """If source term is not in source text, skip."""
        source_text = "goodbye world"
        per_lang = {"es": "adiós mundo"}
        matches = [
            {"source_term": "hello", "target_term": "hola", "is_regex": False},
        ]
        _enforce_dictionary(source_text, per_lang, matches, "batch_1", "row_1")
        assert per_lang["es"] == "adiós mundo"  # unchanged
    # endregion test_enforce_source_not_in_text

    # region test_enforce_empty_values_skipped [C:2] [TYPE Function]
    def test_enforce_empty_values_skipped(self):
        """Empty source_text or dict_matches should be no-ops."""
        per_lang = {"es": "hola mundo"}
        _enforce_dictionary("", per_lang, [{"source_term": "hello", "target_term": "hola", "is_regex": False}], "b", "r")
        assert per_lang["es"] == "hola mundo"

        _enforce_dictionary("hello world", per_lang, [], "b", "r")
        assert per_lang["es"] == "hola mundo"
    # endregion test_enforce_empty_values_skipped


class TestEnforceDictionaryRegex:
    """Verify regex-based enforcement."""

    # region test_enforce_regex_replacement [C:2] [TYPE Function]
    def test_enforce_regex_replacement(self):
        """Regex pattern matches and replaces in translation."""
        source_text = "Contact: 123-45-6789"
        per_lang = {"es": "Contacto: 123-45-6789"}  # SSN not masked
        matches = [
            {"source_term": r"\d{3}-\d{2}-\d{4}", "target_term": "XXX-XX-XXXX", "is_regex": True},
        ]
        _enforce_dictionary(source_text, per_lang, matches, "batch_1", "row_1")
        assert "XXX-XX-XXXX" in per_lang["es"]
        assert "123-45-6789" not in per_lang["es"]
    # endregion test_enforce_regex_replacement

    # region test_enforce_regex_no_match [C:2] [TYPE Function]
    def test_enforce_regex_no_match(self):
        """Regex doesn't match source — nothing changes."""
        source_text = "just text no digits"
        per_lang = {"es": "solo texto sin dígitos"}
        matches = [
            {"source_term": r"\d{3}-\d{2}-\d{4}", "target_term": "XXX-XX-XXXX", "is_regex": True},
        ]
        _enforce_dictionary(source_text, per_lang, matches, "batch_1", "row_1")
        assert per_lang["es"] == "solo texto sin dígitos"  # unchanged
    # endregion test_enforce_regex_no_match

    # region test_enforce_regex_invalid_pattern_skipped [C:2] [TYPE Function]
    def test_enforce_regex_invalid_pattern_skipped(self):
        """Invalid regex pattern in dict match should be silently skipped."""
        source_text = "some text"
        per_lang = {"es": "algún texto"}
        matches = [
            {"source_term": r"[invalid", "target_term": "FIXED", "is_regex": True},
        ]
        # Should not raise
        _enforce_dictionary(source_text, per_lang, matches, "batch_1", "row_1")
        assert per_lang["es"] == "algún texto"  # unchanged
    # endregion test_enforce_regex_invalid_pattern_skipped

    # region test_enforce_regex_target_already_present [C:2] [TYPE Function]
    def test_enforce_regex_target_already_present(self):
        """If target term is already in translation, regex enforcement skips."""
        source_text = "Call 555-1234"
        per_lang = {"es": "Llama al NUM"}
        matches = [
            {"source_term": r"\d{3}-\d{4}", "target_term": "NUM", "is_regex": True},
        ]
        _enforce_dictionary(source_text, per_lang, matches, "batch_1", "row_1")
        assert per_lang["es"] == "Llama al NUM"  # unchanged
    # endregion test_enforce_regex_target_already_present

    # region test_enforce_regex_multi_language [C:2] [TYPE Function]
    def test_enforce_regex_multi_language(self):
        """Regex enforcement applies to all languages."""
        source_text = "Price: 99.99"
        per_lang = {
            "es": "Precio: 99.99 euros",
            "fr": "Prix: 99.99 euros",
        }
        matches = [
            {"source_term": r"\d+\.\d{2}", "target_term": "XX.YY", "is_regex": True},
        ]
        _enforce_dictionary(source_text, per_lang, matches, "batch_1", "row_1")
        assert "XX.YY" in per_lang["es"]
        assert "XX.YY" in per_lang["fr"]
        assert "99.99" not in per_lang["es"]
        assert "99.99" not in per_lang["fr"]
    # endregion test_enforce_regex_multi_language

    # region test_enforce_regex_case_sensitive_matching [C:2] [TYPE Function]
    def test_enforce_regex_case_insensitive(self):
        """Regex enforcement uses IGNORECASE flag."""
        source_text = "HELLO World"
        per_lang = {"es": "HELLO Mundo"}  # "HELLO" not translated
        matches = [
            {"source_term": "hello", "target_term": "hola", "is_regex": True},
        ]
        _enforce_dictionary(source_text, per_lang, matches, "batch_1", "row_1")
        assert "hola" in per_lang["es"]
    # endregion test_enforce_regex_case_insensitive
# #endregion TestEnforceDictionary
