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
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
import React, { useState, useRef, useEffect } from 'react';
import ReactMarkdown from 'react-markdown';
import { Reply, Copy, Check, Maximize2, Info, FileText, Hash, Target } from 'lucide-react';
import { advisors, getAdvisorColors } from '../data/advisors';
import { useTheme } from '../contexts/ThemeContext';

const MessageBubble = ({ 
  message, 
  onReply, 
  onCopy, 
  onExpand,
  showReplyButton = false 
}) => {
  const { isDark } = useTheme();
  const [showTooltip, setShowTooltip] = useState(null);
  const [copiedStates, setCopiedStates] = useState({});
  const [showInfoOverlay, setShowInfoOverlay] = useState(false);
  const overlayRef = useRef(null);

  const handleCopy = async (messageId, content) => {
    try {
      await navigator.clipboard.writeText(content);
      setCopiedStates(prev => ({ ...prev, [messageId]: true }));
      if (onCopy) onCopy(messageId, content);
      
      setTimeout(() => {
        setCopiedStates(prev => ({ ...prev, [messageId]: false }));
      }, 2000);
    } catch (err) {
      console.error('Failed to copy text: ', err);
    }
  };

  const handleExpand = (messageId, persona_id) => {
    if (onExpand) onExpand(messageId, persona_id);
  };

  const handleInfoToggle = () => {
    setShowInfoOverlay(!showInfoOverlay);
  };

  const showTooltipWithDelay = (tooltipType) => {
    setTimeout(() => setShowTooltip(tooltipType), 500);
  };

  const hideTooltip = () => {
    setShowTooltip(null);
  };

  // Close overlay when clicking outside
  useEffect(() => {
    const handleClickOutside = (event) => {
      if (overlayRef.current && !overlayRef.current.contains(event.target)) {
        setShowInfoOverlay(false);
      }
    };

    if (showInfoOverlay) {
      document.addEventListener('mousedown', handleClickOutside);
      return () => {
        document.removeEventListener('mousedown', handleClickOutside);
      };
    }
  }, [showInfoOverlay]);

  // Preprocess markdown content to fix common formatting issues
  const preprocessMarkdown = (content) => {
    if (!content) return '';
    
    // Ensure proper line breaks before numbered lists
    let processed = content.replace(/(\d+\.\s\*\*[^*]+\*\*)/g, '\n\n$1');
    
    // Ensure proper line breaks after list items
    processed = processed.replace(/(\d+\.\s[^\n]+)(?=\s+\d+\.)/g, '$1\n');
    
    // Fix spacing around bold headers
    processed = processed.replace(/(\*\*[^*]+\*\*)/g, '\n\n$1\n\n');
    
    // Clean up multiple consecutive line breaks
    processed = processed.replace(/\n{3,}/g, '\n\n');
    
    // Ensure proper paragraph breaks
    processed = processed.replace(/([.!?])\s+([A-Z])/g, '$1\n\n$2');
    
    return processed.trim();
  };

  // ENHANCED MARKDOWN COMPONENTS WITH BETTER STYLING
  const markdownComponents = {
    // Bold text styling - for headers and key terms
    strong: ({ children }) => (
      <strong style={{ 
        fontWeight: '700',
        color: isDark ? '#ffffff' : '#1f2937',
        display: 'block',
        marginBottom: '0.5rem',
        marginTop: '1rem'
      }}>
        {children}
      </strong>
    ),
    
    // Italic text styling
    em: ({ children }) => (
      <em style={{ 
        fontStyle: 'italic',
        color: isDark ? '#93c5fd' : '#3b82f6',
        fontWeight: '500'
      }}>
        {children}
      </em>
    ),
    
    // Paragraph styling with proper spacing
    p: ({ children }) => (
      <p style={{ 
        marginBottom: '1rem',
        lineHeight: '1.7',
        color: isDark ? '#e5e7eb' : '#374151',
        fontSize: '14px'
      }}>
        {children}
      </p>
    ),
    
    // Unordered list styling
    ul: ({ children }) => (
      <ul style={{ 
        listStyleType: 'disc',
        paddingLeft: '1.5rem',
        marginBottom: '1rem',
        marginTop: '0.5rem',
        color: isDark ? '#e5e7eb' : '#374151'
      }}>
        {children}
      </ul>
    ),
    
    // Ordered list styling with better spacing
    ol: ({ children }) => (
      <ol style={{ 
        listStyleType: 'decimal',
        paddingLeft: '1.5rem',
        marginBottom: '1rem',
        marginTop: '0.5rem',
        color: isDark ? '#e5e7eb' : '#374151',
        counterReset: 'list-counter'
      }}>
        {children}
      </ol>
    ),
    
    // List item styling with proper spacing
    li: ({ children }) => (
      <li style={{ 
        marginBottom: '0.75rem',
        lineHeight: '1.6',
        paddingLeft: '0.25rem'
      }}>
        {children}
      </li>
    ),

    // Headers (in case they use them)
    h1: ({ children }) => (
      <h1 style={{
        fontSize: '1.5rem',
        fontWeight: '700',
        color: isDark ? '#ffffff' : '#1f2937',
        marginBottom: '1rem',
        marginTop: '1.5rem',
        borderBottom: `2px solid ${isDark ? '#374151' : '#e5e7eb'}`,
        paddingBottom: '0.5rem'
      }}>
        {children}
      </h1>
    ),

    h2: ({ children }) => (
      <h2 style={{
        fontSize: '1.25rem',
        fontWeight: '600',
        color: isDark ? '#ffffff' : '#1f2937',
        marginBottom: '0.75rem',
        marginTop: '1.25rem'
      }}>
        {children}
      </h2>
    ),

    h3: ({ children }) => (
      <h3 style={{
        fontSize: '1.125rem',
        fontWeight: '600',
        color: isDark ? '#ffffff' : '#1f2937',
        marginBottom: '0.5rem',
        marginTop: '1rem'
      }}>
        {children}
      </h3>
    ),

    // Code styling
    code: ({ children }) => (
      <code style={{ 
        backgroundColor: isDark ? '#374151' : '#f3f4f6',
        padding: '0.125rem 0.375rem',
        borderRadius: '0.25rem',
        fontSize: '0.875rem',
        fontFamily: 'ui-monospace, SFMono-Regular, Consolas, monospace',
        color: isDark ? '#fbbf24' : '#d97706'
      }}>
        {children}
      </code>
    ),

    // Block quote styling
    blockquote: ({ children }) => (
      <blockquote style={{
        borderLeft: '4px solid ' + (isDark ? '#374151' : '#e5e7eb'),
        paddingLeft: '1rem',
        marginLeft: '0',
        marginBottom: '1rem',
        fontStyle: 'italic',
        color: isDark ? '#9ca3af' : '#6b7280'
      }}>
        {children}
      </blockquote>
    )
  };

  // RAG Metadata Component
  const RagInfoOverlay = ({ ragMetadata, colors }) => {
    const hasDocuments = ragMetadata?.usedDocuments || false;
    const chunksUsed = ragMetadata?.chunksUsed || 0;
    const documentChunks = ragMetadata?.documentChunks || [];

    return (
      <div 
        ref={overlayRef}
        className="rag-info-overlay"
        style={{ 
          borderColor: colors.color + '40',
          backgroundColor: isDark ? '#1f2937' : '#ffffff'
        }}
      >
        <div className="rag-overlay-header" style={{ color: colors.color }}>
          <Info size={14} />
          <span>RAG Information</span>
        </div>
        
        <div className="rag-overlay-content">
          <div className="rag-stat-row">
            <div className="rag-stat-label">Used Documents:</div>
            <div className={`rag-stat-value ${hasDocuments ? 'positive' : 'negative'}`}>
              {hasDocuments ? 'Yes' : 'No'}
            </div>
          </div>

          <div className="rag-stat-row">
            <div className="rag-stat-label">Document Chunks:</div>
            <div className="rag-stat-value">{chunksUsed}</div>
          </div>

          {hasDocuments && documentChunks.length > 0 && (
            <div className="rag-documents-section">
              <div className="rag-section-title">
                <FileText size={12} />
                Referenced Sources
              </div>
              
              {documentChunks.map((chunk, index) => (
                <div key={index} className="rag-document-item">
                  <div className="rag-document-header">
                    <span className="rag-filename">
                      {chunk.metadata?.filename || 'Unknown file'}
                    </span>
                    <span className="rag-relevance">
                      <Target size={10} />
                      {Math.round((chunk.relevance_score || 0) * 100)}%
                    </span>
                  </div>
                  
                  {chunk.text && (
                    <div className="rag-chunk-preview">
                      {chunk.text.substring(0, 120)}
                      {chunk.text.length > 120 && '...'}
                    </div>
                  )}
                </div>
              ))}
            </div>
          )}

          {!hasDocuments && (
            <div className="rag-no-documents">
              <Hash size={12} />
              <span>This response was generated without referencing uploaded documents.</span>
            </div>
          )}
        </div>
      </div>
    );
  };

  if (message.type === 'user') {
    return (
      <div className="user-message-container">
        <div className="user-message">
          {message.replyTo && (
            <div className="reply-indicator">
              <Reply size={14} />
              <span>to {message.replyTo.advisorName}</span>
            </div>
          )}
          <p>{message.content}</p>
        </div>
      </div>
    );
  }

  if (message.type === 'advisor') {
    const advisor = advisors[message.persona_id];
    const Icon = advisor.icon;
    const colors = getAdvisorColors(message.persona_id, isDark);
    const isCopied = copiedStates[message.id];

    return (
      <div className="advisor-message-container">
        <div 
          className="advisor-avatar" 
          style={{ backgroundColor: colors.bgColor }}
        >
          <Icon style={{ color: colors.color }} />
        </div>
        <div 
          className="advisor-message-bubble"
          style={{ 
            backgroundColor: colors.bgColor,
            borderColor: colors.color + '40',
            position: 'relative'
          }}
        >
          <div className="advisor-message-header">
            <h4 
              className="advisor-message-name" 
              style={{ color: colors.color }}
            >
              {advisor.name}
              {message.isReply && <span className="reply-badge">↳ Reply</span>}
              {message.isExpansion && <span className="expansion-badge">⤴ Expanded</span>}
            </h4>
            <span 
              className="message-time"
              style={{ 
                color: colors.color,
                opacity: 0.7 
              }}
            >
              {message.timestamp.toLocaleTimeString([], {
                hour: '2-digit', 
                minute: '2-digit'
              })}
            </span>
          </div>
          
          {/* Enhanced markdown rendering with preprocessing */}
          <div 
            className="advisor-message-text"
            style={{ 
              color: colors.textColor
            }}
          >
            <ReactMarkdown 
              components={markdownComponents}
              // Add these props for better parsing
              remarkPlugins={[]}
              rehypePlugins={[]}
            >
              {preprocessMarkdown(message.content)}
            </ReactMarkdown>
          </div>
          
          {showReplyButton && (
            <div className="message-actions">
              <div className="action-buttons">
                <div className="tooltip-container">
                  <button 
                    className="action-button"
                    onClick={() => onReply && onReply(message)}
                    onMouseEnter={() => showTooltipWithDelay('reply')}
                    onMouseLeave={hideTooltip}
                    style={{ 
                      color: colors.color,
                      borderColor: colors.color + '40'
                    }}
                  >
                    <Reply size={14} />
                  </button>
                  {showTooltip === 'reply' && (
                    <div className="tooltip">Reply to this message</div>
                  )}
                </div>

                <div className="tooltip-container">
                  <button 
                    className="action-button"
                    onClick={() => handleCopy(message.id, message.content)}
                    onMouseEnter={() => showTooltipWithDelay('copy')}
                    onMouseLeave={hideTooltip}
                    style={{ 
                      color: isCopied ? '#10B981' : colors.color,
                      borderColor: isCopied ? '#10B98140' : colors.color + '40'
                    }}
                  >
                    {isCopied ? <Check size={14} /> : <Copy size={14} />}
                  </button>
                  {showTooltip === 'copy' && (
                    <div className="tooltip">
                      {isCopied ? 'Copied!' : 'Copy response'}
                    </div>
                  )}
                </div>

                <div className="tooltip-container">
                  <button 
                    className="action-button"
                    onClick={() => handleExpand(message.id, message.persona_id)}
                    onMouseEnter={() => showTooltipWithDelay('expand')}
                    onMouseLeave={hideTooltip}
                    style={{ 
                      color: colors.color,
                      borderColor: colors.color + '40'
                    }}
                  >
                    <Maximize2 size={14} />
                  </button>
                  {showTooltip === 'expand' && (
                    <div className="tooltip">Expand on this response</div>
                  )}
                </div>
              </div>
            </div>
          )}

          {showInfoOverlay && (
            <RagInfoOverlay 
              ragMetadata={message.ragMetadata} 
              colors={colors}
            />
          )}
        </div>
      </div>
    );
  }

  if (message.type === 'error') {
    return (
      <div className="error-message-container">
        <div className="error-message">
          <p>{message.content}</p>
        </div>
      </div>
    );
  }

  return null;
};

export default MessageBubble;