/*──────────────── main ───────────────────────*/
#[cfg(not(coverage))]
#[tokio::main(worker_threads = 4)]
async fn main() -> anyhow::Result<()> {
    let _guard = init_tracing()?;
    info!("🚀 {} v{} starting up", PKG_NAME, lesavka_server::VERSION);

    panic::set_hook(Box::new(|p| {
        let bt = Backtrace::force_capture();
        error!("💥 panic: {p}\n{bt}");
    }));

    let gadget = UsbGadget::new("lesavka");
    if std::env::var("LESAVKA_DISABLE_UVC").is_err() {
        if std::env::var("LESAVKA_UVC_EXTERNAL").is_ok() {
            info!("📷 UVC control helper external; not spawning");
        } else {
            let bin = uvc_runtime::uvc_ctrl_bin();
            tokio::spawn(uvc_runtime::supervise_uvc_control(bin));
        }
    } else {
        info!("📷 UVC disabled (LESAVKA_DISABLE_UVC set)");
    }
    let handler = Handler::new(gadget.clone()).await?;

    let bind_addr = server_bind_addr()?;
    info!("🌐 lesavka-server listening on {bind_addr}");
    Server::builder()
        .tcp_nodelay(true)
        .max_frame_size(Some(2 * 1024 * 1024))
        .add_service(RelayServer::new(handler))
        .add_service(HandshakeSvc::server())
        .add_service(ReflBuilder::configure().build_v1().unwrap())
        .serve(bind_addr)
        .await?;
    Ok(())
}

#[cfg(coverage)]
#[tokio::main(worker_threads = 2)]
async fn main() -> anyhow::Result<()> {
    let gadget = UsbGadget::new("lesavka");
    let _handler = Handler::new(gadget).await?;
    Err(anyhow::anyhow!("coverage mode skips live gRPC serve loop"))
}
