/**
 * Codex JSON-RPC Types
 *
 * Type definitions for the Codex app-server JSON-RPC protocol,
 * used by the Codex adapter to translate between Codex and Zencode's
 * internal message types.
 */

import type { RecorderManager } from "./recorder.js";

// ─── Codex Item Types ─────────────────────────────────────────────────────────

export interface CodexItem {
  type: string;
  id: string;
  [key: string]: unknown;
}

export interface CodexAgentMessageItem extends CodexItem {
  type: "agentMessage";
  text?: string;
}

export interface CodexCommandExecutionItem extends CodexItem {
  type: "commandExecution";
  command: string | string[];
  cwd?: string;
  status: "inProgress" | "completed" | "failed" | "declined";
  exitCode?: number;
  durationMs?: number;
}

export interface CodexFileChangeItem extends CodexItem {
  type: "fileChange";
  changes?: Array<{ path: string; kind: unknown; diff?: string }>;
  status: "inProgress" | "completed" | "failed" | "declined";
}

export interface CodexMcpToolCallItem extends CodexItem {
  type: "mcpToolCall";
  server: string;
  tool: string;
  status: "inProgress" | "completed" | "failed";
  arguments?: Record<string, unknown>;
  result?: string;
  error?: string;
}

export interface CodexWebSearchItem extends CodexItem {
  type: "webSearch";
  query?: string;
  action?: { type: string; url?: string; pattern?: string };
}

export interface CodexReasoningItem extends CodexItem {
  type: "reasoning";
  summary?: string;
  content?: string;
}

export interface CodexContextCompactionItem extends CodexItem {
  type: "contextCompaction";
}

// ─── Plan Types ───────────────────────────────────────────────────────────────

export interface PlanTodo {
  content: string;
  status: "pending" | "in_progress" | "completed";
  activeForm?: string;
}

// ─── MCP Status Types ─────────────────────────────────────────────────────────

export interface CodexMcpServerStatus {
  name: string;
  tools?: Record<string, { name?: string; annotations?: unknown }>;
  authStatus?: "unsupported" | "notLoggedIn" | "bearerToken" | "oAuth";
}

export interface CodexMcpStatusListResponse {
  data?: CodexMcpServerStatus[];
  nextCursor?: string | null;
}

// ─── Adapter Options ──────────────────────────────────────────────────────────

export interface CodexAdapterOptions {
  model?: string;
  cwd?: string;
  /** Runtime cwd for Codex RPC calls. Falls back to `cwd` when omitted. */
  executionCwd?: string;
  approvalMode?: string;
  sandbox?: "workspace-write" | "danger-full-access";
  /** If provided, resume an existing thread instead of starting a new one. */
  threadId?: string;
  /** Optional recorder for raw message capture. */
  recorder?: RecorderManager;
}

// ─── Utility ──────────────────────────────────────────────────────────────────

/** Safely extract a string kind from a Codex file change entry.
 *  Codex may send kind as a string ("create") or as an object ({ type: "modify" }). */
export function safeKind(kind: unknown): string {
  if (typeof kind === "string") return kind;
  if (kind && typeof kind === "object" && "type" in kind) {
    const t = (kind as Record<string, unknown>).type;
    if (typeof t === "string") return t;
  }
  return "modify";
}
