File size: 25,250 Bytes
ca28016
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
import { cn } from '@/lib/utils';
import { Avatar, AvatarFallback } from '@/components/ui/avatar';
import {
  Bot,
  FileText,
  User,
  FlaskConical,
  Brain,
  Zap,
  Search,
} from 'lucide-react';
import { Skeleton } from './ui/skeleton';
import {
  Accordion,
  AccordionContent,
  AccordionItem,
  AccordionTrigger,
} from '@/components/ui/accordion';
import { StaticHypercubeAnalysis } from './ui/static-hypercube-analysis';

import React from 'react';
import type { Message } from '@/app/page';

type ChatMessageProps = {
  message: Message;
  onConsciousnessDimensionSelect?: (dimension: string) => void;
  selectedConsciousnessDimension?: string;
  onOpenEditor?: (b64: string, prompt?: string) => void;
};

const AssistantMessageContent = ({
  message,
  selectedConsciousnessDimension,
  onOpenEditor,
}: {
  message: Message;
  selectedConsciousnessDimension?: string;
  onOpenEditor?: (b64: string, prompt?: string) => void;
}) => {
  const { content, aetherAnalysis, golemStats, search_results, search_query, imageBase64 } = message as any;
  const cleanContent = typeof content === 'string'
    ? content
        .replace(/^\[\[IMAGE_MODE\]\]\s*/i, '')
        .replace(/\s*\[\[SAFE_MODE=(ON|OFF)\]\]/i, '')
    : content;

  if (imageBase64) {
    const onlyImage = !cleanContent || /^image generated\.?$/i.test((cleanContent || '').trim());
    return (
      <div className={`items-start ${onlyImage ? '' : 'grid grid-cols-1 md:grid-cols-2 gap-4'}`}>
        <div className="relative rounded-xl overflow-hidden bg-white/5 dark:bg-white/5 backdrop-blur-xl border border-white/10 shadow-xl w-full max-w-[min(1200px,95vw)] mx-auto">
          <div className="p-2 border-b border-white/10 text-xs text-muted-foreground flex items-center justify-between">
            <span>Generated Image</span>
            <div className="flex gap-2">
              <button className="px-2 py-1 rounded bg-white/10 hover:bg-white/20" onClick={() => onOpenEditor && onOpenEditor(imageBase64, cleanContent)}>Edit</button>
              <a className="px-2 py-1 rounded bg-white/10 hover:bg-white/20" href={`data:image/png;base64,${imageBase64}`} download={"aether-image.png"}>Download</a>
            </div>
          </div>
          <div className="p-3">
            <img src={`data:image/png;base64,${imageBase64}`} alt="Generated" className="w-full h-auto rounded-lg max-h-[85vh] object-contain" />
          </div>
        </div>
        {!onlyImage && (
          <div className="mt-4 md:mt-0 md:ml-4">
            <p className="whitespace-pre-wrap leading-relaxed">{cleanContent}</p>
          </div>
        )}
      </div>
    );
  }

  return (
    <div className="flex flex-col gap-2">
      <p className="whitespace-pre-wrap leading-relaxed">{cleanContent}</p>

      <Accordion type="multiple" className="w-full space-y-1">
        {search_results && search_results.length > 0 && (
          <AccordionItem value="search-results" className="border-none">
            <AccordionTrigger className="flex w-full items-center justify-start gap-2 rounded-md p-2 text-xs text-muted-foreground hover:bg-card-foreground/5 hover:no-underline -mx-2">
              <Search className="h-4 w-4" />
              <span>Web Search Results for "{search_query}"</span>
            </AccordionTrigger>
            <AccordionContent className="mt-2 border-t border-card-foreground/10 pt-3 text-muted-foreground">
              <div className="space-y-3">
                {search_results.map((result: any, index: number) => (
                  <div key={index} className="text-xs">
                    <a
                      href={result.link}
                      target="_blank"
                      rel="noopener noreferrer"
                      className="text-blue-400 hover:underline"
                    >
                      {result.title}
                    </a>
                    <p className="text-muted-foreground">{result.snippet}</p>
                  </div>
                ))}
              </div>
            </AccordionContent>
          </AccordionItem>
        )}
        {(message as any).aiThoughts && (
          <AccordionItem value="ai-thoughts" className="border-none">
            <AccordionTrigger className="flex w-full items-center justify-start gap-2 rounded-md p-2 text-xs text-muted-foreground hover:bg-card-foreground/5 hover:no-underline -mx-2">
              <Brain className="h-4 w-4" />
              <span>AI Thinking Process ({((message as any).aiThoughts.thinkingTime?.totalTime || 0).toFixed(1)}s)</span>
            </AccordionTrigger>
            <AccordionContent className="mt-2 border-t border-card-foreground/10 pt-3 text-muted-foreground">
              <div className="space-y-4">
                
                {/* Context Analysis */}
                <div className="space-y-2">
                  <div className="flex items-center gap-2 text-xs font-medium text-blue-400">
                    <FileText className="h-3 w-3" />
                    <span>Context Analysis</span>
                    <span className="text-muted-foreground">({((message as any).aiThoughts.thinkingTime?.analysisTime || 0).toFixed(1)}s)</span>
                  </div>
                  <div className="text-xs whitespace-pre-wrap bg-card-foreground/5 p-3 rounded">
                    {(message as any).aiThoughts.contextAnalysis}
                  </div>
                </div>

                {/* Reflection */}
                <div className="space-y-2">
                  <div className="flex items-center gap-2 text-xs font-medium text-purple-400">
                    <Zap className="h-3 w-3" />
                    <span>Response Strategy</span>
                    <span className="text-muted-foreground">({((message as any).aiThoughts.thinkingTime?.reflectionTime || 0).toFixed(1)}s)</span>
                  </div>
                  <div className="text-xs whitespace-pre-wrap bg-card-foreground/5 p-3 rounded">
                    {(message as any).aiThoughts.reflection}
                  </div>
                </div>

                {/* Chat Context */}
                {(message as any).aiThoughts.chatContext && (message as any).aiThoughts.chatContext !== 'No previous context' && (
                  <div className="space-y-2">
                    <div className="flex items-center gap-2 text-xs font-medium text-green-400">
                      <FlaskConical className="h-3 w-3" />
                      <span>Conversation Context</span>
                    </div>
                    <div className="text-xs whitespace-pre-wrap bg-card-foreground/5 p-3 rounded max-h-32 overflow-y-auto">
                      {(message as any).aiThoughts.chatContext}
                    </div>
                  </div>
                )}

                {/* User Insights */}
                <div className="space-y-2">
                  <div className="flex items-center gap-2 text-xs font-medium text-orange-400">
                    <User className="h-3 w-3" />
                    <span>User Insights</span>
                  </div>
                  <div className="text-xs bg-card-foreground/5 p-2 rounded">
                    {(message as any).aiThoughts.userInsights}
                  </div>
                </div>

              </div>
            </AccordionContent>
          </AccordionItem>
        )}
        {aetherAnalysis && (
          <AccordionItem value="aether-analysis" className="border-none">
            <AccordionTrigger className="flex w-full items-center justify-start gap-2 rounded-md p-2 text-xs text-muted-foreground hover:bg-card-foreground/5 hover:no-underline -mx-2 -mb-2">
              <Brain className="h-4 w-4" />
              <span>5D Consciousness Analysis</span>
            </AccordionTrigger>
            <AccordionContent className="mt-2 border-t border-card-foreground/10 pt-3 text-muted-foreground">
              {/* Match 5D modal layout: viz on top, realtime state row below */}
              <div className="space-y-4">
                {/* Static Hypercube Visualization */}
                <div className="space-y-2">
                  <div className="flex items-center gap-2 text-xs font-medium">
                    <Zap className="h-3 w-3" />
                    <span>5D Hypercube Position & Clustering</span>
                  </div>
                  <StaticHypercubeAnalysis
                    aetherData={golemStats?.aether_data}
                    currentDimension={message.consciousnessDimensionUsed || selectedConsciousnessDimension}
                    className="mb-2 h-40"
                  />
                </div>

                {/* Realtime concise state (bottom of hypercube) - match modal with color bullets */}
                {golemStats?.hypercube_state && (
                  <div className="text-xs text-muted-foreground">
                    <div className="grid grid-cols-2 gap-4">
                      <div className="space-y-2">
                        <div className="flex items-start gap-2">
                          <span className="mt-1.5 inline-block h-2 w-2 rounded-full bg-blue-400" />
                          <div>
                            <div className="font-medium">Vertex: {golemStats.hypercube_state.current_vertex}/32</div>
                            <div className="text-muted-foreground">Signature: {golemStats.hypercube_state.consciousness_signature}</div>
                          </div>
                        </div>
                        {/* Active Dimensions chips */}
                        {golemStats.hypercube_state.dimension_activations && (
                          <div>
                            <div className="font-medium">Active Dimensions:</div>
                            <div className="mt-1 flex flex-wrap gap-1">
                              {Object.entries(golemStats.hypercube_state.dimension_activations)
                                .filter(([, active]) => !!active)
                                .map(([dim]) => (
                                  <span key={dim} className="px-2 py-1 bg-blue-100/60 dark:bg-blue-900/60 rounded-full text-xs">
                                    {dim}
                                  </span>
                                ))}
                            </div>
                          </div>
                        )}
                      </div>
                      <div className="space-y-1">
                        <div className="flex items-center gap-2">
                          <span className="inline-block h-2 w-2 rounded-full bg-purple-400" />
                          {typeof golemStats.hypercube_state.consciousness_level === 'number' ? (
                            <div className="font-medium">Consciousness Level: {(Number(golemStats.hypercube_state.consciousness_level) * 100).toFixed(1)}%</div>
                          ) : (
                            <div className="font-medium">Consciousness Level: β€”</div>
                          )}
                        </div>
                        <div className="flex items-center gap-2">
                          <span className="inline-block h-2 w-2 rounded-full bg-green-400" />
                          <div className="font-medium">Aether Patterns: {golemStats.hypercube_state.aether_patterns ?? 'β€”'}</div>
                        </div>
                      </div>
                    </div>
                  </div>
                )}

                {/* Color-coded bullets, one variable per line, no '##' headings */}
                {(() => {
                  // Colors
                  const colors = {
                    consciousness: '#0D9488', // dark turquoise blue
                    signature: {
                      awareness: '#3B82F6',
                      wisdom: '#8B5CF6',
                      compassion: '#10B981',
                      creativity: '#F97316',
                      transcendence: '#EF4444',
                    } as Record<string, string>,
                    vertex: '#F97316', // bright orange
                    level: '#EC4899', // pink
                    patterns: '#FACC15', // bright yellow
                  };

                  const dim = (message as any).consciousnessDimensionUsed || selectedConsciousnessDimension || 'awareness';
                  const signatureColor = colors.signature[dim] || '#60A5FA';

                  // Prefer structured data from golemStats; fall back to parsing aetherAnalysis line
                  let vertex = golemStats?.hypercube_state?.current_vertex !== undefined
                    ? `${golemStats.hypercube_state.current_vertex}/32`
                    : undefined;
                  let signature = golemStats?.hypercube_state?.consciousness_signature as string | undefined;
                  let level = typeof golemStats?.hypercube_state?.consciousness_level === 'number'
                    ? Number(golemStats!.hypercube_state!.consciousness_level).toFixed(3)
                    : undefined;
                  let patterns = (golemStats?.hypercube_state as any)?.aether_patterns as number | undefined;

                  // Parse fallback string if needed
                  if ((!vertex || !signature || !level || patterns === undefined) && aetherAnalysis) {
                    const line = aetherAnalysis;
                    const get = (label: string) => {
                      const re = new RegExp(label + "\\s*:?\\s*([^|]+)", 'i');
                      const m = line.match(re);
                      return m ? m[1].trim() : undefined;
                    };
                    vertex = vertex || get('Vertex');
                    signature = signature || get('Signature');
                    level = level || get('Level');
                    const pat = get('Aether Patterns');
                    if (patterns === undefined && pat) {
                      const num = parseInt(pat.replace(/[^0-9]/g, '')); 
                      patterns = isNaN(num) ? undefined : num;
                    }
                  }

                  // If we still lack data, don't render
                  if (!vertex && !signature && !level && patterns === undefined) return null;

                  const items = [
                    { label: 'Consciousness', value: dim, color: colors.consciousness },
                    { label: 'Signature', value: signature ?? 'β€”', color: signatureColor },
                    { label: 'Vertex', value: vertex ?? 'β€”', color: colors.vertex },
                    { label: 'Level', value: level ?? 'β€”', color: colors.level },
                    { label: 'Aether Patterns', value: patterns ?? 'β€”', color: colors.patterns },
                  ];

                  return (
                    <div className="text-xs">
                      <div className="flex flex-col gap-1">
                        {items.map((it, i) => (
                          <div key={i} className="flex items-center gap-2">
                            <span className="inline-block h-2 w-2 rounded-full" style={{ backgroundColor: it.color }} />
                            <span className="font-medium">{it.label}:</span>
                            <span className="text-muted-foreground">{it.value}</span>
                          </div>
                        ))}
                      </div>
                    </div>
                  );
                })()}
              </div>
            </AccordionContent>
          </AccordionItem>
        )}
      </Accordion>
    </div>
  );
};

