"use client"

import Link from "next/link"
import { Label } from "@/components/ui/label"
import { Switch } from "@/components/ui/switch"
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"
import {
  toggleUltiAiToolGroup,
  ULTIAI_TOOL_GROUPS,
} from "@/lib/ai/ultiai-tool-groups"

type UltiAiToolsCardProps = {
  enabledTools: string[]
  onChange: (enabledTools: string[]) => void
  webSearchSettingsHref?: string
}

export function UltiAiToolsCard({
  enabledTools,
  onChange,
  webSearchSettingsHref = "/mail/settings/automation",
}: UltiAiToolsCardProps) {
  return (
    <Card>
      <CardHeader className="pb-3">
        <CardTitle className="text-sm font-medium">Tools UltiAI</CardTitle>
        <CardDescription>
          Groupes d&apos;outils MCP exposés à l&apos;assistant. La recherche web supporte Brave,
          Bing, DuckDuckGo, SearXNG et API JSON — config dans{" "}
          <Link href={webSearchSettingsHref} className="underline underline-offset-2">
            Automatisations → Recherche
          </Link>{" "}
          (utilisateur) ou Administration → Moteur de recherche (organisation).
        </CardDescription>
      </CardHeader>
      <CardContent className="space-y-2">
        {ULTIAI_TOOL_GROUPS.map((group) => {
          const checked = enabledTools.includes(group.id)
          return (
            <label
              key={group.id}
              className="flex items-start justify-between gap-4 rounded-lg border p-3"
            >
              <div className="min-w-0">
                <Label className="text-sm font-medium">{group.label}</Label>
                <p className="mt-0.5 text-xs text-muted-foreground">{group.description}</p>
              </div>
              <Switch
                checked={checked}
                onCheckedChange={(enabled) =>
                  onChange(toggleUltiAiToolGroup(enabledTools, group.id, enabled))
                }
              />
            </label>
          )
        })}
      </CardContent>
    </Card>
  )
}
