import { useMemo } from "react";
import { API_ENDPOINTS, fetchJSON, withAgentExecutionScope } from "@/lib/api";
import type { WorkerScope } from "@/types/config";
import { useFetchData } from "./useFetchData";

export interface ToolFieldSchema {
  name: string;
  label: string;
  type:
    | "boolean"
    | "number"
    | "password"
    | "select"
    | "string[]"
    | "text"
    | "url";
  required?: boolean;
  default?: unknown;
  placeholder?: string | null;
  description?: string | null;
  requiredWhenFieldChanges?: string | null;
  options?: Array<{ label: string; value: string }> | null;
  validation?: Record<string, unknown> | null;
}

export interface ToolInfo {
  name: string;
  display_name: string;
  description: string;
  category: string;
  status: string;
  setup_type: string;
  default_execution_target?: string | null;
  icon: string | null;
  icon_color: string | null;
  config_fields: ToolFieldSchema[] | null;
  agent_override_fields?: ToolFieldSchema[] | null;
  dependencies: string[] | null;
  auth_provider?: string | null;
  docs_url?: string | null;
  helper_text?: string | null;
  function_names?: string[] | null;
  dashboard_configuration_supported?: boolean;
  execution_scope_supported?: boolean;
}

export interface ToolsResponse {
  tools: ToolInfo[];
  status_authoritative?: boolean;
}

const DEFAULT_RESPONSE: ToolsResponse = {
  tools: [],
  status_authoritative: true,
};

export function useTools(
  agentName?: string | null,
  executionScope?: WorkerScope | null,
) {
  const fetcher = useMemo(
    () => async () => {
      return (await fetchJSON<ToolsResponse>(
        withAgentExecutionScope(API_ENDPOINTS.tools, agentName, executionScope),
      )) as ToolsResponse;
    },
    [agentName, executionScope],
  );
  const { data: response, ...rest } = useFetchData(fetcher, DEFAULT_RESPONSE);
  return {
    tools: response.tools,
    statusAuthoritative: response.status_authoritative ?? true,
    ...rest,
  };
}

// Helper function to map backend tool to frontend integration format
export function mapToolToIntegration(tool: ToolInfo) {
  // Map backend status to frontend status
  let status: "connected" | "not_connected" | "available";
  switch (tool.status) {
    case "available":
      // For tools that require configuration, 'available' means they are configured
      if (
        tool.setup_type === "api_key" ||
        tool.setup_type === "oauth" ||
        tool.setup_type === "special"
      ) {
        status = "connected";
      } else {
        status = "available";
      }
      break;
    case "requires_config":
      status = "not_connected";
      break;
    default:
      status = "available";
  }

  // Map setup_type
  let setup_type: "oauth" | "api_key" | "special" | "none";
  switch (tool.setup_type) {
    case "oauth":
      setup_type = "oauth";
      break;
    case "api_key":
      setup_type = "api_key";
      break;
    case "special":
      setup_type = "special";
      break;
    case "none":
    default:
      setup_type = "none";
      break;
  }

  return {
    id: tool.name,
    name: tool.display_name,
    description: tool.description,
    category: tool.category,
    icon: tool.icon, // This will need to be mapped to React components
    icon_color: tool.icon_color,
    status,
    setup_type,
    config_fields: tool.config_fields,
    dependencies: tool.dependencies,
    docs_url: tool.docs_url,
    helper_text: tool.helper_text,
  };
}
