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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/**
 * Global AI Chat - Floating chat widget connected to backend AI
 * Uses backend MCP chat_completion tool via REST API
 * Supports Smart UI visualization rendering via ```ui:ComponentName``` format
 */

import { useState, useRef, useEffect, useCallback } from 'react';
import {
  MessageSquare, X, Send, Loader2, Minimize2, Maximize2,
  Bot, User, Sparkles, RefreshCw
} from 'lucide-react';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { ScrollArea } from '@/components/ui/scroll-area';
import { Badge } from '@/components/ui/badge';
import { cn } from '@/lib/utils';
import { API_URL } from '@/config/api';
import {
  SmartComponentRenderer,
  parseMessageForComponents,
  UIComponentMessage,
  getAvailableComponents
} from '@/components/smart-ui/SmartComponentRenderer';

interface ChatMessage {
  id: string;
  role: 'user' | 'assistant' | 'system';
  content: string;
  timestamp: Date;
  toolUsed?: string;
}

interface GlobalAIChatProps {
  defaultOpen?: boolean;
  position?: 'bottom-right' | 'bottom-left';
}

export default function GlobalAIChat({
  defaultOpen = false,
  position = 'bottom-right'
}: GlobalAIChatProps) {
  const [isOpen, setIsOpen] = useState(defaultOpen);
  const [isMinimized, setIsMinimized] = useState(false);
  const [messages, setMessages] = useState<ChatMessage[]>([
    {
      id: '1',
      role: 'assistant',
      content: `Hej! Jeg er din AI assistent med adgang til ${getAvailableComponents().length} Smart UI visualiseringskomponenter og systemets MCP tools. Spørg mig om hvad som helst - jeg kan generere diagrammer, grafer, metrics og meget mere!`,
      timestamp: new Date()
    }
  ]);
  const [input, setInput] = useState('');
  const [isLoading, setIsLoading] = useState(false);
  const [isConnected, setIsConnected] = useState(false);
  const scrollRef = useRef<HTMLDivElement>(null);
  const inputRef = useRef<HTMLInputElement>(null);

  // Check backend connection
  const checkConnection = useCallback(async () => {
    try {
      const response = await fetch(`${API_URL}/health`);
      setIsConnected(response.ok);
    } catch {
      setIsConnected(false);
    }
  }, []);

  useEffect(() => {
    checkConnection();
    const interval = setInterval(checkConnection, 30000);
    return () => clearInterval(interval);
  }, [checkConnection]);

  // Auto-scroll to bottom
  useEffect(() => {
    if (scrollRef.current) {
      scrollRef.current.scrollTop = scrollRef.current.scrollHeight;
    }
  }, [messages]);

  // Focus input when opened
  useEffect(() => {
    if (isOpen && !isMinimized && inputRef.current) {
      inputRef.current.focus();
    }
  }, [isOpen, isMinimized]);

  const sendMessage = async () => {
    if (!input.trim() || isLoading) return;

    const userMessage: ChatMessage = {
      id: Date.now().toString(),
      role: 'user',
      content: input.trim(),
      timestamp: new Date()
    };

    setMessages(prev => [...prev, userMessage]);
    setInput('');
    setIsLoading(true);

    try {
      // Try Autonomous Agent first (AI-driven source selection)
      const response = await fetch(`${API_URL}/api/mcp/autonomous/query`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({
          type: 'natural_language',
          params: { query: userMessage.content }
        })
      });

      const data = await response.json();

      if (data.success && data.data) {
        // Autonomous agent found a result
        const assistantMessage: ChatMessage = {
          id: (Date.now() + 1).toString(),
          role: 'assistant',
          content: typeof data.data === 'string' ? data.data : JSON.stringify(data.data, null, 2),
          timestamp: new Date(),
          toolUsed: `autonomous (${data.meta?.source || 'unknown'})`
        };
        setMessages(prev => [...prev, assistantMessage]);
      } else {
        // Fallback: Use MCP route to call chat_completion tool
        const chatResponse = await fetch(`${API_URL}/api/mcp/route`, {
          method: 'POST',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify({
            tool: 'chat_completion',
            params: {
              messages: messages
                .filter(m => m.role !== 'system')
                .concat(userMessage)
                .map(m => ({
                  role: m.role,
                  content: m.content
                })),
              max_tokens: 1000,
              temperature: 0.7
            }
          })
        });

        const chatData = await chatResponse.json();

        // Extract response from various possible formats
        let responseText: string | null = null;
        let toolUsed = 'chat_completion';

        // Format 1: DeepSeek response { result: { content: [{ text: "..." }] } }
        if (chatData.result?.content?.[0]?.text) {
          responseText = chatData.result.content[0].text;
        }
        // Format 2: SRAG response { result: { response: "..." } or { answer: "..." } }
        else if (chatData.result?.response || chatData.result?.answer) {
          responseText = chatData.result.answer || chatData.result.response;
        }
        // Format 3: success with response
        else if (chatData.result?.success && chatData.result?.response) {
          responseText = chatData.result.response;
        }

        if (responseText) {
          const assistantMessage: ChatMessage = {
            id: (Date.now() + 1).toString(),
            role: 'assistant',
            content: responseText,
            timestamp: new Date(),
            toolUsed
          };
          setMessages(prev => [...prev, assistantMessage]);
        } else {
          // Try fallback to srag.query for knowledge-based questions
          const fallbackResponse = await fetch(`${API_URL}/api/mcp/route`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({
              tool: 'srag.query',
              params: {
                naturalLanguageQuery: userMessage.content,
                model: 'deepseek'
              }
            })
          });

          const fallbackData = await fallbackResponse.json();

          // Extract from fallback response
          const fallbackText = fallbackData.result?.answer
            || fallbackData.result?.response
            || fallbackData.result?.content?.[0]?.text
            || 'Beklager, jeg kunne ikke behandle din forespørgsel. Prøv igen.';

          const assistantMessage: ChatMessage = {
            id: (Date.now() + 1).toString(),
            role: 'assistant',
            content: fallbackText,
            timestamp: new Date(),
            toolUsed: 'srag.query'
          };
          setMessages(prev => [...prev, assistantMessage]);
        }
      }
    } catch (error) {
      console.error('Chat error:', error);
      const errorMessage: ChatMessage = {
        id: (Date.now() + 1).toString(),
        role: 'assistant',
        content: 'Der opstod en fejl. Tjek at backend kører på port 3001.',
        timestamp: new Date()
      };
      setMessages(prev => [...prev, errorMessage]);
    } finally {
      setIsLoading(false);
    }
  };

  const clearChat = () => {
    setMessages([{
      id: '1',
      role: 'assistant',
      content: `Chat ryddet. Jeg kan generere visualiseringer med ${getAvailableComponents().length} komponenter. Hvordan kan jeg hjælpe?`,
      timestamp: new Date()
    }]);
  };

  const positionClasses = position === 'bottom-right'
    ? 'right-4 bottom-4'
    : 'left-4 bottom-4';

  if (!isOpen) {
    return (
      <Button
        onClick={() => setIsOpen(true)}
        className={cn(
          "fixed z-50 h-14 w-14 rounded-full shadow-lg",
          "bg-gradient-to-br from-primary to-primary/80 hover:from-primary/90 hover:to-primary/70",
          "transition-all duration-300 hover:scale-110",
          positionClasses
        )}
      >
        <MessageSquare className="h-6 w-6" />
        {!isConnected && (
          <span className="absolute -top-1 -right-1 h-3 w-3 rounded-full bg-destructive animate-pulse" />
        )}
      </Button>
    );
  }

  return (
    <div
      className={cn(
        "fixed z-50 flex flex-col bg-background border border-border rounded-lg shadow-2xl",
        "transition-all duration-300",
        isMinimized ? "h-12 w-80" : "h-[500px] w-[380px]",
        positionClasses
      )}
    >
      {/* Header */}
      <div className="flex items-center justify-between px-4 py-2 border-b border-border bg-secondary/30 rounded-t-lg">
        <div className="flex items-center gap-2">
          <Bot className="h-5 w-5 text-primary" />
          <span className="font-semibold text-sm">AI Assistent</span>
          <div className={cn(
            "h-2 w-2 rounded-full",
            isConnected ? "bg-green-500" : "bg-red-500"
          )} />
        </div>
        <div className="flex items-center gap-1">
          <Button
            variant="ghost"
            size="icon"
            className="h-7 w-7"
            onClick={clearChat}
            title="Ryd chat"
          >
            <RefreshCw className="h-3.5 w-3.5" />
          </Button>
          <Button
            variant="ghost"
            size="icon"
            className="h-7 w-7"
            onClick={() => setIsMinimized(!isMinimized)}
          >
            {isMinimized ? <Maximize2 className="h-3.5 w-3.5" /> : <Minimize2 className="h-3.5 w-3.5" />}
          </Button>
          <Button
            variant="ghost"
            size="icon"
            className="h-7 w-7"
            onClick={() => setIsOpen(false)}
          >
            <X className="h-3.5 w-3.5" />
          </Button>
        </div>
      </div>

      {!isMinimized && (
        <>
          {/* Messages */}
          <ScrollArea className="flex-1 p-4" ref={scrollRef}>
            <div className="space-y-4">
              {messages.map((message) => (
                <div
                  key={message.id}
                  className={cn(
                    "flex gap-3",
                    message.role === 'user' ? "flex-row-reverse" : "flex-row"
                  )}
                >
                  <div className={cn(
                    "flex-shrink-0 h-8 w-8 rounded-full flex items-center justify-center",
                    message.role === 'user'
                      ? "bg-primary text-primary-foreground"
                      : "bg-secondary"
                  )}>
                    {message.role === 'user' ? (
                      <User className="h-4 w-4" />
                    ) : (
                      <Sparkles className="h-4 w-4 text-primary" />
                    )}
                  </div>
                  <div className={cn(
                    "flex-1 max-w-[280px]",
                    message.role === 'user' ? "text-right" : "text-left"
                  )}>
                    <div className={cn(
                      "inline-block p-3 rounded-lg text-sm",
                      message.role === 'user'
                        ? "bg-primary text-primary-foreground rounded-br-none"
                        : "bg-secondary rounded-bl-none"
                    )}>
                      {/* Parse and render Smart UI components from message content */}
                      {message.role === 'assistant' ? (
                        <div className="space-y-2">
                          {parseMessageForComponents(message.content).map((part, idx) => (
                            typeof part === 'string' ? (
                              <p key={idx} className="whitespace-pre-wrap">{part}</p>
                            ) : (
                              <SmartComponentRenderer
                                key={part.id || idx}
                                message={part as UIComponentMessage}
                                className="my-2"
                              />
                            )
                          ))}
                        </div>
                      ) : (
                        <p className="whitespace-pre-wrap">{message.content}</p>
                      )}
                    </div>
                    <div className="flex items-center gap-2 mt-1 text-[10px] text-muted-foreground">
                      <span>
                        {message.timestamp.toLocaleTimeString('da-DK', {
                          hour: '2-digit',
                          minute: '2-digit'
                        })}
                      </span>
                      {message.toolUsed && (
                        <Badge variant="outline" className="text-[8px] py-0 h-4">
                          {message.toolUsed}
                        </Badge>
                      )}
                    </div>
                  </div>
                </div>
              ))}
              {isLoading && (
                <div className="flex gap-3">
                  <div className="h-8 w-8 rounded-full bg-secondary flex items-center justify-center">
                    <Loader2 className="h-4 w-4 animate-spin text-primary" />
                  </div>
                  <div className="bg-secondary p-3 rounded-lg rounded-bl-none">
                    <div className="flex gap-1">
                      <span className="w-2 h-2 bg-primary/50 rounded-full animate-bounce" style={{ animationDelay: '0ms' }} />
                      <span className="w-2 h-2 bg-primary/50 rounded-full animate-bounce" style={{ animationDelay: '150ms' }} />
                      <span className="w-2 h-2 bg-primary/50 rounded-full animate-bounce" style={{ animationDelay: '300ms' }} />
                    </div>
                  </div>
                </div>
              )}
            </div>
          </ScrollArea>

          {/* Input */}
          <div className="p-4 border-t border-border">
            <div className="flex gap-2">
              <Input
                ref={inputRef}
                value={input}
                onChange={(e) => setInput(e.target.value)}
                onKeyDown={(e) => {
                  if (e.key === 'Enter' && !e.shiftKey) {
                    e.preventDefault();
                    sendMessage();
                  }
                }}
                placeholder="Skriv din besked..."
                className="flex-1"
                disabled={isLoading || !isConnected}
              />
              <Button
                onClick={sendMessage}
                disabled={isLoading || !input.trim() || !isConnected}
                size="icon"
              >
                {isLoading ? (
                  <Loader2 className="h-4 w-4 animate-spin" />
                ) : (
                  <Send className="h-4 w-4" />
                )}
              </Button>
            </div>
            {!isConnected && (
              <p className="text-[10px] text-destructive mt-2">
                Ikke forbundet til backend. Tjek at serveren kører.
              </p>
            )}
          </div>
        </>
      )}
    </div>
  );
}