export function ChatMessage({ message, onConsciousnessDimensionSelect, selectedConsciousnessDimension, onOpenEditor }: ChatMessageProps) {
  const isUser = message.role === 'user';

  return (
    <div
      className={cn(
        'group relative flex items-start gap-3',
        isUser ? 'flex-row-reverse' : 'flex-row'
      )}
    >
      <Avatar className="h-8 w-8 shrink-0">
        <AvatarFallback className={cn(
          isUser 
            ? 'bg-primary text-primary-foreground' 
            : 'bg-muted'
        )}>
          {isUser ? <User className="h-4 w-4" /> : <Bot className="h-4 w-4" />}
          </AvatarFallback>
        </Avatar>

      <div
        className={cn(
          'flex flex-col gap-2 rounded-lg px-3 py-2',
          isUser
            ? 'w-fit max-w-[60%] bg-primary text-primary-foreground'
            : 'w-full max-w-[95%] bg-muted/50'
        )}
      >
        {message.file && (
          <div className="flex items-center gap-2 text-xs opacity-70">
            <FileText className="h-3 w-3" />
            <span>{message.file.name}</span>
          </div>
        )}
        
        {isUser ? (
          <p className="whitespace-pre-wrap leading-relaxed">
            {typeof message.content === 'string'
              ? message.content
                  .replace(/^\[\[IMAGE_MODE\]\]\s*/i, '')
                  .replace(/\s*\[\[SAFE_MODE=(ON|OFF)\]\]/i, '')
              : message.content}
          </p>
        ) : (
          <AssistantMessageContent
            message={message}
            selectedConsciousnessDimension={selectedConsciousnessDimension}
            onOpenEditor={onOpenEditor}
          />
        )}
      </div>
    </div>
  );
}

