"""Area #5 v1 — canary 5 scenario deterministic (3 kind + empty + strict violation).

producer mock LLM emit fixture (real LLM call X, VLM 미사용).

3 kind scenarios:
  - canary 1: character kind + character_outlook attached → PASS
  - canary 2: background kind + close framing + background attached → PASS (C2 strict, NOT skipped)
  - canary 3: prop kind + prop attached → PASS

Empty array:
  - canary 4: reference_phrase_kinds=[] (empty) → step 6 passes, no kind compare

Strict violation:
  - canary 5: close framing + background declared + no background meta → RefContractError

NO VLM. structured SOT only (LLM emit producer-side reference_phrase_kinds).
"""
from __future__ import annotations

import pytest
from app.core.ref_contract_validator import validate_attached_refs, RefContractError


def test_canary_1_character_kind_sidecar():
    """Canary 1: scene_detail v26 emit reference_phrase_kinds=['character'],
    attached_meta 에 character_outlook 1+ — PASS."""
    validate_attached_refs(
        rpc={
            "asset_requirements": {
                "required_refs": [
                    {"kind": "character_outlook", "id": "C01O01", "policy": "required"}
                ],
                "readiness_policy": "block_if_missing",
            }
        },
        labeled_refs=[
            ("Reference image 1 — character C01: outlook O01", b"\x89PNG\r\n\x1a\n" + b"\x00" * 100)
        ],
        attached_meta=[("character_outlook", "C01O01")],
        prompt="the character shown in image 1 stands at the threshold (canary 1)",
        is_close_framing=False,
        reference_phrase_kinds=["character"],
    )  # PASS — character declared + character_outlook attached


def test_canary_2_background_kind_sidecar_close_framing_strict():
    """Canary 2: close framing + reference_phrase_kinds=['background'] + attached background
    — PASS (C2 strict, close framing 면제 폐기)."""
    validate_attached_refs(
        rpc={
            "asset_requirements": {
                "required_refs": [
                    {"kind": "background", "id": "L01", "policy": "required"}
                ],
                "readiness_policy": "block_if_missing",
            }
        },
        labeled_refs=[
            ("Reference image 1 — background L01", b"\x89PNG\r\n\x1a\n" + b"\x00" * 100)
        ],
        attached_meta=[("background", "L01")],
        prompt="the doorway from image 1 fills the close-up frame (canary 2)",
        is_close_framing=True,
        reference_phrase_kinds=["background"],
    )  # PASS — background declared + background attached (close framing 무관)


def test_canary_3_prop_kind_sidecar_with_meta():
    """Canary 3: reference_phrase_kinds=['prop'] + attached prop — PASS."""
    validate_attached_refs(
        rpc={
            "asset_requirements": {
                "required_refs": [
                    {"kind": "prop", "id": "P01", "policy": "required"}
                ],
                "readiness_policy": "block_if_missing",
            }
        },
        labeled_refs=[
            ("Reference image 1 — prop P01 photograph", b"\x89PNG\r\n\x1a\n" + b"\x00" * 100)
        ],
        attached_meta=[("prop", "P01")],
        prompt="the photograph shown in image 1 sits on the desk (canary 3)",
        is_close_framing=False,
        reference_phrase_kinds=["prop"],
    )  # PASS


def test_canary_4_empty_array_no_attribution():
    """Canary 4: reference_phrase_kinds=[] (empty) — step 6 passes, no kind compare."""
    validate_attached_refs(
        rpc={"asset_requirements": {"required_refs": [], "readiness_policy": None}},
        labeled_refs=[],
        attached_meta=[],
        prompt="a generic environmental description without reference attribution (canary 4)",
        is_close_framing=False,
        reference_phrase_kinds=[],
    )  # PASS — empty array, no kind to verify


def test_canary_5_close_framing_background_declared_no_meta_fails():
    """Canary 5: close framing + background declared + no background meta —
    RefContractError (C2 strict, NOT skipped — Area #5 v1 핵심)."""
    with pytest.raises(RefContractError, match="reference_phrase_kinds declares kind='background'"):
        validate_attached_refs(
            rpc={
                "asset_requirements": {
                    "required_refs": [
                        {"kind": "character_outlook", "id": "C01O01", "policy": "required"}
                    ],
                    "readiness_policy": "block_if_missing",
                }
            },
            labeled_refs=[
                ("Reference image 1 — character C01: outlook O01", b"\x89PNG\r\n\x1a\n" + b"\x00" * 100)
            ],
            attached_meta=[("character_outlook", "C01O01")],
            prompt="close-up of subject's face (canary 5 — producer contract violation)",
            is_close_framing=True,
            reference_phrase_kinds=["background"],  # declared but no background meta
        )
