// Unified RPC protocol types — covers both Tauri commands and Electrobun RPC schema
// These define the request/response shapes for all backend operations.

import type { AgentWireMessage } from './agent';

// ── PTY ──────────────────────────────────────────────────────────────────────

export interface PtyCreateOptions {
  sessionId: string;
  cols: number;
  rows: number;
  cwd?: string;
  shell?: string;
  args?: string[];
}

export interface PtyCreateResponse {
  ok: boolean;
  sessionId?: string;
  error?: string;
}

// ── Files ────────────────────────────────────────────────────────────────────

export interface FileEntry {
  name: string;
  path: string;
  isDir: boolean;
  size: number;
  ext?: string;
}

export type FileContent =
  | { type: 'Text'; content: string; lang?: string }
  | { type: 'Binary'; message: string }
  | { type: 'TooLarge'; size: number };

// ── Settings ─────────────────────────────────────────────────────────────────

export interface SettingEntry {
  key: string;
  value: string;
}

// ── Session persistence ──────────────────────────────────────────────────────

export interface SessionRecord {
  projectId: string;
  sessionId: string;
  provider: string;
  status: string;
  costUsd: number;
  inputTokens: number;
  outputTokens: number;
  model: string;
  error?: string;
  createdAt: number;
  updatedAt: number;
}

export interface SessionMessageRecord {
  sessionId: string;
  msgId: string;
  role: string;
  content: string;
  toolName?: string;
  toolInput?: string;
  timestamp: number;
  costUsd?: number;
  inputTokens?: number;
  outputTokens?: number;
}

// ── Agent events (backend -> frontend) ───────────────────────────────────────

export interface AgentMessageEvent {
  sessionId: string;
  messages: AgentWireMessage[];
}

export interface AgentStatusEvent {
  sessionId: string;
  status: string;
  error?: string;
}

export interface AgentCostEvent {
  sessionId: string;
  costUsd: number;
  inputTokens: number;
  outputTokens: number;
}

// ── PTY events (backend -> frontend) ─────────────────────────────────────────

export interface PtyOutputEvent {
  sessionId: string;
  /** Base64-encoded or raw UTF-8 data depending on backend */
  data: string;
}

export interface PtyClosedEvent {
  sessionId: string;
  exitCode: number | null;
}

// ── Search ───────────────────────────────────────────────────────────────────

export interface SearchResult {
  resultType: string;
  id: string;
  title: string;
  snippet: string;
  score: number;
}

// ── Remote machine ───────────────────────────────────────────────────────────

export type RemoteMachineStatus = 'connecting' | 'connected' | 'disconnected' | 'error';

export interface RemoteMachine {
  machineId: string;
  label: string;
  url: string;
  status: RemoteMachineStatus;
  latencyMs: number | null;
}
