from __future__ import annotations

import importlib.util
import json
import sys
from pathlib import Path
from types import ModuleType

import numpy as np
import pytest


def _worker_module() -> ModuleType:
    worker_root = Path(__file__).parents[1] / "experiments" / "perception" / "worker"
    path = worker_root / "run_e8_realtime_tracking.py"
    sys.path.insert(0, str(worker_root))
    try:
        spec = importlib.util.spec_from_file_location("e8_realtime_tracking_worker", path)
        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)


def _profile_path(name: str = "e8_realtime_tracking_profile.json") -> Path:
    return Path(__file__).parents[1] / "experiments" / "perception" / "worker" / name


def _envelope(worker: ModuleType, index: int) -> object:
    return worker.FrameEnvelope(
        frame_index=index,
        path=Path(f"frame-{index:06d}.png"),
        timeline={"source_frame_index": 1000 + index},
        scheduled_monotonic=float(index),
        decoded_monotonic=float(index),
        image=np.zeros((1, 1, 3), dtype=np.uint8),
        decode_ms=1.0,
        source_release_lag_ms=0.0,
    )


def test_e8_profile_pins_realtime_policy_and_e5_model() -> None:
    worker = _worker_module()

    profile, digest = worker._read_profile(_profile_path())

    assert len(digest) == 64
    assert profile["model"]["id"] == "yolox_s"
    assert profile["realtime"]["queue_policy"] == "bounded-latest-wins"
    assert profile["realtime"]["queue_capacity"] == 2
    assert profile["acceptance"]["minimum_effective_fps"] == 9.5
    assert profile["acceptance"]["expect_overload"] is False


def test_e8_overload_profile_requires_observed_overload() -> None:
    worker = _worker_module()

    profile, _digest = worker._read_profile(
        _profile_path("e8_realtime_tracking_overload_profile.json")
    )

    assert profile["mode"] == "overload-negative-control"
    assert profile["realtime"]["consumer_delay_ms"] == 140.0
    assert profile["acceptance"]["expect_overload"] is True


def test_e8_profile_rejects_unbounded_queue(tmp_path: Path) -> None:
    worker = _worker_module()
    profile = json.loads(_profile_path().read_text(encoding="utf-8"))
    profile["realtime"]["queue_capacity"] = 0
    path = tmp_path / "profile.json"
    path.write_text(json.dumps(profile), encoding="utf-8")

    with pytest.raises(RuntimeError, match="scheduling"):
        worker._read_profile(path)


def test_e8_latest_wins_queue_discards_oldest_frame() -> None:
    worker = _worker_module()
    queue = worker.LatestWinsQueue(capacity=2)
    queue.publish(_envelope(worker, 1))
    queue.publish(_envelope(worker, 2))
    queue.publish(_envelope(worker, 3))
    queue.close()

    assert queue.take().frame_index == 2
    assert queue.take().frame_index == 3
    assert queue.take() is None
    assert queue.snapshot() == {
        "capacity": 2,
        "final_depth": 0,
        "maximum_depth": 2,
        "published": 3,
        "consumed": 2,
        "dropped_overflow": 1,
        "closed": True,
    }


def test_e8_health_uses_explicit_freshness_thresholds() -> None:
    worker = _worker_module()
    realtime = {"stale_after_ms": 150.0, "unavailable_after_ms": 500.0}

    assert worker._health(149.9, realtime) == "healthy"
    assert worker._health(150.0, realtime) == "stale"
    assert worker._health(500.0, realtime) == "unavailable"
