"""SyntheticAdapter produces a deterministic straight-line trajectory."""

import numpy as np

from gps_denied.testing.datasets.base import PlatformClass
from gps_denied.testing.datasets.synthetic import SyntheticAdapter


def test_synthetic_has_raw_imu():
    adapter = SyntheticAdapter(num_frames=10, fps=5.0)
    assert adapter.capabilities.has_raw_imu is True
    assert adapter.capabilities.platform_class == PlatformClass.SYNTHETIC


def test_synthetic_iter_frames_count():
    adapter = SyntheticAdapter(num_frames=10, fps=5.0)
    frames = list(adapter.iter_frames())
    assert len(frames) == 10
    assert frames[0].frame_idx == 0
    assert frames[-1].frame_idx == 9


def test_synthetic_frame_timestamps_monotonic():
    adapter = SyntheticAdapter(num_frames=10, fps=5.0)
    ts = [f.timestamp_ns for f in adapter.iter_frames()]
    assert ts == sorted(ts)
    # 5 fps → 200 ms spacing
    deltas = np.diff(ts)
    assert np.allclose(deltas, 200_000_000)


def test_synthetic_imu_samples_cover_frames():
    adapter = SyntheticAdapter(num_frames=10, fps=5.0, imu_rate_hz=100.0)
    imu = list(adapter.iter_imu())
    # 10 frames at 5 fps = 2 s of data, at 100 Hz = 200 samples
    assert len(imu) == 200
    # Static trajectory → gravity on z, zero gyro
    assert abs(imu[0].accel[2] + 9.81) < 1e-6
    assert imu[0].gyro == (0.0, 0.0, 0.0)


def test_synthetic_ground_truth_matches_frames():
    adapter = SyntheticAdapter(num_frames=10, fps=5.0)
    poses = list(adapter.iter_ground_truth())
    assert len(poses) == 10
    # Straight-line east at 10 m/s → position at t=0.2s is 2m east of origin
    # (synthetic puts GT at same timestamps as frames)
    assert abs(poses[1].lat - poses[0].lat) < 1e-9  # no latitude change
    assert poses[1].lon > poses[0].lon  # moving east
