File size: 33,999 Bytes
ea94f5e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AI Image Creator - Next Gen</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        'neon-blue': '#00d4ff',
                        'neon-purple': '#8b5cf6',
                        'neon-pink': '#ec4899',
                        'dark-bg': '#0a0a0a',
                        'card-bg': '#1a1a1a',
                        'border-glow': '#00d4ff40'
                    },
                    animation: {
                        'float': 'float 6s ease-in-out infinite',
                        'glow': 'glow 2s ease-in-out infinite alternate',
                        'pulse-slow': 'pulse 3s cubic-bezier(0.4, 0, 0.6, 1) infinite',
                    }
                }
            }
        }
    </script>
    <style>
        @keyframes float {
            0%, 100% { transform: translateY(0px); }
            50% { transform: translateY(-20px); }
        }
        @keyframes glow {
            from { box-shadow: 0 0 20px #00d4ff; }
            to { box-shadow: 0 0 30px #00d4ff, 0 0 40px #00d4ff; }
        }
        .gradient-text {
            background: linear-gradient(45deg, #00d4ff, #8b5cf6, #ec4899);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        .glass-effect {
            background: rgba(255, 255, 255, 0.05);
            backdrop-filter: blur(10px);
            border: 1px solid rgba(255, 255, 255, 0.1);
        }
        .neon-border {
            border: 1px solid #00d4ff;
            box-shadow: 0 0 10px #00d4ff40;
        }
        .neon-border:hover {
            box-shadow: 0 0 20px #00d4ff, 0 0 30px #00d4ff40;
        }
    </style>
</head>
<body class="bg-dark-bg text-white min-h-screen font-sans">
    <!-- Navigation -->
    <nav class="glass-effect fixed top-0 w-full z-50 border-b border-white/10">
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="flex justify-between items-center h-16">
                <div class="flex items-center space-x-4">
                    <div class="w-10 h-10 bg-gradient-to-r from-neon-blue to-neon-purple rounded-lg flex items-center justify-center animate-pulse-slow">
                        <i class="fas fa-magic text-white text-lg"></i>
                    </div>
                    <h1 class="text-2xl font-bold gradient-text">AI Creator</h1>
                </div>
                <div class="hidden md:flex items-center space-x-6">
                    <button onclick="showSection('create')" class="nav-btn text-gray-300 hover:text-neon-blue transition-colors">
                        <i class="fas fa-plus mr-2"></i>Create
                    </button>
                    <button onclick="showSection('gallery')" class="nav-btn text-gray-300 hover:text-neon-blue transition-colors">
                        <i class="fas fa-images mr-2"></i>Gallery
                    </button>
                    <button onclick="showSection('history')" class="nav-btn text-gray-300 hover:text-neon-blue transition-colors">
                        <i class="fas fa-history mr-2"></i>History
                    </button>
                    <button id="authBtn" onclick="showAuthModal()" class="px-6 py-2 bg-gradient-to-r from-neon-blue to-neon-purple rounded-full hover:opacity-90 transition-opacity">
                        <i class="fas fa-sign-in-alt mr-2"></i>Login
                    </button>
                </div>
                <div class="md:hidden">
                    <button onclick="toggleMobileMenu()" class="text-gray-300 hover:text-neon-blue">
                        <i class="fas fa-bars text-xl"></i>
                    </button>
                </div>
            </div>
        </div>
    </nav>

    <!-- Mobile Menu -->
    <div id="mobileMenu" class="md:hidden fixed top-16 left-0 w-full glass-effect border-b border-white/10 hidden">
        <div class="px-4 py-4 space-y-4">
            <button onclick="showSection('create')" class="nav-btn w-full text-left text-gray-300 hover:text-neon-blue transition-colors">
                <i class="fas fa-plus mr-2"></i>Create
            </button>
            <button onclick="showSection('gallery')" class="nav-btn w-full text-left text-gray-300 hover:text-neon-blue transition-colors">
                <i class="fas fa-images mr-2"></i>Gallery
            </button>
            <button onclick="showSection('history')" class="nav-btn w-full text-left text-gray-300 hover:text-neon-blue transition-colors">
                <i class="fas fa-history mr-2"></i>History
            </button>
            <button onclick="showAuthModal()" class="w-full px-6 py-2 bg-gradient-to-r from-neon-blue to-neon-purple rounded-full hover:opacity-90 transition-opacity">
                <i class="fas fa-sign-in-alt mr-2"></i>Login
            </button>
        </div>
    </div>

    <!-- Main Content -->
    <main class="pt-20">
        <!-- Create Section -->
        <section id="createSection" class="section active min-h-screen">
            <div class="max-w-4xl mx-auto px-4 sm:px-6 lg:px-8 py-12">
                <!-- Hero Section -->
                <div class="text-center mb-16">
                    <h2 class="text-6xl font-bold gradient-text mb-6 animate-float">
                        Create Magic
                    </h2>
                    <p class="text-xl text-gray-400 mb-8 max-w-2xl mx-auto">
                        Transform your imagination into stunning visuals with our advanced AI image generation
                    </p>
                    <div class="flex justify-center space-x-4">
                        <div class="w-4 h-4 bg-neon-blue rounded-full animate-pulse"></div>
                        <div class="w-4 h-4 bg-neon-purple rounded-full animate-pulse" style="animation-delay: 0.2s;"></div>
                        <div class="w-4 h-4 bg-neon-pink rounded-full animate-pulse" style="animation-delay: 0.4s;"></div>
                    </div>
                </div>

                <!-- Creation Interface -->
                <div class="glass-effect rounded-3xl p-8 neon-border">
                    <div class="space-y-8">
                        <!-- Prompt Input -->
                        <div class="relative">
                            <textarea id="promptInput" 
                                class="w-full h-32 bg-card-bg border border-white/20 rounded-2xl px-6 py-4 text-white placeholder-gray-500 focus:outline-none focus:border-neon-blue focus:ring-2 focus:ring-neon-blue/20 resize-none"
                                placeholder="Describe your vision... What would you like to create?"></textarea>
                            <div class="absolute bottom-4 right-4">
                                <button id="generateBtn" onclick="generateImage()" 
                                    class="px-8 py-3 bg-gradient-to-r from-neon-blue to-neon-purple rounded-full font-semibold hover:opacity-90 transition-opacity animate-glow">
                                    <i class="fas fa-magic mr-2"></i>Generate
                                </button>
                            </div>
                        </div>

                        <!-- Quick Prompts -->
                        <div class="grid grid-cols-2 md:grid-cols-4 gap-4">
                            <button onclick="setPrompt('A futuristic city with flying cars and neon lights')" 
                                class="quick-prompt glass-effect rounded-xl p-4 hover:neon-border transition-all">
                                <i class="fas fa-city text-neon-blue mb-2"></i>
                                <p class="text-sm">Futuristic City</p>
                            </button>
                            <button onclick="setPrompt('A magical forest with glowing mushrooms and fairy lights')" 
                                class="quick-prompt glass-effect rounded-xl p-4 hover:neon-border transition-all">
                                <i class="fas fa-tree text-neon-purple mb-2"></i>
                                <p class="text-sm">Magical Forest</p>
                            </button>
                            <button onclick="setPrompt('A cyberpunk warrior with neon armor and glowing weapons')" 
                                class="quick-prompt glass-effect rounded-xl p-4 hover:neon-border transition-all">
                                <i class="fas fa-user-ninja text-neon-pink mb-2"></i>
                                <p class="text-sm">Cyberpunk Warrior</p>
                            </button>
                            <button onclick="setPrompt('An underwater palace with coral and sea creatures')" 
                                class="quick-prompt glass-effect rounded-xl p-4 hover:neon-border transition-all">
                                <i class="fas fa-water text-neon-blue mb-2"></i>
                                <p class="text-sm">Underwater Palace</p>
                            </button>
                        </div>

                        <!-- Loading State -->
                        <div id="loadingState" class="hidden text-center py-12">
                            <div class="inline-flex items-center space-x-4">
                                <div class="w-8 h-8 border-4 border-neon-blue border-t-transparent rounded-full animate-spin"></div>
                                <span class="text-neon-blue text-lg">Creating your masterpiece...</span>
                            </div>
                            <div id="queueInfo" class="mt-4 text-gray-400"></div>
                        </div>

                        <!-- Result -->
                        <div id="resultSection" class="hidden">
                            <div class="glass-effect rounded-2xl p-6 neon-border">
                                <img id="generatedImage" class="w-full rounded-xl shadow-2xl" alt="Generated image">
                                <div class="mt-6 flex justify-between items-center">
                                    <button onclick="addToGallery()" class="px-6 py-3 bg-gradient-to-r from-neon-purple to-neon-pink rounded-full hover:opacity-90 transition-opacity">
                                        <i class="fas fa-plus mr-2"></i>Add to Gallery
                                    </button>
                                    <button onclick="downloadImage()" class="px-6 py-3 glass-effect rounded-full hover:neon-border transition-all">
                                        <i class="fas fa-download mr-2"></i>Download
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Gallery Section -->
        <section id="gallerySection" class="section hidden min-h-screen">
            <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-12">
                <div class="text-center mb-12">
                    <h2 class="text-5xl font-bold gradient-text mb-4">Community Gallery</h2>
                    <p class="text-xl text-gray-400">Explore amazing creations from our community</p>
                </div>
                
                <div class="flex justify-center mb-8">
                    <select id="sortOrder" onchange="loadGallery()" class="glass-effect rounded-full px-6 py-3 border border-white/20 focus:outline-none focus:border-neon-blue">
                        <option value="new-old">Newest First</option>
                        <option value="old-new">Oldest First</option>
                    </select>
                </div>

                <div id="galleryGrid" class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-6">
                    <!-- Gallery items will be loaded here -->
                </div>
            </div>
        </section>

        <!-- History Section -->
        <section id="historySection" class="section hidden min-h-screen">
            <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-12">
                <div class="text-center mb-12">
                    <h2 class="text-5xl font-bold gradient-text mb-4">Your Creations</h2>
                    <p class="text-xl text-gray-400">Your personal collection of generated images</p>
                </div>

                <div id="historyGrid" class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-6">
                    <!-- History items will be loaded here -->
                </div>
            </div>
        </section>
    </main>

    <!-- Auth Modal -->
    <div id="authModal" class="fixed inset-0 bg-black/50 backdrop-blur-sm hidden z-50">
        <div class="min-h-screen flex items-center justify-center p-4">
            <div class="glass-effect rounded-3xl border border-white/20 w-full max-w-md neon-border">
                <div class="p-8">
                    <div class="text-center mb-8">
                        <div class="w-16 h-16 bg-gradient-to-r from-neon-blue to-neon-purple rounded-full flex items-center justify-center mx-auto mb-4 animate-pulse-slow">
                            <i class="fas fa-user text-white text-2xl"></i>
                        </div>
                        <h3 class="text-2xl font-bold gradient-text" id="authTitle">Login</h3>
                    </div>

                    <!-- Login Form -->
                    <form id="loginForm" class="space-y-6">
                        <div>
                            <input type="email" id="loginEmail" placeholder="Email" required
                                class="w-full bg-card-bg border border-white/20 rounded-xl px-4 py-3 text-white placeholder-gray-500 focus:outline-none focus:border-neon-blue">
                        </div>
                        <div>
                            <input type="password" id="loginPassword" placeholder="Password" required
                                class="w-full bg-card-bg border border-white/20 rounded-xl px-4 py-3 text-white placeholder-gray-500 focus:outline-none focus:border-neon-blue">
                        </div>
                        <button type="submit" 
                            class="w-full px-6 py-3 bg-gradient-to-r from-neon-blue to-neon-purple rounded-xl font-semibold hover:opacity-90 transition-opacity">
                            Login
                        </button>
                    </form>

                    <!-- Register Form -->
                    <form id="registerForm" class="space-y-6 hidden">
                        <div>
                            <input type="text" id="registerUsername" placeholder="Username" required
                                class="w-full bg-card-bg border border-white/20 rounded-xl px-4 py-3 text-white placeholder-gray-500 focus:outline-none focus:border-neon-blue">
                        </div>
                        <div>
                            <input type="email" id="registerEmail" placeholder="Email" required
                                class="w-full bg-card-bg border border-white/20 rounded-xl px-4 py-3 text-white placeholder-gray-500 focus:outline-none focus:border-neon-blue">
                        </div>
                        <div>
                            <input type="password" id="registerPassword" placeholder="Password" required
                                class="w-full bg-card-bg border border-white/20 rounded-xl px-4 py-3 text-white placeholder-gray-500 focus:outline-none focus:border-neon-blue">
                        </div>
                        <button type="submit" 
                            class="w-full px-6 py-3 bg-gradient-to-r from-neon-purple to-neon-pink rounded-xl font-semibold hover:opacity-90 transition-opacity">
                            Register
                        </button>
                    </form>

                    <div class="text-center mt-6">
                        <button onclick="toggleAuthForm()" class="text-neon-blue hover:text-neon-purple transition-colors" id="toggleAuthBtn">
                            Don't have an account? Register
                        </button>
                    </div>

                    <button onclick="closeAuthModal()" class="absolute top-4 right-4 text-gray-400 hover:text-white">
                        <i class="fas fa-times text-xl"></i>
                    </button>
                </div>
            </div>
        </div>
    </div>

    <!-- Image Preview Modal -->
    <div id="previewModal" class="fixed inset-0 bg-black/50 backdrop-blur-sm hidden z-50">
        <div class="min-h-screen flex items-center justify-center p-4">
            <div class="glass-effect rounded-3xl border border-white/20 w-full max-w-4xl neon-border">
                <div class="p-8">
                    <div class="flex justify-between items-center mb-6">
                        <h3 class="text-2xl font-bold gradient-text">Image Preview</h3>
                        <button onclick="closePreviewModal()" class="text-gray-400 hover:text-white">
                            <i class="fas fa-times text-xl"></i>
                        </button>
                    </div>
                    <img id="previewImage" class="w-full rounded-2xl shadow-2xl" alt="Preview">
                    <p id="previewPrompt" class="mt-4 text-center text-gray-300"></p>
                </div>
            </div>
        </div>
    </div>

    <script>
        let currentUser = null;
        let currentToken = null;
        let generatedImageUrl = null;

        // Navigation
        function showSection(sectionName) {
            document.querySelectorAll('.section').forEach(s => s.classList.add('hidden'));
            document.getElementById(sectionName + 'Section').classList.remove('hidden');
            
            if (sectionName === 'gallery') loadGallery();
            if (sectionName === 'history') loadHistory();
        }

        function toggleMobileMenu() {
            const menu = document.getElementById('mobileMenu');
            menu.classList.toggle('hidden');
        }

        // Auth Functions
        async function checkAuth() {
            const token = localStorage.getItem('token');
            if (!token) {
                updateAuthUI(false);
                return;
            }

            try {
                const response = await fetch('https://gen-image-main.vercel.app/api/auth/status', {
                    headers: { 'Authorization': `Bearer ${token}` }
                });

                if (response.ok) {
                    const data = await response.json();
                    currentUser = data.user;
                    currentToken = token;
                    updateAuthUI(true);
                } else {
                    updateAuthUI(false);
                }
            } catch (error) {
                updateAuthUI(false);
            }
        }

        function updateAuthUI(isAuthenticated) {
            const authBtn = document.getElementById('authBtn');
            if (isAuthenticated) {
                authBtn.innerHTML = `<i class="fas fa-user mr-2"></i>${currentUser.username}`;
                authBtn.onclick = logout;
            } else {
                authBtn.innerHTML = `<i class="fas fa-sign-in-alt mr-2"></i>Login`;
                authBtn.onclick = showAuthModal;
            }
        }

        function showAuthModal() {
            document.getElementById('authModal').classList.remove('hidden');
        }

        function closeAuthModal() {
            document.getElementById('authModal').classList.add('hidden');
        }

        function toggleAuthForm() {
            const loginForm = document.getElementById('loginForm');
            const registerForm = document.getElementById('registerForm');
            const authTitle = document.getElementById('authTitle');
            const toggleBtn = document.getElementById('toggleAuthBtn');

            if (loginForm.classList.contains('hidden')) {
                loginForm.classList.remove('hidden');
                registerForm.classList.add('hidden');
                authTitle.textContent = 'Login';
                toggleBtn.textContent = "Don't have an account? Register";
            } else {
                loginForm.classList.add('hidden');
                registerForm.classList.remove('hidden');
                authTitle.textContent = 'Register';
                toggleBtn.textContent = 'Already have an account? Login';
            }
        }

        async function login(event) {
            event.preventDefault();
            const email = document.getElementById('loginEmail').value;
            const password = document.getElementById('loginPassword').value;

            try {
                const response = await fetch('https://gen-image-main.vercel.app/api/login', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({ email, password })
                });

                if (response.ok) {
                    const data = await response.json();
                    localStorage.setItem('token', data.token);
                    currentUser = data.user;
                    currentToken = data.token;
                    updateAuthUI(true);
                    closeAuthModal();
                } else {
                    const error = await response.json();
                    alert(error.error);
                }
            } catch (error) {
                alert('Login failed');
            }
        }

        async function register(event) {
            event.preventDefault();
            const username = document.getElementById('registerUsername').value;
            const email = document.getElementById('registerEmail').value;
            const password = document.getElementById('registerPassword').value;

            try {
                const response = await fetch('https://gen-image-main.vercel.app/api/register', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({ username, email, password })
                });

                if (response.ok) {
                    const data = await response.json();
                    localStorage.setItem('token', data.token);
                    currentUser = data.user;
                    currentToken = data.token;
                    updateAuthUI(true);
                    closeAuthModal();
                } else {
                    const error = await response.json();
                    alert(error.error);
                }
            } catch (error) {
                alert('Registration failed');
            }
        }

        function logout() {
            localStorage.removeItem('token');
            currentUser = null;
            currentToken = null;
            updateAuthUI(false);
        }

        // Image Generation
        function setPrompt(prompt) {
            document.getElementById('promptInput').value = prompt;
        }

        async function generateImage() {
            const prompt = document.getElementById('promptInput').value.trim();
            if (!prompt) {
                alert('Please enter a prompt');
                return;
            }

            if (!currentToken) {
                alert('Please login to generate images');
                return;
            }

            document.getElementById('loadingState').classList.remove('hidden');
            document.getElementById('resultSection').classList.add('hidden');

            try {
                const response = await fetch('https://gen-image-main.vercel.app/api/generate', {
                    method: 'POST',
                    headers: {
                        'Authorization': `Bearer ${currentToken}`,
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({ prompt })
                });

                const reader = response.body.getReader();
                const decoder = new TextDecoder();

                while (true) {
                    const { done, value } = await reader.read();
                    if (done) break;

                    const chunk = decoder.decode(value);
                    const lines = chunk.split('\n');

                    for (const line of lines) {
                        if (line.startsWith('data: ')) {
                            try {
                                const data = JSON.parse(line.substring(6));
                                
                                if (data.type === 'estimation') {
                                    document.getElementById('queueInfo').textContent = 
                                        `Queue position: ${data.queueSize}, ETA: ${data.eta}s`;
                                } else if (data.type === 'processing') {
                                    document.getElementById('queueInfo').textContent = 'Processing your image...';
                                } else if (data.type === 'success') {
                                    generatedImageUrl = data.originalUrl;
                                    document.getElementById('generatedImage').src = data.originalUrl;
                                    document.getElementById('loadingState').classList.add('hidden');
                                    document.getElementById('resultSection').classList.remove('hidden');
                                    return;
                                } else if (data.type === 'error') {
                                    throw new Error(data.message);
                                }
                            } catch (e) {
                                console.error('Error parsing SSE data:', e);
                            }
                        }
                    }
                }
            } catch (error) {
                document.getElementById('loadingState').classList.add('hidden');
                alert('Generation failed: ' + error.message);
            }
        }

        async function addToGallery() {
            if (!generatedImageUrl) return;

            try {
                const response = await fetch('https://gen-image-main.vercel.app/api/upload-image', {
                    method: 'POST',
                    headers: {
                        'Authorization': `Bearer ${currentToken}`,
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        imageUrl: generatedImageUrl,
                        prompt: document.getElementById('promptInput').value
                    })
                });

                if (response.ok) {
                    alert('Image added to gallery successfully!');
                    loadGallery();
                } else {
                    const error = await response.json();
                    alert(error.error);
                }
            } catch (error) {
                alert('Failed to add to gallery');
            }
        }

        function downloadImage() {
            if (!generatedImageUrl) return;
            
            const link = document.createElement('a');
            link.href = generatedImageUrl;
            link.download = 'generated-image.jpg';
            link.click();
        }

        // Gallery Functions
        async function loadGallery() {
            try {
                const response = await fetch('https://gen-image-main.vercel.app/api/community-images');
                const images = await response.json();
                
                const grid = document.getElementById('galleryGrid');
                grid.innerHTML = '';

                const sortOrder = document.getElementById('sortOrder').value;
                const sortedImages = [...images].sort((a, b) => {
                    const dateA = new Date(a.created_at);
                    const dateB = new Date(b.created_at);
                    return sortOrder === 'old-new' ? dateA - dateB : dateB - dateA;
                });

                sortedImages.forEach(image => {
                    const card = createImageCard(image);
                    grid.appendChild(card);
                });
            } catch (error) {
                console.error('Failed to load gallery:', error);
            }
        }

        async function loadHistory() {
            if (!currentToken) return;

            try {
                const response = await fetch('https://gen-image-main.vercel.app/api/user-generations', {
                    headers: { 'Authorization': `Bearer ${currentToken}` }
                });

                if (response.ok) {
                    const images = await response.json();
                    const grid = document.getElementById('historyGrid');
                    grid.innerHTML = '';

                    if (images.length === 0) {
                        grid.innerHTML = `
                            <div class="col-span-full text-center py-12">
                                <div class="w-24 h-24 bg-gradient-to-r from-neon-blue to-neon-purple rounded-full flex items-center justify-center mx-auto mb-4 animate-pulse-slow">
                                    <i class="fas fa-magic text-white text-3xl"></i>
                                </div>
                                <p class="text-gray-400 text-lg">No images generated yet. Start creating!</p>
                            </div>
                        `;
                        return;
                    }

                    images.forEach(image => {
                        const card = createImageCard(image, true);
                        grid.appendChild(card);
                    });
                }
            } catch (error) {
                console.error('Failed to load history:', error);
            }
        }

        function createImageCard(image, isHistory = false) {
            const card = document.createElement('div');
            card.className = 'glass-effect rounded-2xl overflow-hidden neon-border hover:animate-glow transition-all cursor-pointer';
            
            card.innerHTML = `
                <div class="aspect-square overflow-hidden">
                    <img src="${image.uploaded_url || image.image_url}" alt="${image.prompt}" 
                         class="w-full h-full object-cover hover:scale-105 transition-transform duration-300">
                </div>
                <div class="p-4 space-y-3">
                    <p class="text-gray-300 line-clamp-2 text-sm">${image.prompt}</p>
                    <div class="flex items-center justify-between text-xs">
                        <p class="text-neon-blue flex items-center">
                            <i class="fas fa-user mr-1"></i>${image.username || 'You'}
                        </p>
                        <p class="text-gray-500 flex items-center">
                            <i class="fas fa-clock mr-1"></i>${new Date(image.created_at).toLocaleDateString()}
                        </p>
                    </div>
                    ${isHistory ? `
                        <button onclick="deleteImage(${image.id})" 
                            class="w-full mt-2 px-4 py-2 bg-red-500/20 text-red-400 rounded-xl hover:bg-red-500/30 transition-colors text-sm">
                            <i class="fas fa-trash mr-2"></i>Delete
                        </button>
                    ` : ''}
                </div>
            `;

            card.querySelector('img').addEventListener('click', () => {
                document.getElementById('previewImage').src = image.uploaded_url || image.image_url;
                document.getElementById('previewPrompt').textContent = image.prompt;
                document.getElementById('previewModal').classList.remove('hidden');
            });

            return card;
        }

        async function deleteImage(imageId) {
            if (!confirm('Are you sure you want to delete this image?')) return;

            try {
                const response = await fetch(`https://gen-image-main.vercel.app/api/user-generations/${imageId}`, {
                    method: 'DELETE',
                    headers: { 'Authorization': `Bearer ${currentToken}` }
                });

                if (response.ok) {
                    loadHistory();
                    loadGallery();
                } else {
                    const error = await response.json();
                    alert(error.error);
                }
            } catch (error) {
                alert('Failed to delete image');
            }
        }

        function closePreviewModal() {
            document.getElementById('previewModal').classList.add('hidden');
        }

        // Event Listeners
        document.addEventListener('DOMContentLoaded', () => {
            checkAuth();
            document.getElementById('loginForm').addEventListener('submit', login);
            document.getElementById('registerForm').addEventListener('submit', register);
        });

        // Close modals on outside click
        document.getElementById('authModal').addEventListener('click', (e) => {
            if (e.target === e.currentTarget) closeAuthModal();
        });

        document.getElementById('previewModal').addEventListener('click', (e) => {
            if (e.target === e.currentTarget) closePreviewModal();
        });
    </script>
</body>
</html>