import * as fs from "fs";
import * as path from "path";
import * as os from "os";
import { generateSecureToken } from "../../utils/crypto";
import { writeFileAtomic } from "../../utils/fs";

function buildConfigTemplate(relayToken: string, daemonToken: string): string {
  return `{
  // OpenDevBrowser Plugin Configuration
  // See: https://github.com/anthropics/opendevbrowser#configuration

  "headless": false,
  "profile": "default",
  "persistProfile": true,

  "snapshot": {
    "maxChars": 16000,
    "maxNodes": 1000
  },

  "export": {
    "maxNodes": 1000,
    "inlineStyles": true
  },

  "devtools": {
    "showFullUrls": false,
    "showFullConsole": false
  },

  "security": {
    "allowRawCDP": false,
    "allowNonLocalCdp": false,
    "allowUnsafeExport": false
  },

  "skillPaths": [],
  "skills": {
    "nudge": {
      "enabled": true,
      "keywords": ["quick start", "getting started", "launch", "connect", "setup"],
      "maxAgeMs": 60000
    }
  },

  "continuity": {
    "enabled": true,
    "filePath": "opendevbrowser_continuity.md",
    "nudge": {
      "enabled": true,
      "keywords": [
        "plan",
        "multi-step",
        "multi step",
        "long-running",
        "long running",
        "refactor",
        "migration",
        "rollout",
        "release",
        "upgrade",
        "investigate",
        "follow-up",
        "continue"
      ],
      "maxAgeMs": 60000
    }
  },

  "relayPort": 8787,
  "relayToken": "${relayToken}",
  "daemonPort": 8788,
  "daemonToken": "${daemonToken}",

  "flags": [],

  "checkForUpdates": false
}
`;
}

export function getPluginConfigPath(mode: "global" | "local"): string {
  if (mode === "global") {
    const configDir = process.env.OPENCODE_CONFIG_DIR
      || path.join(os.homedir(), ".config", "opencode");
    return path.join(configDir, "opendevbrowser.jsonc");
  }
  return path.join(process.cwd(), "opendevbrowser.jsonc");
}

export function createPluginConfig(mode: "global" | "local"): { created: boolean; path: string } {
  const configPath = getPluginConfigPath(mode);

  if (fs.existsSync(configPath)) {
    return { created: false, path: configPath };
  }

  const dir = path.dirname(configPath);
  if (!fs.existsSync(dir)) {
    fs.mkdirSync(dir, { recursive: true });
  }

  const relayToken = generateSecureToken();
  const daemonToken = generateSecureToken();
  writeFileAtomic(configPath, buildConfigTemplate(relayToken, daemonToken));
  return { created: true, path: configPath };
}

export function getConfigTemplate(): string {
  return buildConfigTemplate(generateSecureToken(), generateSecureToken());
}
