impl SnapshotReport {
    pub fn from_state(state: &LauncherState, log: &DiagnosticsLog, probe_command: String) -> Self {
        let left_capture = state
            .display_capture_size_choice(0)
            .unwrap_or_else(|| state.capture_size_choice(0));
        let right_capture = state
            .display_capture_size_choice(1)
            .unwrap_or_else(|| state.capture_size_choice(1));
        let left_breakout = state.breakout_size_choice(0);
        let right_breakout = state.breakout_size_choice(1);
        let latest = log.latest();
        let left_stream_caps = latest
            .map(|sample| sample.left_stream_caps_label.clone())
            .unwrap_or_default();
        let right_stream_caps = latest
            .map(|sample| sample.right_stream_caps_label.clone())
            .unwrap_or_default();
        let upstream_camera = latest
            .map(|sample| sample.upstream_camera.clone())
            .unwrap_or_default();
        let upstream_microphone = latest
            .map(|sample| sample.upstream_microphone.clone())
            .unwrap_or_default();
        let upstream_mode = latest
            .map(|sample| sample.upstream_mode.clone())
            .filter(|mode| !mode.trim().is_empty())
            .unwrap_or_else(|| "unknown".to_string());
        let av_delivery_skew_ms =
            (upstream_camera.latest_delivery_age_ms - upstream_microphone.latest_delivery_age_ms)
                .abs();
        let av_enqueue_skew_ms =
            (upstream_camera.latest_enqueue_age_ms - upstream_microphone.latest_enqueue_age_ms)
                .abs();
        let av_sync_health = av_sync_health_label(
            &upstream_camera,
            &upstream_microphone,
            av_delivery_skew_ms,
            av_enqueue_skew_ms,
        )
        .to_string();
        Self {
            client_version: crate::VERSION.to_string(),
            server_version: state.server_version.clone(),
            server_available: state.server_available,
            routing: state.routing,
            view_mode: state.view_mode,
            remote_active: state.remote_active,
            power_state: format!(
                "{} | {} | leases {}",
                state.capture_power.mode,
                state.capture_power.detail,
                state.capture_power.active_leases
            ),
            client_process_cpu_pct: latest
                .map(|sample| sample.client_process_cpu_pct)
                .unwrap_or(0.0),
            server_process_cpu_pct: latest
                .map(|sample| sample.server_process_cpu_pct)
                .unwrap_or(0.0),
            preview_source: format!(
                "{}x{} @ {} fps",
                state.preview_source.width, state.preview_source.height, state.preview_source.fps
            ),
            client_display_limit: format!(
                "{}x{}",
                state.breakout_display.width, state.breakout_display.height
            ),
            left_surface: state.display_surface(0).label().to_string(),
            left_feed_source: match state.feed_source_preset(0) {
                super::state::FeedSourcePreset::ThisEye => "Left Eye".to_string(),
                super::state::FeedSourcePreset::OtherEye => "Right Eye (mirrored)".to_string(),
                super::state::FeedSourcePreset::Off => "Off".to_string(),
            },
            left_capture_profile: capture_profile_label(&left_capture, &left_stream_caps),
            left_capture_transport: left_capture.preset.transport_label().to_string(),
            left_breakout_profile: format!(
                "{} | {}x{}",
                left_breakout.preset.label(),
                left_breakout.width,
                left_breakout.height
            ),
            left_decoder_label: latest
                .map(|sample| {
                    if sample.left_decoder_label.is_empty() {
                        "pending".to_string()
                    } else {
                        sample.left_decoder_label.clone()
                    }
                })
                .unwrap_or_else(|| "pending".to_string()),
            left_stream_spread_ms: latest
                .map(|sample| sample.left_stream_spread_ms)
                .unwrap_or(0.0),
            left_packet_gap_peak_ms: latest
                .map(|sample| sample.left_packet_gap_peak_ms)
                .unwrap_or(0.0),
            left_present_gap_peak_ms: latest
                .map(|sample| sample.left_present_gap_peak_ms)
                .unwrap_or(0.0),
            left_queue_depth: latest.map(|sample| sample.left_queue_depth).unwrap_or(0),
            left_queue_peak: latest.map(|sample| sample.left_queue_peak).unwrap_or(0),
            left_server_source_gap_peak_ms: latest
                .map(|sample| sample.left_server_source_gap_peak_ms)
                .unwrap_or(0.0),
            left_server_send_gap_peak_ms: latest
                .map(|sample| sample.left_server_send_gap_peak_ms)
                .unwrap_or(0.0),
            left_server_queue_peak: latest
                .map(|sample| sample.left_server_queue_peak)
                .unwrap_or(0),
            left_server_encoder_label: latest
                .map(|sample| {
                    if sample.left_server_encoder_label.is_empty() {
                        "pending".to_string()
                    } else {
                        sample.left_server_encoder_label.clone()
                    }
                })
                .unwrap_or_else(|| "pending".to_string()),
            left_stream_caps_label: latest
                .map(|sample| {
                    if sample.left_stream_caps_label.is_empty() {
                        "pending".to_string()
                    } else {
                        sample.left_stream_caps_label.clone()
                    }
                })
                .unwrap_or_else(|| "pending".to_string()),
            left_decoded_caps_label: latest
                .map(|sample| {
                    if sample.left_decoded_caps_label.is_empty() {
                        "pending".to_string()
                    } else {
                        sample.left_decoded_caps_label.clone()
                    }
                })
                .unwrap_or_else(|| "pending".to_string()),
            left_rendered_caps_label: latest
                .map(|sample| {
                    if sample.left_rendered_caps_label.is_empty() {
                        "pending".to_string()
                    } else {
                        sample.left_rendered_caps_label.clone()
                    }
                })
                .unwrap_or_else(|| "pending".to_string()),
            right_surface: state.display_surface(1).label().to_string(),
            right_feed_source: match state.feed_source_preset(1) {
                super::state::FeedSourcePreset::ThisEye => "Right Eye".to_string(),
                super::state::FeedSourcePreset::OtherEye => "Left Eye (mirrored)".to_string(),
                super::state::FeedSourcePreset::Off => "Off".to_string(),
            },
            right_capture_profile: capture_profile_label(&right_capture, &right_stream_caps),
            right_capture_transport: right_capture.preset.transport_label().to_string(),
            right_breakout_profile: format!(
                "{} | {}x{}",
                right_breakout.preset.label(),
                right_breakout.width,
                right_breakout.height
            ),
            right_decoder_label: latest
                .map(|sample| {
                    if sample.right_decoder_label.is_empty() {
                        "pending".to_string()
                    } else {
                        sample.right_decoder_label.clone()
                    }
                })
                .unwrap_or_else(|| "pending".to_string()),
            right_stream_spread_ms: latest
                .map(|sample| sample.right_stream_spread_ms)
                .unwrap_or(0.0),
            right_packet_gap_peak_ms: latest
                .map(|sample| sample.right_packet_gap_peak_ms)
                .unwrap_or(0.0),
            right_present_gap_peak_ms: latest
                .map(|sample| sample.right_present_gap_peak_ms)
                .unwrap_or(0.0),
            right_queue_depth: latest.map(|sample| sample.right_queue_depth).unwrap_or(0),
            right_queue_peak: latest.map(|sample| sample.right_queue_peak).unwrap_or(0),
            right_server_source_gap_peak_ms: latest
                .map(|sample| sample.right_server_source_gap_peak_ms)
                .unwrap_or(0.0),
            right_server_send_gap_peak_ms: latest
                .map(|sample| sample.right_server_send_gap_peak_ms)
                .unwrap_or(0.0),
            right_server_queue_peak: latest
                .map(|sample| sample.right_server_queue_peak)
                .unwrap_or(0),
            right_server_encoder_label: latest
                .map(|sample| {
                    if sample.right_server_encoder_label.is_empty() {
                        "pending".to_string()
                    } else {
                        sample.right_server_encoder_label.clone()
                    }
                })
                .unwrap_or_else(|| "pending".to_string()),
            right_stream_caps_label: latest
                .map(|sample| {
                    if sample.right_stream_caps_label.is_empty() {
                        "pending".to_string()
                    } else {
                        sample.right_stream_caps_label.clone()
                    }
                })
                .unwrap_or_else(|| "pending".to_string()),
            right_decoded_caps_label: latest
                .map(|sample| {
                    if sample.right_decoded_caps_label.is_empty() {
                        "pending".to_string()
                    } else {
                        sample.right_decoded_caps_label.clone()
                    }
                })
                .unwrap_or_else(|| "pending".to_string()),
            right_rendered_caps_label: latest
                .map(|sample| {
                    if sample.right_rendered_caps_label.is_empty() {
                        "pending".to_string()
                    } else {
                        sample.right_rendered_caps_label.clone()
                    }
                })
                .unwrap_or_else(|| "pending".to_string()),
            selected_camera: state.devices.camera.clone(),
            camera_quality_label: state
                .camera_quality
                .map(CameraMode::short_label)
                .unwrap_or_else(|| "default".to_string()),
            upstream_camera_transport: state.effective_webcam_transport().label().to_string(),
            selected_microphone: state.devices.microphone.clone(),
            selected_speaker: state.devices.speaker.clone(),
            media_channels: MediaChannelState {
                camera: state.channels.camera,
                microphone: state.channels.microphone,
                audio: state.channels.audio,
            },
            audio_gain_label: state.audio_gain_label(),
            mic_gain_label: state.mic_gain_label(),
            upstream_camera,
            upstream_microphone,
            upstream_mode,
            av_delivery_skew_ms,
            av_enqueue_skew_ms,
            av_sync_health,
            planner_available: state.upstream_sync.available,
            planner_phase: state.upstream_sync.phase.clone(),
            planner_session_id: state.upstream_sync.session_id,
            planner_live_lag_ms: state.upstream_sync.live_lag_ms,
            planner_skew_ms: state.upstream_sync.planner_skew_ms,
            planner_stale_audio_drops: state.upstream_sync.stale_audio_drops,
            planner_stale_video_drops: state.upstream_sync.stale_video_drops,
            planner_skew_video_drops: state.upstream_sync.skew_video_drops,
            planner_freshness_reanchors: state.upstream_sync.freshness_reanchors,
            planner_startup_timeouts: state.upstream_sync.startup_timeouts,
            planner_video_freezes: state.upstream_sync.video_freezes,
            planner_detail: state.upstream_sync.detail.clone(),
            calibration_available: state.calibration.available,
            calibration_profile: state.calibration.profile.clone(),
            calibration_source: state.calibration.source.clone(),
            calibration_confidence: state.calibration.confidence.clone(),
            calibration_detail: state.calibration.detail.clone(),
            calibration_updated_at: state.calibration.updated_at.clone(),
            factory_audio_offset_us: state.calibration.factory_audio_offset_us,
            factory_video_offset_us: state.calibration.factory_video_offset_us,
            default_audio_offset_us: state.calibration.default_audio_offset_us,
            default_video_offset_us: state.calibration.default_video_offset_us,
            active_audio_offset_us: state.calibration.active_audio_offset_us,
            active_video_offset_us: state.calibration.active_video_offset_us,
            selected_keyboard: state.devices.keyboard.clone(),
            selected_mouse: state.devices.mouse.clone(),
            status: state.status_line(),
            recent_samples: log.iter().cloned().collect(),
            notes: state.notes.clone(),
            recommendations: recommendations_for(state, log),
            probe_command,
        }
    }
}
const AV_SYNC_GOOD_MS: f32 = 35.0;
const AV_SYNC_WATCH_MS: f32 = 80.0;

fn av_sync_health_label(
    camera: &crate::uplink_telemetry::UpstreamStreamTelemetry,
    microphone: &crate::uplink_telemetry::UpstreamStreamTelemetry,
    delivery_skew_ms: f32,
    enqueue_skew_ms: f32,
) -> &'static str {
    if !camera.enabled || !microphone.enabled {
        return "incomplete (camera/mic stream disabled)";
    }
    if !camera.connected || !microphone.connected {
        return "incomplete (uplink not fully connected)";
    }
    let skew = delivery_skew_ms.max(enqueue_skew_ms);
    if skew <= AV_SYNC_GOOD_MS {
        "stable"
    } else if skew <= AV_SYNC_WATCH_MS {
        "watch"
    } else {
        "drift risk"
    }
}
