"""INV-1 — self_declare 는 ADD only. OFF 불가."""

from __future__ import annotations

from policy.routing import decide_routing


def test_deterministic_true_self_false_stays_high_impact(policy):
    """INV-1 핵심: domain.high_impact=True + self_declare=False → high_impact_task=True 유지."""
    # safety_reference 는 high_impact=true 인 도메인
    decision = decide_routing(
        subject_domain="safety_reference",
        content_chars=1000,
        deterministic_keyword_hits=["산업안전보건법"],
        self_declared_high_impact=False,  # 4B 가 "아니다" 말해도
        self_declared_risk_flags=[],
        confidence=0.95,
        policy=policy,
    )
    assert decision.high_impact_task is True, (
        "self_declare=False 로 high_impact 를 OFF 시킬 수 없어야 함 (INV-1 위반)"
    )
    assert decision.escalate_to_26b is True


def test_deterministic_false_self_true_becomes_high_impact(policy):
    """self_declare=True 는 ADD 기능 — deterministic 이 False 여도 high_impact 로 올림."""
    # news_item 은 high_impact=false 인 도메인
    decision = decide_routing(
        subject_domain="news_item",
        content_chars=500,
        deterministic_keyword_hits=[],
        self_declared_high_impact=True,  # 4B 가 "위험하다" 신고
        self_declared_risk_flags=[],
        confidence=0.95,
        policy=policy,
    )
    assert decision.high_impact_task is True


def test_deterministic_false_self_false_stays_low(policy):
    """둘 다 False 면 low."""
    decision = decide_routing(
        subject_domain="news_item",
        content_chars=500,
        deterministic_keyword_hits=[],
        self_declared_high_impact=False,
        self_declared_risk_flags=[],
        confidence=0.95,
        policy=policy,
    )
    assert decision.high_impact_task is False
    # 에스컬레이션은 일어나지 않아야 함 (다른 조건 충족 없음)
    assert decision.escalate_to_26b is False


def test_domain_high_impact_forces_escalation_regardless_of_self(policy):
    """safety_reference 같은 high_impact 도메인은 self_declare 여부와 무관하게 escalate."""
    decision_true = decide_routing(
        subject_domain="msds",
        content_chars=1000,
        self_declared_high_impact=True,
        confidence=0.95,
        policy=policy,
    )
    decision_false = decide_routing(
        subject_domain="msds",
        content_chars=1000,
        self_declared_high_impact=False,
        confidence=0.95,
        policy=policy,
    )
    assert decision_true.escalate_to_26b is True
    assert decision_false.escalate_to_26b is True
    assert decision_true.high_impact_task is True
    assert decision_false.high_impact_task is True
