import type { CollapseProps } from 'antd'
import * as z from 'zod'

export enum AgentToolsType {
  Skill = 'Skill',
  Read = 'Read',
  Task = 'Task',
  Bash = 'Bash',
  Search = 'Search',
  Glob = 'Glob',
  TodoWrite = 'TodoWrite',
  WebSearch = 'WebSearch',
  Grep = 'Grep',
  Write = 'Write',
  WebFetch = 'WebFetch',
  Edit = 'Edit',
  MultiEdit = 'MultiEdit',
  BashOutput = 'BashOutput',
  NotebookEdit = 'NotebookEdit',
  ExitPlanMode = 'ExitPlanMode',
  AskUserQuestion = 'AskUserQuestion',
  ToolSearch = 'ToolSearch'
}

export type TextOutput = {
  type: 'text'
  text: string
}

// Read 工具的类型定义
export interface SkillToolInput {
  /**
   * The skill to use
   */
  skill: string
  args?: string
}

export type SkillToolOutput = string

export interface ReadToolInput {
  /**
   * The absolute path to the file to read
   */
  file_path: string
  /**
   * The line number to start reading from
   */
  offset?: number
  /**
   * The number of lines to read
   */
  limit?: number
}

export type ReadToolOutput = string | TextOutput[]

// Task 工具的类型定义
export type TaskToolInput = {
  /**
   * A short (3-5 word) description of the task
   */
  description: string
  /**
   * The task for the agent to perform
   */
  prompt: string
  /**
   * The type of specialized agent to use for this task
   */
  subagent_type: string
}

export type TaskToolOutput = TextOutput[]

// Bash 工具的类型定义
export type BashToolInput = {
  /**
   * The command to execute
   */
  command: string
  /**
   * Optional timeout in milliseconds (max 600000)
   */
  timeout?: number
  /**
   * Clear, concise description of what this command does in 5-10 words
   */
  description?: string
  /**
   * Set to true to run this command in the background
   */
  run_in_background?: boolean
}

export type BashToolOutput = string

// Search 工具的类型定义
export type SearchToolInput = string

export type SearchToolOutput = string

// Glob 工具的类型定义
export interface GlobToolInput {
  /**
   * The glob pattern to match files against
   */
  pattern: string
  /**
   * The directory to search in (defaults to cwd)
   */
  path?: string
}

export type GlobToolOutput = string

// TodoWrite 工具的类型定义
export interface TodoItem {
  /**
   * The task description
   */
  content: string
  /**
   * The task status
   */
  status: 'pending' | 'in_progress' | 'completed'
  /**
   * Active form of the task description
   */
  activeForm: string
}

export type TodoWriteToolInput = {
  todos: TodoItem[]
}

export type TodoWriteToolOutput = string

// WebSearch 工具的类型定义
export interface WebSearchToolInput {
  /**
   * The search query to use
   */
  query: string
  /**
   * Only include results from these domains
   */
  allowed_domains?: string[]
  /**
   * Never include results from these domains
   */
  blocked_domains?: string[]
}
export type WebSearchToolOutput = string

// WebFetch 工具的类型定义
export type WebFetchToolInput = {
  /**
   * The URL to fetch content from
   */
  url: string
  /**
   * The prompt to run on the fetched content
   */
  prompt: string
}
export type WebFetchToolOutput = string

// Grep 工具的类型定义
export interface GrepToolInput {
  /**
   * The regular expression pattern to search for
   */
  pattern: string
  /**
   * File or directory to search in (defaults to cwd)
   */
  path?: string
  /**
   * Glob pattern to filter files (e.g. "*.js")
   */
  glob?: string
  /**
   * File type to search (e.g. "js", "py", "rust")
   */
  type?: string
  /**
   * Output mode: "content", "files_with_matches", or "count"
   */
  output_mode?: 'content' | 'files_with_matches' | 'count'
  /**
   * Case insensitive search
   */
  '-i'?: boolean
  /**
   * Show line numbers (for content mode)
   */
  '-n'?: boolean
  /**
   * Lines to show before each match
   */
  '-B'?: number
  /**
   * Lines to show after each match
   */
  '-A'?: number
  /**
   * Lines to show before and after each match
   */
  '-C'?: number
  /**
   * Limit output to first N lines/entries
   */
  head_limit?: number
  /**
   * Enable multiline mode
   */
  multiline?: boolean
}

export type GrepToolOutput = string

