//! DDC diagnostic tool — tests FFI operations outside the GUI.
//! Usage: cargo run --bin ddc-diag

fn main() {
    println!("=== Temperlux DDC Diagnostic ===\n");

    // Test 1: detect_displays (uses include_invalid=true + DDC verification)
    println!("--- Test 1: detect_displays() ---");
    match temperlux::display::ddc_ffi::detect_displays() {
        Ok(handles) => {
            println!("  Found {} valid displays", handles.len());
            for h in &handles {
                println!("    dispno={} model='{}' max={} current={}", 
                    h.display_num, h.model_name, h.max_val, h.last_val);
            }
        }
        Err(e) => {
            println!("  FAILED: {}", e);
        }
    }

    // Test 2: DdcService (single-thread handle ownership)
    println!("\n--- Test 2: DdcService ---");
    match temperlux::display::ddc_ffi::DdcService::start() {
        Ok((mut svc, displays)) => {
            println!("  Service started with {} displays", displays.len());
            for d in &displays {
                println!("    dispno={} model='{}' brightness={}/{}", 
                    d.display_num, d.model_name, d.current_brightness, d.max_brightness);
            }

            if displays.len() >= 2 {
                // Test 3: Send brightness commands through the service
                println!("\n--- Test 3: Service brightness commands ---");
                for d in &displays {
                    println!("  Sending set_brightness(50) to dispno={}", d.display_num);
                    svc.set_brightness(d.display_num, 50, 0);
                }

                // Wait for commands to process
                std::thread::sleep(std::time::Duration::from_secs(2));

                // Check latency
                for d in &displays {
                    let latency = svc.get_latency_ms(d.display_num);
                    println!("  dispno={} latency={}ms", d.display_num, latency);
                }

                // Verify with CLI
                println!("\n  CLI verification:");
                for d in &displays {
                    let output = std::process::Command::new("ddcutil")
                        .args(["getvcp", "10", "--display", &d.display_num.to_string(), "--brief"])
                        .output();
                    match output {
                        Ok(o) => {
                            let stdout = String::from_utf8_lossy(&o.stdout);
                            println!("    dispno={} CLI: {}", d.display_num, stdout.trim());
                        }
                        Err(e) => println!("    dispno={} CLI error: {}", d.display_num, e),
                    }
                }

                // Restore to 75
                for d in &displays {
                    svc.set_brightness(d.display_num, 75, 0);
                }
                std::thread::sleep(std::time::Duration::from_secs(1));
            }

            svc.shutdown();
        }
        Err(e) => {
            println!("  FAILED: {}", e);
        }
    }

    println!("\n=== Diagnostic complete ===");
}
