use dbui_core::profile::UserProfile;
use dbui_core::Result;

pub fn load(files: &crate::files::FileService, log: &slog::Logger) -> UserProfile {
  match files.read_json("profile") {
    Ok(p) => p,
    Err(_) => match files.write_json(UserProfile::default(), "profile") {
      Ok(_) => UserProfile::default(),
      Err(e) => {
        slog::warn!(log, "Unable to save default profile: {}", e);
        UserProfile::default()
      }
    }
  }
}

pub fn save(cfg: &crate::AppConfig, profile: &UserProfile) -> Result<()> {
  cfg.files().write_json(profile, "profile")
}
