# #region TestDictionaryPromptBuilder [C:3] [TYPE Module] [SEMANTICS test, dictionary, prompt, context, similarity]
# @BRIEF Validate ContextAwarePromptBuilder operations: Jaccard similarity, context truncation, entry rendering.
# @RELATION BINDS_TO -> [ContextAwarePromptBuilder]

from src.plugins.translate.prompt_builder import ContextAwarePromptBuilder


class TestJaccardSimilarity:
    """Verify Jaccard similarity computation."""

    # region test_jaccard_similarity [C:2] [TYPE Function]
    def test_jaccard_similarity(self):
        builder = ContextAwarePromptBuilder()

        ctx1 = {"schema": "public", "table": "users"}
        ctx2 = {"schema": "public", "table": "users"}
        sim = builder.compute_context_similarity(ctx1, ctx2)
        assert sim == 1.0

        ctx3 = {"schema": "private"}
        sim = builder.compute_context_similarity(ctx1, ctx3)
        assert sim == 0.0

        ctx4 = {"schema": "public", "table": "orders"}
        sim = builder.compute_context_similarity(ctx1, ctx4)
        assert 0.33 <= sim <= 0.34

        assert builder.compute_context_similarity(None, ctx1) == 0.0
        assert builder.compute_context_similarity(ctx1, None) == 0.0
        assert builder.compute_context_similarity({}, ctx1) == 0.0

        ctx5 = {"schema": "PUBLIC", "table": "USERS"}
        sim = builder.compute_context_similarity(ctx1, ctx5)
        assert sim == 1.0
    # endregion test_jaccard_similarity


class TestContextTruncation:
    """Verify context string truncation."""

    # region test_context_truncation [C:2] [TYPE Function]
    def test_context_truncation(self):
        builder = ContextAwarePromptBuilder()

        long_val = "x" * 500
        long_context = {f"key{i}": long_val for i in range(10)}
        entry = {"source_term": "hello", "target_term": "world", "has_context": True, "context_data": long_context, "usage_notes": None}
        rendered = builder.render_entry(entry, priority=False)
        assert len(rendered) < 2200
        assert "...[truncated]" in rendered

        short_entry = {"source_term": "hello", "target_term": "world", "has_context": True, "context_data": {"table": "users"}, "usage_notes": None}
        short_rendered = builder.render_entry(short_entry, priority=False)
        assert "...[truncated]" not in short_rendered
        assert "(context: table=users)" in short_rendered

        priority_rendered = builder.render_entry(short_entry, priority=True)
        assert priority_rendered.startswith("# PRIORITY (context match)")

        notes_entry = {"source_term": "hello", "target_term": "world", "has_context": False, "context_data": None, "usage_notes": "Use for admin panels"}
        notes_rendered = builder.render_entry(notes_entry, priority=False)
        assert "# Usage: Use for admin panels" in notes_rendered
    # endregion test_context_truncation


class TestRenderEntry:
    """Verify render_entry handles both dicts and objects."""

    # region test_render_entry_dict_and_object [C:2] [TYPE Function]
    def test_render_entry_dict_and_object(self):
        builder = ContextAwarePromptBuilder()

        dict_entry = {"source_term": "hello", "target_term": "hola", "has_context": False, "context_data": None, "usage_notes": None}
        dict_result = builder.render_entry(dict_entry)

        class FakeEntry:
            source_term = "hello"
            target_term = "hola"
            has_context = False
            context_data = None
            usage_notes = None

        obj_result = builder.render_entry(FakeEntry())
        assert dict_result == obj_result
        assert dict_result == '"hello" -> "hola"'
    # endregion test_render_entry_dict_and_object


class TestPrioritization:
    """Verify build_context_entries sorts priority entries first."""

    # region test_build_context_entries_prioritization [C:2] [TYPE Function]
    def test_build_context_entries_prioritization(self):
        builder = ContextAwarePromptBuilder()

        entries = [
            {"source_term": "high_match", "target_term": "alta_coincidencia", "has_context": True, "context_data": {"schema": "public", "table": "users"}, "usage_notes": None},
            {"source_term": "no_match", "target_term": "sin_coincidencia", "has_context": True, "context_data": {"schema": "private", "table": "config"}, "usage_notes": None},
            {"source_term": "no_context", "target_term": "sin_contexto", "has_context": False, "context_data": None, "usage_notes": None},
        ]

        row_context = {"schema": "public", "table": "users"}
        rendered = builder.build_context_entries(entries, row_context)

        assert len(rendered) == 3
        assert rendered[0].startswith("# PRIORITY (context match)")
        assert '"high_match"' in rendered[0]
        assert not rendered[1].startswith("# PRIORITY")
        assert not rendered[2].startswith("# PRIORITY")
    # endregion test_build_context_entries_prioritization
# #endregion TestDictionaryPromptBuilder
