"""scene_consistency v6 schema validation."""
import json
from pathlib import Path

import jsonschema
import pytest

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


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


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


class TestV6Schema:
    def test_valid_scene_passes(self):
        jsonschema.validate(_valid_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_missing_visual_inferences_fails(self):
        scene = _valid_scene()
        del scene["fixed_elements"][0]["visual_inferences"]
        with pytest.raises(jsonschema.ValidationError):
            jsonschema.validate(scene, SCHEMA)

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

    def test_missing_confidence_fails(self):
        scene = _valid_scene()
        del scene["fixed_elements"][0]["confidence"]
        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)

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

    def test_all_three_enum_values_valid(self):
        for value in ["high", "medium", "low"]:
            scene = _valid_scene()
            scene["fixed_elements"][0]["confidence"] = value
            jsonschema.validate(scene, SCHEMA)

    def test_empty_lists_pass(self):
        scene = _valid_scene()
        scene["fixed_elements"][0]["source_facts"] = []
        scene["fixed_elements"][0]["visual_inferences"] = []
        scene["fixed_elements"][0]["creative_decisions"] = []
        scene["fixed_elements"][0]["confidence"] = "low"
        jsonschema.validate(scene, SCHEMA)

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