"""Phase 3.5 B2: verifier _SEVERITY_MAP env flag 테스트.

VERIFIER_NUMERIC_PROMOTE 환경변수에 따른 _SEVERITY_MAP 변화 검증.
모듈은 import time 에 env 평가하므로 reload 필요.
"""

from __future__ import annotations

import importlib
import os
import sys

# tests/ → 프로젝트 루트 → app/
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "app"))

import pytest


def _reload_verifier(monkeypatch, value: str | None):
    """env 설정 후 verifier_service 를 reload 하여 _SEVERITY_MAP 재평가."""
    if value is None:
        monkeypatch.delenv("VERIFIER_NUMERIC_PROMOTE", raising=False)
    else:
        monkeypatch.setenv("VERIFIER_NUMERIC_PROMOTE", value)
    from services.search import verifier_service
    importlib.reload(verifier_service)
    return verifier_service


def test_severity_map_off_default(monkeypatch):
    """env 미설정 → numeric_conflict critical 은 medium (기존 동작)."""
    vs = _reload_verifier(monkeypatch, None)
    assert vs._SEVERITY_MAP["numeric_conflict"]["critical"] == "medium"
    assert vs._SEVERITY_MAP["numeric_conflict"]["minor"] == "medium"
    assert vs._NUMERIC_PROMOTE is False


def test_severity_map_on_critical_promoted(monkeypatch):
    """VERIFIER_NUMERIC_PROMOTE=1 → critical 만 strong, minor 는 medium 유지."""
    vs = _reload_verifier(monkeypatch, "1")
    assert vs._SEVERITY_MAP["numeric_conflict"]["critical"] == "strong"
    assert vs._SEVERITY_MAP["numeric_conflict"]["minor"] == "medium"
    assert vs._NUMERIC_PROMOTE is True


def test_severity_map_off_explicit_zero(monkeypatch):
    """VERIFIER_NUMERIC_PROMOTE=0 명시 → off (default 와 동일)."""
    vs = _reload_verifier(monkeypatch, "0")
    assert vs._SEVERITY_MAP["numeric_conflict"]["critical"] == "medium"
    assert vs._NUMERIC_PROMOTE is False


def test_direct_negation_invariant(monkeypatch):
    """direct_negation 은 env 무관 항상 strong (불변 — 안전장치)."""
    for value in [None, "0", "1"]:
        vs = _reload_verifier(monkeypatch, value)
        assert vs._SEVERITY_MAP["direct_negation"]["critical"] == "strong"
        assert vs._SEVERITY_MAP["direct_negation"]["minor"] == "strong"
