import * as fs from 'fs';
import * as path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

// 工具名到子目录的映射
const TOOL_CATEGORY_MAP: Record<string, string> = {
  // shell
  bash: 'shell',
  // filesystem
  read_file: 'filesystem',
  write_file: 'filesystem',
  edit_file: 'filesystem',
  multi_edit: 'filesystem',
  list_directory: 'filesystem',
  create_directory: 'filesystem',
  search_files: 'filesystem',
  grep_content: 'filesystem',
  get_file_info: 'filesystem',
  move_file: 'filesystem',
  copy_file: 'filesystem',
  delete_file: 'filesystem',
  // web
  web_search: 'web',
  web_extract: 'web',
  // git
  git_status: 'git',
  git_diff: 'git',
  git_log: 'git',
  git_branch: 'git',
  git_add: 'git',
  git_commit: 'git',
  git_push: 'git',
  git_pull: 'git',
  git_checkout: 'git',
  git_stash: 'git',
  // todo
  todo_read: 'todo',
  todo_write: 'todo',
};

export function loadDescription(toolName: string): string {
  const category = TOOL_CATEGORY_MAP[toolName];
  const filePath = category
    ? path.join(__dirname, 'descriptions', category, `${toolName}.txt`)
    : path.join(__dirname, 'descriptions', `${toolName}.txt`);

  try {
    return fs.readFileSync(filePath, 'utf-8').trim();
  } catch {
    throw new Error(`无法加载工具描述文件: ${filePath}`);
  }
}
