"""scene_consistency v7 schema validation — Area #4 (element_scope enum required)."""
import json
from pathlib import Path

import jsonschema
import pytest

PROMPT_DIR = (
    Path(__file__).resolve().parent.parent.parent.parent
    / "prompts" / "_base" / "scene_consistency" / "7.202605181210"
)
SCHEMA = json.loads((PROMPT_DIR / "schema.json").read_text())


def _valid_element():
    return {
        "element_id": "body_pose_canonical",
        "element_type": "character_state",
        "character_name": "C##",
        "description": "two-line description",
        "element_scope": "full",
        "applies_to_shots": [1, 3],
        "source_facts": ["S## action: motionless"],
        "visual_inferences": ["left-side"],
        "creative_decisions": ["element_scope full"],
        "confidence": "high",
    }


def _valid_scene():
    return {
        "scene_index": 12,
        "analysis_summary": "summary",
        "fixed_elements": [_valid_element()],
    }


class TestV7Schema:
    def test_valid_scene_passes(self):
        jsonschema.validate(_valid_scene(), SCHEMA)

    def test_element_scope_full_passes(self):
        scene = _valid_scene()
        scene["fixed_elements"][0]["element_scope"] = "full"
        jsonschema.validate(scene, SCHEMA)

    def test_element_scope_close_passes(self):
        scene = _valid_scene()
        scene["fixed_elements"][0]["element_scope"] = "close"
        jsonschema.validate(scene, SCHEMA)

    def test_missing_element_scope_fails(self):
        scene = _valid_scene()
        del scene["fixed_elements"][0]["element_scope"]
        with pytest.raises(jsonschema.ValidationError):
            jsonschema.validate(scene, SCHEMA)

    def test_invalid_element_scope_enum_fails(self):
        scene = _valid_scene()
        scene["fixed_elements"][0]["element_scope"] = "medium"
        with pytest.raises(jsonschema.ValidationError):
            jsonschema.validate(scene, SCHEMA)

    def test_element_scope_null_fails(self):
        scene = _valid_scene()
        scene["fixed_elements"][0]["element_scope"] = None
        with pytest.raises(jsonschema.ValidationError):
            jsonschema.validate(scene, SCHEMA)

    def test_element_scope_empty_string_fails(self):
        scene = _valid_scene()
        scene["fixed_elements"][0]["element_scope"] = ""
        with pytest.raises(jsonschema.ValidationError):
            jsonschema.validate(scene, SCHEMA)

    def test_additional_properties_rejected(self):
        scene = _valid_scene()
        scene["fixed_elements"][0]["framing_scope"] = "full"  # legacy name should reject
        with pytest.raises(jsonschema.ValidationError):
            jsonschema.validate(scene, SCHEMA)

    def test_missing_source_facts_fails(self):
        scene = _valid_scene()
        del scene["fixed_elements"][0]["source_facts"]
        with pytest.raises(jsonschema.ValidationError):
            jsonschema.validate(scene, SCHEMA)

    def test_legacy_confidence_rejected(self):
        scene = _valid_scene()
        scene["fixed_elements"][0]["confidence"] = "legacy"
        with pytest.raises(jsonschema.ValidationError):
            jsonschema.validate(scene, SCHEMA)
