const STORAGE_KEY = "gitpub-desktop-settings-v1";

export function getDefaultSettings() {
  return {
    theme: "dark",
    gitExecutablePath: "",
    defaultCloneDirectory: "",
    externalEditorPath: "",
    autoFetchOnRepoOpen: false,
    lastSelectedRepoPath: "",
    activeServerId: null,
    servers: [],
    recentRepositories: [],
  };
}

export function loadSettings() {
  try {
    const rawValue = localStorage.getItem(STORAGE_KEY);
    if (!rawValue) return getDefaultSettings();

    const parsed = JSON.parse(rawValue);
    return { ...getDefaultSettings(), ...parsed };
  } catch (error) {
    console.error("Failed to load settings:", error);
    return getDefaultSettings();
  }
}

export function saveSettings(settings) {
  localStorage.setItem(STORAGE_KEY, JSON.stringify(settings));
}
