export const LOCAL_RUNTIME_NOTE = 'Lite 学员包本地运行时：通过 stdin 接收 AIPROS Skill 任务，在会话工作目录生成文件产物。'

const PRESETS = [
  {
    code: 'codex',
    runtimeCode: 'local-codex',
    name: '本地 Codex CLI',
    command: 'codex',
    args: ['exec', '--json', '--skip-git-repo-check', '--dangerously-bypass-approvals-and-sandbox', '-C', '{workdir}', '-'],
    healthcheckArgs: ['--version'],
    installHint: '安装 Codex CLI 后，先在终端完成登录，再运行 npm run runtime:setup -- codex。',
  },
  {
    code: 'qoder',
    runtimeCode: 'local-qoder',
    name: '本地 Qoder',
    command: 'qoder',
    args: ['-'],
    healthcheckArgs: ['--version'],
    aliases: ['qodr', 'qoder-cloud-agent', 'qoder-agent'],
    installHint: '安装 Qoder CLI 后，先在终端完成登录，再运行 npm run runtime:setup -- qoder。',
  },
  {
    code: 'claude-code',
    runtimeCode: 'local-claude-code',
    name: '本地 Claude Code',
    command: 'claude',
    args: ['-p', '--dangerously-skip-permissions'],
    healthcheckArgs: ['--version'],
    aliases: ['claude', 'claude-code'],
    installHint: '安装 Claude Code 后，先在终端完成登录，再运行 npm run runtime:setup -- claude-code。',
  },
  {
    code: 'codebuddy',
    runtimeCode: 'local-codebuddy',
    name: '本地 CodeBuddy',
    command: 'codebuddy',
    args: ['-'],
    healthcheckArgs: ['--version'],
    aliases: ['workbuddy', 'local-codebuddy'],
    installHint: '安装 CodeBuddy/WorkBuddy CLI 后，按实际命令名覆盖 --command。',
  },
  {
    code: 'trae',
    runtimeCode: 'local-trae',
    name: '本地 Trae Work',
    command: 'trae',
    args: ['-'],
    healthcheckArgs: ['--version'],
    aliases: ['trae-work'],
    installHint: '安装 Trae Work CLI 后，按实际命令名覆盖 --command。',
  },
  {
    code: 'custom',
    runtimeCode: 'local-custom',
    name: '自定义本地 CLI',
    command: '',
    args: ['-'],
    healthcheckArgs: ['--version'],
    installHint: '使用 --command 指定任意能从 stdin 读取任务的本地 AI CLI。',
  },
]

export function listRuntimePresets() {
  return PRESETS.map(p => ({ ...p, aliases: [...(p.aliases || [])] }))
}

export function normalizePresetCode(code = '') {
  const value = String(code || '').trim().toLowerCase()
  const found = PRESETS.find(p => p.code === value || (p.aliases || []).includes(value))
  return found?.code || value
}

export function getRuntimePreset(code = '') {
  const normalized = normalizePresetCode(code)
  const found = PRESETS.find(p => p.code === normalized)
  if (!found) return null
  return { ...found, aliases: [...(found.aliases || [])] }
}

export function buildRuntimePatch(code, overrides = {}) {
  const preset = getRuntimePreset(code)
  if (!preset) throw new Error(`未知本地运行时：${code}`)
  const command = String(overrides.command ?? preset.command ?? '').trim()
  return {
    name: overrides.name || preset.name,
    command,
    args: Array.isArray(overrides.args) ? overrides.args : preset.args,
    healthcheckArgs: Array.isArray(overrides.healthcheckArgs) ? overrides.healthcheckArgs : preset.healthcheckArgs,
    purposeScopes: ['create', 'evaluate', 'run'],
    timeoutMs: Number(overrides.timeoutMs || 300000),
    enabled: overrides.enabled === true,
    notes: overrides.notes || LOCAL_RUNTIME_NOTE,
  }
}
