use std::sync::{Mutex, OnceLock};
use vault::tiers::Vault;

static VAULT: OnceLock<Mutex<Vault>> = OnceLock::new();

pub fn vault() -> &'static Mutex<Vault> {
    VAULT.get_or_init(|| {
        let mut v = Vault::new();
        v.load_from_disk();
        Mutex::new(v)
    })
}

/// Flush durable tiers to disk (runs pruning first) — call before process exit.
pub fn flush() {
    if let Some(m) = VAULT.get() {
        if let Ok(mut v) = m.lock() {
            let _ = v.flush_to_disk();
        }
    }
}
