File size: 22,808 Bytes
3c62f53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
function initChatInterface() {
  // Create  chat container
  const chatContainer = document.createElement("div");
  chatContainer.id = "chat-container";
  chatContainer.style.cssText = `

    position: fixed;

    bottom: 24px;

    right: 24px;

    width: 380px;

    max-width: 90vw;

    height: 600px;

    max-height: 80vh;

    background-color: #ffffff;

    border-radius: 20px;

    box-shadow: 0 15px 40px rgba(0,0,0,0.12);

    border: 1px solid rgba(0,0,0,0.05);

    z-index: 10000;

    display: flex;

    flex-direction: column;

    font-family: 'Segoe UI', Roboto, sans-serif;

    overflow: hidden;

    transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);

    transform: translateY(20px);

    opacity: 0;

  `;

  // Chat header
  chatContainer.innerHTML = `

    <div id="chat-header" style="

      padding: 16px 20px;

      background: #1d355d;

      cursor: pointer;

      display: flex;

      justify-content: space-between;

      align-items: center;

      color: white;

    ">

      <div style="display: flex; align-items: center; gap: 12px;">

        <div style="

          width: 40px;

          height: 40px;

          background-color: rgba(255,255,255,0.1);

          border-radius: 50%;

          display: flex;

          align-items: center;

          justify-content: center;

          font-size: 20px;

        "><i class="ri-customer-service-2-line"></i></div>

        <div>

          <h3 style="margin: 0; font-size: 16px; font-weight: 600; letter-spacing: 0.5px;">Furniture Expert</h3>

          <p style="margin: 0; font-size: 12px; color: #D4A017;">Online & ready to help</p>

        </div>

      </div>

      <div style="display: flex; gap: 8px;">

        <button id="minimize-chat" style="

          background: transparent;

          border: none;

          width: 30px;

          height: 30px;

          border-radius: 50%;

          color: white;

          font-size: 18px;

          cursor: pointer;

          transition: background 0.2s;

        "><i class="ri-subtract-line"></i></button>

        <button id="close-chat" style="

          background: transparent;

          border: none;

          width: 30px;

          height: 30px;

          border-radius: 50%;

          color: white;

          font-size: 18px;

          cursor: pointer;

          transition: background 0.2s;

        "><i class="ri-close-line"></i></button>

      </div>

    </div>

    

    <div id="chat-box" style="

      flex-grow: 1;

      overflow-y: auto;

      padding: 20px;

      background: #fdfdfd;

      display: flex;

      flex-direction: column;

      gap: 16px;

    "></div>

    

    <div id="suggestion-box" style="

      padding: 12px 16px;

      background: #ffffff;

      border-top: 1px solid #f0f0f0;

      display: flex;

      gap: 8px;

      overflow-x: auto;

    "></div>

    

    <div style="

      padding: 16px;

      background: #ffffff;

      border-top: 1px solid #f0f0f0;

      display: flex;

      gap: 12px;

      align-items: center;

    ">

      <div style="position: relative; flex-grow: 1;">

        <input type="text" id="user-input" placeholder="Type your message..." style="

          width: 100%;

          padding: 14px 20px;

          padding-right: 45px;

          border: 1px solid #e2e8f0;

          background: #f8fafc;

          border-radius: 30px;

          outline: none;

          font-size: 14px;

          transition: border-color 0.3s;

        ">

        <label for="image-upload" style="

          position: absolute;

          right: 15px;

          top: 50%;

          transform: translateY(-50%);

          cursor: pointer;

          color: #94a3b8;

          font-size: 18px;

          transition: color 0.3s;

        ">

          <i class="ri-camera-line"></i>

          <input type="file" id="image-upload" accept="image/*" style="display: none">

        </label>

      </div>

      <button id="send-button" style="

        padding: 0;

        width: 48px;

        height: 48px;

        background: #1d355d;

        color: white;

        border: none;

        border-radius: 50%;

        cursor: pointer;

        display: flex;

        align-items: center;

        justify-content: center;

        font-size: 20px;

        transition: transform 0.2s, background 0.2s;

        box-shadow: 0 4px 10px rgba(29, 53, 93, 0.2);

      "><i class="ri-send-plane-2-fill"></i></button>

    </div>

  `;

  document.body.appendChild(chatContainer);

  // Floating chat button
  const chatButton = document.createElement("button");
  chatButton.id = "chat-button";
  chatButton.innerHTML = `

    <i class="ri-customer-service-2-fill"></i>

  `;

  chatButton.style.cssText = `

    position: fixed;

    bottom: 24px;

    right: 24px;

    width: 65px;

    height: 65px;

    background: #1d355d;

    color: white;

    border: none;

    border-radius: 50%;

    cursor: pointer;

    box-shadow: 0 8px 25px rgba(29, 53, 93, 0.3);

    z-index: 10000;

    display: flex;

    align-items: center;

    justify-content: center;

    font-size: 28px;

    transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);

    animation: float 4s ease-in-out infinite;

    display: none;

  `;

  const style = document.createElement("style");
  style.textContent = `

    @keyframes float {

      0% { transform: translateY(0px); }

      50% { transform: translateY(-8px); }

      100% { transform: translateY(0px); }

    }

    

    @keyframes popIn {

      0% { transform: translateY(30px); opacity: 0; }

      100% { transform: translateY(0); opacity: 1; }

    }

    

    #minimize-chat:hover, #close-chat:hover {

      background: rgba(255,255,255,0.1) !important;

    }

    

    #send-button:hover {

      background: #D4A017 !important;

      transform: scale(1.05);

    }

    

    #chat-button:hover {

      transform: scale(1.1);

      background: #D4A017 !important;

    }

    

    #user-input:focus {

      border-color: #1d355d !important;

      background: #ffffff !important;

    }

    

    label[for="image-upload"]:hover {

      color: #1d355d !important;

    }

  `;
  document.head.appendChild(style);

  document.body.appendChild(chatButton);

  // Show pop-up animation when page loads
  setTimeout(() => {
    chatContainer.style.animation = "popIn 0.4s ease-out forwards";

    // Add welcome message
    displayAssistantResponse(`

      <div style="margin-bottom: 8px;">

        Assalam-o-Alaikum! πŸ‘‹ I'm your personal furniture assistant.

      </div>

      <div style="margin-bottom: 8px;">

        I can help you:

        <ul style="margin: 8px 0; padding-left: 20px;">

          <li>Find products</li>

          <li>Compare prices</li>

          <li>Suggest alternatives</li>

          <li>Place orders</li>

        </ul>

      </div>

      <div>

        How can I help you today?

      </div>

    `);
    showSuggestions([
      "Show sofas under 50,000",
      "Best plants for home",
      "New arrivals",
      "Help me order",
    ]);
  }, 1000);

  // Toggle chat visibility
  const chatHeader = document.getElementById("chat-header");
  const minimizeBtn = document.getElementById("minimize-chat");
  const closeBtn = document.getElementById("close-chat");

  chatHeader.addEventListener("click", () => {
    if (chatContainer.style.display === "none") {
      chatContainer.style.display = "flex";
      minimizeBtn.textContent = "βˆ’";
    }
  });

  minimizeBtn.addEventListener("click", (e) => {
    e.stopPropagation();
    chatContainer.style.height =
      chatContainer.style.height === "60px" ? "500px" : "60px";
    minimizeBtn.textContent = chatContainer.style.height === "60px" ? "+" : "βˆ’";
  });

  closeBtn.addEventListener("click", (e) => {
    e.stopPropagation();
    chatContainer.style.display = "none";
    chatButton.style.display = "flex";
  });

  chatButton.addEventListener("click", () => {
    chatContainer.style.display = "flex";
    chatButton.style.display = "none";
    minimizeBtn.textContent = "βˆ’";
    chatContainer.style.height = "500px";
  });

  // Add typing indicator function
  window.showTypingIndicator = function () {
    const chatBox = document.getElementById("chat-box");
    const typingDiv = document.createElement("div");
    typingDiv.id = "typing-indicator";
    typingDiv.style.cssText = `

      text-align: left;

      margin: 10px 0;

      display: flex;

      align-items: center;

      gap: 8px;

    `;
    typingDiv.innerHTML = `

      <div style="

        display: inline-block;

        padding: 8px 12px;

        background: #f5f5f5;

        border-radius: 18px;

      ">

        <div class="typing-dots">

          <div></div>

          <div></div>

          <div></div>

        </div>

      </div>

    `;
    chatBox.appendChild(typingDiv);
    chatBox.scrollTop = chatBox.scrollHeight;

    // Add typing dots animation
    const typingStyle = document.createElement("style");
    typingStyle.textContent = `

      .typing-dots {

        display: flex;

        gap: 4px;

      }

      .typing-dots div {

        width: 8px;

        height: 8px;

        background: #666;

        border-radius: 50%;

        animation: typingAnimation 1.4s infinite ease-in-out;

      }

      .typing-dots div:nth-child(1) { animation-delay: 0s; }

      .typing-dots div:nth-child(2) { animation-delay: 0.2s; }

      .typing-dots div:nth-child(3) { animation-delay: 0.4s; }

      @keyframes typingAnimation {

        0%, 60%, 100% { transform: translateY(0); }

        30% { transform: translateY(-5px); }

      }

    `;
    document.head.appendChild(typingStyle);
  };

  // Remove typing indicator function
  window.hideTypingIndicator = function () {
    const typingIndicator = document.getElementById("typing-indicator");
    if (typingIndicator) {
      typingIndicator.remove();
    }
  };
}

