/**
 * 工具层 Pluggable Operations 接口定义。
 *
 * 所有 builtin 工具（bash / read_file / write_file / edit / patch / grep / find_files / list_dir）
 * 通过这些接口访问文件系统和子进程，不再直接 import fs 或 child_process。
 */

// ─── FileOperations ───

export interface FileOperations {
  readFile(absolutePath: string): Promise<Buffer>;
  writeFile(absolutePath: string, content: string | Buffer): Promise<void>;
  appendFile(absolutePath: string, content: string | Buffer): Promise<void>;
  access(absolutePath: string, mode: 'read' | 'write' | 'readwrite'): Promise<void>;
  isDirectory(absolutePath: string): Promise<boolean>;
  readDir(absolutePath: string): Promise<DirEntry[]>;
  mkdir(absolutePath: string): Promise<void>;
  realpath(absolutePath: string): Promise<string>;
}

export interface DirEntry {
  name: string;
  isDirectory: boolean;
  size: number;
}

// ─── ProcessOperations ───

export interface ProcessOperations {
  exec(
    command: string,
    cwd: string,
    options: ProcessExecOptions,
  ): Promise<ProcessExecResult>;
}

export interface ProcessExecOptions {
  onData: (chunk: Buffer, stream: 'stdout' | 'stderr') => void;
  signal?: AbortSignal;
  timeout?: number;
  env?: NodeJS.ProcessEnv;
}

export interface ProcessExecResult {
  exitCode: number | null;
  signal: NodeJS.Signals | null;
}

// ─── SearchOperations ───

export interface SearchOperations {
  ripgrep(args: string[], cwd: string, signal?: AbortSignal): Promise<SearchExecResult>;
  fd(args: string[], cwd: string, signal?: AbortSignal): Promise<SearchExecResult>;
}

export interface SearchExecResult {
  stdout: string;
  stderr: string;
  exitCode: number | null;
}

// ─── 聚合接口 ───

export interface ToolOperations {
  file: FileOperations;
  process: ProcessOperations;
  search: SearchOperations;
}
