import type { ProviderConfigPatch } from '@/api/types';

/**
 * v2.3 Phase 5 (design.md §7.3) — a SMALL curated catalog of ACP coding agents
 * the user might register. We deliberately do NOT port Paseo's 30+ entry list.
 *
 * Non-goal: we never install anything. Each entry is a manual-install hint
 * (`installUrl` / `installCmd`) plus the config `command` that gets written into
 * `/data/coder-providers.json`. The user installs the CLI themselves; until the
 * binary is on PATH the provider shows as "Not installed". Commands are
 * editable after adding — versions are aliased/untrimmed on purpose; pin on your
 * own host once verified.
 */
export interface AcpCatalogEntry {
  id: string;
  label: string;
  description: string;
  /** Config command written verbatim into providers[id].command: [binary, ...args]. */
  command: [string, ...string[]];
  /** Where to install the CLI manually — we LINK, never install. */
  installUrl: string;
  /** Optional suggested install command, shown as a copyable hint. */
  installCmd?: string;
}

export const ACP_PROVIDER_CATALOG: AcpCatalogEntry[] = [
  {
    id: 'amp-acp',
    label: 'Amp',
    description: 'Sourcegraph Amp — agentic coding CLI with an ACP bridge.',
    command: ['amp-acp'],
    installUrl: 'https://ampcode.com/',
    installCmd: 'npm i -g @sourcegraph/amp',
  },
  {
    id: 'gemini',
    label: 'Gemini CLI',
    description: 'Google Gemini CLI in ACP mode (--experimental-acp).',
    command: ['gemini', '--experimental-acp'],
    installUrl: 'https://github.com/google-gemini/gemini-cli',
    installCmd: 'npm i -g @google/gemini-cli',
  },
  {
    id: 'cline',
    label: 'Cline',
    description: 'Cline coding agent over ACP (run via npx).',
    command: ['npx', '-y', 'cline', '--acp'],
    installUrl: 'https://cline.bot/',
  },
  {
    id: 'claude-code-acp',
    label: 'Claude Code (ACP)',
    description: "Zed's ACP adapter for Claude Code — distinct from the built-in PTY claude provider.",
    command: ['npx', '-y', '@zed-industries/claude-code-acp'],
    installUrl: 'https://github.com/zed-industries/claude-code-acp',
  },
  {
    id: 'pi-acp',
    label: 'Pi',
    description: 'Example custom ACP entry — build the binary from source, then edit the command.',
    command: ['pi-acp'],
    installUrl: 'https://agentclientprotocol.com/',
  },
];

/**
 * Build the PATCH body that registers a catalog entry: a single-id partial
 * providers map with the custom-ACP override (extends:'acp' + label + command),
 * enabled. Sent to PATCH /api/providers/config (then refreshProviders([id])).
 */
export function buildAcpProviderConfigPatch(entry: AcpCatalogEntry): ProviderConfigPatch {
  return {
    providers: {
      [entry.id]: {
        extends: 'acp',
        label: entry.label,
        description: entry.description,
        command: entry.command,
        enabled: true,
      },
    },
  };
}