// ======= Enhanced Chat Message Handling =======
async function sendChatMessage() {

    try {
    const response = await fetch("/chat", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ message: message }),
    });

    const data = await response.json();
    hideTypingIndicator();

    // Handle WhatsApp-specific responses
    if (data.reply && data.reply.includes("whatsapp")) {
      displayAssistantResponse(
        "I've sent the order details to your WhatsApp. Please check your messages!"
      );
    } else {
      displayAssistantResponse(data.reply);
    }

    // Show suggestions if available
    if (data.suggestions) {
      showSuggestions(data.suggestions);
    }
  } catch (error) {
    // ... error handling ...
  }
  const input = document.getElementById("user-input");
  const message = input.value.trim();
  const imageUpload = document.getElementById("image-upload");
  const chatBox = document.getElementById("chat-box");

  if (imageUpload.files[0]) {
    // Handle image upload
    const file = imageUpload.files[0];
    const reader = new FileReader();

    reader.onload = async function (e) {
      // Display user's image in chat
      chatBox.innerHTML += `

        <div style="

          margin: 8px 0;

          align-self: flex-end;

          max-width: 85%;

        ">

          <div style="

            padding: 8px;

            background: #1d355d;

            border-radius: 18px 18px 4px 18px;

            box-shadow: 0 4px 10px rgba(29, 53, 93, 0.1);

            text-align: left;

          ">

            <img src="${e.target.result}" style="

              max-width: 250px;

              max-height: 250px;

              border-radius: 10px;

              display: block;

            ">

            <div style="

              font-size: 11px;

              color: rgba(255,255,255,0.7);

              text-align: right;

              padding-top: 6px;

            ">You</div>

          </div>

        </div>

      `;

      showTypingIndicator();

      try {
        const response = await fetch("/chat", {
          method: "POST",
          headers: { "Content-Type": "application/json" },
          body: JSON.stringify({ image: e.target.result }),
        });

        const data = await response.json();
        hideTypingIndicator();
        displayAssistantResponse(data.reply);

        // Show suggestions if available
        if (data.suggestions) {
          showSuggestions(data.suggestions);
        }
      } catch (error) {
        hideTypingIndicator();
        displayAssistantResponse(
          "❌ Couldn't process the image. Please try again or describe what you're looking for."
        );
      }

      imageUpload.value = "";
      chatBox.scrollTop = chatBox.scrollHeight;
    };

    reader.readAsDataURL(file);
  } else if (message) {
    // Handle text message
    chatBox.innerHTML += `

      <div style="

        margin: 8px 0;

        align-self: flex-end;

        max-width: 85%;

      ">

        <div style="

          padding: 12px 16px;

          background: #1d355d;

          color: white;

          border-radius: 18px 18px 4px 18px;

          word-wrap: break-word;

          box-shadow: 0 4px 10px rgba(29, 53, 93, 0.1);

          font-size: 14px;

          text-align: left;

        ">

          ${message}

          <div style="

            font-size: 11px;

            color: rgba(255,255,255,0.7);

            text-align: right;

            padding-top: 6px;

          ">You</div>

        </div>

      </div>

    `;
    input.value = "";

    showTypingIndicator();

    try {
      const response = await fetch("/chat", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ message: message }),
      });

      const data = await response.json();
      hideTypingIndicator();
      displayAssistantResponse(data.reply);

      // Show suggestions if available
      if (data.suggestions) {
        showSuggestions(data.suggestions);
      }
    } catch (error) {
      hideTypingIndicator();
      displayAssistantResponse(
        "❌ Sorry, I'm having trouble responding. Please try again in a moment."
      );
    }
  }
}

