"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import { useState } from "react";
import { useQuery } from "@tanstack/react-query";
import { Bot, Loader2, CheckCircle2, XCircle, ChevronDown, ChevronRight, Server, Wrench, AlertTriangle } from "lucide-react";
import { Button } from "@open-mercato/ui/primitives/button";
import {
  CommandPaletteProvider,
  CommandPalette,
  useCommandPaletteContext
} from "../../../../frontend/index.js";
async function fetchHealth() {
  const res = await fetch("/api/ai_assistant/health");
  if (!res.ok) throw new Error("Failed to fetch health");
  return res.json();
}
async function fetchSettings() {
  const res = await fetch("/api/ai_assistant/settings");
  if (!res.ok) throw new Error("Failed to fetch settings");
  return res.json();
}
async function fetchTools() {
  const res = await fetch("/api/ai_assistant/tools");
  if (!res.ok) throw new Error("Failed to fetch tools");
  return res.json();
}
function AiAssistantSettingsContent() {
  const [toolsExpanded, setToolsExpanded] = useState(false);
  const { setIsOpen } = useCommandPaletteContext();
  const healthQuery = useQuery({
    queryKey: ["ai-assistant", "health"],
    queryFn: fetchHealth,
    refetchInterval: 1e4,
    staleTime: 5e3
  });
  const settingsQuery = useQuery({
    queryKey: ["ai-assistant", "settings"],
    queryFn: fetchSettings,
    staleTime: 6e4
  });
  const toolsQuery = useQuery({
    queryKey: ["ai-assistant", "tools"],
    queryFn: fetchTools,
    staleTime: 6e4
  });
  const openAiAssistant = () => {
    setIsOpen(true);
  };
  const isLoading = healthQuery.isLoading || settingsQuery.isLoading || toolsQuery.isLoading;
  if (isLoading) {
    return /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2 text-muted-foreground py-8", children: [
      /* @__PURE__ */ jsx(Loader2, { className: "h-4 w-4 animate-spin" }),
      "Loading settings..."
    ] });
  }
  const health = healthQuery.data;
  const settings = settingsQuery.data;
  const tools = toolsQuery.data?.tools || [];
  const toolsByModule = tools.reduce((acc, tool) => {
    const module = tool.module || "other";
    if (!acc[module]) acc[module] = [];
    acc[module].push(tool);
    return acc;
  }, {});
  const provider = settings?.provider;
  return /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-6", children: [
    /* @__PURE__ */ jsxs("div", { className: "space-y-1", children: [
      /* @__PURE__ */ jsxs("h1", { className: "text-2xl font-bold flex items-center gap-2", children: [
        /* @__PURE__ */ jsx(Bot, { className: "h-6 w-6" }),
        "AI Assistant Settings"
      ] }),
      /* @__PURE__ */ jsx("p", { className: "text-muted-foreground", children: "Configure and monitor the AI assistant" })
    ] }),
    /* @__PURE__ */ jsx("div", { className: "rounded-lg border bg-card p-6", children: /* @__PURE__ */ jsxs("div", { className: "flex items-start justify-between", children: [
      /* @__PURE__ */ jsxs("div", { className: "space-y-1", children: [
        /* @__PURE__ */ jsxs("h2", { className: "text-lg font-semibold flex items-center gap-2", children: [
          /* @__PURE__ */ jsx(Bot, { className: "h-5 w-5" }),
          "Test AI Assistant"
        ] }),
        /* @__PURE__ */ jsx("p", { className: "text-sm text-muted-foreground", children: "Click the button to open the AI Assistant command palette." })
      ] }),
      /* @__PURE__ */ jsxs(Button, { onClick: openAiAssistant, size: "lg", className: "gap-2", children: [
        /* @__PURE__ */ jsx(Bot, { className: "h-4 w-4" }),
        "Open AI Assistant"
      ] })
    ] }) }),
    /* @__PURE__ */ jsxs("div", { className: "rounded-lg border bg-card p-6", children: [
      /* @__PURE__ */ jsxs("h2", { className: "text-sm font-semibold mb-4 flex items-center gap-2", children: [
        /* @__PURE__ */ jsx(Server, { className: "h-4 w-4" }),
        "Configuration"
      ] }),
      /* @__PURE__ */ jsxs("div", { className: "bg-muted/50 rounded-md p-4 space-y-2", children: [
        /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2 text-sm", children: [
          /* @__PURE__ */ jsx("span", { className: "text-muted-foreground", children: "Provider:" }),
          /* @__PURE__ */ jsx("span", { className: "font-medium", children: provider?.name || "Anthropic" }),
          provider?.configured ? /* @__PURE__ */ jsxs("span", { className: "flex items-center gap-1 text-status-success-text text-xs", children: [
            /* @__PURE__ */ jsx(CheckCircle2, { className: "h-3 w-3" }),
            "Configured"
          ] }) : /* @__PURE__ */ jsxs("span", { className: "flex items-center gap-1 text-status-warning-text text-xs", children: [
            /* @__PURE__ */ jsx(XCircle, { className: "h-3 w-3" }),
            "Not configured"
          ] })
        ] }),
        /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2 text-sm", children: [
          /* @__PURE__ */ jsx("span", { className: "text-muted-foreground", children: "Model:" }),
          /* @__PURE__ */ jsx("code", { className: "font-mono text-xs bg-background px-1.5 py-0.5 rounded", children: provider?.model || "claude-haiku-4-5-20251001" })
        ] }),
        /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2 text-sm", children: [
          /* @__PURE__ */ jsx("span", { className: "text-muted-foreground", children: "Required:" }),
          /* @__PURE__ */ jsxs("span", { children: [
            "Set ",
            /* @__PURE__ */ jsx("code", { className: "font-mono text-xs bg-background px-1.5 py-0.5 rounded", children: provider?.envKey || "ANTHROPIC_API_KEY" }),
            " in .env"
          ] })
        ] })
      ] }),
      settings?.availableProviders && settings.availableProviders.length > 1 && /* @__PURE__ */ jsxs("div", { className: "mt-4", children: [
        /* @__PURE__ */ jsx("p", { className: "text-xs text-muted-foreground mb-2", children: "Available Providers:" }),
        /* @__PURE__ */ jsx("div", { className: "flex flex-wrap gap-2", children: settings.availableProviders.map((p) => /* @__PURE__ */ jsxs(
          "div",
          {
            className: `px-2 py-1 rounded text-xs ${p.id === provider?.id ? "bg-primary text-primary-foreground" : p.configured ? "bg-status-success-bg text-status-success-text" : "bg-muted text-muted-foreground"}`,
            children: [
              p.name,
              p.id === provider?.id && " (active)",
              p.id !== provider?.id && p.configured && " (ready)"
            ]
          },
          p.id
        )) })
      ] }),
      /* @__PURE__ */ jsxs("p", { className: "text-xs text-muted-foreground mt-3", children: [
        "Set ",
        /* @__PURE__ */ jsx("code", { className: "font-mono text-overline bg-muted px-1 rounded", children: "OM_AI_PROVIDER" }),
        " in .env to change provider (anthropic, openai, google)."
      ] })
    ] }),
    /* @__PURE__ */ jsxs("div", { className: "rounded-lg border bg-card p-6", children: [
      /* @__PURE__ */ jsxs("h2", { className: "text-sm font-semibold mb-4 flex items-center gap-2", children: [
        /* @__PURE__ */ jsx(AlertTriangle, { className: "h-4 w-4" }),
        "Requirements"
      ] }),
      /* @__PURE__ */ jsxs("div", { className: "bg-muted/50 rounded-md p-4 space-y-3", children: [
        /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2 text-sm", children: [
          /* @__PURE__ */ jsx("span", { className: "text-muted-foreground", children: "Full-Text Search:" }),
          health?.search?.available ? /* @__PURE__ */ jsxs("span", { className: "flex items-center gap-1 text-status-success-text", children: [
            /* @__PURE__ */ jsx(CheckCircle2, { className: "h-3 w-3" }),
            "Meilisearch connected"
          ] }) : /* @__PURE__ */ jsxs("span", { className: "flex items-center gap-1 text-status-warning-text", children: [
            /* @__PURE__ */ jsx(XCircle, { className: "h-3 w-3" }),
            "Not available"
          ] })
        ] }),
        /* @__PURE__ */ jsx("p", { className: "text-xs text-muted-foreground", children: "A full-text search driver (Meilisearch) is required for API endpoint discovery. Endpoints are indexed automatically when the MCP server starts." })
      ] })
    ] }),
    /* @__PURE__ */ jsxs("div", { className: "rounded-lg border bg-card p-6", children: [
      /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between mb-4", children: [
        /* @__PURE__ */ jsxs("h2", { className: "text-sm font-semibold flex items-center gap-2", children: [
          /* @__PURE__ */ jsx(Server, { className: "h-4 w-4" }),
          "OpenCode Connection"
        ] }),
        healthQuery.isFetching && !healthQuery.isLoading && /* @__PURE__ */ jsx(Loader2, { className: "h-3 w-3 animate-spin text-muted-foreground" })
      ] }),
      /* @__PURE__ */ jsxs("div", { className: "grid gap-4 sm:grid-cols-2", children: [
        /* @__PURE__ */ jsx("div", { className: `p-4 rounded-lg border-2 ${health?.status === "ok" && health.opencode?.healthy ? "border-status-success-border bg-status-success-bg" : "border-destructive/50 bg-destructive/5"}`, children: /* @__PURE__ */ jsxs("div", { className: "flex items-start justify-between", children: [
          /* @__PURE__ */ jsxs("div", { children: [
            /* @__PURE__ */ jsx("p", { className: "text-sm font-medium", children: "OpenCode Server" }),
            /* @__PURE__ */ jsx("p", { className: "text-xs text-muted-foreground mt-1", children: health?.status === "ok" && health.opencode?.healthy ? /* @__PURE__ */ jsxs("span", { className: "flex items-center gap-1 text-status-success-text", children: [
              /* @__PURE__ */ jsx(CheckCircle2, { className: "h-3 w-3" }),
              "Connected"
            ] }) : /* @__PURE__ */ jsxs("span", { className: "flex items-center gap-1 text-destructive", children: [
              /* @__PURE__ */ jsx(XCircle, { className: "h-3 w-3" }),
              health?.message || "Disconnected"
            ] }) }),
            health?.opencode?.version && /* @__PURE__ */ jsxs("p", { className: "text-xs text-muted-foreground mt-1", children: [
              "Version: ",
              health.opencode.version
            ] }),
            /* @__PURE__ */ jsx("p", { className: "text-xs text-muted-foreground mt-1", children: health?.url || "http://localhost:4096" })
          ] }),
          health?.status === "ok" && health.opencode?.healthy && /* @__PURE__ */ jsx("div", { className: "flex h-5 w-5 items-center justify-center rounded-full bg-status-success-bg text-status-success-icon", children: /* @__PURE__ */ jsx("svg", { className: "h-3 w-3", fill: "none", viewBox: "0 0 24 24", stroke: "currentColor", strokeWidth: 3, children: /* @__PURE__ */ jsx("path", { strokeLinecap: "round", strokeLinejoin: "round", d: "M5 13l4 4L19 7" }) }) })
        ] }) }),
        health?.mcp && Object.entries(health.mcp).map(([name, mcpStatus]) => /* @__PURE__ */ jsx(
          "div",
          {
            className: `p-4 rounded-lg border-2 ${mcpStatus.status === "connected" ? "border-status-success-border bg-status-success-bg" : mcpStatus.status === "connecting" ? "border-status-warning-border bg-status-warning-bg" : "border-destructive/50 bg-destructive/5"}`,
            children: /* @__PURE__ */ jsxs("div", { className: "flex items-start justify-between", children: [
              /* @__PURE__ */ jsxs("div", { children: [
                /* @__PURE__ */ jsx("p", { className: "text-sm font-medium", children: "MCP Server" }),
                /* @__PURE__ */ jsx("p", { className: "text-xs text-muted-foreground mt-1", children: mcpStatus.status === "connected" ? /* @__PURE__ */ jsxs("span", { className: "flex items-center gap-1 text-status-success-text", children: [
                  /* @__PURE__ */ jsx(CheckCircle2, { className: "h-3 w-3" }),
                  "Connected"
                ] }) : mcpStatus.status === "connecting" ? /* @__PURE__ */ jsxs("span", { className: "flex items-center gap-1 text-status-warning-text", children: [
                  /* @__PURE__ */ jsx(Loader2, { className: "h-3 w-3 animate-spin" }),
                  "Connecting..."
                ] }) : /* @__PURE__ */ jsxs("span", { className: "flex items-center gap-1 text-destructive", children: [
                  /* @__PURE__ */ jsx(XCircle, { className: "h-3 w-3" }),
                  mcpStatus.error || "Failed"
                ] }) }),
                /* @__PURE__ */ jsx("p", { className: "text-xs text-muted-foreground mt-1", children: name }),
                /* @__PURE__ */ jsx("p", { className: "text-xs text-muted-foreground mt-1", children: "localhost:3001" })
              ] }),
              mcpStatus.status === "connected" && /* @__PURE__ */ jsx("div", { className: "flex h-5 w-5 items-center justify-center rounded-full bg-status-success-bg text-status-success-icon", children: /* @__PURE__ */ jsx("svg", { className: "h-3 w-3", fill: "none", viewBox: "0 0 24 24", stroke: "currentColor", strokeWidth: 3, children: /* @__PURE__ */ jsx("path", { strokeLinecap: "round", strokeLinejoin: "round", d: "M5 13l4 4L19 7" }) }) })
            ] })
          },
          name
        )),
        (!health?.mcp || Object.keys(health.mcp).length === 0) && /* @__PURE__ */ jsx("div", { className: "p-4 rounded-lg border-2 border-border bg-muted/30", children: /* @__PURE__ */ jsxs("div", { children: [
          /* @__PURE__ */ jsx("p", { className: "text-sm font-medium", children: "MCP Server" }),
          /* @__PURE__ */ jsx("p", { className: "text-xs text-muted-foreground mt-1", children: /* @__PURE__ */ jsxs("span", { className: "flex items-center gap-1", children: [
            /* @__PURE__ */ jsx(XCircle, { className: "h-3 w-3" }),
            "Not connected"
          ] }) }),
          /* @__PURE__ */ jsx("p", { className: "text-xs text-muted-foreground mt-1", children: "localhost:3001" })
        ] }) })
      ] })
    ] }),
    /* @__PURE__ */ jsxs("div", { className: "rounded-lg border bg-card p-6", children: [
      /* @__PURE__ */ jsxs(
        "button",
        {
          onClick: () => setToolsExpanded(!toolsExpanded),
          className: "w-full flex items-center justify-between text-left",
          children: [
            /* @__PURE__ */ jsxs("h2", { className: "text-sm font-semibold flex items-center gap-2", children: [
              /* @__PURE__ */ jsx(Wrench, { className: "h-4 w-4" }),
              "MCP Tools (",
              tools.length,
              " tools)"
            ] }),
            toolsExpanded ? /* @__PURE__ */ jsx(ChevronDown, { className: "h-4 w-4 text-muted-foreground" }) : /* @__PURE__ */ jsx(ChevronRight, { className: "h-4 w-4 text-muted-foreground" })
          ]
        }
      ),
      toolsExpanded && /* @__PURE__ */ jsx("div", { className: "mt-4 space-y-4", children: Object.entries(toolsByModule).map(([module, moduleTools]) => /* @__PURE__ */ jsxs("div", { className: "space-y-2", children: [
        /* @__PURE__ */ jsx("h3", { className: "text-xs font-medium text-muted-foreground uppercase tracking-wider", children: module }),
        /* @__PURE__ */ jsx("div", { className: "space-y-1", children: moduleTools.map((tool) => /* @__PURE__ */ jsxs("div", { className: "pl-2 border-l-2 border-muted py-1", children: [
          /* @__PURE__ */ jsx("p", { className: "text-sm font-medium", children: tool.name }),
          /* @__PURE__ */ jsx("p", { className: "text-xs text-muted-foreground", children: tool.description })
        ] }, tool.name)) })
      ] }, module)) })
    ] })
  ] });
}
function AiAssistantSettingsPageClient() {
  return /* @__PURE__ */ jsxs(CommandPaletteProvider, { tenantId: "", organizationId: null, children: [
    /* @__PURE__ */ jsx(AiAssistantSettingsContent, {}),
    /* @__PURE__ */ jsx(CommandPalette, {})
  ] });
}
var AiAssistantSettingsPageClient_default = AiAssistantSettingsPageClient;
export {
  AiAssistantSettingsPageClient,
  AiAssistantSettingsPageClient_default as default
};
//# sourceMappingURL=AiAssistantSettingsPageClient.js.map
