const fs = require('fs');
const path = require('path');
const os = require('os');

function getDefaultConfig() {
  return {
    projectsDirectory: path.join(os.homedir(), 'Projects'),
    refreshInterval: 10,
    launchAtLogin: true,
    showLandoProjects: true,
    showDDEVProjects: true,
    defaultTerminal: 'Terminal',
    showInDock: false,
    recentProjects: [],
    favoriteProjects: [],
    filterPresets: []
  };
}

function createConfig(configPath) {
  return {
    get: (key) => {
      try {
        if (fs.existsSync(configPath)) {
          const data = JSON.parse(fs.readFileSync(configPath, 'utf8'));
          return data[key] !== undefined ? data[key] : getDefaultConfig()[key];
        }
      } catch (error) {
        // Fall through to default
      }
      return getDefaultConfig()[key];
    },
    set: (key, value) => {
      try {
        let data = {};
        if (fs.existsSync(configPath)) {
          data = JSON.parse(fs.readFileSync(configPath, 'utf8'));
        }
        data[key] = value;
        fs.writeFileSync(configPath, JSON.stringify(data, null, 2));
      } catch (error) {
        // Silently fail on write errors
      }
    }
  };
}

module.exports = { getDefaultConfig, createConfig };