// Enhanced assistant response display with proper link handling
function displayAssistantResponse(response) {
  const chatBox = document.getElementById("chat-box");
  let formattedResponse = "";

  // Handle object responses (from tools)
  if (typeof response === "object" && response !== null) {
    // Add the main message text
    if (response.message) {
      formattedResponse += `<div>${response.message}</div>`;
    }

    // Handle link objects
    if (response.link && typeof response.link === "object") {
      const linkText = response.link.text || "View Details";
      const linkUrl = response.link.url || "#";

      formattedResponse += `

        <div style="margin-top: 8px;">

          <a href="${linkUrl}" target="_blank" 

            style="display: inline-block; 

                  padding: 8px 16px; 

                  background-color: #f4a742; 

                  color: white; 

                  text-decoration: none; 

                  border-radius: 18px; 

                  font-weight: 500; 

                  transition: background-color 0.2s;">

            ${linkText}

          </a>

        </div>

      `;
    }

    // Handle direct string links in response
    if (response.url) {
      formattedResponse += `

        <div style="margin-top: 8px;">

          <a href="${response.url}" target="_blank" 

            style="display: inline-block; 

                  padding: 8px 16px; 

                  background-color: #f4a742; 

                  color: white; 

                  text-decoration: none; 

                  border-radius: 18px; 

                  font-weight: 500;">

            ${response.text || "Click Here"}

          </a>

        </div>

      `;
    }
  }
  // Handle string responses
  else if (typeof response === "string") {
    // Convert URLs to clickable links
    formattedResponse = response.replace(
      /(https?:\/\/[^\s]+)/g,
      '<a href="$1" target="_blank" style="color:#2196F3;text-decoration:underline">$1</a>'
    );
  }
  // Handle other response types
  else {
    formattedResponse = String(response);
  }

  // Add timestamp
  const now = new Date();
  const timeString = now.toLocaleTimeString([], {
    hour: "2-digit",
    minute: "2-digit",
  });

  // Append the final formatted message to the chat box
  chatBox.innerHTML += `

    <div style="

      text-align: left;

      margin: 8px 0;

      align-self: flex-start;

    ">

      <div style="

        display: inline-block;

        padding: 14px 18px;

        background: white;

        color: #334155;

        border-radius: 18px 18px 18px 4px;

        max-width: 85%;

        word-wrap: break-word;

        box-shadow: 0 4px 12px rgba(0,0,0,0.06);

        border: 1px solid rgba(0,0,0,0.02);

        font-size: 14px;

        line-height: 1.5;

      ">

        ${formattedResponse}

        <div style="

          font-size: 11px;

          color: #94a3b8;

          text-align: left;

          padding-top: 8px;

          display: flex;

          align-items: center;

          gap: 6px;

        ">

          <i class="ri-customer-service-2-fill"></i>

          <span>Assistant</span>

          <span>β€’</span>

          <span>${timeString}</span>

        </div>

      </div>

    </div>

  `;

  chatBox.scrollTop = chatBox.scrollHeight;
}

