export interface ToolFileCapabilityProjection {
  tags: string[];
  summary: string;
}

export interface ToolRendererCapabilityProjection {
  custom: boolean;
  streaming: boolean;
  metaKinds: Array<'path' | 'offset' | 'log' | 'count' | 'mode'>;
  showsArgs: 'never' | 'running' | 'always';
  collapsibleResult: boolean;
  description: string;
  bindingStatus: 'custom' | 'default';
}

type RendererCapabilityWithoutBinding = Omit<ToolRendererCapabilityProjection, 'custom' | 'bindingStatus'>;

const relativePathToolNames = new Set([
  'read_file',
  'write_file',
  'list_dir',
  'find_files',
  'grep',
  'patch',
  'bash',
]);

const truncationAwareToolNames = new Set([
  'read_file',
  'find_files',
  'grep',
  'list_dir',
  'patch',
  'bash',
]);

const limitAwareToolNames = new Set([
  'find_files',
  'grep',
  'list_dir',
]);

const rendererCapabilities = new Map<string, RendererCapabilityWithoutBinding>([
  ['bash', {
    streaming: true,
    metaKinds: ['path', 'log'],
    showsArgs: 'running',
    collapsibleResult: true,
    description: '识别工作目录与完整日志路径',
  }],
  ['edit', {
    streaming: true,
    metaKinds: ['path'],
    showsArgs: 'always',
    collapsibleResult: true,
    description: '展示编辑参数与目标文件路径',
  }],
  ['patch', {
    streaming: true,
    metaKinds: ['path', 'offset', 'mode'],
    showsArgs: 'always',
    collapsibleResult: true,
    description: '展示补丁参数、匹配策略与首个变更行',
  }],
  ['write_file', {
    streaming: true,
    metaKinds: ['path'],
    showsArgs: 'running',
    collapsibleResult: false,
    description: '展示写入目标文件路径',
  }],
  ['read_file', {
    streaming: true,
    metaKinds: ['path', 'offset'],
    showsArgs: 'running',
    collapsibleResult: true,
    description: '识别文件路径、分页参数与继续读取 offset',
  }],
  ['list_dir', {
    streaming: true,
    metaKinds: ['path', 'count', 'mode'],
    showsArgs: 'running',
    collapsibleResult: false,
    description: '展示目录路径、结果上限与截断提示',
  }],
  ['find_files', {
    streaming: true,
    metaKinds: ['path', 'mode', 'count'],
    showsArgs: 'running',
    collapsibleResult: false,
    description: '展示搜索根目录、模式与结果上限',
  }],
  ['grep', {
    streaming: true,
    metaKinds: ['path', 'mode', 'offset', 'count'],
    showsArgs: 'running',
    collapsibleResult: true,
    description: '展示搜索目录、内容模式、Glob 与结果上限',
  }],
]);

export function buildToolFileCapabilityProjection(name: string): ToolFileCapabilityProjection {
  const tags: string[] = [];

  if (relativePathToolNames.has(name)) {
    tags.push('支持相对路径');
    tags.push('支持 ~ 路径');
  }
  if (truncationAwareToolNames.has(name)) {
    tags.push('支持截断提示');
  }
  if (name === 'read_file') {
    tags.push('支持 offset 续读');
  }
  if (name === 'patch') {
    tags.push('返回首变更行');
    tags.push('返回差异预览');
  }
  if (limitAwareToolNames.has(name)) {
    tags.push('支持 limit');
  }

  return {
    tags,
    summary: fileCapabilitySummary(name),
  };
}

export function buildToolRendererCapabilityProjection(name: string): ToolRendererCapabilityProjection {
  const capability = rendererCapabilities.get(name);
  if (!capability) {
    return {
      custom: false,
      streaming: false,
      metaKinds: [],
      showsArgs: 'running',
      collapsibleResult: true,
      description: '默认文本结果展示',
      bindingStatus: 'default',
    };
  }

  return {
    custom: true,
    bindingStatus: 'custom',
    ...capability,
  };
}

function fileCapabilitySummary(name: string): string {
  switch (name) {
    case 'read_file':
      return '支持相对路径解析、Unicode 空格与截图路径兼容、图片/二进制识别，以及 offset 分段续读';
    case 'write_file':
      return '支持相对路径解析，并返回写入大小与行数';
    case 'list_dir':
      return '支持默认当前会话目录、排序、目录标记、大小展示，以及条目数和输出大小截断提示';
    case 'find_files':
      return '支持默认当前会话目录、glob、limit 与结果截断提示';
    case 'grep':
      return '支持默认当前会话目录、glob、limit、隐藏文件搜索、长行截断与结果截断提示';
    case 'patch':
      return '支持相对路径解析、模糊匹配替换、串行化写入，并返回首个变更行与差异预览';
    case 'bash':
      return '支持相对 cwd 解析、长输出截断与完整日志文件落盘';
    default:
      return '';
  }
}
