"""Tests for the pending_qc, icu_escalated, and needs_review shot states."""
import pytest


def test_pending_qc_is_valid_status():
    from recoil.execution.execution_store import SHOT_VALID_STATUSES
    assert "pending_qc" in SHOT_VALID_STATUSES
    assert "icu_escalated" in SHOT_VALID_STATUSES
    assert "needs_review" in SHOT_VALID_STATUSES


def test_keyframe_can_transition_to_pending_qc():
    from recoil.execution.execution_store import VALID_TRANSITIONS
    # From keyframe_generating, we should be able to enter pending_qc
    assert "pending_qc" in VALID_TRANSITIONS.get("keyframe_generating", frozenset())
    # Same from video_processing
    assert "pending_qc" in VALID_TRANSITIONS.get("video_processing", frozenset())


def test_pending_qc_can_transition_to_terminal_states():
    from recoil.execution.execution_store import VALID_TRANSITIONS
    pending_transitions = VALID_TRANSITIONS.get("pending_qc", frozenset())
    # Re-check can promote to:
    assert "keyframe_generated" in pending_transitions
    assert "video_complete" in pending_transitions
    # Or escalate to:
    assert "needs_review" in pending_transitions
    assert "icu_escalated" in pending_transitions


def test_icu_escalated_has_outgoing_transitions():
    """icu_escalated must NOT be a dead-end state — JT needs to be able to
    move shots out of ICU after manual review."""
    from recoil.execution.execution_store import VALID_TRANSITIONS
    icu_transitions = VALID_TRANSITIONS.get("icu_escalated", frozenset())
    assert len(icu_transitions) > 0
    # At minimum, manual review should route to needs_review
    assert "needs_review" in icu_transitions or "abandoned" in icu_transitions


def test_pending_qc_state_is_persisted_after_update(tmp_path):
    """Behavioral test (Opus Finding 23): actually call update_shot and verify
    the new state persists through the store, not just a dict lookup."""
    from recoil.execution.execution_store import ExecutionStore
    store = ExecutionStore(project="test_phase_2_5", db_path=tmp_path)
    store.insert_shot({
        "shot_id": "EP001_SH01",
        "status": "keyframe_generating",
        "scene_index": 0,
    })
    store.update_shot("EP001_SH01", status="pending_qc")
    shot = store.get_shot("EP001_SH01")
    assert shot is not None
    assert shot["status"] == "pending_qc"
