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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React, { useState, useEffect } from 'react';
import { motion } from 'framer-motion';
import { Card, CardContent, CardHeader, CardTitle, CardDescription, CardFooter } from "@/components/ui/card";
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Textarea } from '@/components/ui/textarea';
import { useToast } from "@/components/ui/use-toast";
import { supabase } from '@/lib/supabaseClient';
import { MessageSquare, Plus, Send, Clock, CheckCircle, AlertCircle, X, ChevronDown, ChevronUp, RefreshCw } from 'lucide-react';
import { Badge } from '@/components/ui/badge';
import { Separator } from '@/components/ui/separator';

const SupportTicketComponent = ({ session }) => {
  const { toast } = useToast();
  const [tickets, setTickets] = useState([]);
  const [loading, setLoading] = useState(true);
  const [creating, setCreating] = useState(false);
  const [submitting, setSubmitting] = useState(false);
  const [expandedTicket, setExpandedTicket] = useState(null);
  const [newMessage, setNewMessage] = useState('');
  const [newTicket, setNewTicket] = useState({
    subject: '',
    message: '',
    order_id: ''
  });

  // Mock ticket data - in a real app, this would come from Supabase
  const mockTickets = [
    {
      id: 'TKT-001',
      subject: 'Order Delivery Delay',
      status: 'Open',
      created_at: '2023-11-15T10:30:00',
      updated_at: '2023-11-15T14:45:00',
      messages: [
        {
          id: 1,
          sender: 'user',
          message: 'My order #ORD-001 was supposed to be delivered yesterday but I haven\'t received it yet. Can you please check the status?',
          timestamp: '2023-11-15T10:30:00'
        },
        {
          id: 2,
          sender: 'support',
          message: 'Thank you for reaching out. I apologize for the delay. I\'ve checked your order and there seems to be a slight delay with the courier. Your package is expected to be delivered by tomorrow. We\'ll keep you updated.',
          timestamp: '2023-11-15T14:45:00'
        }
      ]
    },
    {
      id: 'TKT-002',
      subject: 'Product Quality Issue',
      status: 'Closed',
      created_at: '2023-10-28T09:15:00',
      updated_at: '2023-10-30T11:20:00',
      messages: [
        {
          id: 1,
          sender: 'user',
          message: 'I received my Garam Masala but the packaging was damaged and some of the product had spilled.',
          timestamp: '2023-10-28T09:15:00'
        },
        {
          id: 2,
          sender: 'support',
          message: 'I\'m sorry to hear about the damaged packaging. We take product quality very seriously. Could you please send a photo of the damaged package?',
          timestamp: '2023-10-28T15:30:00'
        },
        {
          id: 3,
          sender: 'user',
          message: 'I\'ve sent the photos to your email address.',
          timestamp: '2023-10-29T10:45:00'
        },
        {
          id: 4,
          sender: 'support',
          message: 'Thank you for the photos. We\'ve processed a replacement order for you which will be shipped today. You should receive it within 3-5 business days. We\'ve also added a complimentary sample of our new Chat Masala as a token of apology for the inconvenience.',
          timestamp: '2023-10-29T14:20:00'
        },
        {
          id: 5,
          sender: 'user',
          message: 'Thank you for the quick resolution! Looking forward to trying the Chat Masala.',
          timestamp: '2023-10-30T09:10:00'
        },
        {
          id: 6,
          sender: 'support',
          message: 'You\'re welcome! We\'re glad we could resolve this to your satisfaction. Please let us know if you need anything else. Enjoy the Chat Masala!',
          timestamp: '2023-10-30T11:20:00'
        }
      ]
    }
  ];

  useEffect(() => {
    // In a real app, fetch tickets from Supabase
    // For now, we'll use mock data
    const fetchTickets = async () => {
      try {
        setLoading(true);
        // Simulate API call delay
        setTimeout(() => {
          setTickets(mockTickets);
          setLoading(false);
        }, 1000);

        // In a real implementation, you would fetch from Supabase:
        // const { data, error } = await supabase
        //   .from('support_tickets')
        //   .select('*')
        //   .eq('user_id', session.user.id)
        //   .order('created_at', { ascending: false });
        //
        // if (error) throw error;
        // setTickets(data || []);
      } catch (error) {
        console.error('Error fetching tickets:', error);
        toast({
          title: 'Error',
          description: 'Failed to load support tickets',
          variant: 'destructive',
        });
      } finally {
        setLoading(false);
      }
    };

    if (session) {
      fetchTickets();
    }
  }, [session, toast]);

  const handleCreateTicket = async (e) => {
    e.preventDefault();
    try {
      setSubmitting(true);

      // Validate form
      if (!newTicket.subject || !newTicket.message) {
        toast({
          title: 'Missing Information',
          description: 'Please fill in all required fields',
          variant: 'destructive',
        });
        setSubmitting(false);
        return;
      }

      // In a real app, create ticket in Supabase
      // For now, simulate API call and add to local state
      setTimeout(() => {
        const newTicketObj = {
          id: `TKT-${Math.floor(Math.random() * 1000).toString().padStart(3, '0')}`,
          subject: newTicket.subject,
          status: 'Open',
          created_at: new Date().toISOString(),
          updated_at: new Date().toISOString(),
          messages: [
            {
              id: 1,
              sender: 'user',
              message: newTicket.message,
              timestamp: new Date().toISOString()
            }
          ]
        };

        setTickets([newTicketObj, ...tickets]);
        setNewTicket({
          subject: '',
          message: '',
          order_id: ''
        });
        setCreating(false);
        setSubmitting(false);

        toast({
          title: 'Ticket Created',
          description: 'Your support ticket has been successfully created',
          className: 'toast-success-theme',
        });
      }, 1500);

      // In a real implementation:
      // const { data, error } = await supabase
      //   .from('support_tickets')
      //   .insert([{
      //     user_id: session.user.id,
      //     subject: newTicket.subject,
      //     status: 'Open',
      //     order_id: newTicket.order_id || null
      //   }])
      //   .select();
      //
      // if (error) throw error;
      //
      // // Add initial message
      // const { error: messageError } = await supabase
      //   .from('ticket_messages')
      //   .insert([{
      //     ticket_id: data[0].id,
      //     user_id: session.user.id,
      //     message: newTicket.message,
      //     is_from_support: false
      //   }]);
      //
      // if (messageError) throw messageError;
    } catch (error) {
      console.error('Error creating ticket:', error);
      toast({
        title: 'Error',
        description: 'Failed to create support ticket',
        variant: 'destructive',
      });
    } finally {
      setSubmitting(false);
    }
  };

  const handleSendMessage = async (ticketId) => {
    if (!newMessage.trim()) return;

    try {
      setSubmitting(true);

      // In a real app, add message to Supabase
      // For now, update local state
      setTimeout(() => {
        const updatedTickets = tickets.map(ticket => {
          if (ticket.id === ticketId) {
            const updatedMessages = [
              ...ticket.messages,
              {
                id: ticket.messages.length + 1,
                sender: 'user',
                message: newMessage,
                timestamp: new Date().toISOString()
              }
            ];

            return {
              ...ticket,
              messages: updatedMessages,
              updated_at: new Date().toISOString()
            };
          }
          return ticket;
        });

        setTickets(updatedTickets);
        setNewMessage('');
        setSubmitting(false);

        // Simulate support response after 3 seconds
        setTimeout(() => {
          const updatedTicketsWithResponse = tickets.map(ticket => {
            if (ticket.id === ticketId) {
              const updatedMessages = [
                ...ticket.messages,
                {
                  id: ticket.messages.length + 1,
                  sender: 'user',
                  message: newMessage,
                  timestamp: new Date().toISOString()
                },
                {
                  id: ticket.messages.length + 2,
                  sender: 'support',
                  message: 'Thank you for your message. Our support team will get back to you shortly.',
                  timestamp: new Date(Date.now() + 5000).toISOString()
                }
              ];

              return {
                ...ticket,
                messages: updatedMessages,
                updated_at: new Date(Date.now() + 5000).toISOString()
              };
            }
            return ticket;
          });

          setTickets(updatedTicketsWithResponse);
        }, 3000);
      }, 1000);

      // In a real implementation:
      // const { error } = await supabase
      //   .from('ticket_messages')
      //   .insert([{
      //     ticket_id: ticketId,
      //     user_id: session.user.id,
      //     message: newMessage,
      //     is_from_support: false
      //   }]);
      //
      // if (error) throw error;
      //
      // // Update ticket updated_at timestamp
      // await supabase
      //   .from('support_tickets')
      //   .update({ updated_at: new Date() })
      //   .eq('id', ticketId);
    } catch (error) {
      console.error('Error sending message:', error);
      toast({
        title: 'Error',
        description: 'Failed to send message',
        variant: 'destructive',
      });
    } finally {
      setSubmitting(false);
    }
  };

  const formatDate = (dateString) => {
    const options = { year: 'numeric', month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit' };
    return new Date(dateString).toLocaleDateString('en-US', options);
  };

  const getStatusColor = (status) => {
    switch (status.toLowerCase()) {
      case 'open':
        return 'bg-green-100 text-green-800';
      case 'closed':
        return 'bg-stone-100 text-stone-800';
      case 'pending':
        return 'bg-amber-100 text-amber-800';
      default:
        return 'bg-blue-100 text-blue-800';
    }
  };

  const toggleTicket = (ticketId) => {
    setExpandedTicket(expandedTicket === ticketId ? null : ticketId);
  };

  return (
    <div className="space-y-6">
      <div className="flex justify-between items-center">
        <h2 className="text-2xl font-bold text-amber-800 flex items-center">
          <MessageSquare className="mr-2 h-6 w-6" /> Support Tickets
        </h2>
        {!creating && (
          <Button 
            onClick={() => setCreating(true)} 
            className="bg-amber-600 hover:bg-amber-700 text-white"
          >
            <Plus className="mr-2 h-4 w-4" /> New Ticket
          </Button>
        )}
      </div>

      {creating && (
        <motion.div
          initial={{ opacity: 0, y: -20 }}
          animate={{ opacity: 1, y: 0 }}
          transition={{ duration: 0.3 }}
        >
          <Card className="border-amber-200 bg-amber-50/50">
            <CardHeader>
              <CardTitle className="text-xl text-amber-800">Create New Support Ticket</CardTitle>
              <CardDescription>Please provide details about your issue or question</CardDescription>
            </CardHeader>
            <CardContent>
              <form onSubmit={handleCreateTicket} className="space-y-4">
                <div>
                  <Label htmlFor="subject" className="text-stone-700">Subject *</Label>
                  <Input
                    id="subject"
                    value={newTicket.subject}
                    onChange={(e) => setNewTicket({...newTicket, subject: e.target.value})}
                    className="mt-1 bg-white border-amber-200 focus:border-amber-600"
                    placeholder="Brief description of your issue"
                    required
                  />
                </div>
                <div>
                  <Label htmlFor="order_id" className="text-stone-700">Order ID (if applicable)</Label>
                  <Input
                    id="order_id"
                    value={newTicket.order_id}
                    onChange={(e) => setNewTicket({...newTicket, order_id: e.target.value})}
                    className="mt-1 bg-white border-amber-200 focus:border-amber-600"
                    placeholder="e.g. ORD-001"
                  />
                </div>
                <div>
                  <Label htmlFor="message" className="text-stone-700">Message *</Label>
                  <Textarea
                    id="message"
                    value={newTicket.message}
                    onChange={(e) => setNewTicket({...newTicket, message: e.target.value})}
                    className="mt-1 bg-white border-amber-200 focus:border-amber-600"
                    placeholder="Please describe your issue in detail"
                    rows={5}
                    required
                  />
                </div>
                <div className="flex justify-end space-x-2 pt-2">
                  <Button 
                    type="button" 
                    variant="outline" 
                    onClick={() => {
                      setCreating(false);
                      setNewTicket({
                        subject: '',
                        message: '',
                        order_id: ''
                      });
                    }}
                    disabled={submitting}
                    className="border-amber-200"
                  >
                    Cancel
                  </Button>
                  <Button 
                    type="submit" 
                    disabled={submitting}
                    className="bg-amber-600 hover:bg-amber-700 text-white"
                  >
                    {submitting ? (
                      <>
                        <motion.div
                          animate={{ rotate: 360 }}
                          transition={{ duration: 1, repeat: Infinity, ease: "linear" }}
                          className="w-5 h-5 border-2 border-white border-t-transparent rounded-full mr-2"
                        ></motion.div>
                        Submitting...
                      </>
                    ) : (
                      <>
                        <Send className="mr-2 h-4 w-4" /> Submit Ticket
                      </>
                    )}
                  </Button>
                </div>
              </form>
            </CardContent>
          </Card>
        </motion.div>
      )}

      {loading ? (
        <div className="text-center py-8">
          <motion.div
            animate={{ rotate: 360 }}
            transition={{ duration: 2, repeat: Infinity, ease: "linear" }}
            className="w-8 h-8 border-4 border-amber-600 border-t-transparent rounded-full mx-auto mb-4"
          ></motion.div>
          <p className="text-stone-600">Loading your support tickets...</p>
        </div>
      ) : tickets.length === 0 ? (
        <div className="text-center py-12 bg-amber-50 rounded-lg border border-amber-200">
          <MessageSquare className="h-12 w-12 text-amber-400 mx-auto mb-4" />
          <h3 className="text-lg font-medium text-amber-800 mb-2">No Support Tickets</h3>
          <p className="text-stone-600 mb-6">You haven't created any support tickets yet</p>
          {!creating && (
            <Button 
              onClick={() => setCreating(true)} 
              className="bg-amber-600 hover:bg-amber-700 text-white"
            >
              <Plus className="mr-2 h-4 w-4" /> Create Your First Ticket
            </Button>
          )}
        </div>
      ) : (
        <div className="space-y-4">
          {tickets.map((ticket) => (
            <Card key={ticket.id} className={`border-l-4 ${ticket.status.toLowerCase() === 'open' ? 'border-l-green-500' : 'border-l-stone-400'}`}>
              <CardHeader className="pb-3">
                <div className="flex justify-between items-start">
                  <div>
                    <CardTitle className="text-lg text-amber-800 flex items-center">
                      {ticket.subject}
                      <Badge className={`ml-3 ${getStatusColor(ticket.status)}`}>
                        {ticket.status}
                      </Badge>
                    </CardTitle>
                    <CardDescription className="mt-1 flex items-center">
                      <span className="text-xs text-stone-500 flex items-center">
                        <Clock className="h-3 w-3 mr-1" /> Created: {formatDate(ticket.created_at)}
                      </span>
                      <span className="mx-2 text-stone-300">|</span>
                      <span className="text-xs text-stone-500 flex items-center">
                        <RefreshCw className="h-3 w-3 mr-1" /> Updated: {formatDate(ticket.updated_at)}
                      </span>
                    </CardDescription>
                  </div>
                  <Button 
                    variant="ghost" 
                    size="sm" 
                    className="h-8 w-8 p-0 text-stone-500"
                    onClick={() => toggleTicket(ticket.id)}
                  >
                    {expandedTicket === ticket.id ? (
                      <ChevronUp className="h-5 w-5" />
                    ) : (
                      <ChevronDown className="h-5 w-5" />
                    )}
                  </Button>
                </div>
              </CardHeader>
              
              {expandedTicket === ticket.id && (
                <>
                  <Separator className="mx-6 bg-amber-100" />
                  <CardContent className="pt-4">
                    <div className="space-y-4 mb-4">
                      {ticket.messages.map((msg) => (
                        <div 
                          key={msg.id} 
                          className={`flex ${msg.sender === 'user' ? 'justify-end' : 'justify-start'}`}
                        >
                          <div 
                            className={`max-w-[80%] rounded-lg p-3 ${msg.sender === 'user' 
                              ? 'bg-amber-100 text-stone-800' 
                              : 'bg-white border border-amber-200 text-stone-800'}`}
                          >
                            <div className="text-sm">{msg.message}</div>
                            <div className="text-xs text-stone-500 mt-1 flex justify-end items-center">
                              {formatDate(msg.timestamp)}
                              {msg.sender === 'support' && (
                                <CheckCircle className="h-3 w-3 ml-1 text-green-600" />
                              )}
                            </div>
                          </div>
                        </div>
                      ))}
                    </div>
                    
                    {ticket.status.toLowerCase() === 'open' && (
                      <div className="mt-4">
                        <div className="flex space-x-2">
                          <Input
                            value={newMessage}
                            onChange={(e) => setNewMessage(e.target.value)}
                            placeholder="Type your reply here..."
                            className="flex-grow bg-white border-amber-200 focus:border-amber-600"
                            disabled={submitting}
                          />
                          <Button 
                            onClick={() => handleSendMessage(ticket.id)}
                            disabled={!newMessage.trim() || submitting}
                            className="bg-amber-600 hover:bg-amber-700 text-white"
                          >
                            {submitting ? (
                              <motion.div
                                animate={{ rotate: 360 }}
                                transition={{ duration: 1, repeat: Infinity, ease: "linear" }}
                                className="w-5 h-5 border-2 border-white border-t-transparent rounded-full"
                              ></motion.div>
                            ) : (
                              <Send className="h-4 w-4" />
                            )}
                          </Button>
                        </div>
                      </div>
                    )}
                  </CardContent>
                </>
              )}
            </Card>
          ))}
        </div>
      )}
    </div>
  );
};

export default SupportTicketComponent;