"""Unit tests for ngsite.product_match.find_matches — the cross-merchant
same-product matcher behind the /p/ price-comparison panel. Precision-FIRST:
a wrong "cheaper elsewhere" match is worse than a missing one, so these lock
the HARD rejects (different brand / caliber / platform / model = NOT the same
product) and the masquerade guards (thread pitch, AR10/AR15 platform, and
caliber must not be read as distinctive model numbers). See
docs/product-match-comparison.md.
"""
from ngsite import product_match as pm


def _row(id, company, description, price, type="ch"):
    return dict(id=id, company=company, type=type, description=description,
                price=price, url="/x")


def _run(target, others):
    return pm.find_matches([target] + others, target)


def test_same_product_across_merchants_matches():
    tgt = _row(1, "Rainier Arms", "Radian Raptor-LT Ambi Charging Handle AR-15", 79.95)
    others = [
        _row(2, "Brownells", "RADIAN WEAPONS - AR-15 RAPTOR-LT CHARGING HANDLE", 79.95),
        _row(3, "MidwayUSA", "Radian Raptor-LT AR-15 Ambidextrous Charging Handle", 79.99),
    ]
    res = _run(tgt, others)
    hit = {m["company"] for m in res["confident"] + res["possible"]}
    assert hit == {"Brownells", "MidwayUSA"}


def test_different_brand_rejected():
    # Breek Arms is NOT Radian — must not match even though both are AR10 CHs.
    tgt = _row(1, "Rainier", "Radian Weapons Raptor SD Ambi Charging Handle AR10/SR25", 159.95)
    others = [_row(2, "PSA", "Breek Arms Warhammer MOD2 Ambi Charging Handle AR10 BRK530", 65.99)]
    res = _run(tgt, others)
    assert res["confident"] == [] and res["possible"] == []


def test_platform_conflict_rejected():
    # AR10 charging handle != AR15 charging handle (same brand, diff platform).
    tgt = _row(1, "Rainier", "Radian Raptor SD Charging Handle AR10/SR25", 159.95)
    others = [_row(2, "Brownells", "Radian Weapons AR-15 Raptor Charging Handle", 79.95)]
    res = _run(tgt, others)
    assert all("AR-15" not in m["description"]
               for m in res["confident"] + res["possible"])


def test_caliber_conflict_rejected():
    # 9mm BCG != 5.56 BCG even though same brand.
    tgt = _row(1, "Bauer", "Angstadt Arms Bolt Carrier Group .223/556", 117.99, type="bcg")
    others = [_row(2, "PSA", "Angstadt Arms 9mm Bolt Carrier Group AA09BCGNIT", 189.99, type="bcg")]
    res = _run(tgt, others)
    assert res["confident"] == [] and res["possible"] == []


def test_thread_pitch_not_a_model_number():
    # 1/2x28 is a common thread SPEC shared by many muzzle devices, not a model.
    tgt = _row(1, "PrimaryArms", "SureFire Muzzle Device Shim Kit 1/2x28", 13.0, type="muzzle")
    others = [_row(2, "Rainier", "Milspec Crush Washer 1/2x28 Stainless", 2.0, type="muzzle")]
    res = _run(tgt, others)
    assert res["confident"] == [] and res["possible"] == []


def test_shared_model_number_is_confident():
    tgt = _row(1, "Bauer", "Angstadt Arms BCG .223/556 AA56BC", 117.99, type="bcg")
    others = [_row(2, "PSA", "Angstadt Arms Bolt Carrier Group Black .223/556 AA56BC", 125.99, type="bcg")]
    res = _run(tgt, others)
    assert len(res["confident"]) == 1 and res["confident"][0]["company"] == "PSA"


def test_same_company_and_self_excluded():
    tgt = _row(1, "Rainier", "Radian Raptor-LT Charging Handle AR-15", 79.95)
    others = [
        _row(1, "Rainier", "Radian Raptor-LT Charging Handle AR-15", 79.95),   # self dup
        _row(2, "Rainier", "Radian Raptor-LT Charging Handle AR-15", 79.95),   # same company
    ]
    res = _run(tgt, others)
    assert res["confident"] == [] and res["possible"] == []


def test_cheapest_per_company_kept():
    tgt = _row(1, "Rainier", "Radian Raptor-LT Charging Handle AR-15", 100.0)
    others = [
        _row(2, "Brownells", "Radian Raptor-LT AR-15 Charging Handle", 90.0),
        _row(3, "Brownells", "Radian Raptor-LT AR-15 Charging Handle", 80.0),  # cheaper dup
    ]
    res = _run(tgt, others)
    hits = res["confident"] + res["possible"]
    assert len(hits) == 1 and hits[0]["price"] == 80.0


def test_non_safe_type_returns_nothing():
    # barrels are HELD until attribute extraction (variant risk).
    tgt = _row(1, "Faxon", "Faxon 16in Gunner Barrel 5.56", 200.0, type="barrel")
    others = [_row(2, "PSA", "Faxon 16in Gunner Barrel 5.56", 190.0, type="barrel")]
    res = _run(tgt, others)
    assert res["confident"] == [] and res["possible"] == []