// Show quick reply suggestions
function showSuggestions(suggestions) {
  const suggestionBox = document.getElementById("suggestion-box");
  suggestionBox.innerHTML = "";

  suggestions.forEach((suggestion) => {
    const button = document.createElement("button");
    button.textContent = suggestion;
    button.style.cssText = `

      padding: 8px 14px;

      background: white;

      color: #1d355d;

      border: 1px solid #e2e8f0;

      border-radius: 20px;

      font-size: 13px;

      font-weight: 500;

      cursor: pointer;

      white-space: nowrap;

      transition: all 0.2s;

      box-shadow: 0 2px 4px rgba(0,0,0,0.02);

      flex-shrink: 0;

    `;
    button.addEventListener("mouseenter", () => {
      button.style.background = "#1d355d";
      button.style.color = "white";
      button.style.borderColor = "#1d355d";
    });
    button.addEventListener("mouseleave", () => {
      button.style.background = "white";
      button.style.color = "#1d355d";
      button.style.borderColor = "#e2e8f0";
    });
    button.addEventListener("click", () => {
      document.getElementById("user-input").value = suggestion;
      sendChatMessage();
    });
    suggestionBox.appendChild(button);
  });
}

// ======= Event Listeners =======
document.addEventListener("DOMContentLoaded", () => {
  initChatInterface();

  // Handle Enter key in input
  const input = document.getElementById("user-input");
  input?.addEventListener("keypress", (e) => {
    if (e.key === "Enter") {
      sendChatMessage();
    }
  });

  // Handle send button click
  const sendButton = document.getElementById("send-button");
  sendButton?.addEventListener("click", sendChatMessage);

  // Handle image upload
  const imageUpload = document.getElementById("image-upload");
  imageUpload?.addEventListener("change", () => {
    if (imageUpload.files[0]) {
      sendChatMessage();
    }
  });
});

