use libadwaita as adw;
use libadwaita::prelude::{AdwDialogExt, AlertDialogExt, ApplicationExt};

use crate::app::context::AppContext;
use crate::core::config::AppConfig;
use crate::runner::paths::{ResolvedPaths, StartupValidation};

pub fn preflight() -> Result<AppContext, StartupValidation> {
    let paths = ResolvedPaths::discover().map_err(StartupValidation::from_single)?;
    let mut validation = paths.validate();
    if validation.has_errors() {
        return Err(validation);
    }

    let config = AppConfig::load(&paths).map_err(StartupValidation::from_single)?;
    validation.extend(config.validate(&paths));
    if validation.has_errors() {
        return Err(validation);
    }

    Ok(AppContext::new(config, paths))
}

pub fn present_startup_failure_dialog(app: &adw::Application, validation: &StartupValidation) {
    let dialog = adw::AlertDialog::builder()
        .heading("Startup checks failed")
        .body(validation.to_string())
        .close_response("close")
        .default_response("close")
        .build();
    dialog.add_response("close", "Close");
    dialog.set_response_appearance("close", adw::ResponseAppearance::Suggested);

    let app = app.clone();
    dialog.connect_response(None, move |_, _| {
        app.quit();
    });

    dialog.present(Option::<&gtk4::Widget>::None);
}
