//! Dashboard — landing screen showing vault status and quick links.

use eframe::egui;

#[derive(Default)]
pub struct DashboardState {
    // No state to hold yet — derived from App-level vault_status.
}

pub enum DashboardAction {
    GoToTab(crate::Tab),
}

pub fn render(
    ui: &mut egui::Ui,
    _state: &mut DashboardState,
    vault_unlocked: bool,
) -> Option<DashboardAction> {
    let mut action = None;

    ui.add_space(14.0);
    ui.heading("envseal");
    ui.label(
        egui::RichText::new("Sudo can't read your keys.")
            .size(13.0)
            .weak(),
    );
    ui.add_space(20.0);

    egui::Frame::new()
        .fill(ui.visuals().faint_bg_color)
        .corner_radius(10.0)
        .inner_margin(18.0)
        .show(ui, |ui| {
            ui.horizontal(|ui| {
                let (color, label) = if vault_unlocked {
                    (egui::Color32::from_rgb(0x22, 0xc5, 0x5e), "Vault unlocked")
                } else {
                    (egui::Color32::from_rgb(0xf5, 0x9e, 0x0b), "Vault locked")
                };
                ui.colored_label(color, egui::RichText::new(label).size(15.0).strong());
            });
            ui.add_space(4.0);
            if vault_unlocked {
                ui.label(
                    egui::RichText::new(
                        "The master key is loaded in this process. You can store, peek, \
                         revoke, and edit settings without re-entering your passphrase \
                         until you click 'Lock vault' in the sidebar.",
                    )
                    .size(12.0)
                    .weak(),
                );
            } else {
                ui.label(
                    egui::RichText::new(
                        "The first action that needs the vault will pop a passphrase \
                         prompt. If you haven't set one up yet, you'll be guided through \
                         creating it.",
                    )
                    .size(12.0)
                    .weak(),
                );
            }
        });

    ui.add_space(16.0);
    ui.label(egui::RichText::new("Quick actions").strong());
    ui.add_space(6.0);

    ui.horizontal(|ui| {
        if ui
            .add(
                egui::Button::new("Store a new secret")
                    .min_size(egui::vec2(180.0, 32.0))
                    .corner_radius(8.0),
            )
            .clicked()
        {
            action = Some(DashboardAction::GoToTab(crate::Tab::Store));
        }
        ui.add_space(8.0);
        if ui
            .add(
                egui::Button::new("View stored secrets")
                    .min_size(egui::vec2(180.0, 32.0))
                    .corner_radius(8.0),
            )
            .clicked()
        {
            action = Some(DashboardAction::GoToTab(crate::Tab::Secrets));
        }
    });

    ui.add_space(8.0);
    ui.horizontal(|ui| {
        if ui
            .add(
                egui::Button::new("Audit log")
                    .min_size(egui::vec2(180.0, 32.0))
                    .corner_radius(8.0),
            )
            .clicked()
        {
            action = Some(DashboardAction::GoToTab(crate::Tab::Audit));
        }
        ui.add_space(8.0);
        if ui
            .add(
                egui::Button::new("Security settings")
                    .min_size(egui::vec2(180.0, 32.0))
                    .corner_radius(8.0),
            )
            .clicked()
        {
            action = Some(DashboardAction::GoToTab(crate::Tab::Settings));
        }
    });

    action
}
