import { readFile, writeFile, mkdir } from 'node:fs/promises';
import path from 'node:path';

export async function getMcpSettings(cwd) {
  try { return JSON.parse(await readFile(path.join(cwd, '.claude', 'settings.json'), 'utf8')); }
  catch { return {}; }
}

export async function writeMcpSettings(cwd) {
  const settingsPath = path.join(cwd, '.claude', 'settings.json');
  const existing = await getMcpSettings(cwd);
  const updated = {
    ...existing,
    mcpServers: {
      ...(existing.mcpServers || {}),
      'chrome-devtools': { command: 'npx', args: ['-y', 'chrome-devtools-mcp', '--browserUrl', 'http://127.0.0.1:9222'] }
    }
  };
  await mkdir(path.dirname(settingsPath), { recursive: true });
  await writeFile(settingsPath, JSON.stringify(updated, null, 2), 'utf8');
}
