#[cfg(not(coverage))]
enum PowerMessage {
    Refresh(std::result::Result<CapturePowerStatus, String>),
    Command(std::result::Result<CapturePowerStatus, String>),
}

#[cfg(not(coverage))]
enum CalibrationMessage {
    Refresh(std::result::Result<CalibrationStatus, String>),
    Command(std::result::Result<CalibrationStatus, String>),
}

#[cfg(not(coverage))]
enum UpstreamSyncMessage {
    Refresh(std::result::Result<UpstreamSyncStatus, String>),
}

#[cfg(not(coverage))]
enum RelayMessage {
    Spawned(std::result::Result<RelayChild, String>),
}

#[cfg(not(coverage))]
enum CapsMessage {
    Refresh(HandshakeProbe),
}

#[cfg(not(coverage))]
enum ClipboardMessage {
    Finished(std::result::Result<String, String>),
}

#[cfg(not(coverage))]
const NETWORK_TELEMETRY_WINDOW: Duration = Duration::from_secs(8);

#[cfg(not(coverage))]
const UPSTREAM_EPOCH_HEAL_DISCONNECT_COOLDOWN: Duration = Duration::from_secs(3);

#[cfg(not(coverage))]
fn disconnect_cooldown_remaining(now: Instant, until: Option<Instant>) -> Option<Duration> {
    until
        .and_then(|deadline| deadline.checked_duration_since(now))
        .filter(|remaining| !remaining.is_zero())
}

#[cfg(not(coverage))]
fn disconnect_cooldown_status(remaining: Duration) -> String {
    let seconds = remaining.as_millis().saturating_add(999) / 1_000;
    format!("Upstream audio is refreshing; disconnect unlocks in {seconds}s.")
}

#[cfg(not(coverage))]
fn usb_audio_kernel_support_missing() -> bool {
    Command::new("modinfo")
        .arg("snd_usb_audio")
        .status()
        .map(|status| !status.success())
        .unwrap_or(true)
}

#[cfg(not(coverage))]
#[derive(Default)]
struct NetworkTelemetry {
    rtt_samples: VecDeque<(Instant, f32)>,
    failures: VecDeque<Instant>,
}

#[cfg(not(coverage))]
#[derive(Clone, Copy, Debug, Default)]
struct NetworkSnapshot {
    rtt_ms: f32,
    probe_spread_ms: f32,
    probe_loss_pct: f32,
}

#[cfg(not(coverage))]
impl NetworkTelemetry {
    fn record(&mut self, probe: &HandshakeProbe) {
        let now = Instant::now();
        self.trim(now);
        if let Some(rtt_ms) = probe.rtt_ms {
            self.rtt_samples.push_back((now, rtt_ms));
        } else {
            self.failures.push_back(now);
        }
        self.trim(now);
    }

    fn snapshot(&mut self) -> NetworkSnapshot {
        let now = Instant::now();
        self.trim(now);
        let rtt_ms = self.rtt_samples.back().map(|(_, rtt)| *rtt).unwrap_or(0.0);
        let probe_spread_ms = network_spread_ms(&self.rtt_samples);
        let probe_count = self.rtt_samples.len() + self.failures.len();
        let probe_loss_pct = if probe_count == 0 {
            0.0
        } else {
            self.failures.len() as f32 * 100.0 / probe_count as f32
        };
        NetworkSnapshot {
            rtt_ms,
            probe_spread_ms,
            probe_loss_pct,
        }
    }

    fn trim(&mut self, now: Instant) {
        while let Some((oldest, _)) = self.rtt_samples.front().copied() {
            if now.saturating_duration_since(oldest) > NETWORK_TELEMETRY_WINDOW {
                let _ = self.rtt_samples.pop_front();
            } else {
                break;
            }
        }
        while let Some(oldest) = self.failures.front().copied() {
            if now.saturating_duration_since(oldest) > NETWORK_TELEMETRY_WINDOW {
                let _ = self.failures.pop_front();
            } else {
                break;
            }
        }
    }
}

#[cfg(not(coverage))]
fn retained_stage_selection(current: Option<&str>, values: &[String]) -> Option<String> {
    current
        .filter(|selected| values.iter().any(|value| value == *selected))
        .map(str::to_string)
        .or_else(|| values.first().cloned())
}

#[cfg(not(coverage))]
fn retained_input_selection(current: Option<&str>, values: &[String]) -> Option<String> {
    current
        .filter(|selected| values.iter().any(|value| value == *selected))
        .map(str::to_string)
}

#[cfg(not(coverage))]
fn selected_camera_quality(combo: &gtk::ComboBoxText) -> Option<CameraMode> {
    combo.active_id().as_deref().and_then(CameraMode::from_id)
}

#[cfg(not(coverage))]
fn sync_camera_quality_selection(
    combo: &gtk::ComboBoxText,
    state: &mut LauncherState,
    catalog: &DeviceCatalog,
) {
    state.normalize_camera_quality(catalog);
    sync_camera_quality_combo(
        combo,
        &state.camera_quality_options(catalog),
        state.camera_quality,
    );
}
