/**
 * Cline config generator.
 *
 * Config file: cline_mcp_settings.json
 * Format:      { "mcpServers": { "name": { "command", "args", "env", "alwaysAllow", "disabled" } } }
 *
 * Key differences from canonical:
 *   - Extra fields: "alwaysAllow" (string array), "disabled" (boolean)
 *   - SSE servers use "url" + "headers" instead of "command"/"args"
 */

import { join } from "node:path";
import type { AppMetadata, LooseServerConfigType } from "@getmcp/core";
import {
  BaseGenerator,
  toStdioFields,
  toRemoteFields,
  home,
  appData,
  configHome,
  safeExistsSync,
} from "./base.js";

export class ClineGenerator extends BaseGenerator {
  app: AppMetadata = {
    id: "cline",
    name: "Cline",
    description: "AI coding assistant VS Code extension",
    configPaths: null,
    globalConfigPaths: {
      darwin:
        "~/Library/Application Support/Code/User/globalStorage/saoudrizwan.claude-dev/settings/cline_mcp_settings.json",
      win32:
        "%AppData%\\Code\\User\\globalStorage\\saoudrizwan.claude-dev\\settings\\cline_mcp_settings.json",
      linux:
        "~/.config/Code/User/globalStorage/saoudrizwan.claude-dev/settings/cline_mcp_settings.json",
    },
    configFormat: "json",
    docsUrl: "https://docs.cline.bot/mcp-servers/configuring-mcp-servers",
  };

  protected override transformStdio(config: LooseServerConfigType): Record<string, unknown> {
    return {
      ...toStdioFields(config),
      alwaysAllow: [],
      disabled: false,
    };
  }

  protected override transformRemote(config: LooseServerConfigType): Record<string, unknown> {
    return {
      ...toRemoteFields(config),
      alwaysAllow: [],
      disabled: false,
    };
  }

  override detectInstalled(): boolean {
    const extId = "saoudrizwan.claude-dev";
    switch (process.platform) {
      case "darwin":
        return safeExistsSync(
          join(home, "Library", "Application Support", "Code", "User", "globalStorage", extId),
        );
      case "win32":
        return safeExistsSync(join(appData, "Code", "User", "globalStorage", extId));
      default:
        return safeExistsSync(join(configHome, "Code", "User", "globalStorage", extId));
    }
  }
}
