"""MARSLVIGAdapter — reads pre-extracted per-topic files from a sequence dir."""

from pathlib import Path

import pytest

from gps_denied.testing.datasets.base import (
    DatasetNotAvailableError,
    PlatformClass,
)
from gps_denied.testing.datasets.mars_lvig import MARSLVIGAdapter


@pytest.fixture
def fake_mars_seq(tmp_path: Path) -> Path:
    seq = tmp_path / "HKairport_01"
    (seq / "images").mkdir(parents=True)
    for ts in (100_000_000, 200_000_000):
        (seq / "images" / f"{ts}.jpg").write_bytes(b"\xff\xd8\xff\xd9")
    (seq / "imu.csv").write_text(
        "timestamp_ns,ax,ay,az,gx,gy,gz\n"
        "100000000,0.0,0.0,-9.81,0.0,0.0,0.0\n"
        "200000000,0.0,0.0,-9.81,0.0,0.0,0.0\n"
    )
    (seq / "gnss.csv").write_text(
        "timestamp_ns,lat,lon,alt\n"
        "100000000,22.32,114.17,100.0\n"
        "200000000,22.3201,114.1702,100.0\n"
    )
    return seq


def test_raises_when_missing(tmp_path: Path):
    with pytest.raises(DatasetNotAvailableError):
        MARSLVIGAdapter(tmp_path / "nope")


def test_capabilities(fake_mars_seq: Path):
    adapter = MARSLVIGAdapter(fake_mars_seq)
    cap = adapter.capabilities
    assert cap.has_raw_imu is True
    assert cap.has_rtk_gt is True
    assert cap.platform_class == PlatformClass.ROTARY


def test_iter_frames(fake_mars_seq: Path):
    adapter = MARSLVIGAdapter(fake_mars_seq)
    frames = list(adapter.iter_frames())
    assert len(frames) == 2
    assert frames[0].timestamp_ns == 100_000_000


def test_iter_imu(fake_mars_seq: Path):
    adapter = MARSLVIGAdapter(fake_mars_seq)
    imu = list(adapter.iter_imu())
    assert len(imu) == 2
    assert imu[0].accel == (0.0, 0.0, -9.81)


def test_iter_ground_truth(fake_mars_seq: Path):
    adapter = MARSLVIGAdapter(fake_mars_seq)
    gt = list(adapter.iter_ground_truth())
    assert len(gt) == 2
    assert gt[0].lat == pytest.approx(22.32)
