"""Teste US-011 (PRD 5.8) — helper pur `text_rules_overlap`.

Avertisment neblocant cand o regula text noua se suprapune (substring, oricare
directie) cu una existenta. Helper determinist, fara DB.
"""

from __future__ import annotations

from app.mapping import text_rules_overlap


def test_overlap_substring_ambele_directii():
    """Overlap = pattern nou substring al unei reguli existente SAU invers."""
    existing = [
        {"pattern": "verificare faruri", "cod_prestatie": "OE-3"},
        {"pattern": "schimb ulei", "cod_prestatie": "OE-1"},
    ]
    # nou „verificare" e substring al „verificare faruri" -> overlap
    hits = text_rules_overlap("verificare", existing)
    assert len(hits) == 1
    assert hits[0]["pattern"] == "verificare faruri"

    # invers: nou „verificare faruri spate" CONTINE „verificare faruri" -> overlap
    hits2 = text_rules_overlap("verificare faruri spate", existing)
    assert len(hits2) == 1
    assert hits2[0]["pattern"] == "verificare faruri"


def test_fara_overlap():
    """Pattern fara nicio relatie de substring -> lista goala.

    Pattern IDENTIC (dupa normalizare) cu unul existent NU e overlap: e un upsert
    (update al codului), nu o suprapunere care merita avertisment.
    """
    existing = [
        {"pattern": "verificare", "cod_prestatie": "OE-2"},
    ]
    assert text_rules_overlap("schimb ulei", existing) == []
    # identic dupa normalizare -> update, nu overlap
    assert text_rules_overlap("VERIFICARE", existing) == []
    # fara reguli existente
    assert text_rules_overlap("verificare", []) == []


def test_overlap_normalizat_diacritice():
    """Normalizarea (diacritice + caz) se aplica pe ambele parti la match."""
    existing = [
        {"pattern": "verificare completa", "cod_prestatie": "OE-2"},
    ]
    # „Verificăre" -> „VERIFICARE", substring al „VERIFICARE COMPLETA" -> overlap
    hits = text_rules_overlap("Verificăre", existing)
    assert len(hits) == 1
    assert hits[0]["pattern"] == "verificare completa"
