"""CP-7 Phase 2 — Scene construction + add_beat tests."""

import sys
import pathlib

sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent.parent.parent.parent))
from recoil.core.paths import ensure_pipeline_importable  # noqa: E402
ensure_pipeline_importable()

import pytest  # noqa: E402

from recoil.pipeline.core.take import Beat, Scene  # noqa: E402


def test_scene_minimal_and_metadata():
    s = Scene(scene_id="ep001_sc02", scene_metadata={"location": "hallway"})
    assert s.scene_id == "ep001_sc02" and s.beats == []
    assert s.scene_metadata == {"location": "hallway"}


def test_scene_with_beats():
    s = Scene(scene_id="ep001_sc02",
              beats=[Beat(beat_id="EP001_SH02"), Beat(beat_id="EP001_SH03")])
    assert len(s.beats) == 2


def test_scene_rejects_invalid_construction():
    with pytest.raises(ValueError):
        Scene(scene_id="")
    with pytest.raises(TypeError):
        Scene(scene_id="s1", beats="not a list")  # type: ignore
    with pytest.raises(TypeError):
        Scene(scene_id="s1", beats=[{"beat_id": "b1"}])  # type: ignore


def test_scene_rejects_duplicate_beat_id():
    with pytest.raises(ValueError):
        Scene(scene_id="s1", beats=[Beat(beat_id="dup"), Beat(beat_id="dup")])


def test_scene_add_beat_and_dup_check():
    s = Scene(scene_id="s1")
    s.add_beat(Beat(beat_id="b1"))
    assert len(s.beats) == 1
    with pytest.raises(ValueError):
        s.add_beat(Beat(beat_id="b1"))
    with pytest.raises(TypeError):
        s.add_beat({"beat_id": "b2"})  # type: ignore
