import type OpenAI from "openai"
import accessMcpResource from "./access_mcp_resource"
import { apply_diff } from "./apply_diff"
import applyPatch from "./apply_patch"
import askFollowupQuestion from "./ask_followup_question"
import askMultipleChoice from "./ask_multiple_choice"
import sequentialThinking from "./sequential_thinking"
import attemptCompletion from "./attempt_completion"
// import codebaseSearch from "./codebase_search"
// import costrictCheckpoint from "./costrict_checkpoint"
import editTool from "./edit"
import executeCommand from "./execute_command"
import fileOutline from "./file_outline"
import generateImage from "./generate_image"
import listFiles from "./list_files"
import newTask from "./new_task"
import readCommandOutput from "./read_command_output"
import { createReadFileTool, type ReadFileToolOptions } from "./read_file"
import runSlashCommand from "./run_slash_command"
import skill from "./skill"
import searchReplace from "./search_replace"
import edit_file from "./edit_file"
import searchFiles from "./search_files"
import switchMode from "./switch_mode"
import updateTodoList from "./update_todo_list"
import writeToFile from "./write_to_file"

export { getMcpServerTools } from "./mcp_server"
export { convertOpenAIToolToAnthropic, convertOpenAIToolsToAnthropic } from "./converters"
export type { ReadFileToolOptions } from "./read_file"

import {
	getLiteReadFileDescription,
	getLiteWriteToFileDescription,
	getLiteApplyPatchDescription,
	getLiteApplyDiffDescription,
	getLiteSearchFilesDescription,
	getLiteListFilesDescription,
	getLiteExecuteCommandDescription,
	getLiteFileOutlineDescription,
	getLiteSequentialThinkingDescription,
	getLiteAskFollowupQuestionDescription,
	getLiteAttemptCompletionDescription,
	getLiteBrowserActionDescription,
	getLiteSwitchModeDescription,
	getLiteNewTaskDescription,
	getLiteUpdateTodoListDescription,
	getLiteSkillDescription,
	// getLiteCodebaseSearchDescription,
	getLiteAccessMcpResourceDescription,
	getLiteGenerateImageDescription,
	getLiteRunSlashCommandDescription,
	getLiteEditFileDescription,
	getLiteAskMultipleChoiceDescription,
	getLiteSearchAndReplaceDescription,
	getLiteSearchReplaceDescription,
	getLiteReadCommandOutputDescription,
	getLiteCostrictCheckpointDescription,
} from "../lite-descriptions"

/**
 * Options for customizing the native tools array.
 */
export interface NativeToolsOptions {
	/** Whether the model supports image processing (default: false) */
	supportsImages?: boolean
	useLitePrompts?: boolean
}

function getLiteDescription(tool: OpenAI.Chat.ChatCompletionFunctionTool): string {
	switch (tool.function!.name) {
		case "access_mcp_resource":
			return getLiteAccessMcpResourceDescription()
		case "apply_diff":
			return getLiteApplyDiffDescription()
		case "apply_patch":
			return getLiteApplyPatchDescription()
		case "ask_followup_question":
			return getLiteAskFollowupQuestionDescription()
		case "ask_multiple_choice":
			return getLiteAskMultipleChoiceDescription()
		case "attempt_completion":
			return getLiteAttemptCompletionDescription()
		case "browser_action":
			return getLiteBrowserActionDescription()
		// case "costrict_checkpoint": // todo: implement
		// 	return getLiteCostrictCheckpointDescription()
		// case "codebase_search":
		// 	return getLiteCodebaseSearchDescription()
		case "execute_command":
			return getLiteExecuteCommandDescription()
		case "file_outline":
			return getLiteFileOutlineDescription()
		case "sequential_thinking":
			return getLiteSequentialThinkingDescription()
		case "skill":
			return getLiteSkillDescription()
		case "generate_image":
			return getLiteGenerateImageDescription()
		case "list_files":
			return getLiteListFilesDescription()
		case "new_task":
			return getLiteNewTaskDescription()
		case "read_file":
			return getLiteReadFileDescription()
		case "read_command_output":
			return getLiteReadCommandOutputDescription()
		case "run_slash_command":
			return getLiteRunSlashCommandDescription()
		case "edit_file":
			return getLiteEditFileDescription()
		case "search_and_replace":
			return getLiteSearchAndReplaceDescription()
		case "search_files":
			return getLiteSearchFilesDescription()
		case "search_replace":
			return getLiteSearchReplaceDescription()
		case "switch_mode":
			return getLiteSwitchModeDescription()
		case "update_todo_list":
			return getLiteUpdateTodoListDescription()
		case "write_to_file":
			return getLiteWriteToFileDescription()
		default:
			return tool.function!.description!
	}
}

/**
 * Get native tools array, optionally customizing based on settings.
 *
 * @param options - Configuration options for the tools
 * @returns Array of native tool definitions
 */
export function getNativeTools(options: NativeToolsOptions = {}): OpenAI.Chat.ChatCompletionTool[] {
	const { supportsImages = false, useLitePrompts = false } = options

	const readFileOptions: ReadFileToolOptions = {
		supportsImages,
	}

	return [
		accessMcpResource,
		apply_diff,
		applyPatch,
		askFollowupQuestion,
		askMultipleChoice,
		sequentialThinking,
		attemptCompletion,
		// codebaseSearch,
		// costrictCheckpoint,
		executeCommand,
		fileOutline,
		generateImage,
		listFiles,
		newTask,
		readCommandOutput,
		createReadFileTool(readFileOptions),
		runSlashCommand,
		skill,
		searchReplace,
		edit_file,
		editTool,
		searchFiles,
		switchMode,
		updateTodoList,
		writeToFile,
	].map((item) => {
		if (item.type === "function") {
			return {
				...item,
				function: {
					...item.function,
					description: useLitePrompts ? getLiteDescription(item) : item.function.description,
				},
			}
		}
		return item
	}) satisfies OpenAI.Chat.ChatCompletionTool[]
}

// Backward compatibility: export default tools with line ranges enabled
export const nativeTools = getNativeTools()