// Write 工具的类型定义
export type WriteToolInput = {
  /**
   * The absolute path to the file to write
   */
  file_path: string
  /**
   * The content to write to the file
   */
  content: string
}

export type WriteToolOutput = string

// Edit 工具的类型定义
export type EditToolInput = {
  /**
   * The absolute path to the file to modify
   */
  file_path: string
  /**
   * The text to replace
   */
  old_string: string
  /**
   * The text to replace it with (must be different from old_string)
   */
  new_string: string
  /**
   * Replace all occurrences of old_string (default false)
   */
  replace_all?: boolean
}
export type EditToolOutput = string

// MultiEdit 工具的类型定义
export type MultiEditToolInput = {
  /**
   * The absolute path to the file to modify
   */
  file_path: string
  /**
   * Array of edit operations to perform sequentially
   */
  edits: Array<{
    /**
     * The text to replace
     */
    old_string: string
    /**
     * The text to replace it with
     */
    new_string: string
    /**
     * Replace all occurrences (default false)
     */
    replace_all?: boolean
  }>
}
export type MultiEditToolOutput = string

// BashOutput 工具的类型定义
export type BashOutputToolInput = {
  /**
   * The ID of the background shell to retrieve output from
   */
  bash_id: string
  /**
   * Optional regex to filter output lines
   */
  filter?: string
}
export type BashOutputToolOutput = string

// NotebookEdit 工具的类型定义
export type NotebookEditToolInput = {
  /**
   * The absolute path to the Jupyter notebook file
   */
  notebook_path: string
  /**
   * The ID of the cell to edit
   */
  cell_id?: string
  /**
   * The new source for the cell
   */
  new_source: string
  /**
   * The type of the cell (code or markdown)
   */
  cell_type?: 'code' | 'markdown'
  /**
   * The type of edit (replace, insert, delete)
   */
  edit_mode?: 'replace' | 'insert' | 'delete'
}
export type NotebookEditToolOutput = string

// ExitPlanModeToolInput
export type ExitPlanModeToolInput = {
  /**
   * The plan to run by the user for approval
   */
  plan: string
}
export type ExitPlanModeToolOutput = string

// ToolSearch 工具的类型定义
export interface ToolSearchToolInput {
  /**
   * Query to find deferred tools
   */
  query: string
  /**
   * Maximum number of results to return
   */
  max_results?: number
}
/**
 * ToolSearch output is an array of tool_reference objects when matches are found,
 * or a string message when no matches are found.
 */
export const ToolSearchToolOutputSchema = z.union([
  z.array(z.object({ type: z.literal('tool_reference'), tool_name: z.string() })),
  z.string()
])
export type ToolSearchToolOutput = z.infer<typeof ToolSearchToolOutputSchema>

// AskUserQuestion 工具的类型定义 (使用 Zod)
export const AskUserQuestionOptionSchema = z.object({
  /** The display text for this option */
  label: z.string(),
  /** Explanation of what this option means */
  description: z.string().optional()
})

export const AskUserQuestionItemSchema = z.object({
  /** The complete question to ask the user */
  question: z.string(),
  /** Very short label displayed as a chip/tag (max 12 chars) */
  header: z.string(),
  /** The available choices for this question (2-4 options) */
  options: z.array(AskUserQuestionOptionSchema),
  /** Set to true to allow multiple selections */
  multiSelect: z.boolean()
})

export const AskUserQuestionAnswerSchema = z.record(z.string(), z.string())

export const AskUserQuestionToolInputSchema = z.object({
  /** Questions to ask the user (1-4 questions) */
  questions: z.array(AskUserQuestionItemSchema),
  answers: AskUserQuestionAnswerSchema.optional()
})

// 从 Zod schema 推断类型
export type AskUserQuestionOption = z.infer<typeof AskUserQuestionOptionSchema>
export type AskUserQuestionItem = z.infer<typeof AskUserQuestionItemSchema>
export type AskUserQuestionToolInput = z.infer<typeof AskUserQuestionToolInputSchema>
export type AskUserQuestionAnswer = z.infer<typeof AskUserQuestionAnswerSchema>

/**
 * Safely parse AskUserQuestionToolInput from unknown data.
 * Returns undefined if the data doesn't match the expected structure.
 */
export function parseAskUserQuestionToolInput(value: unknown): AskUserQuestionToolInput | undefined {
  const result = AskUserQuestionToolInputSchema.safeParse(value)
  return result.success ? result.data : undefined
}

