"""QC false-positive fix 검증 (커밋 금지).

기존 생성된 guide PNG(s5/s14/s21/s29)에 ★새 QC 프롬프트★를 재실행해
text_or_marker_leakage 가 underlay 고유 배경텍스트(s21 사무실 벽 게시판 등)를
더이상 오판하지 않는지 확인. 마네킹은 깨끗하므로 4건 모두 PASS 기대.
"""
import os
import sys

_BACKEND = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "backend"))
os.chdir(_BACKEND)
sys.path.insert(0, _BACKEND)

GALLERY = os.path.join(os.path.dirname(__file__), "gallery")
CASES = [
    ("s5-L04B06", 1), ("s14-L04B08", 1), ("s21-L14B01", 1), ("s29-L21B01", 1),
]


def main():
    from app.core.config import settings
    if not os.environ.get("OPENAI_API_KEY") and getattr(settings, "openai_api_key", None):
        os.environ["OPENAI_API_KEY"] = settings.openai_api_key
    from app.modules.pipeline.indoor_shared_pose_provider import qc_indoor_pose_guide
    from app.services.indoor_shared_pose_guide_service import evaluate_guide_qc
    print("qc_model:", settings.indoor_shared_pose_guide_qc_model)

    for name, exp in CASES:
        png_path = os.path.join(GALLERY, f"indoor-{name}_guide.png")
        with open(png_path, "rb") as f:
            png = f.read()
        try:
            verdict = qc_indoor_pose_guide(png, expected_figures=exp)
        except Exception as exc:  # noqa: BLE001
            print(f"{name}: QC ERROR {type(exc).__name__}: {exc}")
            continue
        ok, reason = evaluate_guide_qc(verdict, expected_figures=exp)
        print(f"{name}: {'PASS' if ok else 'FAIL'} reason={reason} | "
              f"text_leak={verdict.get('text_or_marker_leakage')} "
              f"layout={verdict.get('layout_preserved')} "
              f"photoreal={verdict.get('photoreal_person')} "
              f"cloth/face={verdict.get('clothing_or_face')} "
              f"redraw={verdict.get('environment_redraw')} "
              f"mannequins={verdict.get('mannequin_count')}")


if __name__ == "__main__":
    main()
