import os

FORBIDDEN_IMPORTS = ("from rapidfuzz import", "import rapidfuzz")
SCAN_ROOTS = (
    os.path.normpath("src/fmatch/saas/services"),
    os.path.normpath("src/fmatch/saas/operators"),
)

# Legacy exceptions to remove as files are migrated to MatchGateway.
ALLOWED_FILES = {
    os.path.normpath("src/fmatch/saas/services/engine_client.py"),
    os.path.normpath("src/fmatch/saas/services/promotion_candidate_search.py"),
    os.path.normpath("src/fmatch/saas/services/sfdc_bulk_matcher.py"),
    os.path.normpath("src/fmatch/saas/operators/detect_duplicate_likelihood.py"),
}


def test_no_rapidfuzz_in_services() -> None:
    allowed = {os.path.normpath(path) for path in ALLOWED_FILES}
    for root in SCAN_ROOTS:
        for dirpath, _, filenames in os.walk(root):
            for filename in filenames:
                if not filename.endswith(".py"):
                    continue
                path = os.path.normpath(os.path.join(dirpath, filename))
                if path in allowed:
                    continue
                with open(path, "r", encoding="utf-8") as handle:
                    content = handle.read()
                for forbidden in FORBIDDEN_IMPORTS:
                    assert (
                        forbidden not in content
                    ), f"Found {forbidden} in {path}"
