#![allow(dead_code)]

// Integration coverage for live media control-file propagation.
//
// Scope: include the shared live-media control grammar and exercise real file
// writes/refreshes using temporary files.
// Targets: `client/src/live_media_control.rs`.
// Why: live device switching must restart only the selected media feed without
// forcing a relay reconnect or silently keeping stale device choices.

#[path = "../../../../client/src/live_media_control.rs"]
#[allow(dead_code)]
mod live_media_control;

use lesavka_common::audio_transport::UpstreamAudioCodec;
use live_media_control::{
    LiveMediaControls, MEDIA_CONTROL_ENV, MediaControlState, write_media_control_request,
};
use temp_env::with_var;

#[test]
fn live_device_switch_writes_and_reads_all_selected_media_choices() {
    let state = MediaControlState::with_devices_and_codecs(
        true,
        true,
        true,
        Some("/dev/video-lesavka-brio".to_string()),
        Some("1280x720@30".to_string()),
        Some("alsa_input.usb-focusrite.analog-stereo".to_string()),
        Some("alsa_output.usb-headphones.analog-stereo".to_string()),
        Some("hevc".to_string()),
        UpstreamAudioCodec::Opus,
        true,
    );
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("media.control");

    write_media_control_request(&path, state.clone()).expect("write live media controls");
    let raw = std::fs::read_to_string(&path).expect("read live media controls");

    for marker in [
        "camera=1",
        "microphone=1",
        "audio=1",
        "camera_source=b64:",
        "camera_profile=b64:",
        "microphone_source=b64:",
        "audio_sink=b64:",
        "camera_codec=hevc",
        "audio_codec=opus",
        "noise_suppression=1",
        "nonce=",
    ] {
        assert!(
            raw.contains(marker),
            "control file should include {marker}: {raw}"
        );
    }
    with_var(
        MEDIA_CONTROL_ENV,
        Some(path.to_string_lossy().as_ref()),
        || {
            let controls = LiveMediaControls::from_env(MediaControlState::new(false, false, false));
            assert_eq!(controls.refresh(), state);
        },
    );
}

#[test]
fn malformed_live_control_update_keeps_last_good_state() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("media.control");
    let first = MediaControlState::with_devices(
        true,
        false,
        true,
        Some("camera-a".to_string()),
        Some("1920x1080@30".to_string()),
        None,
        Some("speaker-a".to_string()),
    );
    write_media_control_request(&path, first.clone()).expect("write first control state");

    with_var(
        MEDIA_CONTROL_ENV,
        Some(path.to_string_lossy().as_ref()),
        || {
            let controls = LiveMediaControls::from_env(MediaControlState::new(false, false, false));
            assert_eq!(controls.refresh(), first);

            std::fs::write(&path, "camera=maybe microphone=1 audio=1").expect("write bad state");
            assert_eq!(
                controls.refresh(),
                first,
                "bad live-control files should not replace the last known safe state"
            );
        },
    );
}
