'use strict';

const AGENT_CAPABILITIES = Object.freeze({
  claude: Object.freeze({
    agentType: 'claude',
    providerId: 'claude-code',
    label: 'Claude Code',
    liveStatus: true,
    stateDetector: true,
    structuredTranscript: true,
    promptNavigation: 'transcript',
    review: true,
    resume: true,
    resumeSpec: (id) => ({ cmd: 'claude', args: ['--resume', id] }),
  }),
  codex: Object.freeze({
    agentType: 'codex',
    providerId: 'codex',
    label: 'Codex',
    liveStatus: true,
    stateDetector: true,
    structuredTranscript: true,
    promptNavigation: 'transcript',
    review: true,
    resume: true,
    resumeSpec: (id) => ({ cmd: 'codex', args: ['resume', id] }),
  }),
  gemini: Object.freeze({
    agentType: 'gemini',
    providerId: 'gemini',
    label: 'Gemini',
    liveStatus: true,
    stateDetector: true,
    structuredTranscript: false,
    promptNavigation: 'terminal',
    review: false,
    resume: true,
    resumeSpec: (id) => ({ cmd: 'gemini', args: ['--session', id] }),
  }),
  opencode: Object.freeze({
    agentType: 'opencode',
    providerId: 'opencode',
    label: 'OpenCode',
    liveStatus: true,
    stateDetector: true,
    structuredTranscript: false,
    promptNavigation: 'terminal',
    review: false,
    resume: false,
    resumeSpec: () => null,
  }),
  walle: Object.freeze({
    agentType: 'walle',
    providerId: 'walle',
    label: 'Wall-E',
    liveStatus: true,
    stateDetector: false,
    structuredTranscript: true,
    promptNavigation: 'chat',
    review: true,
    resume: false,
    resumeSpec: () => null,
  }),
  cursor: Object.freeze({
    agentType: 'cursor',
    providerId: 'cursor',
    label: 'Cursor',
    liveStatus: true,
    stateDetector: true,
    structuredTranscript: false,
    promptNavigation: 'terminal',
    review: false,
    resume: true,
    // Cursor chats are cloud-backed: there's no local transcript (JSONL/SQLite)
    // under ~/.cursor or ~/.local/share/cursor-agent that CTM can tail, so we
    // never capture a concrete chatId. Targeted resume would need Cursor's
    // cloud API. Fall back to `cursor-agent --resume` with no id, which opens
    // Cursor's own session picker — the working resume path today.
    resumeSpec: (id) => ({ cmd: 'cursor-agent', args: id ? ['--resume', id] : ['--resume'] }),
  }),
  shell: Object.freeze({
    agentType: 'shell',
    providerId: 'shell',
    label: 'Shell',
    liveStatus: false,
    stateDetector: false,
    structuredTranscript: false,
    promptNavigation: 'terminal',
    review: false,
    resume: false,
    resumeSpec: () => null,
  }),
});

function detectAgentType(cmd) {
  if (!cmd) return null;
  const s = String(cmd).toLowerCase();
  if (s.includes('claude')) return 'claude';
  if (s.includes('codex')) return 'codex';
  if (s.includes('gemini')) return 'gemini';
  if (s.includes('opencode') || s.includes('open-code')) return 'opencode';
  if (s.includes('cursor')) return 'cursor';
  if (s === 'walle' || s.includes('wall-e') || s.includes('walle')) return 'walle';
  return null;
}

function normalizeAgentType(value) {
  if (!value) return null;
  const s = String(value).toLowerCase().replace(/_/g, '-');
  if (AGENT_CAPABILITIES[s]) return s;
  if (s === 'claude-code') return 'claude';
  if (s === 'gemini-cli') return 'gemini';
  if (s === 'opencode-cli' || s === 'open-code') return 'opencode';
  if (s === 'cursor-agent' || s === 'cursor-cli') return 'cursor';
  return detectAgentType(s);
}

function getAgentCapabilities(value) {
  const type = normalizeAgentType(value) || 'shell';
  return AGENT_CAPABILITIES[type] || AGENT_CAPABILITIES.shell;
}

function canResumeAgent(value) {
  return !!getAgentCapabilities(value).resume;
}

function getResumeSpec(agentType, resumeId) {
  if (!resumeId) return null;
  const caps = getAgentCapabilities(agentType);
  if (!caps.resume || typeof caps.resumeSpec !== 'function') return null;
  return caps.resumeSpec(resumeId);
}

module.exports = {
  AGENT_CAPABILITIES,
  detectAgentType,
  normalizeAgentType,
  getAgentCapabilities,
  canResumeAgent,
  getResumeSpec,
};
