from __future__ import annotations

import importlib.util
import io
import json
import sys
from argparse import Namespace
from pathlib import Path

import pytest


def _module() -> object:
    perception = Path(__file__).resolve().parents[1] / "experiments" / "perception"
    worker = perception / "worker"
    sys.path.insert(0, str(perception))
    sys.path.insert(0, str(worker))
    try:
        spec = importlib.util.spec_from_file_location(
            "e15_shadow_inference_test",
            worker / "run_e15_shadow_inference.py",
        )
        assert spec is not None and spec.loader is not None
        module = importlib.util.module_from_spec(spec)
        sys.modules[spec.name] = module
        spec.loader.exec_module(module)
        return module
    finally:
        sys.path.pop(0)
        sys.path.pop(0)


def test_e15_profile_pins_replay_shadow_authority_and_bounded_runtime() -> None:
    module = _module()
    profile_path = (
        Path(__file__).resolve().parents[1]
        / "experiments"
        / "perception"
        / "worker"
        / "e15_shadow_inference_profile.json"
    )

    profile, digest = module.read_live_profile(profile_path)

    assert len(digest) == 64
    assert profile["mode"] == "replay-shadow-gate"
    assert profile["authority"] == {
        "commands_enabled": False,
        "navigation_or_safety_accepted": False,
    }
    assert profile["transport"] == {
        "wire_schema": "missioncore.live-perception-wire/v1",
        "camera_media": "persistent-fmp4-pyav",
        "pyav_version": "18.0.0",
        "maximum_media_buffer_bytes": 8 * 1024 * 1024,
        "camera_metadata_capacity": 16,
    }
    assert profile["scheduling"] == {
        "detector_queue_capacity": 2,
        "semantic_queue_capacity": 1,
        "semantic_sample_every_frames": 5,
        "semantic_ttl_ms": 750.0,
        "sensor_wait_ms": 90.0,
    }
    assert profile["acceptance"]["minimum_fused_fraction"] == 0.85
    assert profile["acceptance"]["maximum_p95_world_state_age_ms"] == 200.0


def test_e15_profile_rejects_command_authority(tmp_path: Path) -> None:
    module = _module()
    source = (
        Path(__file__).resolve().parents[1]
        / "experiments"
        / "perception"
        / "worker"
        / "e15_shadow_inference_profile.json"
    )
    value = json.loads(source.read_text())
    value["authority"]["commands_enabled"] = True
    changed = tmp_path / "unsafe-profile.json"
    changed.write_text(json.dumps(value))

    with pytest.raises(RuntimeError, match="profile contract"):
        module.read_live_profile(changed)


def test_persistent_worker_request_is_single_run_d_backed_and_token_bounded(
    tmp_path: Path,
) -> None:
    module = _module()
    service = Namespace(
        output_root=tmp_path,
        listen_host="127.0.0.1",
        listen_port=18020,
        command="serve",
        max_duration_seconds=180.0,
        marker="preserved",
    )

    run = module._persistent_run_arguments(
        service,
        {
            "request_id": "physical-k1-shadow-001",
            "output_name": "physical-k1-shadow-001",
            "token": "a" * 64,
            "max_duration_seconds": 90,
        },
    )

    assert run.command == "run"
    assert run.output == tmp_path / "physical-k1-shadow-001"
    assert run.token == "a" * 64
    assert run.token_stdin is False
    assert run.max_duration_seconds == 90.0
    assert run.marker == "preserved"
    assert not hasattr(run, "listen_host")
    assert not hasattr(run, "output_root")

    with pytest.raises(RuntimeError, match="request contract"):
        module._persistent_run_arguments(
            service,
            {
                "request_id": "physical-k1-shadow-002",
                "output_name": "physical-k1-shadow-002",
                "token": "short",
            },
        )
    with pytest.raises(RuntimeError, match="request contract"):
        module._persistent_run_arguments(
            service,
            {
                "request_id": "physical-k1-shadow-004",
                "output_name": "physical-k1-shadow-004",
                "token": "b" * 64,
                "max_duration_seconds": 181,
            },
        )
    with pytest.raises(RuntimeError, match="request contract"):
        module._persistent_run_arguments(
            service,
            {
                "request_id": "physical-k1-shadow-003",
                "output_name": "../escape",
                "token": "b" * 64,
            },
        )


def test_runtime_telemetry_captures_bounded_queue_snapshots() -> None:
    module = _module()
    stream = io.StringIO()
    telemetry = module._RuntimeTelemetry(
        stream,
        interval_seconds=1.0,
        snapshotters={
            "detector": lambda: {
                "capacity": 2,
                "maximum_depth": 1,
                "final_depth": 0,
            }
        },
    )

    telemetry._sample()
    summary = telemetry.summary()

    assert summary["sample_count"] == 1
    assert summary["final_queues"]["detector"]["capacity"] == 2
    row = json.loads(stream.getvalue())
    assert row["schema_version"] == "missioncore.worker-runtime-telemetry/v1"
    assert row["queues"]["detector"]["maximum_depth"] == 1
