# #region Test.FuzzyMatching [C:3] [TYPE Module] [SEMANTICS test,fuzzy,matching,rapidfuzz]
# @BRIEF Tests for core/utils/matching.py — suggest_mappings.
# @RELATION BINDS_TO -> [FuzzyMatching]

from pathlib import Path
import sys

sys.path.insert(0, str(Path(__file__).parent.parent.parent / "src"))

import pytest


# #region test_suggest_mappings [C:2] [TYPE Function]
# @BRIEF Test suggest_mappings for various database name scenarios.
class TestSuggestMappings:
    def test_empty_targets_returns_empty(self):
        from src.core.utils.matching import suggest_mappings
        result = suggest_mappings(
            [{"uuid": "s1", "database_name": "db1"}],
            [],
        )
        assert result == []

    def test_exact_match_returns_high_confidence(self):
        from src.core.utils.matching import suggest_mappings
        result = suggest_mappings(
            [{"uuid": "s1", "database_name": "sales_db"}],
            [{"uuid": "t1", "database_name": "sales_db"}],
        )
        assert len(result) == 1
        assert result[0]["confidence"] == 1.0
        assert result[0]["source_db"] == "sales_db"
        assert result[0]["target_db"] == "sales_db"

    def test_fuzzy_match_above_threshold(self):
        from src.core.utils.matching import suggest_mappings
        result = suggest_mappings(
            [{"uuid": "s1", "database_name": "sales_database"}],
            [{"uuid": "t1", "database_name": "sales_db"}],
            threshold=60,
        )
        assert len(result) == 1
        assert result[0]["confidence"] > 0.6

    def test_low_similarity_below_threshold(self):
        from src.core.utils.matching import suggest_mappings
        result = suggest_mappings(
            [{"uuid": "s1", "database_name": "completely_different"}],
            [{"uuid": "t1", "database_name": "sales_db"}],
            threshold=90,  # High threshold
        )
        assert len(result) == 0

    def test_multiple_sources_multiple_targets(self):
        from src.core.utils.matching import suggest_mappings
        result = suggest_mappings(
            [
                {"uuid": "s1", "database_name": "customers"},
                {"uuid": "s2", "database_name": "orders"},
            ],
            [
                {"uuid": "t1", "database_name": "customers"},
                {"uuid": "t2", "database_name": "orders_db"},
                {"uuid": "t3", "database_name": "products"},
            ],
            threshold=60,
        )
        assert len(result) == 2
        # customers should match customers exactly
        assert result[0]["source_db"] == "customers"
        # orders should fuzzy match orders_db
        assert result[1]["source_db"] == "orders"

    def test_empty_source_returns_empty(self):
        from src.core.utils.matching import suggest_mappings
        result = suggest_mappings([], [{"uuid": "t1", "database_name": "db"}])
        assert result == []

    def test_fuzzy_match_token_sort(self):
        from src.core.utils.matching import suggest_mappings
        # token_sort_ratio compares sorted tokens, so "sales db" vs "db sales" should match
        result = suggest_mappings(
            [{"uuid": "s1", "database_name": "sales_db"}],
            [{"uuid": "t1", "database_name": "db_sales"}],
            threshold=60,
        )
        # token_sort_ratio normalizes token order, so these should match well
        assert len(result) == 1
        assert result[0]["confidence"] > 0.5

    def test_handles_missing_keys_gracefully(self):
        from src.core.utils.matching import suggest_mappings
        with pytest.raises(KeyError):
            suggest_mappings(
                [{"uuid": "s1"}],  # missing database_name
                [{"uuid": "t1", "database_name": "db"}],
            )
# #endregion test_suggest_mappings
# #endregion Test.FuzzyMatching