// ListMcpResourcesToolInput
export type ListMcpResourcesToolInput = {
  /**
   * Optional server name to filter resources by
   */
  server?: string
}
// ReadMcpResourceToolInput
export type ReadMcpResourceToolInput = {
  /**
   * The MCP server name
   */
  server: string
  /**
   * The resource URI to read
   */
  uri: string
}
export type KillBashToolInput = {
  /**
   * The ID of the background shell to kill
   */
  shell_id: string
}
// 联合类型
export type ToolInput =
  | TaskToolInput
  | BashToolInput
  | BashOutputToolInput
  | EditToolInput
  | MultiEditToolInput
  | ReadToolInput
  | WriteToolInput
  | GlobToolInput
  | GrepToolInput
  | KillBashToolInput
  | NotebookEditToolInput
  | WebFetchToolInput
  | WebSearchToolInput
  | TodoWriteToolInput
  | ExitPlanModeToolInput
  | ListMcpResourcesToolInput
  | ReadMcpResourceToolInput
  | AskUserQuestionToolInput
  | ToolSearchToolInput

export type ToolOutput = ReadToolOutput | TaskToolOutput | BashToolOutput | ToolSearchToolOutput
// These types are all just aliases for string, duplicating BashToolOutput.
// They will be added back later if more complex type distinctions are needed.
// | SearchToolOutput
// | GlobToolOutput
// | TodoWriteToolOutput
// | WebSearchToolOutput
// | GrepToolOutput
// | WebFetchToolOutput
// | WriteToolOutput
// | EditToolOutput
// | MultiEditToolOutput
// | BashOutputToolOutput
// | NotebookEditToolOutput
// | ExitPlanModeToolOutput

// 工具渲染器接口
export interface ToolRenderer {
  render: (props: { input: ToolInput; output?: ToolOutput }) => React.ReactElement
}

// 工具类型到输入类型的映射（用于文档和类型提示）
export interface ToolInputMap {
  [AgentToolsType.Skill]: SkillToolInput
  [AgentToolsType.Read]: ReadToolInput
  [AgentToolsType.Task]: TaskToolInput
  [AgentToolsType.Bash]: BashToolInput
  [AgentToolsType.Search]: SearchToolInput
  [AgentToolsType.Glob]: GlobToolInput
  [AgentToolsType.TodoWrite]: TodoWriteToolInput
  [AgentToolsType.WebSearch]: WebSearchToolInput
  [AgentToolsType.Grep]: GrepToolInput
  [AgentToolsType.Write]: WriteToolInput
  [AgentToolsType.WebFetch]: WebFetchToolInput
  [AgentToolsType.Edit]: EditToolInput
  [AgentToolsType.MultiEdit]: MultiEditToolInput
  [AgentToolsType.BashOutput]: BashOutputToolInput
  [AgentToolsType.NotebookEdit]: NotebookEditToolInput
  [AgentToolsType.ExitPlanMode]: ExitPlanModeToolInput
  [AgentToolsType.ToolSearch]: ToolSearchToolInput
}

// 工具类型到输出类型的映射
export interface ToolOutputMap {
  [AgentToolsType.Skill]: SkillToolOutput
  [AgentToolsType.Read]: ReadToolOutput
  [AgentToolsType.Task]: TaskToolOutput
  [AgentToolsType.Bash]: BashToolOutput
  [AgentToolsType.Search]: SearchToolOutput
  [AgentToolsType.Glob]: GlobToolOutput
  [AgentToolsType.TodoWrite]: TodoWriteToolOutput
  [AgentToolsType.WebSearch]: WebSearchToolOutput
  [AgentToolsType.Grep]: GrepToolOutput
  [AgentToolsType.Write]: WriteToolOutput
  [AgentToolsType.WebFetch]: WebFetchToolOutput
  [AgentToolsType.Edit]: EditToolOutput
  [AgentToolsType.MultiEdit]: MultiEditToolOutput
  [AgentToolsType.BashOutput]: BashOutputToolOutput
  [AgentToolsType.NotebookEdit]: NotebookEditToolOutput
  [AgentToolsType.ExitPlanMode]: ExitPlanModeToolOutput
  [AgentToolsType.ToolSearch]: ToolSearchToolOutput
}

// 通用工具渲染器函数类型 - 接受宽松的输入类型
export type ToolRendererFn = (props: {
  input?: ToolInput | Record<string, unknown> | string
  output?: ToolOutput | unknown
}) => NonNullable<CollapseProps['items']>[number]

// 工具渲染器映射类型
export type ToolRenderersMap = Record<AgentToolsType, ToolRendererFn>
