"use client"

import Link from "next/link"
import { Sparkles } from "lucide-react"
import { AiChatIframe } from "@/components/ai/ai-chat-iframe"
import { useAiConfig } from "@/lib/api/hooks/use-ai-queries"
import { Button } from "@/components/ui/button"

export default function ChatPage() {
  const { data: config, isLoading, isError } = useAiConfig()

  if (isLoading) {
    return (
      <div className="flex h-dvh items-center justify-center text-sm text-muted-foreground">
        Chargement UltiAI…
      </div>
    )
  }

  if (isError) {
    return (
      <div className="flex h-dvh flex-col items-center justify-center gap-3 px-6 text-center">
        <Sparkles className="h-8 w-8 text-muted-foreground" />
        <p className="text-sm text-muted-foreground">
          Impossible de contacter l&apos;API UltiAI. Vérifiez que le backend est démarré.
        </p>
      </div>
    )
  }

  if (!config?.enabled) {
    return (
      <div className="flex h-dvh flex-col items-center justify-center gap-3 px-6 text-center">
        <Sparkles className="h-8 w-8 text-muted-foreground" />
        <p className="max-w-md text-sm text-muted-foreground">
          UltiAI n&apos;est pas activé. Activez le plugin{" "}
          <strong>UltiAI</strong> dans Administration → Plugins (puis enregistrez), ou définissez{" "}
          <code className="rounded bg-muted px-1">AI_ASSISTANT_ENABLED=true</code> dans le
          déploiement backend.
        </p>
        <Button asChild variant="outline" size="sm">
          <Link href="/admin/settings/plugins">Ouvrir les plugins</Link>
        </Button>
      </div>
    )
  }

  return (
    <AiChatIframe
      publicPath={config.public_path}
      context={{ app: "standalone", temporary: false }}
      className="h-dvh w-full border-0"
    />
  )
}
