"""Neutral CRA applicability verdict (Eingangstür): legal duty vs market pull."""
from compliance.services.cra_applicability import (
    ZWINGEND, RATSAM, NICHT_BETROFFEN, COMPONENT, MACHINE_INTEGRATOR,
    MV_DIREKT, MV_SICHERHEITSRELEVANT, MV_NICHT,
    compute_verdict, compute_machinery_verdict, maturity, in_scope, EVIDENCE_ITEMS,
)


class TestInScope:
    def test_digital_element_classes_are_in_scope(self):
        for c in ("STANDARD", "IMPORTANT_I", "IMPORTANT_II", "CRITICAL"):
            assert in_scope(c) is True

    def test_not_in_scope(self):
        assert in_scope("NOT_IN_SCOPE") is False
        assert in_scope("") is False


class TestVerdict:
    def test_zwingend_when_in_scope_and_after_cutoff(self):
        v = compute_verdict("STANDARD", True)
        assert v["tier"] == ZWINGEND
        assert v["in_scope"] is True

    def test_ratsam_when_in_scope_but_not_after_cutoff(self):
        v = compute_verdict("STANDARD", False)
        assert v["tier"] == RATSAM
        assert any("Geltungszeitraum" in r for r in v["reasons"])

    def test_nicht_betroffen_when_no_digital_elements(self):
        v = compute_verdict("NOT_IN_SCOPE", True)
        assert v["tier"] == NICHT_BETROFFEN
        assert v["in_scope"] is False

    def test_unknown_market_date_assumed_after_cutoff(self):
        # None = unknown -> conservative -> zwingend
        v = compute_verdict("STANDARD", None)
        assert v["tier"] == ZWINGEND
        assert v["placed_on_market_after_cutoff"] is True

    def test_component_has_market_pull_without_explicit_signal(self):
        v = compute_verdict("STANDARD", True, producer_type=COMPONENT)
        assert v["market_pull"] is True
        assert any("Markt-Druck" in r for r in v["reasons"])

    def test_customers_request_sets_market_pull(self):
        v = compute_verdict("STANDARD", True, producer_type="end_device", customers_request=True)
        assert v["market_pull"] is True

    def test_end_device_no_signal_no_market_pull(self):
        v = compute_verdict("STANDARD", True, producer_type="end_device")
        assert v["market_pull"] is False

    def test_not_betroffen_with_market_pull_adds_hint_not_pull_reason(self):
        v = compute_verdict("NOT_IN_SCOPE", True, producer_type=COMPONENT)
        assert v["tier"] == NICHT_BETROFFEN
        assert any("anfragen" in r for r in v["reasons"])

    def test_class_passthrough_uppercased(self):
        v = compute_verdict("important_ii", True)
        assert v["cra_class"] == "IMPORTANT_II"


class TestMachineryVerdict:
    def test_machine_integrator_is_direct(self):
        v = compute_machinery_verdict(producer_type=MACHINE_INTEGRATOR)
        assert v["tier"] == MV_DIREKT
        assert v["cyber_safety_bridge"] is True

    def test_is_machinery_flag_is_direct(self):
        v = compute_machinery_verdict(producer_type="end_device", is_machinery=True)
        assert v["tier"] == MV_DIREKT

    def test_component_with_person_hazard_is_safety_relevant(self):
        # OWIS-with-laser borderline: component, not a machine, but can harm persons
        v = compute_machinery_verdict(
            producer_type=COMPONENT, safety_relevant=True, hazard_types=["laser_radiation", "movement_crush"],
        )
        assert v["tier"] == MV_SICHERHEITSRELEVANT
        assert v["cyber_safety_bridge"] is True
        assert {h["key"] for h in v["hazards"]} == {"laser_radiation", "movement_crush"}

    def test_hazard_types_alone_imply_safety_relevant(self):
        v = compute_machinery_verdict(producer_type=COMPONENT, hazard_types=["force_energy"])
        assert v["tier"] == MV_SICHERHEITSRELEVANT

    def test_component_no_hazard_not_relevant(self):
        v = compute_machinery_verdict(producer_type=COMPONENT)
        assert v["tier"] == MV_NICHT
        assert v["cyber_safety_bridge"] is False

    def test_safety_component_is_direct(self):
        v = compute_machinery_verdict(producer_type=COMPONENT, is_safety_component=True)
        assert v["tier"] == MV_DIREKT

    def test_unknown_hazard_keys_ignored(self):
        v = compute_machinery_verdict(producer_type=COMPONENT, hazard_types=["nonsense"])
        assert v["hazards"] == []
        assert v["tier"] == MV_NICHT


class TestMaturity:
    def test_empty_is_zero(self):
        m = maturity([])
        assert m["pct"] == 0
        assert len(m["missing"]) == len(EVIDENCE_ITEMS)
        assert m["present"] == []

    def test_partial(self):
        m = maturity(["sbom", "vdp"])
        assert m["pct"] == round(100 * 2 / len(EVIDENCE_ITEMS))
        assert {e["key"] for e in m["present"]} == {"sbom", "vdp"}

    def test_unknown_keys_ignored(self):
        m = maturity(["sbom", "nonsense"])
        assert {e["key"] for e in m["present"]} == {"sbom"}

    def test_full(self):
        m = maturity([e["key"] for e in EVIDENCE_ITEMS])
        assert m["pct"] == 100
        assert m["missing"] == []