export function LoadingMessage() {
  const [currentPhase, setCurrentPhase] = React.useState(1);
  const [phaseStartTime, setPhaseStartTime] = React.useState(Date.now());
  const [elapsedTime, setElapsedTime] = React.useState(0);

  React.useEffect(() => {
    const interval = setInterval(() => {
      const now = Date.now();
      const elapsed = (now - phaseStartTime) / 1000;
      setElapsedTime(elapsed);

      // Switch phases based on elapsed time
      if (elapsed > 8 && currentPhase === 1) {
        setCurrentPhase(2);
        setPhaseStartTime(now);
      } else if (elapsed > 5 && currentPhase === 2) {
        setCurrentPhase(3);
        setPhaseStartTime(now);
      }
    }, 100);

    return () => clearInterval(interval);
  }, [currentPhase, phaseStartTime]);

  const getPhaseText = () => {
    switch (currentPhase) {
      case 1: return `Analyzing context and query... (${elapsedTime.toFixed(1)}s)`;
      case 2: return `Reflecting on approach... (${elapsedTime.toFixed(1)}s)`;
      case 3: return `Generating response... (${elapsedTime.toFixed(1)}s)`;
      default: return 'AI is thinking...';
    }
  };

  const getPhaseIcon = () => {
    switch (currentPhase) {
      case 1: return <FileText className="h-3 w-3 text-blue-400" />;
      case 2: return <Zap className="h-3 w-3 text-purple-400" />;
      case 3: return <Brain className="h-3 w-3 text-green-400" />;
      default: return <Brain className="h-3 w-3" />;
    }
  };

  return (
    <div className="group relative flex items-start gap-3">
      <Avatar className="h-8 w-8 shrink-0">
        <AvatarFallback className="bg-muted">
          <Bot className="h-4 w-4" />
        </AvatarFallback>
      </Avatar>
      <div className="flex max-w-[80%] flex-col gap-2 rounded-lg bg-muted/50 px-3 py-2">
        <div className="flex items-center gap-2">
          <div className="flex gap-1">
            <div className="h-2 w-2 animate-bounce rounded-full bg-muted-foreground/60 [animation-delay:-0.3s]" />
            <div className="h-2 w-2 animate-bounce rounded-full bg-muted-foreground/60 [animation-delay:-0.15s]" />
            <div className="h-2 w-2 animate-bounce rounded-full bg-muted-foreground/60" />
          </div>
          <span className="text-xs text-muted-foreground">AI is thinking...</span>
        </div>
        
        {/* Live Thinking Process */}
        <Accordion type="multiple" className="w-full space-y-1" defaultValue={["thinking-process"]}>
          <AccordionItem value="thinking-process" className="border-none">
            <AccordionTrigger className="flex w-full items-center justify-start gap-2 rounded-md p-2 text-xs text-muted-foreground hover:bg-card-foreground/5 hover:no-underline -mx-2">
              <Brain className="h-4 w-4" />
              <span>AI Thinking Process (Live)</span>
            </AccordionTrigger>
            <AccordionContent className="mt-2 border-t border-card-foreground/10 pt-3 text-muted-foreground">
              <div className="space-y-3">
                
                {/* Current Phase */}
                <div className="space-y-2">
                  <div className="flex items-center gap-2 text-xs font-medium">
                    {getPhaseIcon()}
                    <span>Current Phase</span>
                  </div>
                  <div className="text-xs bg-card-foreground/5 p-2 rounded">
                    {getPhaseText()}
                  </div>
                </div>

                {/* Phase Progress */}
                <div className="space-y-1">
                  <div className="flex items-center gap-2 text-xs">
                    <div className={`w-2 h-2 rounded-full ${currentPhase >= 1 ? 'bg-blue-400' : 'bg-muted'}`}></div>
                    <span className={currentPhase === 1 ? 'text-blue-400' : 'text-muted-foreground'}>
                      Context Analysis {currentPhase > 1 ? 'βœ“' : '...'}
                    </span>
                  </div>
                  <div className="flex items-center gap-2 text-xs">
                    <div className={`w-2 h-2 rounded-full ${currentPhase >= 2 ? 'bg-purple-400' : 'bg-muted'}`}></div>
                    <span className={currentPhase === 2 ? 'text-purple-400' : currentPhase > 2 ? 'text-muted-foreground' : 'text-muted-foreground'}>
                      Strategy Reflection {currentPhase > 2 ? 'βœ“' : currentPhase === 2 ? '...' : ''}
                    </span>
                  </div>
                  <div className="flex items-center gap-2 text-xs">
                    <div className={`w-2 h-2 rounded-full ${currentPhase >= 3 ? 'bg-green-400' : 'bg-muted'}`}></div>
                    <span className={currentPhase === 3 ? 'text-green-400' : 'text-muted-foreground'}>
                      Response Generation {currentPhase === 3 ? '...' : ''}
                    </span>
                  </div>
                </div>

              </div>
            </AccordionContent>
          </AccordionItem>
        </Accordion>
      </div>
    </div>
  );
}