// ======= Navbar Section =======
const menuIcon = document.getElementById("menuIcon");
const navLinks = document.querySelector(".nav-links");

menuIcon.addEventListener("click", () => {
  navLinks.classList.toggle("active");
});

// ======= Product Cards Section =======
document.addEventListener("DOMContentLoaded", () => {
  const items = {
    plant: [
      "/static/images/plant3.png",
      "/static/images/plant4.webp",
      "/static/images/plant5.png",
      "/static/images/plant6.png",
    ],
    table: [
      "/static/images/table2.png",
      "/static/images/table3.webp",
      "/static/images/table4.webp",
      "/static/images/table5.png",
    ],
    lamp: [
      "/static/images/lamp1.webp",
      "/static/images/lamp2.webp",
      "/static/images/lamp3.webp",
      "/static/images/lamp4.png",
      "/static/images/lamp5.webp",
      "/static/images/lamp6.webp",
    ],
    sofa: [
      "/static/images/sofa3.webp",
      "/static/images/sofa4.webp",
      "/static/images/sofa5.png",
      "/static/images/sofa6.png",
    ],
  };

  document.querySelectorAll(".plus-btn").forEach((button) => {
    button.addEventListener("click", () => {
      const item = button.dataset.item;
      const card = document.getElementById(`${item}-card`);
      const mainImage = button.previousElementSibling;

      // Hide if already active
      if (card.classList.contains("active")) {
        card.innerHTML = "";
        card.classList.remove("active");
        return;
      }

      // Apply card styling
      Object.assign(card.style, {
        display: "grid",
        gridTemplateColumns: "repeat(auto-fit, minmax(80px, 1fr))",
        gap: "10px",
        padding: "10px",
        border: "2px solid #ccc",
        borderRadius: "10px",
        marginTop: "12px",
        maxWidth: "100%",
        backgroundColor: "#f5f5f5",
      });

      card.innerHTML = "";

      items[item].forEach((imgSrc) => {
        const img = document.createElement("img");
        img.src = imgSrc;
        img.alt = `${item} option`;

        // Apply image styles
        Object.assign(img.style, {
          width: "100%",
          maxWidth: "80px",
          height: "80px",
          objectFit: "cover",
          borderRadius: "8px",
          cursor: "pointer",
          transition: "transform 0.2s, border 0.2s",
          border: "2px solid transparent",
        });

        img.addEventListener("mouseenter", () => {
          img.style.transform = "scale(1.1)";
          img.style.border = "2px solid #48c6ef";
        });

        img.addEventListener("mouseleave", () => {
          img.style.transform = "scale(1)";
          img.style.border = "2px solid transparent";
        });

        img.addEventListener("click", () => {
          mainImage.src = imgSrc;
        });

        card.appendChild(img);
      });

      card.classList.add("active");
    });
  });
});