// Compatibility coverage for profile-specific server-to-RCT calibration.
//
// Scope: ensure the calibrated MJPEG and HEVC ingress profiles both cover every
// supported UVC mode.
// Targets: server/src/calibration/profile_offsets.rs and
// scripts/manual/run_server_to_rc_mode_matrix.sh.
// Why: the HEVC path decodes and re-encodes before UVC, so it must never
// silently borrow the MJPEG timing map unless explicitly asked.

use lesavka_server::calibration::{
    FACTORY_HEVC_AUDIO_MODE_OFFSETS_US, FACTORY_HEVC_OPUS_AUDIO_MODE_OFFSETS_US,
    FACTORY_HEVC_OPUS_VIDEO_MODE_OFFSETS_US, FACTORY_HEVC_VIDEO_MODE_OFFSETS_US,
    FACTORY_MJPEG_AUDIO_MODE_OFFSETS_US, FACTORY_MJPEG_OPUS_AUDIO_MODE_OFFSETS_US,
    FACTORY_MJPEG_OPUS_VIDEO_MODE_OFFSETS_US, FACTORY_MJPEG_PCM_AUDIO_MODE_OFFSETS_US,
    FACTORY_MJPEG_VIDEO_MODE_OFFSETS_US,
};

const MATRIX_SCRIPT: &str = include_str!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/scripts/manual/run_server_to_rc_mode_matrix.sh"
));

const SUPPORTED_MODES: [&str; 4] = ["1280x720@20", "1280x720@30", "1920x1080@20", "1920x1080@30"];

fn map_contains_mode(map: &str, mode: &str) -> bool {
    map.split(',')
        .any(|entry| entry.starts_with(&format!("{mode}=")))
}

#[test]
fn hevc_and_mjpeg_factory_maps_cover_all_supported_uvc_modes() {
    for mode in SUPPORTED_MODES {
        assert!(map_contains_mode(FACTORY_MJPEG_AUDIO_MODE_OFFSETS_US, mode));
        assert!(map_contains_mode(FACTORY_MJPEG_VIDEO_MODE_OFFSETS_US, mode));
        assert!(map_contains_mode(
            FACTORY_MJPEG_OPUS_AUDIO_MODE_OFFSETS_US,
            mode
        ));
        assert!(map_contains_mode(
            FACTORY_MJPEG_OPUS_VIDEO_MODE_OFFSETS_US,
            mode
        ));
        assert!(map_contains_mode(FACTORY_HEVC_AUDIO_MODE_OFFSETS_US, mode));
        assert!(map_contains_mode(FACTORY_HEVC_VIDEO_MODE_OFFSETS_US, mode));
        assert!(map_contains_mode(
            FACTORY_HEVC_OPUS_VIDEO_MODE_OFFSETS_US,
            mode
        ));
    }
}

#[test]
fn hevc_profile_defaults_are_separate_from_mjpeg_profile_defaults() {
    assert_ne!(
        FACTORY_MJPEG_VIDEO_MODE_OFFSETS_US, FACTORY_HEVC_VIDEO_MODE_OFFSETS_US,
        "HEVC decode-to-MJPEG should retain its own calibrated timing profile"
    );
    assert_eq!(
        FACTORY_MJPEG_PCM_AUDIO_MODE_OFFSETS_US,
        FACTORY_HEVC_AUDIO_MODE_OFFSETS_US
    );
    assert_ne!(
        FACTORY_MJPEG_OPUS_AUDIO_MODE_OFFSETS_US, FACTORY_MJPEG_AUDIO_MODE_OFFSETS_US,
        "MJPEG+Opus should preserve the blind client-to-RCT audio calibration"
    );
    assert_ne!(
        FACTORY_MJPEG_OPUS_VIDEO_MODE_OFFSETS_US, FACTORY_MJPEG_VIDEO_MODE_OFFSETS_US,
        "MJPEG+Opus should not silently borrow the PCM video timing map"
    );
    assert_eq!(
        FACTORY_HEVC_OPUS_AUDIO_MODE_OFFSETS_US, FACTORY_HEVC_AUDIO_MODE_OFFSETS_US,
        "HEVC+Opus keeps the PCM audio baseline until a dedicated lab pass lands"
    );
}

#[test]
fn mode_matrix_script_can_calibrate_profiles_without_overwriting_each_other() {
    for marker in [
        "LESAVKA_SERVER_RC_PROFILE=${LESAVKA_SERVER_RC_PROFILE:-mjpeg}",
        "LESAVKA_SERVER_RC_NORMALIZED_PROFILE=hevc",
        "LESAVKA_SERVER_RC_HEVC_MODE_DELAYS_US",
        "LESAVKA_UPSTREAM_MJPEG_VIDEO_PLAYOUT_MODE_OFFSETS_US",
        "LESAVKA_UPSTREAM_HEVC_VIDEO_PLAYOUT_MODE_OFFSETS_US",
        "LESAVKA_UPSTREAM_HEVC_OPUS_AUDIO_PLAYOUT_MODE_OFFSETS_US",
    ] {
        assert!(
            MATRIX_SCRIPT.contains(marker),
            "mode matrix should preserve profile marker {marker}"
        );
    }
}
