"""W21B-wave-2 — background_master_plan surface_role contract guards.

These tests only check deterministic schema/validator/catalog wiring. Visual
quality remains a canary review concern.
"""
from __future__ import annotations

import json
from pathlib import Path

import pytest


def _repo_root() -> Path:
    return Path(__file__).resolve().parents[3]


def test_v6_schema_requires_background_surface_role_only():
    schema_path = (
        _repo_root()
        / "prompts"
        / "_base"
        / "background_master_plan"
        / "6.202605290248"
        / "schema.json"
    )
    schema = json.loads(schema_path.read_text(encoding="utf-8"))

    fp_props = schema["properties"]["floor_plans"]["items"]["properties"]
    bg_item = schema["properties"]["backgrounds"]["items"]
    bg_props = bg_item["properties"]

    assert "surface_role" not in fp_props
    assert bg_props["surface_role"]["enum"] == [
        "interior_room",
        "exterior_plate",
        "transition_zone",
        "site_surface",
    ]
    assert "surface_role" in bg_item["required"]
    assert "minItems" not in bg_props["depends_on_fp"]


def test_step_uses_background_master_plan_v6_and_schema_5():
    from app.core.steps.background_master_plan_step import (
        PROMPT_VERSION,
        SCHEMA_VERSION,
    )

    assert PROMPT_VERSION == "6.202605290248"
    assert SCHEMA_VERSION == 5


def test_raw_validator_surface_role_allows_exterior_without_fp():
    from app.modules.pipeline.background_master_plan import (
        validate_master_plan_raw_intent,
    )

    plan = {
        "group_id": "g_ext",
        "rationale_summary": "surface role exterior fixture",
        "floor_plans": [],
        "backgrounds": [
            {
                "loc_id": "L04",
                "space_key_hint": "main",
                "time_phase": "day",
                "state_class": "quiet",
                "surface_role": "exterior_plate",
                "applies_to_shots": ["S4_Shot1"],
                "sub_location_label": "roof exterior",
                "state_label_raw": "quiet exterior plate",
                "depends_on_fp": [],
                "depends_on_bg": [],
            }
        ],
    }
    validate_master_plan_raw_intent(
        plan,
        expected_group_id="g_ext",
        group_loc_ids={"L04"},
        group_shot_ids={"S4_Shot1"},
        location_profiles={"L04": {"kind": "single_space", "allowed_space_keys": ["main"]}},
    )


def test_raw_validator_surface_role_rejects_interior_without_fp():
    from app.modules.pipeline.background_master_plan import (
        validate_master_plan_raw_intent,
    )

    plan = {
        "group_id": "g_int",
        "rationale_summary": "surface role interior fixture",
        "floor_plans": [],
        "backgrounds": [
            {
                "loc_id": "L05",
                "space_key_hint": "main",
                "time_phase": "day",
                "state_class": "quiet",
                "surface_role": "interior_room",
                "applies_to_shots": ["S5_Shot1"],
                "sub_location_label": "room interior",
                "state_label_raw": "quiet room",
                "depends_on_fp": [],
                "depends_on_bg": [],
            }
        ],
    }
    with pytest.raises(ValueError, match="interior_room.*depends_on_fp"):
        validate_master_plan_raw_intent(
            plan,
            expected_group_id="g_int",
            group_loc_ids={"L05"},
            group_shot_ids={"S5_Shot1"},
            location_profiles={"L05": {"kind": "single_space", "allowed_space_keys": ["main"]}},
        )
