"""End-to-end: clusterer vs real ep_001_plan.json.

Reads the actual production plan via load_plan() and asserts the batch
count matches the expectations in _recon_dispatch_table.md.
"""
from __future__ import annotations

import sys
from pathlib import Path

import pytest

_REPO_ROOT = Path(__file__).resolve().parents[4]
if str(_REPO_ROOT) not in sys.path:
    sys.path.insert(0, str(_REPO_ROOT))

EP001_PLAN = _REPO_ROOT / "projects" / "tartarus" / "state" / "visual" / "plans" / "ep_001_plan.json"


@pytest.mark.skipif(
    not EP001_PLAN.exists(),
    reason="EP001 plan not available; skipping integration test",
)
def test_dry_run_against_real_ep001_plan_emits_expected_batches(tmp_path):
    """Dispatch table groups EP001 into 9-11 scene-coherent batches (max=4, JT 2026-05-17)."""
    from recoil.pipeline._lib.plan_loader import load_plan
    from recoil.pipeline._lib.scene_clusterer import cluster_shots_into_batches

    plan = load_plan(EP001_PLAN)
    assert len(plan.shots) == 37, f"expected 37 shots, got {len(plan.shots)}"

    batches = cluster_shots_into_batches(plan.shots)
    assert 9 <= len(batches) <= 11, (
        f"expected 9-11 batches (max_batch_size=4), got {len(batches)}: "
        + ", ".join(b.batch_id for b in batches)
    )

    # Each batch is location-coherent.
    for b in batches:
        locs = {s.location_id for s in b.shots}
        assert len(locs) == 1, f"{b.batch_id} crosses locations: {locs}"

    # Every shot ended up in some batch.
    flat = [s.shot_id for b in batches for s in b.shots]
    assert len(flat) == 37
    assert len(set(flat)) == 37