export function ImageLoadingMessage({ progress = 0, elapsedSeconds, status }: { progress?: number, elapsedSeconds?: number, status?: string }) {
  const [elapsed, setElapsed] = React.useState(elapsedSeconds || 0);

  React.useEffect(() => {
    if (typeof elapsedSeconds === 'number') {
      setElapsed(elapsedSeconds);
      return;
    }
    const start = Date.now();
    const i = setInterval(() => {
      const t = (Date.now() - start) / 1000;
      setElapsed(t);
    }, 120);
    return () => clearInterval(i);
  }, [elapsedSeconds]);

  return (
    <div className="group relative flex items-start gap-3">
      <Avatar className="h-8 w-8 shrink-0">
        <AvatarFallback className="bg-muted">
          <Bot className="h-4 w-4" />
        </AvatarFallback>
      </Avatar>
      <div className="flex max-w-[80%] flex-col gap-2 rounded-lg bg-muted/50 px-3 py-2">
        <div className="flex items-center gap-2">
          <div className="flex gap-1">
            <div className="h-2 w-2 animate-bounce rounded-full bg-emerald-500/70 [animation-delay:-0.3s]" />
            <div className="h-2 w-2 animate-bounce rounded-full bg-emerald-500/70 [animation-delay:-0.15s]" />
            <div className="h-2 w-2 animate-bounce rounded-full bg-emerald-500/70" />
          </div>
          <span className="text-xs text-muted-foreground">{status ? `${status.charAt(0).toUpperCase()}${status.slice(1)}` : 'Generating image...'}</span>
        </div>

        <Accordion type="single" className="w-full space-y-1" defaultValue={["image-process"] as any}>
          <AccordionItem value="image-process" className="border-none">
            <AccordionTrigger className="flex w-full items-center justify-start gap-2 rounded-md p-2 text-xs text-muted-foreground hover:bg-card-foreground/5 hover:no-underline -mx-2">
              <Brain className="h-4 w-4 text-emerald-400" />
              <span>Image Generation (Live)</span>
            </AccordionTrigger>
            <AccordionContent className="mt-2 border-t border-card-foreground/10 pt-3 text-muted-foreground">
              <div className="space-y-2">
                <div className="flex items-center justify-between text-xs">
                  <span className="font-medium">Rendering</span>
                  <span className="text-muted-foreground">{(elapsed ?? 0).toFixed(1)}s</span>
                </div>
                <div className="h-2 w-full rounded-full bg-emerald-500/10 overflow-hidden">
                  <div
                    className="h-full rounded-full bg-gradient-to-r from-emerald-400 via-emerald-500 to-emerald-600 animate-pulse"
                    style={{ width: `${Math.max(2, Math.min(100, progress))}%` }}
                  />
                </div>
                <div className="text-[11px] text-muted-foreground">Optimized low-VRAM pipeline active</div>
              </div>
            </AccordionContent>
          </AccordionItem>
        </Accordion>
      </div>
    </div>
  );
}