// Agent types — shared between Tauri and Electrobun frontends

// ── Provider ─────────────────────────────────────────────────────────────────

export type ProviderId = 'claude' | 'codex' | 'ollama' | 'aider';

/** What a provider can do — UI gates features on these flags */
export interface ProviderCapabilities {
  hasProfiles: boolean;
  hasSkills: boolean;
  hasModelSelection: boolean;
  hasSandbox: boolean;
  supportsSubagents: boolean;
  supportsCost: boolean;
  supportsResume: boolean;
}

/** Static metadata about a provider */
export interface ProviderMeta {
  id: ProviderId;
  name: string;
  description: string;
  capabilities: ProviderCapabilities;
  /** Name of the sidecar runner file (e.g. 'claude-runner.mjs') */
  sidecarRunner: string;
  /** Default model identifier, if applicable */
  defaultModel?: string;
  /** Available model presets for dropdown selection */
  models?: { id: string; label: string }[];
}

/** Per-provider configuration (stored in settings) */
export interface ProviderSettings {
  enabled: boolean;
  defaultModel?: string;
  /** Provider-specific config blob */
  config: Record<string, unknown>;
}

// ── Agent status ─────────────────────────────────────────────────────────────

export type AgentStatus = 'idle' | 'starting' | 'running' | 'done' | 'error';

// ── Agent message (internal display format) ──────────────────────────────────

export type MsgRole = 'user' | 'assistant' | 'tool-call' | 'tool-result' | 'thinking' | 'system';

export interface AgentMessage {
  id: string;
  role: MsgRole;
  content: string;
  toolName?: string;
  toolInput?: string;
  toolPath?: string;
  timestamp: number;
}

// ── Agent message (wire format from sidecar) ─────────────────────────────────

export type AgentMessageType =
  | 'init'
  | 'text'
  | 'thinking'
  | 'tool_call'
  | 'tool_result'
  | 'status'
  | 'compaction'
  | 'cost'
  | 'error'
  | 'unknown';

export interface AgentWireMessage {
  id: string;
  type: AgentMessageType;
  parentId?: string;
  content: unknown;
  timestamp: number;
}

// ── Agent session state ──────────────────────────────────────────────────────

export interface AgentSession {
  id: string;
  sdkSessionId?: string;
  status: AgentStatus;
  model?: string;
  prompt: string;
  messages: AgentMessage[];
  costUsd: number;
  inputTokens: number;
  outputTokens: number;
  numTurns: number;
  durationMs: number;
  error?: string;
  // Agent Teams: parent/child hierarchy
  parentSessionId?: string;
  parentToolUseId?: string;
  childSessionIds: string[];
}

// ── Agent start options (backend-agnostic) ───────────────────────────────────

export interface AgentStartOptions {
  provider?: ProviderId;
  sessionId: string;
  prompt: string;
  cwd?: string;
  model?: string;
  systemPrompt?: string;
  maxTurns?: number;
  maxBudgetUsd?: number;
  permissionMode?: string;
  settingSources?: string[];
  claudeConfigDir?: string;
  additionalDirectories?: string[];
  worktreeName?: string;
  providerConfig?: Record<string, unknown>;
  extraEnv?: Record<string, string>;
}
