File size: 116,149 Bytes
8ed5ac1
 
 
 
 
08c2fcd
8fdf1e4
8ed5ac1
 
 
 
 
 
 
 
 
 
 
 
 
df15537
08c2fcd
 
3432460
df15537
 
 
 
637dbbd
 
 
 
 
 
 
 
 
630e9fb
 
 
 
 
 
 
 
 
 
8ed5ac1
56b6ee6
 
 
 
 
 
8ed5ac1
 
 
 
 
 
 
 
 
 
 
08c2fcd
227b8f4
 
 
 
 
08c2fcd
227b8f4
08c2fcd
227b8f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
08c2fcd
 
 
227b8f4
 
 
 
 
 
 
 
 
 
 
b32c828
 
227b8f4
e6821c4
 
 
 
 
 
 
 
8fdf1e4
630e9fb
 
58fc638
 
 
 
 
 
 
 
d1b4a04
58fc638
662d544
8ed5ac1
e8e0147
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a2e8511
 
51bb097
b4662d6
 
68329d1
 
 
 
a2e8511
 
 
 
 
51bb097
a2e8511
6de8fab
 
 
 
 
 
 
 
 
 
 
 
 
7ac53fe
bbbd5f6
7ac53fe
bbbd5f6
 
 
7ac53fe
 
 
bbbd5f6
 
7ac53fe
 
a2e8511
 
 
 
 
 
a44b826
 
 
a2e8511
 
e6821c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a2e8511
e6821c4
 
a2e8511
 
da4f0fc
 
116ff62
da4f0fc
 
 
 
 
 
 
 
 
116ff62
 
 
 
 
 
d7ccb89
 
 
 
 
 
 
da4f0fc
 
 
 
df3dc5a
 
 
da4f0fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a44b826
a2e8511
 
a44b826
dfdf7e7
 
 
 
f48f381
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a44b826
dfdf7e7
 
 
 
 
 
a2e8511
a44b826
a2e8511
 
8ed5ac1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3432460
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ed5ac1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
da4f0fc
8ed5ac1
da4f0fc
8ed5ac1
 
 
 
 
 
 
 
 
56b6ee6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ed5ac1
da4f0fc
8ed5ac1
 
 
 
 
 
 
 
 
 
 
da4f0fc
8ed5ac1
 
 
 
 
 
 
 
 
 
 
da4f0fc
8ed5ac1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
da4f0fc
8ed5ac1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
da4f0fc
8ed5ac1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
da4f0fc
8ed5ac1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
08c2fcd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
227b8f4
 
08c2fcd
da4f0fc
08c2fcd
 
0a08210
 
 
08c2fcd
0a08210
8e8cd50
 
 
 
 
 
 
 
 
08c2fcd
 
 
0a08210
08c2fcd
 
0a08210
08c2fcd
 
 
0a08210
08c2fcd
 
 
0a08210
 
 
08c2fcd
 
 
0a08210
08c2fcd
 
 
0a08210
08c2fcd
be7a2aa
 
 
0a08210
08c2fcd
0a08210
08c2fcd
 
 
0a08210
08c2fcd
be7a2aa
 
 
0a08210
08c2fcd
0a08210
08c2fcd
 
 
0a08210
08c2fcd
0a08210
 
08c2fcd
 
 
0a08210
 
 
08c2fcd
0a08210
08c2fcd
0a08210
08c2fcd
 
 
0a08210
08c2fcd
0a08210
08c2fcd
 
 
 
 
 
0a08210
08c2fcd
 
0a08210
08c2fcd
207f9e0
0a08210
207f9e0
 
0a08210
207f9e0
0a08210
207f9e0
 
08c2fcd
 
 
 
 
0a08210
08c2fcd
 
0a08210
08c2fcd
 
 
0a08210
 
 
08c2fcd
 
0a08210
 
207f9e0
0a08210
207f9e0
08c2fcd
 
 
 
da4f0fc
08c2fcd
 
 
 
 
 
 
54e2305
08c2fcd
 
 
 
 
 
 
 
54e2305
 
08c2fcd
 
54e2305
08c2fcd
 
 
 
 
54e2305
08c2fcd
 
 
 
54e2305
08c2fcd
d9b2055
7fb5af7
 
08c2fcd
 
 
 
 
 
 
 
54e2305
d9b2055
08c2fcd
 
 
 
 
 
 
 
 
54e2305
08c2fcd
 
 
 
 
 
 
 
 
 
 
8ed5ac1
ddb7f6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3432460
 
ddb7f6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3432460
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d0fd74
 
56b6ee6
 
 
 
 
 
 
 
 
 
 
 
3432460
ddb7f6a
 
 
 
 
3d0fd74
 
 
ddb7f6a
 
 
 
 
 
 
56b6ee6
ddb7f6a
 
56b6ee6
 
 
 
 
 
 
 
 
 
 
ddb7f6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ed5ac1
da4f0fc
8ed5ac1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a2e8511
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b4662d6
 
 
 
a2e8511
a8dbc72
2e645b0
a2e8511
 
a8dbc72
2e645b0
a2e8511
a8dbc72
a2e8511
 
 
 
 
 
 
 
 
 
 
662d544
 
 
7ac53fe
a44b826
a2e8511
 
 
 
 
a8dbc72
 
a2e8511
51bb097
 
ac9962b
51bb097
 
 
8fdf1e4
51bb097
157125d
a44b826
157125d
 
 
 
51bb097
 
a2e8511
913f3b4
a44b826
a8dbc72
7ac53fe
157125d
ab5ee8c
 
157125d
ab5ee8c
 
 
 
 
 
 
 
 
 
116ff62
 
ab5ee8c
116ff62
68329d1
116ff62
 
 
 
2d3c175
116ff62
 
 
2d3c175
116ff62
 
 
 
 
 
 
 
 
 
 
 
2d3c175
 
 
116ff62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab5ee8c
 
 
 
 
 
116ff62
ab5ee8c
116ff62
ab5ee8c
 
 
58fc638
2d3c175
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
157125d
662d544
 
 
f7e6892
662d544
a2e8511
a8dbc72
 
a2e8511
 
 
 
 
df3dc5a
a2e8511
6a00dc7
df3dc5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a00dc7
 
 
df3dc5a
 
 
 
 
a2e8511
207f9e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54e2305
 
 
207f9e0
 
54e2305
 
 
 
 
 
 
 
 
207f9e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ac53fe
 
 
 
0e9cdf3
 
 
 
 
 
7ac53fe
 
 
 
dfdf7e7
 
 
 
 
58fc638
f7e6892
 
 
58fc638
 
 
 
 
 
f7e6892
58fc638
f7e6892
 
df3dc5a
f7e6892
df3dc5a
 
f7e6892
 
 
 
 
8fdf1e4
 
f7e6892
8fdf1e4
f7e6892
dfdf7e7
d5410d2
 
dfdf7e7
630e9fb
 
 
 
 
 
 
 
 
 
207f9e0
 
 
 
 
 
 
 
 
 
630e9fb
 
df3dc5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ba39ac
630e9fb
cc735d7
 
 
630e9fb
 
 
 
 
 
 
 
 
4ba39ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
207f9e0
 
 
 
 
 
 
 
 
 
 
 
 
630e9fb
 
 
 
 
 
 
 
 
 
 
 
 
 
b4662d6
 
 
 
 
7ac53fe
6d8a023
 
 
 
c4ce70b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d8a023
 
 
 
 
c4ce70b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d8a023
7ac53fe
8ed5ac1
 
 
 
64663bd
207f9e0
 
 
 
 
 
 
 
 
 
 
7ac53fe
 
b4662d6
 
 
 
 
8ed5ac1
630e9fb
 
 
 
 
 
 
 
 
 
 
 
 
 
207f9e0
 
 
 
 
 
 
 
 
630e9fb
 
4ba39ac
630e9fb
cc735d7
 
 
630e9fb
 
 
 
 
 
 
 
4ba39ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
207f9e0
 
 
 
 
 
 
 
 
 
 
 
 
630e9fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b4662d6
 
 
 
8ed5ac1
 
b149ffe
8ed5ac1
 
b4662d6
 
 
 
 
8ed5ac1
 
b149ffe
8ed5ac1
 
b4662d6
 
 
 
 
8ed5ac1
 
b149ffe
8ed5ac1
 
b4662d6
 
 
 
 
8ed5ac1
df15537
b149ffe
df15537
 
 
 
 
 
 
 
 
da4f0fc
df15537
 
 
 
 
 
 
 
 
 
 
5febe49
df15537
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9bcde88
4971aea
df15537
 
 
 
 
 
 
 
 
4971aea
 
 
 
 
 
 
 
 
 
 
 
 
df15537
 
 
 
 
 
 
 
 
 
 
 
 
 
 
637dbbd
da4f0fc
637dbbd
 
 
 
 
 
 
 
 
 
 
 
5c810f6
 
 
 
 
 
 
 
 
 
 
637dbbd
 
 
 
 
 
 
 
 
 
5c810f6
637dbbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5c810f6
 
 
 
 
 
 
 
 
 
 
 
637dbbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5c810f6
 
 
 
 
 
 
 
675ab86
 
 
637dbbd
 
675ab86
 
 
 
 
 
 
 
 
 
 
 
 
 
5febe49
675ab86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5febe49
675ab86
 
 
 
 
 
 
 
637dbbd
 
 
 
 
 
 
675ab86
637dbbd
 
 
8ed5ac1
 
b149ffe
8ed5ac1
 
 
 
 
 
 
b149ffe
8ed5ac1
 
68634f6
 
a44b826
a2e8511
 
a44b826
7ac53fe
 
 
8ed5ac1
89707b0
8ed5ac1
 
207f9e0
8ed5ac1
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
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
# -*- coding: utf-8 -*-
# Set matplotlib config directory to avoid permission issues
import os
os.environ['MPLCONFIGDIR'] = '/tmp/matplotlib'

from flask import Flask, request, jsonify, send_from_directory, redirect, url_for, session, render_template_string, make_response, Response, stream_with_context
from datetime import timedelta
import torch
from PIL import Image
import numpy as np
import io
from io import BytesIO
import base64
import uuid
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
import time
from flask_cors import CORS
import json
import sys
import requests
import asyncio
from threading import Thread
import tempfile
try:
    from openai import OpenAI
except Exception as _e:
    OpenAI = None
try:
    # LangChain for RAG answering
    from langchain_openai import ChatOpenAI
    from langchain_core.prompts import ChatPromptTemplate
    from langchain_core.output_parsers import StrOutputParser
except Exception as _e:
    ChatOpenAI = None
    ChatPromptTemplate = None
    StrOutputParser = None
from flask_login import (
    LoginManager,
    UserMixin,
    login_user,
    logout_user,
    login_required,
    current_user,
    fresh_login_required,
    login_fresh,
)

try:
    # PEFT for LoRA adapters (optional)
    from peft import PeftModel
except Exception as _e:
    PeftModel = None

# Fix for SQLite3 version compatibility with ChromaDB
try:
    import pysqlite3
    sys.modules['sqlite3'] = pysqlite3
except ImportError:
    print("Warning: pysqlite3 not found, using built-in sqlite3")

import chromadb
from chromadb.utils import embedding_functions

app = Flask(__name__, static_folder='static')

# Import product comparison coordinator with detailed debugging
print("=" * 80)
print("[STARTUP DEBUG] ๐Ÿš€ Testing product_comparison import at startup...")
print("=" * 80)

try:
    print("[DEBUG] Attempting to import product_comparison module...")
    from product_comparison import get_product_comparison_coordinator, decode_base64_image
    print("[DEBUG] โœ“ Product comparison module imported successfully!")
    print(f"[DEBUG] โœ“ get_product_comparison_coordinator: {get_product_comparison_coordinator}")
    print(f"[DEBUG] โœ“ decode_base64_image: {decode_base64_image}")
    
    # Test coordinator creation
    print("[DEBUG] Testing coordinator creation...")
    test_coordinator = get_product_comparison_coordinator()
    print(f"[DEBUG] โœ“ Test coordinator created: {type(test_coordinator).__name__}")
    
except ImportError as e:
    print(f"[DEBUG] โŒ Product comparison import failed: {e}")
    print(f"[DEBUG] โŒ Import error type: {type(e).__name__}")
    print(f"[DEBUG] โŒ Import error args: {e.args}")
    import traceback
    print("[DEBUG] โŒ Full import traceback:")
    traceback.print_exc()
    print("Warning: Product comparison module not available")
    get_product_comparison_coordinator = None
    decode_base64_image = None
except Exception as e:
    print(f"[DEBUG] โŒ Unexpected error during import: {e}")
    print(f"[DEBUG] โŒ Error type: {type(e).__name__}")
    import traceback
    print("[DEBUG] โŒ Full traceback:")
    traceback.print_exc()
    get_product_comparison_coordinator = None
    decode_base64_image = None

print("=" * 80)
print(f"[STARTUP DEBUG] ๐Ÿ Import test completed. Coordinator available: {get_product_comparison_coordinator is not None}")
print(f"[STARTUP DEBUG] ๐Ÿ“ Current working directory: {os.getcwd()}")
print(f"[STARTUP DEBUG] ๐Ÿ“‚ Files in current directory: {os.listdir('.')}")
print("=" * 80)
# ํ™˜๊ฒฝ ๋ณ€์ˆ˜์—์„œ ๋น„๋ฐ€ ํ‚ค๋ฅผ ๊ฐ€์ ธ์˜ค๊ฑฐ๋‚˜, ์—†์œผ๋ฉด ์•ˆ์ „ํ•œ ๋žœ๋ค ํ‚ค ์ƒ์„ฑ
secret_key = os.environ.get('FLASK_SECRET_KEY')
if not secret_key:
    import secrets
    secret_key = secrets.token_hex(16)  # 32์ž ๊ธธ์ด์˜ ๋žœ๋ค 16์ง„์ˆ˜ ๋ฌธ์ž์—ด ์ƒ์„ฑ
    print("WARNING: FLASK_SECRET_KEY ํ™˜๊ฒฝ ๋ณ€์ˆ˜๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ๋žœ๋ค ํ‚ค๋ฅผ ์ƒ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.")
    print("์„œ๋ฒ„ ์žฌ์‹œ์ž‘ ์‹œ ์„ธ์…˜์ด ๋ชจ๋‘ ๋งŒ๋ฃŒ๋ฉ๋‹ˆ๋‹ค. ํ”„๋กœ๋•์…˜ ํ™˜๊ฒฝ์—์„œ๋Š” ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•˜์„ธ์š”.")
app.secret_key = secret_key  # ์„ธ์…˜ ์•”ํ˜ธํ™”๋ฅผ ์œ„ํ•œ ๋น„๋ฐ€ ํ‚ค
app.config['CORS_HEADERS'] = 'Content-Type'
# Remember cookie (Flask-Login) โ€” minimize duration to prevent auto re-login
app.config['REMEMBER_COOKIE_DURATION'] = timedelta(seconds=1)
# Cookie settings for Hugging Face Spaces compatibility
app.config['REMEMBER_COOKIE_SECURE'] = False
app.config['REMEMBER_COOKIE_HTTPONLY'] = False  # Allow JS access for debugging
app.config['REMEMBER_COOKIE_SAMESITE'] = None  # Most permissive for iframe
# Session cookie settings - most permissive for HF Spaces
app.config['SESSION_COOKIE_SECURE'] = False
app.config['SESSION_COOKIE_HTTPONLY'] = False  # Allow JS access
app.config['SESSION_COOKIE_SAMESITE'] = None  # Most permissive for iframe
app.config['SESSION_COOKIE_PATH'] = '/'
app.config['SESSION_COOKIE_DOMAIN'] = None
CORS(app, supports_credentials=True)  # Enable CORS for all routes with credentials

# Dynamic HTTPS detection and cookie security settings
@app.before_request
def configure_cookies_for_https():
    """Dynamically configure cookie security based on HTTPS detection"""
    is_https = (
        request.is_secure or 
        request.headers.get('X-Forwarded-Proto') == 'https' or
        request.headers.get('X-Forwarded-Ssl') == 'on' or
        os.environ.get('HTTPS', '').lower() == 'true'
    )
    
    if is_https:
        app.config['SESSION_COOKIE_SECURE'] = True
        app.config['REMEMBER_COOKIE_SECURE'] = True

# ์‹œํฌ๋ฆฟ ํ‚ค ์„ค์ • (์„ธ์…˜ ์•”ํ˜ธํ™”์— ์‚ฌ์šฉ)
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY', 'vision_llm_agent_secret_key')
app.config['SESSION_TYPE'] = 'filesystem'
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(seconds=120)  # ์„ธ์…˜ ์œ ํšจ ์‹œ๊ฐ„ (2๋ถ„)
app.config['SESSION_REFRESH_EACH_REQUEST'] = False  # ์ ˆ๋Œ€ ๋งŒ๋ฃŒ(๋กœ๊ทธ์ธ ๊ธฐ์ค€ 2๋ถ„ ํ›„ ๋งŒ๋ฃŒ)
# Fix session cookie issues in Hugging Face Spaces
app.config['SESSION_COOKIE_NAME'] = 'session'
app.config['SESSION_COOKIE_DOMAIN'] = None  # Allow any domain
app.config['SESSION_KEY_PREFIX'] = 'session:'

# Flask-Login ์„ค์ •
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'
login_manager.session_protection = 'strong'

# When authentication is required or session is not fresh, redirect to login instead of 401
login_manager.refresh_view = 'login'

@login_manager.unauthorized_handler
def handle_unauthorized():
    # For non-authenticated access, send user to login
    return redirect(url_for('login'))

@login_manager.needs_refresh_handler
def handle_needs_refresh():
    # For non-fresh sessions (e.g., after expiry or only remember-cookie), send to login
    return redirect(url_for('login'))

# ์„ธ์…˜ ์„ค์ •
import tempfile
from flask_session import Session

# ์ž„์‹œ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ถŒํ•œ ๋ฌธ์ œ ํ•ด๊ฒฐ
session_dir = tempfile.gettempdir()
app.config['SESSION_TYPE'] = 'filesystem'
app.config['SESSION_PERMANENT'] = True
app.config['SESSION_USE_SIGNER'] = True
app.config['SESSION_FILE_DIR'] = session_dir
print(f"Using session directory: {session_dir}")
Session(app)

# ์‚ฌ์šฉ์ž ํด๋ž˜์Šค ์ •์˜
class User(UserMixin):
    def __init__(self, id, username, password):
        self.id = id
        self.username = username
        self.password = password
    
    def get_id(self):
        return str(self.id)  # Flask-Login์€ ๋ฌธ์ž์—ด ID๋ฅผ ์š”๊ตฌํ•จ

# ํ…Œ์ŠคํŠธ์šฉ ์‚ฌ์šฉ์ž (์‹ค์ œ ํ™˜๊ฒฝ์—์„œ๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์‚ฌ์šฉ ๊ถŒ์žฅ)
# ํ™˜๊ฒฝ ๋ณ€์ˆ˜์—์„œ ์‚ฌ์šฉ์ž ๊ณ„์ • ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ (๊ธฐ๋ณธ๊ฐ’ ์—†์Œ)
admin_username = os.environ.get('ADMIN_USERNAME', 'admin')
admin_password = os.environ.get('ADMIN_PASSWORD')
user_username = os.environ.get('USER_USERNAME', 'user')
user_password = os.environ.get('USER_PASSWORD')

# ํ™˜๊ฒฝ ๋ณ€์ˆ˜๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์„ ๊ฒฝ์šฐ ๊ฒฝ๊ณ  ๋ฉ”์‹œ์ง€ ์ถœ๋ ฅ
if not admin_password or not user_password:
    print("ERROR: ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ADMIN_PASSWORD ๋˜๋Š” USER_PASSWORD๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.")
    print("Hugging Face Spaces์—์„œ ๋ฐ˜๋“œ์‹œ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.")
    print("Settings > Repository secrets์—์„œ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•˜์„ธ์š”.")
    # ํ™˜๊ฒฝ ๋ณ€์ˆ˜๊ฐ€ ์—†์„ ๊ฒฝ์šฐ ์ž„์‹œ ๋น„๋ฐ€๋ฒˆํ˜ธ ์ƒ์„ฑ (๊ฐœ๋ฐœ์šฉ)
    import secrets
    if not admin_password:
        admin_password = secrets.token_hex(8)  # ์ž„์‹œ ๋น„๋ฐ€๋ฒˆํ˜ธ ์ƒ์„ฑ
        print(f"WARNING: ์ž„์‹œ admin ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค: {admin_password}")
    if not user_password:
        user_password = secrets.token_hex(8)  # ์ž„์‹œ ๋น„๋ฐ€๋ฒˆํ˜ธ ์ƒ์„ฑ
        print(f"WARNING: ์ž„์‹œ user ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค: {user_password}")

users = {
    admin_username: User('1', admin_username, admin_password),
    user_username: User('2', user_username, user_password)
}

# Manual authentication check function
def check_authentication():
    """Check authentication via session, cookies, or headers. Returns (user_id, username) or (None, None)"""
    # Check session first
    user_id = session.get('user_id')
    username = session.get('username')
    
    # Fallback to cookies if session is empty
    if not user_id or not username:
        user_id = request.cookies.get('auth_user_id')
        username = request.cookies.get('auth_username')
    
    # Fallback to headers (for localStorage-based auth)
    if not user_id or not username:
        user_id = request.headers.get('X-Auth-User-ID')
        username = request.headers.get('X-Auth-Username')
        print(f"[DEBUG] Auth from headers: user_id={user_id}, username={username}")
    
    # Fallback to query params (for SSE/EventSource where custom headers aren't supported)
    if not user_id or not username:
        user_id = request.args.get('uid')
        username = request.args.get('uname')
        if user_id or username:
            print(f"[DEBUG] Auth from query params: user_id={user_id}, username={username}")
    
    if not user_id or not username:
        return None, None
    
    # Verify user exists
    for stored_username, user_obj in users.items():
        # users maps username -> User instance; compare using attributes
        if str(getattr(user_obj, 'id', None)) == str(user_id) and stored_username == username:
            return user_id, username
    
    return None, None

def require_auth():
    """Decorator replacement for @login_required that supports cookie fallback"""
    def decorator(f):
        def decorated_function(*args, **kwargs):
            user_id, username = check_authentication()
            if not user_id:
                return jsonify({"error": "Authentication required"}), 401
            return f(*args, **kwargs)
        decorated_function.__name__ = f.__name__
        return decorated_function
    return decorator

# ์‚ฌ์šฉ์ž ๋กœ๋” ํ•จ์ˆ˜
@login_manager.user_loader
def load_user(user_id):
    print(f"Loading user with ID: {user_id}")
    # ์„ธ์…˜ ๋””๋ฒ„๊ทธ ์ •๋ณด ์ถœ๋ ฅ
    print(f"Session data in user_loader: {dict(session)}")
    print(f"Current request cookies: {request.cookies}")
    
    # Check session first, then fallback to cookies
    session_user_id = session.get('user_id')
    if not session_user_id:
        # Try to get from cookies
        cookie_user_id = request.cookies.get('auth_user_id')
        cookie_username = request.cookies.get('auth_username')
        print(f"Session empty, checking cookies: user_id={cookie_user_id}, username={cookie_username}")
        
        if cookie_user_id and cookie_username:
            # Verify cookie user exists
            for username, user in users.items():
                if str(user.id) == str(cookie_user_id) and username == cookie_username:
                    print(f"User found via cookies: {username}, ID: {user.id}")
                    # Restore session from cookies
                    session['user_id'] = user.id
                    session['username'] = username
                    session.modified = True
                    return user
    
    # Original session-based lookup
    for username, user in users.items():
        if str(user.id) == str(user_id):  # ํ™•์‹คํ•œ ๋ฌธ์ž์—ด ๋น„๊ต
            print(f"User found: {username}, ID: {user.id}")
            # ์„ธ์…˜ ์ •๋ณด ์—…๋ฐ์ดํŠธ
            session['user_id'] = user.id
            session['username'] = username
            session.modified = True
            return user
    print(f"User not found with ID: {user_id}")
    return None

# Model initialization
print("Loading models... This may take a moment.")

# Image embedding model (CLIP) for vector search
clip_model = None
clip_processor = None
try:
    from transformers import CLIPProcessor, CLIPModel
    
    # ์ž„์‹œ ๋””๋ ‰ํ† ๋ฆฌ ์‚ฌ์šฉ
    import tempfile
    temp_dir = tempfile.gettempdir()
    os.environ["TRANSFORMERS_CACHE"] = temp_dir
    
    # CLIP ๋ชจ๋ธ ๋กœ๋“œ (์ด๋ฏธ์ง€ ์ž„๋ฒ ๋”ฉ์šฉ)
    clip_model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
    clip_processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
    
    print("CLIP model loaded successfully")
except Exception as e:
    print("Error loading CLIP model:", e)
    clip_model = None
    clip_processor = None

# Vector DB ์ดˆ๊ธฐํ™”
vector_db = None
image_collection = None
object_collection = None
try:
    # ChromaDB ํด๋ผ์ด์–ธํŠธ ์ดˆ๊ธฐํ™” (์ธ๋ฉ”๋ชจ๋ฆฌ DB)
    vector_db = chromadb.Client()
    
    # ์ž„๋ฒ ๋”ฉ ํ•จ์ˆ˜ ์„ค์ •
    ef = embedding_functions.DefaultEmbeddingFunction()
    
    # ์ด๋ฏธ์ง€ ์ปฌ๋ ‰์…˜ ์ƒ์„ฑ
    image_collection = vector_db.create_collection(
        name="image_collection",
        embedding_function=ef,
        get_or_create=True
    )
    
    # ๊ฐ์ฒด ์ธ์‹ ๊ฒฐ๊ณผ ์ปฌ๋ ‰์…˜ ์ƒ์„ฑ
    object_collection = vector_db.create_collection(
        name="object_collection",
        embedding_function=ef,
        get_or_create=True
    )
    
    print("Vector DB initialized successfully")
except Exception as e:
    print("Error initializing Vector DB:", e)
    vector_db = None
    image_collection = None
    object_collection = None

# YOLOv8 model
yolo_model = None
try:
    import os
    from ultralytics import YOLO
    
    # ๋ชจ๋ธ ํŒŒ์ผ ๊ฒฝ๋กœ - ์ž„์‹œ ๋””๋ ‰ํ† ๋ฆฌ ์‚ฌ์šฉ
    import tempfile
    temp_dir = tempfile.gettempdir()
    model_path = os.path.join(temp_dir, "yolov8n.pt")
    
    # ๋ชจ๋ธ ํŒŒ์ผ์ด ์—†์œผ๋ฉด ์ง์ ‘ ๋‹ค์šด๋กœ๋“œ
    if not os.path.exists(model_path):
        print(f"Downloading YOLOv8 model to {model_path}...")
        try:
            os.system(f"wget -q https://ultralytics.com/assets/yolov8n.pt -O {model_path}")
            print("YOLOv8 model downloaded successfully")
        except Exception as e:
            print(f"Error downloading YOLOv8 model: {e}")
            # ๋‹ค์šด๋กœ๋“œ ์‹คํŒจ ์‹œ ๋Œ€์ฒด URL ์‹œ๋„
            try:
                os.system(f"wget -q https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8n.pt -O {model_path}")
                print("YOLOv8 model downloaded from alternative source")
            except Exception as e2:
                print(f"Error downloading from alternative source: {e2}")
                # ๋งˆ์ง€๋ง‰ ๋Œ€์•ˆ์œผ๋กœ ์ง์ ‘ ๋ชจ๋ธ URL ์‚ฌ์šฉ
                try:
                    os.system(f"curl -L https://ultralytics.com/assets/yolov8n.pt --output {model_path}")
                    print("YOLOv8 model downloaded using curl")
                except Exception as e3:
                    print(f"All download attempts failed: {e3}")
    
    # ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์„ค์ • - ์„ค์ • ํŒŒ์ผ ๊ฒฝ๋กœ ์ง€์ •
    os.environ["YOLO_CONFIG_DIR"] = temp_dir
    os.environ["MPLCONFIGDIR"] = temp_dir
    
    yolo_model = YOLO(model_path)  # Using the nano model for faster inference
    print("YOLOv8 model loaded successfully")
except Exception as e:
    print("Error loading YOLOv8 model:", e)
    yolo_model = None

# DETR model (DEtection TRansformer)
detr_processor = None
detr_model = None
try:
    from transformers import DetrImageProcessor, DetrForObjectDetection
    
    detr_processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
    detr_model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50")
    
    print("DETR model loaded successfully")
except Exception as e:
    print("Error loading DETR model:", e)
    detr_processor = None
    detr_model = None

# ViT model
vit_processor = None
vit_model = None
try:
    from transformers import ViTImageProcessor, ViTForImageClassification
    vit_processor = ViTImageProcessor.from_pretrained("google/vit-base-patch16-224")
    vit_model = ViTForImageClassification.from_pretrained("google/vit-base-patch16-224")
    print("ViT model loaded successfully")
except Exception as e:
    print("Error loading ViT model:", e)
    vit_processor = None
    vit_model = None

# Get device information
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Using device: {device}")

# llama.cpp (GGUF) support
llama_cpp = None
llama_cpp_model = None
gguf_model_path = None
try:
    import llama_cpp as llama_cpp

    def ensure_q4_gguf_model():
        """Download a TinyLlama Q4 GGUF model if not present and return the local path."""
        global gguf_model_path
        cache_dir = os.path.join(tempfile.gettempdir(), "gguf_models")
        os.makedirs(cache_dir, exist_ok=True)
        # Use a small, permissively accessible TinyLlama GGUF
        filename = "TinyLlama-1.1B-Chat-v1.0.Q4_K_M.gguf"
        gguf_model_path = os.path.join(cache_dir, filename)
        if not os.path.exists(gguf_model_path):
            try:
                url = (
                    "https://huggingface.co/TinyLlama/"
                    "TinyLlama-1.1B-Chat-v1.0-GGUF/resolve/main/"
                    + filename
                )
                print(f"[GGUF] Downloading model from {url} -> {gguf_model_path}")
                with requests.get(url, stream=True, timeout=60) as r:
                    r.raise_for_status()
                    with open(gguf_model_path, 'wb') as f:
                        for chunk in r.iter_content(chunk_size=8192):
                            if chunk:
                                f.write(chunk)
                print("[GGUF] Download complete")
            except Exception as e:
                print(f"[GGUF] Failed to download GGUF model: {e}")
                return None
        return gguf_model_path

    def get_llama_cpp_model():
        """Lazy-load llama.cpp model from local GGUF path."""
        global llama_cpp_model
        if llama_cpp_model is not None:
            return llama_cpp_model
        model_path = ensure_q4_gguf_model()
        if not model_path:
            return None
        try:
            print(f"[GGUF] Loading llama.cpp model: {model_path}")
            llama_cpp_model = llama_cpp.Llama(
                model_path=model_path,
                n_ctx=4096,
                n_threads=max(1, os.cpu_count() or 1),
                n_gpu_layers=0,  # CPU-friendly default; adjust if GPU offload available
                verbose=False,
            )
            print("[GGUF] llama.cpp model loaded")
        except Exception as e:
            print(f"[GGUF] Failed to load llama.cpp model: {e}")
            llama_cpp_model = None
        return llama_cpp_model
except Exception as _e:
    llama_cpp = None
    llama_cpp_model = None
    gguf_model_path = None

# LLM model (using an open-access model instead of Llama 4 which requires authentication)
llm_model = None
llm_tokenizer = None
try:
    from transformers import AutoModelForCausalLM, AutoTokenizer
    
    print("Loading LLM model... This may take a moment.")
    model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"  # Using TinyLlama as an open-access alternative
    
    llm_tokenizer = AutoTokenizer.from_pretrained(model_name)
    llm_model = AutoModelForCausalLM.from_pretrained(
        model_name,
        torch_dtype=torch.float16,
        # Removing options that require accelerate package
        # device_map="auto",
        # load_in_8bit=True
    ).to(device)
    print("LLM model loaded successfully")
except Exception as e:
    print(f"Error loading LLM model: {e}")
    llm_model = None
    llm_tokenizer = None

def process_llm_query(vision_results, user_query):
    """Process a query with the LLM model using vision results and user text"""
    if llm_model is None or llm_tokenizer is None:
        return {"error": "LLM model not available"}
    
    # ๊ฒฐ๊ณผ ๋ฐ์ดํ„ฐ ์š”์•ฝ (ํ† ํฐ ๊ธธ์ด ์ œํ•œ์„ ์œ„ํ•ด)
    summarized_results = []
    
    # ๊ฐ์ฒด ํƒ์ง€ ๊ฒฐ๊ณผ ์š”์•ฝ
    if isinstance(vision_results, list):
        # ์ตœ๋Œ€ 10๊ฐœ ๊ฐ์ฒด๋งŒ ํฌํ•จ
        for i, obj in enumerate(vision_results[:10]):
            if isinstance(obj, dict):
                # ํ•„์š”ํ•œ ์ •๋ณด๋งŒ ์ถ”์ถœ
                summary = {
                    "label": obj.get("label", "unknown"),
                    "confidence": obj.get("confidence", 0),
                }
                summarized_results.append(summary)
    
    # Create a prompt combining vision results and user query
    prompt = f"""You are an AI assistant analyzing image detection results. 
    Here are the objects detected in the image: {json.dumps(summarized_results, indent=2)}
    
    User question: {user_query}
    
    Please provide a detailed analysis based on the detected objects and the user's question.
    """
    
    # Tokenize and generate response
    try:
        start_time = time.time()
        
        # ํ† ํฐ ๊ธธ์ด ํ™•์ธ ๋ฐ ์ œํ•œ
        tokens = llm_tokenizer.encode(prompt)
        if len(tokens) > 1500:  # ์•ˆ์ „ ๋งˆ์ง„ ์„ค์ •
            prompt = f"""You are an AI assistant analyzing image detection results.
            The image contains {len(summarized_results)} detected objects.
            
            User question: {user_query}
            
            Please provide a general analysis based on the user's question.
            """
        
        inputs = llm_tokenizer(prompt, return_tensors="pt").to(device)
        with torch.no_grad():
            output = llm_model.generate(
                **inputs,
                max_new_tokens=512,
                temperature=0.7,
                top_p=0.9,
                do_sample=True
            )
        
        response_text = llm_tokenizer.decode(output[0], skip_special_tokens=True)
        
        # Remove the prompt from the response
        if response_text.startswith(prompt):
            response_text = response_text[len(prompt):].strip()
        
        inference_time = time.time() - start_time
        
        return {
            "response": response_text,
            "performance": {
                "inference_time": round(inference_time, 3),
                "device": "GPU" if torch.cuda.is_available() else "CPU"
            }
        }
    except Exception as e:
        return {"error": f"Error processing LLM query: {str(e)}"}

def image_to_base64(img):
    """Convert PIL Image to base64 string"""
    buffered = io.BytesIO()
    img.save(buffered, format="PNG")
    img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
    return img_str

def process_yolo(image):
    if yolo_model is None:
        return {"error": "YOLOv8 model not loaded"}
    
    # Measure inference time
    start_time = time.time()
    
    # Convert to numpy if it's a PIL image
    if isinstance(image, Image.Image):
        image_np = np.array(image)
    else:
        image_np = image
        
    # Run inference
    results = yolo_model(image_np)
    
    # Process results
    result_image = results[0].plot()
    result_image = Image.fromarray(result_image)
    
    # Get detection information
    boxes = results[0].boxes
    class_names = results[0].names
    
    # Format detection results
    detections = []
    for box in boxes:
        class_id = int(box.cls[0].item())
        class_name = class_names[class_id]
        confidence = round(box.conf[0].item(), 2)
        bbox = box.xyxy[0].tolist()
        bbox = [round(x) for x in bbox]
        detections.append({
            "class": class_name,
            "confidence": confidence,
            "bbox": bbox
        })
    
    # Calculate inference time
    inference_time = time.time() - start_time
    
    # Add inference time and device info
    device_info = "GPU" if torch.cuda.is_available() else "CPU"
    
    return {
        "image": image_to_base64(result_image),
        "detections": detections,
        "performance": {
            "inference_time": round(inference_time, 3),
            "device": device_info
        }
    }

def process_detr(image):
    if detr_model is None or detr_processor is None:
        return {"error": "DETR model not loaded"}
    
    # Measure inference time
    start_time = time.time()
    
    # Prepare image for the model
    inputs = detr_processor(images=image, return_tensors="pt")
    
    # Run inference
    with torch.no_grad():
        outputs = detr_model(**inputs)
    
    # Process results
    target_sizes = torch.tensor([image.size[::-1]])
    results = detr_processor.post_process_object_detection(
        outputs, target_sizes=target_sizes, threshold=0.9
    )[0]
    
    # Create a copy of the image to draw on
    result_image = image.copy()
    fig, ax = plt.subplots(1)
    ax.imshow(result_image)
    
    # Format detection results
    detections = []
    for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
        box = [round(i) for i in box.tolist()]
        class_name = detr_model.config.id2label[label.item()]
        confidence = round(score.item(), 2)
        
        # Draw rectangle
        rect = Rectangle((box[0], box[1]), box[2] - box[0], box[3] - box[1],
                         linewidth=2, edgecolor='r', facecolor='none')
        ax.add_patch(rect)
        
        # Add label
        plt.text(box[0], box[1], "{}: {}".format(class_name, confidence), 
                 bbox=dict(facecolor='white', alpha=0.8))
        
        detections.append({
            "class": class_name,
            "confidence": confidence,
            "bbox": box
        })
    
    # Save figure to image
    buf = io.BytesIO()
    plt.tight_layout()
    plt.axis('off')
    plt.savefig(buf, format='png', bbox_inches='tight', pad_inches=0)
    buf.seek(0)
    result_image = Image.open(buf)
    plt.close(fig)
    
    # Calculate inference time
    inference_time = time.time() - start_time
    
    # Add inference time and device info
    device_info = "GPU" if torch.cuda.is_available() else "CPU"
    
    return {
        "image": image_to_base64(result_image),
        "detections": detections,
        "performance": {
            "inference_time": round(inference_time, 3),
            "device": device_info
        }
    }

def process_vit(image):
    if vit_model is None or vit_processor is None:
        return {"error": "ViT model not loaded"}
    
    # Measure inference time
    start_time = time.time()
    
    # Prepare image for the model
    inputs = vit_processor(images=image, return_tensors="pt")
    
    # Run inference
    with torch.no_grad():
        outputs = vit_model(**inputs)
        logits = outputs.logits
    
    # Get the predicted class
    predicted_class_idx = logits.argmax(-1).item()
    prediction = vit_model.config.id2label[predicted_class_idx]
    
    # Get top 5 predictions
    probs = torch.nn.functional.softmax(logits, dim=-1)[0]
    top5_prob, top5_indices = torch.topk(probs, 5)
    
    results = []
    for i, (prob, idx) in enumerate(zip(top5_prob, top5_indices)):
        class_name = vit_model.config.id2label[idx.item()]
        results.append({
            "rank": i+1,
            "class": class_name,
            "probability": round(prob.item(), 3)
        })
    
    # Calculate inference time
    inference_time = time.time() - start_time
    
    # Add inference time and device info
    device_info = "GPU" if torch.cuda.is_available() else "CPU"
    
    return {
        "top_predictions": results,
        "performance": {
            "inference_time": round(inference_time, 3),
            "device": device_info
        }
    }

@app.route('/api/detect/yolo', methods=['POST'])
@require_auth()
def yolo_detect():
    
    if 'image' not in request.files:
        return jsonify({"error": "No image provided"}), 400
    
    file = request.files['image']
    image = Image.open(file.stream)
    
    result = process_yolo(image)
    return jsonify(result)

# --- HF CausalLM + LoRA helper caches ---
hf_tokenizers = {}
hf_base_models = {}
hf_lora_models = {}

def _preferred_dtype():
    try:
        return torch.float16 if torch.cuda.is_available() else torch.float32
    except Exception:
        return torch.float32

def load_hf_base_and_tokenizer(base_id: str, tok_id: str = None):
    """Load and cache base CausalLM and tokenizer. tok_id can point to adapter repo to use its tokenizer."""
    global hf_tokenizers, hf_base_models
    tok_key = tok_id or base_id
    if tok_key not in hf_tokenizers:
        hf_tokenizers[tok_key] = AutoTokenizer.from_pretrained(tok_key, use_fast=True)
    if base_id not in hf_base_models:
        hf_base_models[base_id] = AutoModelForCausalLM.from_pretrained(
            base_id,
            torch_dtype=_preferred_dtype(),
            use_safetensors=False,
        ).to(device)
    return hf_tokenizers[tok_key], hf_base_models[base_id]

def load_hf_lora_model(base_id: str, adapter_id: str):
    """Load and cache a PEFT-wrapped LoRA model from base + adapter. Returns model or raises if PEFT unavailable."""
    if PeftModel is None:
        raise RuntimeError("PEFT (peft) is not installed; cannot load LoRA adapter")
    key = f"{base_id}::{adapter_id}"
    if key in hf_lora_models:
        return hf_lora_models[key]
    # Create a fresh base to avoid mutating the cached base used for non-LoRA runs
    base = AutoModelForCausalLM.from_pretrained(
        base_id,
        torch_dtype=_preferred_dtype(),
        use_safetensors=False,
    ).to(device)
    lora_model = PeftModel.from_pretrained(base, adapter_id).eval().to(device)
    hf_lora_models[key] = lora_model
    return lora_model

@app.route('/api/detect/detr', methods=['POST'])
@require_auth()
def detr_detect():
    if 'image' not in request.files:
        return jsonify({"error": "No image provided"}), 400
    
    file = request.files['image']
    image = Image.open(file.stream)
    
    result = process_detr(image)
    return jsonify(result)

@app.route('/api/classify/vit', methods=['POST'])
@require_auth()
def vit_classify():
    if 'image' not in request.files:
        return jsonify({"error": "No image provided"}), 400
    
    file = request.files['image']
    image = Image.open(file.stream)
    
    result = process_vit(image)
    return jsonify(result)

@app.route('/api/analyze', methods=['POST'])
@require_auth()
def analyze_with_llm():
    # Check if required data is in the request
    if not request.json:
        return jsonify({"error": "No JSON data provided"}), 400
    
    # Extract vision results and user query from request
    data = request.json
    if 'visionResults' not in data or 'userQuery' not in data:
        return jsonify({"error": "Missing required fields: visionResults or userQuery"}), 400
    
    vision_results = data['visionResults']
    user_query = data['userQuery']

    # Process the query with LLM
    result = process_llm_query(vision_results, user_query)

    return jsonify(result)

def generate_image_embedding(image):
    """CLIP ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฏธ์ง€ ์ž„๋ฒ ๋”ฉ ์ƒ์„ฑ"""
    if clip_model is None or clip_processor is None:
        return None

    try:
        # ์ด๋ฏธ์ง€ ์ „์ฒ˜๋ฆฌ
        inputs = clip_processor(images=image, return_tensors="pt")

        # ์ด๋ฏธ์ง€ ์ž„๋ฒ ๋”ฉ ์ƒ์„ฑ
        with torch.no_grad():
            image_features = clip_model.get_image_features(**inputs)

        # ์ž„๋ฒ ๋”ฉ ์ •๊ทœํ™” ๋ฐ numpy ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜
        image_embedding = image_features.squeeze().cpu().numpy()
        normalized_embedding = image_embedding / np.linalg.norm(image_embedding)

        return normalized_embedding.tolist()
    except Exception as e:
        print(f"Error generating image embedding: {e}")
        return None

@app.route('/api/similar-images', methods=['POST'])
@require_auth()
def find_similar_images():
    """์œ ์‚ฌ ์ด๋ฏธ์ง€ ๊ฒ€์ƒ‰ API"""
    if clip_model is None or clip_processor is None or image_collection is None:
        return jsonify({"error": "Image embedding model or vector DB not available"})

    try:
        # ์š”์ฒญ์—์„œ ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ ์ถ”์ถœ
        if 'image' not in request.files and 'image' not in request.form:
            return jsonify({"error": "No image provided"})

        if 'image' in request.files:
            # ํŒŒ์ผ๋กœ ์—…๋กœ๋“œ๋œ ๊ฒฝ์šฐ
            image_file = request.files['image']
            image = Image.open(image_file).convert('RGB')
        else:
            # base64๋กœ ์ธ์ฝ”๋”ฉ๋œ ๊ฒฝ์šฐ
            image_data = request.form['image']
            if image_data.startswith('data:image'):
                # Remove the data URL prefix if present
                image_data = image_data.split(',')[1]
            image = Image.open(BytesIO(base64.b64decode(image_data))).convert('RGB')

        # ์ด๋ฏธ์ง€ ID ์ƒ์„ฑ (์ž„์‹œ)
        image_id = str(uuid.uuid4())

        # ์ด๋ฏธ์ง€ ์ž„๋ฒ ๋”ฉ ์ƒ์„ฑ
        embedding = generate_image_embedding(image)
        if embedding is None:
            return jsonify({"error": "Failed to generate image embedding"})

        # ํ˜„์žฌ ์ด๋ฏธ์ง€๋ฅผ DB์— ์ถ”๊ฐ€ (์„ ํƒ์ )
        # image_collection.add(
        #    ids=[image_id],
        #    embeddings=[embedding]
        # )

        # ์œ ์‚ฌ ์ด๋ฏธ์ง€ ๊ฒ€์ƒ‰
        results = image_collection.query(
            query_embeddings=[embedding],
            n_results=5  # ์ƒ์œ„ 5๊ฐœ ๊ฒฐ๊ณผ ๋ฐ˜ํ™˜
        )

        # ๊ฒฐ๊ณผ ํฌ๋งทํŒ…
        similar_images = []
        if len(results['ids'][0]) > 0:
            for i, img_id in enumerate(results['ids'][0]):
                similar_images.append({
                    "id": img_id,
                    "distance": float(results['distances'][0][i]) if 'distances' in results else 0.0,
                    "metadata": results['metadatas'][0][i] if 'metadatas' in results else {}
                })

        return jsonify({
            "query_image_id": image_id,
            "similar_images": similar_images
        })

    except Exception as e:
        print(f"Error in similar-images API: {e}")
        return jsonify({"error": str(e)}), 500

@app.route('/api/add-to-collection', methods=['POST'])
@require_auth()
def add_to_collection():
    """์ด๋ฏธ์ง€๋ฅผ ๋ฒกํ„ฐ DB์— ์ถ”๊ฐ€ํ•˜๋Š” API"""
    if clip_model is None or clip_processor is None or image_collection is None:
        return jsonify({"error": "Image embedding model or vector DB not available"})

    try:
        # ์š”์ฒญ์—์„œ ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ ์ถ”์ถœ
        if 'image' not in request.files and 'image' not in request.form:
            return jsonify({"error": "No image provided"})

        # ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ์ถ”์ถœ
        metadata = {}
        if 'metadata' in request.form:
            metadata = json.loads(request.form['metadata'])

        # ์ด๋ฏธ์ง€ ID (์ œ๊ณต๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ ์ž๋™ ์ƒ์„ฑ)
        image_id = request.form.get('id', str(uuid.uuid4()))

        if 'image' in request.files:
            # ํŒŒ์ผ๋กœ ์—…๋กœ๋“œ๋œ ๊ฒฝ์šฐ
            image_file = request.files['image']
            image = Image.open(image_file).convert('RGB')
        else:
            # base64๋กœ ์ธ์ฝ”๋”ฉ๋œ ๊ฒฝ์šฐ
            image_data = request.form['image']
            if image_data.startswith('data:image'):
                # Remove the data URL prefix if present
                image_data = image_data.split(',')[1]
            image = Image.open(BytesIO(base64.b64decode(image_data))).convert('RGB')

        # ์ด๋ฏธ์ง€ ์ž„๋ฒ ๋”ฉ ์ƒ์„ฑ
        embedding = generate_image_embedding(image)
        if embedding is None:
            return jsonify({"error": "Failed to generate image embedding"})
            
        # ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ๋ฅผ base64๋กœ ์ธ์ฝ”๋”ฉํ•˜์—ฌ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ์— ์ถ”๊ฐ€
        buffered = BytesIO()
        image.save(buffered, format="JPEG")
        img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
        metadata['image_data'] = img_str

        # ์ด๋ฏธ์ง€๋ฅผ DB์— ์ถ”๊ฐ€
        image_collection.add(
            ids=[image_id],
            embeddings=[embedding],
            metadatas=[metadata]
        )

        return jsonify({
            "success": True,
            "image_id": image_id,
            "message": "Image added to collection"
        })

    except Exception as e:
        print(f"Error in add-to-collection API: {e}")
        return jsonify({"error": str(e)}), 500

@app.route('/api/add-detected-objects', methods=['POST'])
@require_auth()
def add_detected_objects():
    """๊ฐ์ฒด ์ธ์‹ ๊ฒฐ๊ณผ๋ฅผ ๋ฒกํ„ฐ DB์— ์ถ”๊ฐ€ํ•˜๋Š” API"""
    if clip_model is None or object_collection is None:
        return jsonify({"error": "Image embedding model or vector DB not available"})

    try:
        # ๋””๋ฒ„๊น…: ์š”์ฒญ ๋ฐ์ดํ„ฐ ๋กœ๊น…
        print("[DEBUG] Received request in add-detected-objects")
        
        # ์š”์ฒญ์—์„œ ์ด๋ฏธ์ง€์™€ ๊ฐ์ฒด ๊ฒ€์ถœ ๊ฒฐ๊ณผ ๋ฐ์ดํ„ฐ ์ถ”์ถœ
        data = request.json
        print(f"[DEBUG] Request data keys: {list(data.keys()) if data else 'None'}")
        
        if not data:
            print("[DEBUG] Error: No data received in request")
            return jsonify({"error": "No data received"})
            
        if 'image' not in data:
            print("[DEBUG] Error: 'image' key not found in request data")
            return jsonify({"error": "Missing image data"})
            
        if 'objects' not in data:
            print("[DEBUG] Error: 'objects' key not found in request data")
            return jsonify({"error": "Missing objects data"})
        
        # ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ ๋””๋ฒ„๊น…
        print(f"[DEBUG] Image data type: {type(data['image'])}")
        print(f"[DEBUG] Image data starts with: {data['image'][:50]}...") # ์ฒ˜์Œ 50์ž๋งŒ ์ถœ๋ ฅ
        
        # ๊ฐ์ฒด ๋ฐ์ดํ„ฐ ๋””๋ฒ„๊น…
        print(f"[DEBUG] Objects data type: {type(data['objects'])}")
        print(f"[DEBUG] Objects count: {len(data['objects']) if isinstance(data['objects'], list) else 'Not a list'}")
        if isinstance(data['objects'], list) and len(data['objects']) > 0:
            print(f"[DEBUG] First object keys: {list(data['objects'][0].keys()) if isinstance(data['objects'][0], dict) else 'Not a dict'}")
        
        # ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ
        image_data = data['image']
        if image_data.startswith('data:image'):
            image_data = image_data.split(',')[1]
        
        image = Image.open(BytesIO(base64.b64decode(image_data))).convert('RGB')
        image_width, image_height = image.size
        
        # ์ด๋ฏธ์ง€ ID
        image_id = data.get('imageId', str(uuid.uuid4()))
        
        # ๊ฐ์ฒด ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ
        objects = data['objects']
        object_ids = []
        object_embeddings = []
        object_metadatas = []
        
        for obj in objects:
            # ๊ฐ์ฒด ID ์ƒ์„ฑ
            object_id = f"{image_id}_{str(uuid.uuid4())[:8]}"
            
            # ๋ฐ”์šด๋”ฉ ๋ฐ•์Šค ์ •๋ณด ์ถ”์ถœ
            bbox = obj.get('bbox', [])
            
            # ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ์˜ bbox [x1, y1, x2, y2] ์ฒ˜๋ฆฌ
            if isinstance(bbox, list) and len(bbox) >= 4:
                x1 = bbox[0] / image_width  # ์ •๊ทœํ™”๋œ ์ขŒํ‘œ๋กœ ๋ณ€ํ™˜
                y1 = bbox[1] / image_height
                x2 = bbox[2] / image_width
                y2 = bbox[3] / image_height
                width = x2 - x1
                height = y2 - y1
            # ๋”•์…”๋„ˆ๋ฆฌ ํ˜•ํƒœ์˜ bbox {'x': x, 'y': y, 'width': width, 'height': height} ์ฒ˜๋ฆฌ
            elif isinstance(bbox, dict):
                x1 = bbox.get('x', 0)
                y1 = bbox.get('y', 0)
                width = bbox.get('width', 0)
                height = bbox.get('height', 0)
            else:
                # ๊ธฐ๋ณธ๊ฐ’ ์„ค์ •
                x1, y1, width, height = 0, 0, 0, 0
            
            # ๋ฐ”์šด๋”ฉ ๋ฐ•์Šค๋ฅผ ์ด๋ฏธ์ง€ ์ขŒํ‘œ๋กœ ๋ณ€ํ™˜
            x1_px = int(x1 * image_width)
            y1_px = int(y1 * image_height)
            width_px = int(width * image_width)
            height_px = int(height * image_height)
            
            # ๊ฐ์ฒด ์ด๋ฏธ์ง€ ์ž๋ฅด๊ธฐ
            try:
                object_image = image.crop((x1_px, y1_px, x1_px + width_px, y1_px + height_px))
                
                # ์ž„๋ฒ ๋”ฉ ์ƒ์„ฑ
                embedding = generate_image_embedding(object_image)
                if embedding is None:
                    continue
                
                # ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ๊ตฌ์„ฑ
                # bbox๋ฅผ JSON ๋ฌธ์ž์—ด๋กœ ์ง๋ ฌํ™”ํ•˜์—ฌ ChromaDB ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ์ œํ•œ ์šฐํšŒ
                bbox_json = json.dumps({
                    "x": x1,
                    "y": y1,
                    "width": width,
                    "height": height
                })
                
                # ๊ฐ์ฒด ์ด๋ฏธ์ง€๋ฅผ base64๋กœ ์ธ์ฝ”๋”ฉ
                buffered = BytesIO()
                object_image.save(buffered, format="JPEG")
                img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
                
                metadata = {
                    "image_id": image_id,
                    "class": obj.get('class', ''),
                    "confidence": obj.get('confidence', 0),
                    "bbox": bbox_json,  # JSON ๋ฌธ์ž์—ด๋กœ ์ €์žฅ
                    "image_data": img_str  # ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€
                }
                
                object_ids.append(object_id)
                object_embeddings.append(embedding)
                object_metadatas.append(metadata)
            except Exception as e:
                print(f"Error processing object: {e}")
                continue
        
        # Add to vector DB
        if object_ids and object_embeddings and object_metadatas:
            object_collection.add(
                ids=object_ids,
                embeddings=object_embeddings,
                metadatas=object_metadatas
            )
            
            return jsonify({
                "success": True,
                "message": f"Added {len(object_ids)} objects to collection",
                "object_ids": object_ids
            })
        else:
            return jsonify({
                "warning": "No valid objects to add"
            })
            
    except Exception as e:
        print(f"Error in add-detected-objects API: {e}")
        return jsonify({"error": str(e)}), 500


# Product Comparison API Endpoints


@app.route('/api/product/compare/start', methods=['POST'])
@require_auth()
def start_product_comparison():
    """Start a new product comparison session"""
    print(f"[DEBUG] ๐Ÿš€ start_product_comparison endpoint called")
    print(f"[DEBUG] ๐Ÿ” get_product_comparison_coordinator: {get_product_comparison_coordinator}")
    
    if get_product_comparison_coordinator is None:
        print(f"[DEBUG] โŒ Product comparison coordinator is None - returning 500")
        # Try to import again and show the error
        print(f"[DEBUG] ๐Ÿ”„ Attempting emergency import test...")
        try:
            from product_comparison import get_product_comparison_coordinator as test_coordinator
            print(f"[DEBUG] ๐ŸŽฏ Emergency import succeeded: {test_coordinator}")
        except Exception as e:
            print(f"[DEBUG] โŒ Emergency import failed: {e}")
            import traceback
            traceback.print_exc()
        return jsonify({"error": "Product comparison module not available"}), 500
    
    try:
        print(f"[DEBUG] ๐Ÿ“ Processing request data...")
        # Generate session ID if provided in form or query params, otherwise create new one
        session_id = request.form.get('session_id') or request.args.get('session_id') or str(uuid.uuid4())
        print(f"[DEBUG] ๐Ÿ†” Session ID: {session_id}")
        
        # Get analysis type if provided (info, compare, value, recommend)
        analysis_type = request.form.get('analysisType') or request.args.get('analysisType', 'info')
        print(f"[DEBUG] ๐Ÿ“Š Analysis type: {analysis_type}")
        
        # Process images from FormData or JSON
        images = []
        print(f"[DEBUG] ๐Ÿ–ผ๏ธ Processing images...")
        print(f"[DEBUG] ๐Ÿ“‹ Request files: {list(request.files.keys())}")
        print(f"[DEBUG] ๐Ÿ“‹ Request form: {dict(request.form)}")
        
        # Check if request is multipart form data
        if request.files:
            print(f"[DEBUG] ๐Ÿ“ Processing multipart form data...")
            # Handle FormData with file uploads (from frontend)
            if 'image1' in request.files and request.files['image1']:
                img1 = request.files['image1']
                print(f"[DEBUG] ๐Ÿ–ผ๏ธ Processing image1: {img1.filename}")
                try:
                    # Load image and convert to RGB to ensure it's fully loaded in memory
                    image = Image.open(img1.stream).convert('RGB')
                    images.append(image)
                    print(f"[DEBUG] โœ… Image1 processed successfully")
                except Exception as e:
                    print(f"[DEBUG] โŒ Error processing image1: {e}")
                    
            if 'image2' in request.files and request.files['image2']:
                img2 = request.files['image2']
                print(f"[DEBUG] ๐Ÿ–ผ๏ธ Processing image2: {img2.filename}")
                try:
                    # Load image and convert to RGB to ensure it's fully loaded in memory
                    image = Image.open(img2.stream).convert('RGB')
                    images.append(image)
                    print(f"[DEBUG] โœ… Image2 processed successfully")
                except Exception as e:
                    print(f"[DEBUG] โŒ Error processing image2: {e}")
                    
        # Fallback to JSON with base64 images (for API testing)
        elif request.json and 'images' in request.json:
            print(f"[DEBUG] ๐Ÿ“‹ Processing JSON with base64 images...")
            image_data_list = request.json.get('images', [])
            for i, image_data in enumerate(image_data_list):
                print(f"[DEBUG] ๐Ÿ–ผ๏ธ Processing base64 image {i+1}")
                img = decode_base64_image(image_data)
                if img is not None:
                    images.append(img)
                    print(f"[DEBUG] โœ… Base64 image {i+1} processed successfully")
                else:
                    print(f"[DEBUG] โŒ Failed to decode base64 image {i+1}")
                    
        print(f"[DEBUG] ๐Ÿ“Š Total images processed: {len(images)}")
        if not images:
            print(f"[DEBUG] โŒ No valid images provided - returning 400")
            return jsonify({"error": "No valid images provided"}), 400
        
        # Get coordinator instance
        print(f"[DEBUG] ๐ŸŽฏ Getting coordinator instance...")
        coordinator = get_product_comparison_coordinator()
        print(f"[DEBUG] โœ… Coordinator obtained: {type(coordinator).__name__}")
        
        # Pass the analysis type and session metadata to the coordinator
        session_metadata = {
            'analysis_type': analysis_type,
            'timestamp': time.strftime('%Y-%m-%d %H:%M:%S')
        }
        print(f"[DEBUG] ๐Ÿ“‹ Session metadata: {session_metadata}")
        
        # Start processing in a background thread
        print(f"[DEBUG] ๐Ÿงต Starting background processing thread...")
        def run_async_task(loop):
            try:
                print(f"[DEBUG] ๐Ÿ”„ Setting event loop and starting coordinator.process_images...")
                asyncio.set_event_loop(loop)
                loop.run_until_complete(coordinator.process_images(session_id, images, session_metadata))
                print(f"[DEBUG] โœ… coordinator.process_images completed successfully")
            except Exception as e:
                print(f"[DEBUG] โŒ Error in async task: {e}")
                import traceback
                traceback.print_exc()
        
        loop = asyncio.new_event_loop()
        thread = Thread(target=run_async_task, args=(loop,))
        thread.daemon = True
        thread.start()
        print(f"[DEBUG] ๐Ÿš€ Background thread started")
        
        # Return session ID for client to use with streaming endpoint
        response_data = {
            "session_id": session_id,
            "message": "Product comparison started",
            "status": "processing"
        }
        print(f"[DEBUG] โœ… Returning success response: {response_data}")
        return jsonify(response_data)
        
    except Exception as e:
        print(f"[DEBUG] โŒ Exception in start_product_comparison: {e}")
        print(f"[DEBUG] โŒ Exception type: {type(e).__name__}")
        import traceback
        print(f"[DEBUG] โŒ Full traceback:")
        traceback.print_exc()
        return jsonify({"error": str(e)}), 500


@app.route('/api/product/compare/stream/<session_id>', methods=['GET'])
@require_auth()
def stream_product_comparison(session_id):
    """Stream updates from a product comparison session"""
    if get_product_comparison_coordinator is None:
        return jsonify({"error": "Product comparison module not available"}), 500
    
    def generate():
        """Generate SSE events for streaming"""
        print(f"[DEBUG] ๐ŸŒŠ Starting SSE stream for session: {session_id}")
        coordinator = get_product_comparison_coordinator()
        last_message_index = 0
        retry_count = 0
        max_retries = 300  # 5 minutes at 1 second intervals
        
        while retry_count < max_retries:
            # Get current status
            status = coordinator.get_session_status(session_id)
            print(f"[DEBUG] ๐Ÿ“Š Session {session_id} status: {status}")
            
            if status is None:
                # Session not found
                print(f"[DEBUG] โŒ Session {session_id} not found")
                yield f"data: {json.dumps({'error': 'Session not found'})}\n\n"
                break
            
            # Get all messages
            messages = coordinator.get_session_messages(session_id)
            print(f"[DEBUG] ๐Ÿ“ Session {session_id} has {len(messages) if messages else 0} messages")
            
            # Send any new messages
            if messages and len(messages) > last_message_index:
                new_messages = messages[last_message_index:]
                print(f"[DEBUG] ๐Ÿ“ค Sending {len(new_messages)} new messages")
                for msg in new_messages:
                    print(f"[DEBUG] ๐Ÿ“จ Message: {msg}")
                    # msg is already a dict with message, agent, timestamp
                    yield f"data: {json.dumps(msg)}\n\n"
                last_message_index = len(messages)
            
            # Send current status
            yield f"data: {json.dumps({'status': status})}\n\n"
            
            # If completed or error, send final result and end stream
            if status in ['completed', 'error']:
                result = coordinator.get_session_result(session_id)
                print(f"[DEBUG] ๐Ÿ Session {session_id} finished with status: {status}")
                print(f"[DEBUG] ๐ŸŽฏ Final result: {result}")
                yield f"data: {json.dumps({'final_result': result})}\n\n"
                break
            
            # Wait before next update
            time.sleep(1)
            retry_count += 1
        
        # End the stream if we've reached max retries
        if retry_count >= max_retries:
            print(f"[DEBUG] โฐ Session {session_id} timed out after {max_retries} retries")
            yield f"data: {json.dumps({'error': 'Timeout waiting for results'})}\n\n"
    
    return Response(
        stream_with_context(generate()),
        mimetype="text/event-stream",
        headers={
            'Cache-Control': 'no-cache',
            'X-Accel-Buffering': 'no',
            'Content-Type': 'text/event-stream',
        }
    )

# ============================
# LLM LoRA Compare Endpoints
# ============================

# Simple in-memory session store for LoRA compare
lora_sessions = {}

def lora_add_message(session_id, message, msg_type="info"):
    sess = lora_sessions.get(session_id)
    if not sess:
        return
    ts = time.strftime('%Y-%m-%d %H:%M:%S')
    sess['messages'].append({
        'message': message,
        'type': msg_type,
        'timestamp': ts
    })

@app.route('/api/llama/compare/start', methods=['POST'])
@require_auth()
def start_llama_lora_compare():
    """Start a LoRA-vs-Base comparison session (text or image+text prompt)."""
    session_id = request.form.get('session_id') or str(uuid.uuid4())
    prompt = request.form.get('prompt', '')
    # Default to local GGUF TinyLlama Q4 model via llama.cpp
    base_model_id = request.form.get('baseModel', 'gguf:tinyllama-q4km')
    lora_path = request.form.get('loraPath', '')
    image_b64 = None
    if 'image' in request.files:
        try:
            img = Image.open(request.files['image'].stream).convert('RGB')
            buffer = BytesIO()
            img.save(buffer, format='PNG')
            image_b64 = base64.b64encode(buffer.getvalue()).decode('utf-8')
        except Exception as _e:
            pass

    # Initialize session
    lora_sessions[session_id] = {
        'status': 'processing',
        'messages': [],
        'result': None,
    }
    lora_add_message(session_id, 'LoRA comparison started', 'system')

    def worker():
        try:
            lora_add_message(session_id, f"Base model: {base_model_id}")
            if lora_path:
                lora_add_message(session_id, f"LoRA adapter: {lora_path}")
            else:
                lora_add_message(session_id, "No LoRA adapter provided; using mock output.")

            # Prepare prompt
            full_prompt = prompt or 'Describe the content.'
            if image_b64:
                lora_add_message(session_id, 'Image provided; running vision+language prompt.')

            # Run base inference (best-effort)
            start_base = time.time()
            base_output = None
            try:
                # If base_model_id indicates GGUF, use llama.cpp
                if base_model_id.startswith('gguf:') and llama_cpp is not None:
                    model = get_llama_cpp_model()
                    if model is None:
                        raise RuntimeError('GGUF model unavailable')
                    # Simple chat-style prompt
                    prompt_text = f"You are a helpful assistant.\nUser: {full_prompt}\nAssistant:"
                    res = model(
                        prompt=prompt_text,
                        max_tokens=128,
                        temperature=0.7,
                        top_p=0.9,
                        stop=["User:", "\n\n"],
                    )
                    text = res.get('choices', [{}])[0].get('text', '').strip()
                    # Ensure we don't return an empty base output
                    base_output = text or "[empty]"
                elif '/' in base_model_id:
                    # HF Transformers path (e.g., openlm-research/open_llama_3b)
                    tok_id = lora_path or base_model_id  # user expects tokenizer from adapter repo when provided
                    tokenizer_hf, base_model_hf = load_hf_base_and_tokenizer(base_model_id, tok_id)
                    inputs = tokenizer_hf(full_prompt, return_tensors='pt').to(device)
                    with torch.no_grad():
                        out = base_model_hf.generate(**inputs, max_new_tokens=128, temperature=0.7, top_p=0.9)
                    text = tokenizer_hf.decode(out[0], skip_special_tokens=True)
                    if text.startswith(full_prompt):
                        stripped = text[len(full_prompt):].strip()
                        text = stripped if stripped else text
                    base_output = text or "[empty]"
                elif llm_model is not None and llm_tokenizer is not None:
                    inputs = llm_tokenizer(full_prompt, return_tensors='pt').to(device)
                    with torch.no_grad():
                        out = llm_model.generate(**inputs, max_new_tokens=128, temperature=0.7, top_p=0.9)
                    text = llm_tokenizer.decode(out[0], skip_special_tokens=True)
                    if text.startswith(full_prompt):
                        stripped = text[len(full_prompt):].strip()
                        text = stripped if stripped else text
                    base_output = text or "[empty]"
                else:
                    base_output = f"[mock] Base response for: {full_prompt[:80]}..."
            except Exception as e:
                base_output = f"[error] Base inference failed: {e}"
            base_latency = int((time.time() - start_base) * 1000)
            lora_add_message(session_id, f"Base inference done in {base_latency} ms")

            # Run LoRA inference (prefer HF+PEFT when adapter provided)
            start_lora = time.time()
            try:
                if lora_path and '/' in base_model_id:
                    tokenizer_hf, _ = load_hf_base_and_tokenizer(base_model_id, lora_path)
                    lora_model = load_hf_lora_model(base_model_id, lora_path)
                    inputs = tokenizer_hf(full_prompt, return_tensors='pt').to(device)
                    with torch.no_grad():
                        out = lora_model.generate(**inputs, max_new_tokens=128, temperature=0.7, top_p=0.9)
                    text = tokenizer_hf.decode(out[0], skip_special_tokens=True)
                    if text.startswith(full_prompt):
                        stripped = text[len(full_prompt):].strip()
                        text = stripped if stripped else text
                    lora_output = text or "[empty]"
                else:
                    lora_output = f"[mock] LoRA response (no adapter) for: {full_prompt[:80]}..."
            except Exception as e:
                lora_output = f"[error] LoRA inference failed: {e}"
            lora_latency = int((time.time() - start_lora) * 1000)
            lora_add_message(session_id, f"LoRA inference done in {lora_latency} ms")

            lora_sessions[session_id]['result'] = {
                'prompt': full_prompt,
                'image': image_b64,
                'base': { 'output': base_output, 'latency_ms': base_latency },
                'lora': { 'output': lora_output, 'latency_ms': lora_latency },
            }
            lora_sessions[session_id]['status'] = 'completed'
            lora_add_message(session_id, 'Comparison completed', 'system')
        except Exception as e:
            lora_sessions[session_id]['status'] = 'error'
            lora_sessions[session_id]['result'] = {
                'error': str(e)
            }
            lora_add_message(session_id, f"Error: {e}", 'error')

    Thread(target=worker, daemon=True).start()
    return jsonify({ 'session_id': session_id, 'status': 'processing' })


@app.route('/api/llama/compare/stream/<session_id>', methods=['GET'])
@require_auth()
def stream_llama_lora_compare(session_id):
    """SSE stream for LoRA comparison progress and final result."""
    def generate():
        last_idx = 0
        retries = 0
        max_retries = 300
        while retries < max_retries:
            sess = lora_sessions.get(session_id)
            if not sess:
                yield f"data: {json.dumps({'error': 'Session not found'})}\n\n"
                break
            msgs = sess['messages']
            if len(msgs) > last_idx:
                for m in msgs[last_idx:]:
                    yield f"data: {json.dumps(m)}\n\n"
                last_idx = len(msgs)
            yield f"data: {json.dumps({'status': sess['status']})}\n\n"
            if sess['status'] in ('completed', 'error'):
                yield f"data: {json.dumps({'final_result': sess['result']})}\n\n"
                break
            time.sleep(1)
            retries += 1
        if retries >= max_retries:
            yield f"data: {json.dumps({'error': 'Timeout waiting for results'})}\n\n"

    return Response(
        stream_with_context(generate()),
        mimetype='text/event-stream',
        headers={
            'Cache-Control': 'no-cache',
            'X-Accel-Buffering': 'no',
            'Content-Type': 'text/event-stream',
        }
    )

@app.route('/api/search-similar-objects', methods=['POST'])
@require_auth()
def search_similar_objects():
    """์œ ์‚ฌํ•œ ๊ฐ์ฒด ๊ฒ€์ƒ‰ API"""
    print("[DEBUG] Received request in search-similar-objects")
    
    if clip_model is None or object_collection is None:
        print("[DEBUG] Error: Image embedding model or vector DB not available")
        return jsonify({"error": "Image embedding model or vector DB not available"})

    try:
        # ์š”์ฒญ ๋ฐ์ดํ„ฐ ์ถ”์ถœ
        data = request.json
        print(f"[DEBUG] Request data keys: {list(data.keys()) if data else 'None'}")
        
        if not data:
            print("[DEBUG] Error: Missing request data")
            return jsonify({"error": "Missing request data"})
        
        # ๊ฒ€์ƒ‰ ์œ ํ˜• ๊ฒฐ์ •
        search_type = data.get('searchType', 'image')
        n_results = int(data.get('n_results', 5))  # ๊ฒฐ๊ณผ ๊ฐœ์ˆ˜
        print(f"[DEBUG] Search type: {search_type}, n_results: {n_results}")
        
        query_embedding = None
        
        if search_type == 'image' and 'image' in data:
            # ์ด๋ฏธ์ง€๋กœ ๊ฒ€์ƒ‰ํ•˜๋Š” ๊ฒฝ์šฐ
            print("[DEBUG] Searching by image")
            image_data = data['image']
            if image_data.startswith('data:image'):
                image_data = image_data.split(',')[1]
            
            try:
                image = Image.open(BytesIO(base64.b64decode(image_data))).convert('RGB')
                query_embedding = generate_image_embedding(image)
                print(f"[DEBUG] Generated image embedding: {type(query_embedding)}, shape: {len(query_embedding) if query_embedding is not None else 'None'}")
            except Exception as e:
                print(f"[DEBUG] Error generating image embedding: {e}")
                return jsonify({"error": f"Error processing image: {str(e)}"}), 500
        
        elif search_type == 'object' and 'objectId' in data:
            # ๊ฐ์ฒด ID๋กœ ๊ฒ€์ƒ‰ํ•˜๋Š” ๊ฒฝ์šฐ
            object_id = data['objectId']
            result = object_collection.get(ids=[object_id], include=["embeddings"])
            
            if result and "embeddings" in result and len(result["embeddings"]) > 0:
                query_embedding = result["embeddings"][0]
        
        elif search_type == 'class' and 'class_name' in data:
            # ํด๋ž˜์Šค ์ด๋ฆ„์œผ๋กœ ๊ฒ€์ƒ‰ํ•˜๋Š” ๊ฒฝ์šฐ
            print("[DEBUG] Searching by class name")
            class_name = data['class_name']
            print(f"[DEBUG] Class name: {class_name}")
            filter_query = {"class": {"$eq": class_name}}
            
            try:
                # ํด๋ž˜์Šค๋กœ ํ•„ํ„ฐ๋งํ•˜์—ฌ ๊ฒ€์ƒ‰
                print(f"[DEBUG] Querying with filter: {filter_query}")
                # Use get method instead of query for class-based filtering
                results = object_collection.get(
                    where=filter_query,
                    limit=n_results,
                    include=["metadatas", "embeddings", "documents"]
                )
                
                print(f"[DEBUG] Query results: {results['ids'][0] if 'ids' in results and len(results['ids']) > 0 else 'No results'}")
                formatted_results = format_object_results(results)
                print(f"[DEBUG] Formatted results count: {len(formatted_results)}")
                
                return jsonify({
                    "success": True,
                    "searchType": "class",
                    "results": formatted_results
                })
            except Exception as e:
                print(f"[DEBUG] Error in class search: {e}")
                return jsonify({"error": f"Error in class search: {str(e)}"}), 500
        
        else:
            print(f"[DEBUG] Invalid search parameters: {data}")
            return jsonify({"error": "Invalid search parameters"})
        
        if query_embedding is None:
            print("[DEBUG] Error: Failed to generate query embedding")
            return jsonify({"error": "Failed to generate query embedding"})
        
        try:
            # ์œ ์‚ฌ๋„ ๊ฒ€์ƒ‰ ์‹คํ–‰
            print(f"[DEBUG] Running similarity search with embedding of length {len(query_embedding)}")
            results = object_collection.query(
                query_embeddings=[query_embedding],
                n_results=n_results,
                include=["metadatas", "distances"]
            )
            
            print(f"[DEBUG] Query results: {results['ids'][0] if 'ids' in results and len(results['ids']) > 0 else 'No results'}")
            formatted_results = format_object_results(results)
            print(f"[DEBUG] Formatted results count: {len(formatted_results)}")
            
            return jsonify({
                "success": True,
                "searchType": search_type,
                "results": formatted_results
            })
        except Exception as e:
            print(f"[DEBUG] Error in similarity search: {e}")
            return jsonify({"error": f"Error in similarity search: {str(e)}"}), 500
    
    except Exception as e:
        print(f"Error in search-similar-objects API: {e}")
        return jsonify({"error": str(e)}), 500

def format_object_results(results):
    """๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ ํฌ๋งทํŒ… - ChromaDB query ๋ฐ get ๋ฉ”์„œ๋“œ ๊ฒฐ๊ณผ ๋ชจ๋‘ ์ฒ˜๋ฆฌ"""
    formatted_results = []
    
    print(f"[DEBUG] Formatting results: {results.keys() if results else 'None'}")
    
    if not results:
        print("[DEBUG] No results to format")
        return formatted_results
    
    try:
        # Check if this is a result from 'get' method (class search) or 'query' method (similarity search)
        is_get_result = 'ids' in results and isinstance(results['ids'], list) and not isinstance(results['ids'][0], list) if 'ids' in results else False
        
        if is_get_result:
            # Handle results from 'get' method (flat structure)
            print("[DEBUG] Processing results from get method (class search)")
            if len(results['ids']) == 0:
                return formatted_results
                
            for i, obj_id in enumerate(results['ids']):
                try:
                    # Extract object info
                    metadata = results['metadatas'][i] if 'metadatas' in results else {}
                    
                    # Deserialize bbox if stored as JSON string
                    if 'bbox' in metadata and isinstance(metadata['bbox'], str):
                        try:
                            metadata['bbox'] = json.loads(metadata['bbox'])
                        except:
                            pass  # Keep as is if deserialization fails
                    
                    result_item = {
                        "id": obj_id,
                        "metadata": metadata
                    }
                    
                    # No distance in get results
                    
                    # Check if image data is already in metadata
                    if 'image_data' not in metadata:
                        print(f"[DEBUG] Image data not found in metadata for object {obj_id}")
                    else:
                        print(f"[DEBUG] Image data found in metadata for object {obj_id}")
                    
                    formatted_results.append(result_item)
                except Exception as e:
                    print(f"[DEBUG] Error formatting get result {i}: {e}")
        else:
            # Handle results from 'query' method (nested structure)
            print("[DEBUG] Processing results from query method (similarity search)")
            if 'ids' not in results or len(results['ids']) == 0 or len(results['ids'][0]) == 0:
                return formatted_results
                
            for i, obj_id in enumerate(results['ids'][0]):
                try:
                    # Extract object info
                    metadata = results['metadatas'][0][i] if 'metadatas' in results and len(results['metadatas']) > 0 else {}
                    
                    # Deserialize bbox if stored as JSON string
                    if 'bbox' in metadata and isinstance(metadata['bbox'], str):
                        try:
                            metadata['bbox'] = json.loads(metadata['bbox'])
                        except:
                            pass  # Keep as is if deserialization fails
                    
                    result_item = {
                        "id": obj_id,
                        "metadata": metadata
                    }
                    
                    if 'distances' in results and len(results['distances']) > 0:
                        result_item["distance"] = float(results['distances'][0][i])
                    
                    # Check if image data is already in metadata
                    if 'image_data' not in metadata:
                        try:
                            # Try to get original image via image ID
                            image_id = metadata.get('image_id')
                            if image_id:
                                print(f"[DEBUG] Image data not found in metadata for object {obj_id} with image_id {image_id}")
                        except Exception as e:
                            print(f"[DEBUG] Error checking image data for result {i}: {e}")
                    else:
                        print(f"[DEBUG] Image data found in metadata for object {obj_id}")
                    
                    formatted_results.append(result_item)
                except Exception as e:
                    print(f"[DEBUG] Error formatting query result {i}: {e}")
    except Exception as e:
        print(f"[DEBUG] Error in format_object_results: {e}")
    
    return formatted_results

# ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€ HTML ํ…œํ”Œ๋ฆฟ
LOGIN_TEMPLATE = '''
<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Vision LLM Agent - ๋กœ๊ทธ์ธ</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f5f5f5;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        .login-container {
            background-color: white;
            padding: 2rem;
            border-radius: 8px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 400px;
        }
        h1 {
            text-align: center;
            color: #4a6cf7;
            margin-bottom: 1.5rem;
        }
        .form-group {
            margin-bottom: 1rem;
        }
        label {
            display: block;
            margin-bottom: 0.5rem;
            font-weight: bold;
        }
        input {
            width: 100%;
            padding: 0.75rem;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 1rem;
        }
        button {
            width: 100%;
            padding: 0.75rem;
            background-color: #4a6cf7;
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 1rem;
            cursor: pointer;
            margin-top: 1rem;
        }
        button:hover {
            background-color: #3a5cd8;
        }
        .error-message {
            color: #e74c3c;
            margin-top: 1rem;
            text-align: center;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <h1>Vision LLM Agent</h1>
        <form action="/login" method="post" autocomplete="off">
            <!-- hidden dummy fields to discourage Chrome autofill -->
            <input type="text" name="fakeusernameremembered" style="display:none" tabindex="-1" autocomplete="off">
            <input type="password" name="fakepasswordremembered" style="display:none" tabindex="-1" autocomplete="off">
            <div class="form-group">
                <label for="username">Username</label>
                <input type="text" id="username" name="username" value="" placeholder="Enter username" required autocomplete="username" autocapitalize="none" autocorrect="off" spellcheck="false">
            </div>
            <div class="form-group">
                <label for="password">Password</label>
                <input type="password" id="password" name="password" value="" placeholder="Enter password" required autocomplete="current-password" autocapitalize="none" autocorrect="off" spellcheck="false">
            </div>
            <button type="submit">Login</button>
            {% if error %}
            <p class="error-message">{{ error }}</p>
            {% endif %}
        </form>
    </div>
</body>
</html>
'''

@app.route('/login', methods=['GET', 'POST'])
def login():
    # ์ด๋ฏธ ๋กœ๊ทธ์ธ๋œ ์‚ฌ์šฉ์ž๋Š” ๋ฉ”์ธ ํŽ˜์ด์ง€๋กœ ๋ฆฌ๋””๋ ‰์…˜ (remove fresh requirement for HF Spaces)
    if current_user.is_authenticated:
        print(f"User already authenticated as: {current_user.username}, redirecting to index")
        return redirect('/index.html')
    
    error = None
    if request.method == 'POST':
        username = request.form.get('username')
        password = request.form.get('password')
        
        print(f"Login attempt: username={username}")
        
        if username in users and users[username].password == password:
            # ๋กœ๊ทธ์ธ ์„ฑ๊ณต ์‹œ ์„ธ์…˜์— ์‚ฌ์šฉ์ž ์ •๋ณด ์ €์žฅ
            user = users[username]
            login_user(user, remember=False)  # 2๋ถ„ ์„ธ์…˜ ๋งŒ๋ฃŒ๋ฅผ ์œ„ํ•ด remember ๋น„ํ™œ์„ฑํ™”
            session['user_id'] = user.id
            session['username'] = username
            session.permanent = True
            session.modified = True  # ์„ธ์…˜ ๋ณ€๊ฒฝ ์‚ฌํ•ญ ์ฆ‰์‹œ ์ ์šฉ
            
            # Debug session data after setting
            print(f"Login successful for user: {username}, ID: {user.id}")
            print(f"Session data after login: {dict(session)}")
            
            # Force session save by accessing it
            _ = session.get('user_id')
            
            # ๋ฆฌ๋””๋ ‰์…˜ ์ฒ˜๋ฆฌ
            next_page = request.args.get('next')
            if next_page and next_page.startswith('/') and next_page != '/login':
                print(f"Redirecting to: {next_page}")
                return redirect(next_page)
            print("Redirecting to index.html")
            
            # Serve index.html directly with cookies to avoid redirect issues
            print("Serving index.html directly with auth cookies")
            
            # Read index.html file
            index_path = os.path.join(app.static_folder, 'index.html')
            try:
                with open(index_path, 'r', encoding='utf-8') as f:
                    html = f.read()
            except Exception as e:
                print(f"[DEBUG] Failed to read index.html: {e}")
                return "Error loading page", 500

            # Add session debug script
            # Alternative: Use localStorage instead of cookies for HF Spaces
            auth_script = f"""
            <script>
            console.log('[DEBUG] HF Spaces detected - using localStorage for auth persistence');
            
            // Store auth data in localStorage (not affected by iframe restrictions)
            localStorage.setItem('auth_user_id', '{user.id}');
            localStorage.setItem('auth_username', '{username}');
            localStorage.setItem('auth_timestamp', Date.now().toString());
            
            console.log('[DEBUG] Auth data stored in localStorage');
            console.log('[DEBUG] user_id:', localStorage.getItem('auth_user_id'));
            console.log('[DEBUG] username:', localStorage.getItem('auth_username'));
            
            // Override fetch to include auth headers
            const originalFetch = window.fetch;
            window.fetch = function(url, options = {{}}) {{
                // Add auth headers for API calls
                if (url.startsWith('/api/')) {{
                    options.headers = options.headers || {{}};
                    options.headers['X-Auth-User-ID'] = localStorage.getItem('auth_user_id');
                    options.headers['X-Auth-Username'] = localStorage.getItem('auth_username');
                    console.log('[DEBUG] Added auth headers to API call:', url);
                }}
                return originalFetch(url, options);
            }};
            
            // Heartbeat to keep session alive
            setInterval(() => {{
                const userId = localStorage.getItem('auth_user_id');
                const username = localStorage.getItem('auth_username');
                
                if (userId && username) {{
                    fetch('/api/heartbeat', {{
                        method: 'POST',
                        headers: {{
                            'X-Auth-User-ID': userId,
                            'X-Auth-Username': username,
                            'Content-Type': 'application/json'
                        }}
                    }}).then(response => {{
                        console.log('Heartbeat sent, status:', response.status);
                    }}).catch(error => {{
                        console.log('Heartbeat failed:', error);
                    }});
                }}
            }}, 30000);
            </script>
            """
            
            # Insert script before </body>
            if '</body>' in html:
                html = html.replace('</body>', auth_script + '\n</body>')
            else:
                html += auth_script
            
            response = make_response(html)
            response.headers['Content-Type'] = 'text/html; charset=utf-8'
            
            # Set cookies in response headers with maximum compatibility
            response.set_cookie('auth_user_id', str(user.id), 
                              max_age=7200,  # 2 hours
                              secure=False,  # Force insecure for HF Spaces
                              httponly=False,  # Allow JavaScript access
                              samesite=None,  # Most permissive
                              path='/')  # Ensure path is set
            response.set_cookie('auth_username', username,
                              max_age=7200,  # 2 hours  
                              secure=False,  # Force insecure for HF Spaces
                              httponly=False,  # Allow JavaScript access
                              samesite=None,  # Most permissive
                              path='/')  # Ensure path is set
        
            print(f"[DEBUG] Set cookies: auth_user_id={user.id}, auth_username={username}")
            print(f"[DEBUG] Cookie settings: secure=False, httponly=False, samesite=None, path=/")
            
            response.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate'
            response.headers['Pragma'] = 'no-cache'
            response.headers['Expires'] = '0'
            
            return response
        else:
            error = 'Invalid username or password'
            print(f"Login failed: {error}")
    
    return render_template_string(LOGIN_TEMPLATE, error=error)

@app.route('/logout')
def logout():
    # Clear server-side session
    logout_user()
    session.clear()
    # Return a small HTML that clears client-side localStorage and expires cookies, then redirects
    html = """
    <html><head><meta charset="utf-8"><title>Logging out...</title></head>
    <body>
    <script>
      try {
        localStorage.removeItem('auth_user_id');
        localStorage.removeItem('auth_username');
        localStorage.removeItem('auth_timestamp');
      } catch (e) {}
      window.location.href = '/login';
    </script>
    </body></html>
    """
    resp = make_response(html)
    # Expire auth cookies explicitly
    resp.set_cookie('auth_user_id', '', expires=0, path='/')
    resp.set_cookie('auth_username', '', expires=0, path='/')
    resp.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, max-age=0'
    resp.headers['Pragma'] = 'no-cache'
    resp.headers['Expires'] = '0'
    return resp

@app.route('/api/heartbeat', methods=['POST'])
def heartbeat():
    """Keep session alive; support header-based auth for HF Spaces"""
    uid, uname = check_authentication()
    if uid and uname:
        return jsonify({"status": "alive", "user_id": uid, "username": uname})
    return jsonify({"status": "no_session"}), 401

@app.route('/product-comparison-lite', methods=['GET'])
@login_required
def product_comparison_lite_page():
    """Serve a lightweight two-image Product Comparison page (no React build required)."""
    html = '''<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Product Comparison (Lite)</title>
  <style>
    body { font-family: Arial, sans-serif; margin: 16px; }
    .row { display: flex; gap: 16px; flex-wrap: wrap; }
    .col { flex: 1 1 320px; min-width: 280px; }
    .card { border: 1px solid #ddd; border-radius: 8px; padding: 12px; }
    .preview-box { width: 100%; height: 45vh; max-height: 520px; border: 1px dashed #ccc; display:flex; align-items:center; justify-content:center; overflow:hidden; border-radius:6px; background:#fafafa; }
    .preview-box img { max-width: 100%; max-height: 100%; width: auto; height: auto; object-fit: contain; }
    input[type="file"] { display: none; }
    .file-button { padding: 8px 12px; background: #3f51b5; color: white; border: none; border-radius: 4px; cursor: pointer; font-size: 14px; }
    .file-button:hover { background: #303f9f; }
    .controls { margin-top: 12px; display:flex; gap: 8px; align-items:center; }
    button { padding: 10px 14px; background: #3f51b5; color: #fff; border: none; border-radius: 4px; cursor: pointer; }
    button:disabled { background: #9aa0c3; cursor: not-allowed; }
    .log { white-space: pre-wrap; background:#fff; color:#333; border: 1px solid #ddd; padding:10px; border-radius:6px; height:180px; overflow:auto; font-size: 12px; }
    .result { margin-top: 12px; }
  </style>
</head>
<body>
  <h2>Product Comparison (Lite)</h2>
  <div class="row">
    <div class="col">
      <div class="card">
        <h3>Product Image 1</h3>
        <div class="preview-box"><img id="img1" alt="Product 1 Preview" style="display:none;" /></div>
        <div class="controls">
          <input type="file" id="file1" accept="image/*" />
          <button type="button" class="file-button" onclick="document.getElementById('file1').click()">Choose Image File</button>
        </div>
      </div>
    </div>
    <div class="col">
      <div class="card">
        <h3>Product Image 2</h3>
        <div class="preview-box"><img id="img2" alt="Product 2 Preview" style="display:none;" /></div>
        <div class="controls">
          <input type="file" id="file2" accept="image/*" />
          <button type="button" class="file-button" onclick="document.getElementById('file2').click()">Choose Image File</button>
        </div>
      </div>
    </div>
  </div>
  <div class="controls" style="margin-top:16px;">
    <button id="compareBtn" disabled onclick="startCompare()">Compare Products</button>
    <a href="/index.html" style="margin-left:8px;">Back to App</a>
  </div>
  <h3>Analysis Progress</h3>
  <div id="log" class="log"></div>
  <h3>Comparison Results</h3>
  <pre id="result" class="result"></pre>

  <script>
    // Proactively unregister any active service workers
    (function(){
      if ('serviceWorker' in navigator) {
        try {
          navigator.serviceWorker.getRegistrations().then(function(regs){
            regs.forEach(function(r){ r.unregister(); });
          });
        } catch (e) { /* ignore */ }
      }
    })();

    let file1 = null, file2 = null;
    
    function handleFile(i, input) {
      console.log('handleFile called for image', i, 'with input:', input);
      const f = input.files && input.files[0];
      if (!f) {
        console.log('No file selected');
        return;
      }
      console.log('File selected:', f.name, 'size:', f.size, 'type:', f.type);
      
      const url = URL.createObjectURL(f);
      console.log('Object URL created:', url);
      
      const img = document.getElementById('img'+i);
      console.log('Image element found:', img);
      
      if (img) {
        img.src = url;
        img.style.display = 'block';
        img.style.visibility = 'visible';
        console.log('Image src set and display changed to block');
        
        img.onload = function() {
          console.log('Image ' + i + ' loaded successfully, dimensions:', img.naturalWidth, 'x', img.naturalHeight);
        };
        img.onerror = function() {
          console.error('Failed to load image ' + i);
        };
      }
      
      if (i === 1) {
        file1 = f;
        console.log('File1 set:', f.name);
      } else {
        file2 = f;
        console.log('File2 set:', f.name);
      }
      updateButton();
    }
    
    function updateButton(){
      const btn = document.getElementById('compareBtn');
      const enabled = file1 && file2;
      btn.disabled = !enabled;
      console.log('Button state updated. File1:', !!file1, 'File2:', !!file2, 'Enabled:', enabled);
    }
    
    async function startCompare() {
      if (!file1 || !file2) return;
      const formData = new FormData();
      formData.append('image1', file1);
      formData.append('image2', file2);
      try {
        const response = await fetch('/api/product/compare/start', {
          method: 'POST',
          body: formData
        });
        const data = await response.json();
        if (data.session_id) {
          streamResults(data.session_id);
        }
      } catch (error) {
        console.error('Error starting comparison:', error);
      }
    }
    
    function streamResults(sessionId) {
      const eventSource = new EventSource('/api/product/compare/stream/' + sessionId);
      const logDiv = document.getElementById('log');
      const resultDiv = document.getElementById('result');
      eventSource.onmessage = function(event) {
        const data = JSON.parse(event.data);
        console.log('Received SSE data:', data);
        
        if (data.message) {
          logDiv.textContent += data.message + '\\n';
          logDiv.scrollTop = logDiv.scrollHeight;
        } else if (data.status) {
          logDiv.textContent += 'Status: ' + data.status + '\\n';
          logDiv.scrollTop = logDiv.scrollHeight;
        } else if (data.final_result) {
          resultDiv.textContent = JSON.stringify(data.final_result, null, 2);
          eventSource.close();
        } else if (data.error) {
          logDiv.textContent += 'Error: ' + data.error + '\\n';
          logDiv.scrollTop = logDiv.scrollHeight;
          eventSource.close();
        }
      };
      eventSource.onerror = function() {
        eventSource.close();
      };
    }

    // Add event listeners after DOM loads
    document.addEventListener('DOMContentLoaded', function() {
      document.getElementById('file1').addEventListener('change', function() {
        handleFile(1, this);
      });
      document.getElementById('file2').addEventListener('change', function() {
        handleFile(2, this);
      });
    });
  </script>
</body>
</html>'''
    resp = make_response(html)
    resp.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, max-age=0'
    resp.headers['Pragma'] = 'no-cache'
    resp.headers['Expires'] = '0'
    return resp

# ์ •์  ํŒŒ์ผ ์„œ๋น™์„ ์œ„ํ•œ ๋ผ์šฐํŠธ (๋กœ๊ทธ์ธ ๋ถˆํ•„์š”)
@app.route('/static/<path:filename>')
def serve_static(filename):
    print(f"Serving static file: {filename}")
    resp = send_from_directory(app.static_folder, filename)
    # Prevent caching of static assets to reflect latest frontend changes
    resp.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, max-age=0'
    resp.headers['Pragma'] = 'no-cache'
    resp.headers['Expires'] = '0'
    return resp

# ์ธ๋ฑ์Šค HTML ์ง์ ‘ ์„œ๋น™ (๋กœ๊ทธ์ธ ํ•„์š”)
@app.route('/index.html')
def serve_index_html():
    # ์„ธ์…˜ ๋ฐ ์ฟ ํ‚ค ๋””๋ฒ„๊ทธ ์ •๋ณด
    print(f"Request to /index.html - Session data: {dict(session)}")
    print(f"Request to /index.html - Cookies: {request.cookies}")
    print(f"Request to /index.html - User authenticated: {current_user.is_authenticated}")
    
    # Check session data first, then fallback to auth cookies
    user_id = session.get('user_id')
    username = session.get('username')
    
    # Fallback to auth cookies if session is empty
    if not user_id or not username:
        user_id = request.cookies.get('auth_user_id')
        username = request.cookies.get('auth_username')
        print(f"Session empty, checking cookies: user_id={user_id}, username={username}")
    
    if not user_id or not username:
        print("No session or cookie data found, redirecting to login")
        return redirect(url_for('login'))
    
    # Verify user exists (users maps username -> User instance)
    user_found = False
    for stored_username, user_obj in users.items():
        if str(getattr(user_obj, 'id', None)) == str(user_id) and stored_username == username:
            user_found = True
            break
    
    if not user_found:
        print(f"User not found: {username} (ID: {user_id}), redirecting to login")
        return redirect(url_for('login'))
    
    print(f"Serving index.html for authenticated user: {username} (ID: {user_id})")
    # ์„ธ์…˜ ์ƒํƒœ ๋””๋ฒ„๊ทธ
    print(f"Session data: user_id={user_id}, username={username}, is_permanent={session.get('permanent', False)}")
    
    # ์„ธ์…˜ ๋งŒ๋ฃŒ๋ฅผ ์˜๋„๋Œ€๋กœ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ์—ฌ๊ธฐ์„œ ์„ธ์…˜์„ ๊ฐฑ์‹ ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
    # ์ฃผ์˜: ์„ธ์…˜์— ์“ฐ๊ธฐ(๋˜๋Š” session.modified=True)๋Š” Flask-Session์—์„œ ๋งŒ๋ฃŒ์‹œ๊ฐ„์„ ์—ฐ์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    
    # index.html์„ ์ฝ์–ด ํ•˜ํŠธ๋น„ํŠธ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ฃผ์ž…
    index_path = os.path.join(app.static_folder, 'index.html')
    try:
        with open(index_path, 'r', encoding='utf-8') as f:
            html = f.read()
    except Exception as e:
        print(f"[DEBUG] Failed to read index.html for injection: {e}")
        return send_from_directory(app.static_folder, 'index.html')

    heartbeat_script = """
    <style>
      /* Override preview image sizing from old builds without rebuild */
      .preview-image { max-width: 100% !important; max-height: 100% !important; width: auto !important; height: auto !important; object-fit: contain !important; }
      /* Ensure container is not too tall on desktop */
      .preview-image-container, .image-container { height: 45vh !important; }
      @media (max-width: 600px) { .preview-image-container, .image-container { height: 35vh !important; } }
      /* Product Comparison navigation button */
      .product-comparison-nav { position: fixed; top: 20px; right: 20px; z-index: 9999; background: #3f51b5; color: white; padding: 12px 16px; border-radius: 8px; text-decoration: none; font-weight: bold; box-shadow: 0 4px 8px rgba(0,0,0,0.2); transition: background 0.3s; }
      .product-comparison-nav:hover { background: #303f9f; color: white; text-decoration: none; }
    </style>
    <script>
    (function(){
      // Ensure auth headers are attached on all API calls using localStorage (HF Spaces blocks cookies)
      try {
        const originalFetch = window.fetch;
        window.fetch = function(url, options = {}) {
          if (typeof url === 'string' && url.indexOf('/api/') === 0) {
            options.headers = options.headers || {};
            const uid = localStorage.getItem('auth_user_id');
            const uname = localStorage.getItem('auth_username');
            if (uid && uname) {
              options.headers['X-Auth-User-ID'] = uid;
              options.headers['X-Auth-Username'] = uname;
              // console.debug('[DEBUG] (index) Added auth headers to API call:', url);
            }
          }
          return originalFetch.apply(this, [url, options]);
        }
      } catch (e) {
        console.warn('Failed to install fetch auth header override:', e);
      }
      // 1) ์„ธ์…˜ ์ƒํƒœ ์ฃผ๊ธฐ ์ฒดํฌ (๋งŒ๋ฃŒ์‹œ ๋กœ๊ทธ์ธ์œผ๋กœ)
      function checkSession(){
        fetch('/api/status', {credentials: 'include', redirect: 'manual'}).then(function(res){
          var redirected = res.redirected || (res.url && res.url.indexOf('/login') !== -1);
          if(res.status !== 200 || redirected){
            window.location.href = '/login';
          }
        }).catch(function(){
          // ๋„คํŠธ์›Œํฌ ์˜ค๋ฅ˜ ๋“ฑ๋„ ๋กœ๊ทธ์ธ์œผ๋กœ ์œ ๋„
          window.location.href = '/login';
        });
      }
      checkSession();
      setInterval(checkSession, 30000);

      // 2) ์‚ฌ์šฉ์ž ๋น„ํ™œ์„ฑ(๋ฌด๋™์ž‘) 2๋ถ„ ํ›„ ์ž๋™ ๋กœ๊ทธ์•„์›ƒ
      var idleMs = 120000; // 2๋ถ„
      var idleTimer;
      function triggerLogout(){
        // ์„œ๋ฒ„ ์„ธ์…˜ ์ •๋ฆฌ ํ›„ ๋กœ๊ทธ์ธ ํ™”๋ฉด์œผ๋กœ
        window.location.href = '/logout';
      }
      function resetIdle(){
        if (idleTimer) clearTimeout(idleTimer);
        idleTimer = setTimeout(triggerLogout, idleMs);
      }
      ['click','mousemove','keydown','scroll','touchstart','visibilitychange'].forEach(function(evt){
        window.addEventListener(evt, resetIdle, {passive:true});
      });
      resetIdle();

      // 3) Add Product Comparison navigation button - DISABLED
      function addProductComparisonButton() {
        // Disabled - no longer adding Product Comparison button to main UI
        return;
      }
      
      // Add button after DOM is ready
      if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', addProductComparisonButton);
      } else {
        addProductComparisonButton();
      }
    })();
    </script>
    """

    try:
        if '</body>' in html:
            html = html.replace('</body>', heartbeat_script + '</body>')
        else:
            html = html + heartbeat_script
    except Exception as e:
        print(f"[DEBUG] Failed to inject heartbeat script: {e}")
        return send_from_directory(app.static_folder, 'index.html')

    resp = make_response(html)
    # Prevent sensitive pages from being cached
    resp.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, max-age=0'
    resp.headers['Pragma'] = 'no-cache'
    resp.headers['Expires'] = '0'
    return resp

# Static files should be accessible without login requirements
@app.route('/static/<path:filename>')
def static_files(filename):
    print(f"Serving static file: {filename}")
    # Two possible locations after CRA build copy:
    # 1) Top-level:    static/<filename>
    # 2) Nested build: static/static/<filename>
    top_level_path = os.path.join(app.static_folder, filename)
    nested_dir = os.path.join(app.static_folder, 'static')
    nested_path = os.path.join(nested_dir, filename)

    try:
        if os.path.exists(top_level_path):
            return send_from_directory(app.static_folder, filename)
        elif os.path.exists(nested_path):
            # Serve from nested build directory
            return send_from_directory(nested_dir, filename)
        else:
            # Fallback: try as-is (may help in some edge cases)
            return send_from_directory(app.static_folder, filename)
    except Exception as e:
        print(f"[DEBUG] Error serving static file '{filename}': {e}")
        # Final fallback to avoid leaking stack traces
        return ('Not Found', 404)

# Add explicit handlers for JS files that are failing
@app.route('/static/js/<path:filename>')
def static_js_files(filename):
    print(f"Serving JS file: {filename}")
    # Try top-level static/js and nested static/static/js
    top_js_dir = os.path.join(app.static_folder, 'js')
    nested_js_dir = os.path.join(app.static_folder, 'static', 'js')
    top_js_path = os.path.join(top_js_dir, filename)
    nested_js_path = os.path.join(nested_js_dir, filename)

    try:
        if os.path.exists(top_js_path):
            return send_from_directory(top_js_dir, filename)
        elif os.path.exists(nested_js_path):
            return send_from_directory(nested_js_dir, filename)
        else:
            # As a fallback, let the generic static handler try
            return static_files(os.path.join('js', filename))
    except Exception as e:
        print(f"[DEBUG] Error serving JS file '{filename}': {e}")
        return ('Not Found', 404)

# ๊ธฐ๋ณธ ๊ฒฝ๋กœ ๋ฐ ๊ธฐํƒ€ ๊ฒฝ๋กœ ์ฒ˜๋ฆฌ (๋กœ๊ทธ์ธ ํ•„์š”)
@app.route('/', defaults={'path': ''}, methods=['GET'])
@app.route('/<path:path>', methods=['GET'])
def serve_react(path):
    """Serve React frontend"""
    print(f"Serving React frontend for path: {path}, user: {current_user.username if current_user.is_authenticated else 'not authenticated'}")
    
    # Skip specific routes that have their own handlers (but not index.html)
    if path in ['product-comparison-lite', 'login', 'logout', 'similar-images', 'object-detection-search', 'model-vector-db', 'openai-chat']:
        # Let Flask find the specific route handler
        from flask import abort
        abort(404)  # This will cause Flask to try other routes
    
    # For root path, redirect to index.html to ensure consistent behavior
    if path == "":
        return redirect('/index.html')
    
    # ์ •์  ํŒŒ์ผ ์ฒ˜๋ฆฌ๋Š” ์ด์ œ ๋ณ„๋„ ๋ผ์šฐํŠธ์—์„œ ์ฒ˜๋ฆฌ
    if path != "" and os.path.exists(os.path.join(app.static_folder, path)):
        resp = send_from_directory(app.static_folder, path)
        resp.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, max-age=0'
        resp.headers['Pragma'] = 'no-cache'
        resp.headers['Expires'] = '0'
        return resp
    else:
        # React ์•ฑ์˜ index.html ์„œ๋น™ (ํ•˜ํŠธ๋น„ํŠธ ์Šคํฌ๋ฆฝํŠธ ์ฃผ์ž…)
        index_path = os.path.join(app.static_folder, 'index.html')
        try:
            with open(index_path, 'r', encoding='utf-8') as f:
                html = f.read()
        except Exception as e:
            print(f"[DEBUG] Failed to read index.html for injection (serve_react): {e}")
            resp = send_from_directory(app.static_folder, 'index.html')
            resp.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, max-age=0'
            resp.headers['Pragma'] = 'no-cache'
            resp.headers['Expires'] = '0'
            return resp

        heartbeat_script = """
        <style>
          /* Override preview image sizing from old builds without rebuild */
          .preview-image { max-width: 100% !important; max-height: 100% !important; width: auto !important; height: auto !important; object-fit: contain !important; }
          .preview-image-container, .image-container { height: 45vh !important; }
          @media (max-width: 600px) { .preview-image-container, .image-container { height: 35vh !important; } }
          /* Product Comparison navigation button */
          .product-comparison-nav { position: fixed; top: 20px; right: 20px; z-index: 9999; background: #3f51b5; color: white; padding: 12px 16px; border-radius: 8px; text-decoration: none; font-weight: bold; box-shadow: 0 4px 8px rgba(0,0,0,0.2); transition: background 0.3s; }
          .product-comparison-nav:hover { background: #303f9f; color: white; text-decoration: none; }
        </style>
        <script>
        (function(){
          // 1) ์„ธ์…˜ ์ƒํƒœ ์ฃผ๊ธฐ ์ฒดํฌ (๋งŒ๋ฃŒ์‹œ ๋กœ๊ทธ์ธ์œผ๋กœ)
          function checkSession(){
            fetch('/api/status', {credentials: 'include', redirect: 'manual'}).then(function(res){
              var redirected = res.redirected || (res.url && res.url.indexOf('/login') !== -1);
              if(res.status !== 200 || redirected){
                window.location.href = '/login';
              }
            }).catch(function(){
              window.location.href = '/login';
            });
          }
          checkSession();
          setInterval(checkSession, 30000);

          // 2) ์‚ฌ์šฉ์ž ๋น„ํ™œ์„ฑ(๋ฌด๋™์ž‘) 2๋ถ„ ํ›„ ์ž๋™ ๋กœ๊ทธ์•„์›ƒ
          var idleMs = 120000; // 2๋ถ„
          var idleTimer;
          function triggerLogout(){
            window.location.href = '/logout';
          }
          function resetIdle(){
            if (idleTimer) clearTimeout(idleTimer);
            idleTimer = setTimeout(triggerLogout, idleMs);
          }
          ['click','mousemove','keydown','scroll','touchstart','visibilitychange'].forEach(function(evt){
            window.addEventListener(evt, resetIdle, {passive:true});
          });
          resetIdle();

          // 3) Add Product Comparison navigation button - DISABLED
          function addProductComparisonButton() {
            // Disabled - no longer adding Product Comparison button to main UI
            return;
          }
          
          // Add button after DOM is ready
          if (document.readyState === 'loading') {
            document.addEventListener('DOMContentLoaded', addProductComparisonButton);
          } else {
            addProductComparisonButton();
          }
        })();
        </script>
        """

        try:
            if '</body>' in html:
                html = html.replace('</body>', heartbeat_script + '</body>')
            else:
                html = html + heartbeat_script
        except Exception as e:
            print(f"[DEBUG] Failed to inject heartbeat script (serve_react): {e}")
            resp = send_from_directory(app.static_folder, 'index.html')
            resp.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, max-age=0'
            resp.headers['Pragma'] = 'no-cache'
            resp.headers['Expires'] = '0'
            return resp

        resp = make_response(html)
        resp.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, max-age=0'
        resp.headers['Pragma'] = 'no-cache'
        resp.headers['Expires'] = '0'
        return resp

@app.route('/similar-images', methods=['GET'])
@login_required
def similar_images_page():
    """Serve similar images search page"""
    resp = send_from_directory(app.static_folder, 'similar-images.html')
    resp.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, max-age=0'
    resp.headers['Pragma'] = 'no-cache'
    resp.headers['Expires'] = '0'
    return resp

@app.route('/object-detection-search', methods=['GET'])
@login_required
def object_detection_search_page():
    """Serve object detection search page"""
    resp = send_from_directory(app.static_folder, 'object-detection-search.html')
    resp.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, max-age=0'
    resp.headers['Pragma'] = 'no-cache'
    resp.headers['Expires'] = '0'
    return resp

@app.route('/model-vector-db', methods=['GET'])
@login_required
def model_vector_db_page():
    """Serve model vector DB UI page"""
    resp = send_from_directory(app.static_folder, 'model-vector-db.html')
    resp.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, max-age=0'
    resp.headers['Pragma'] = 'no-cache'
    resp.headers['Expires'] = '0'
    return resp

@app.route('/openai-chat', methods=['GET'])
@login_required
def openai_chat_page():
    """Serve OpenAI chat UI page"""
    resp = send_from_directory(app.static_folder, 'openai-chat.html')
    resp.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, max-age=0'
    resp.headers['Pragma'] = 'no-cache'
    resp.headers['Expires'] = '0'
    return resp

@app.route('/api/openai/chat', methods=['POST'])
@require_auth()
def openai_chat_api():
    """Forward chat request to OpenAI Chat Completions API.
    Expects JSON: { prompt: string, model?: string, api_key?: string, system?: string }
    Uses OPENAI_API_KEY from environment if api_key not provided.
    """
    try:
        data = request.get_json(force=True)
    except Exception:
        return jsonify({"error": "Invalid JSON body"}), 400

    prompt = (data or {}).get('prompt', '').strip()
    model = (data or {}).get('model') or os.environ.get('OPENAI_MODEL', 'gpt-5-mini')
    system = (data or {}).get('system') or 'You are a helpful assistant.'
    api_key = (data or {}).get('api_key') or os.environ.get('OPENAI_API_KEY')

    if not prompt:
        return jsonify({"error": "Missing 'prompt'"}), 400
    if not api_key:
        return jsonify({"error": "Missing OpenAI API key. Provide in request or set OPENAI_API_KEY env."}), 400

    # Prefer official Python SDK if available
    if OpenAI is None:
        return jsonify({"error": "OpenAI Python package not installed on server"}), 500

    try:
        start = time.time()
        client = OpenAI(api_key=api_key)

        # Perform chat completion
        chat = client.chat.completions.create(
            model=model,
            messages=[
                {"role": "system", "content": system},
                {"role": "user", "content": prompt},
            ],
        )
        latency = round(time.time() - start, 3)
    except Exception as e:
        # Log detailed error for diagnostics
        try:
            import traceback
            traceback.print_exc()
        except Exception:
            pass
        err_msg = str(e)
        print(f"[OpenAI Chat Error] model={model} err={err_msg}")
        return jsonify({
            "error": "OpenAI SDK call failed",
            "detail": err_msg,
            "model": model
        }), 502

    try:
        content = chat.choices[0].message.content if chat and chat.choices else ''
        usage = getattr(chat, 'usage', None)
        usage = usage.model_dump() if hasattr(usage, 'model_dump') else (usage or {})
    except Exception as e:
        return jsonify({"error": f"Failed to parse SDK response: {str(e)}"}), 500

    return jsonify({
        'response': content,
        'model': model,
        'usage': usage,
        'latency_sec': latency
    })

@app.route('/api/vision-rag/query', methods=['POST'])
@require_auth()
def vision_rag_query():
    """Vision RAG endpoint.
    Expects JSON with one of the following query modes and a user question:
      - { userQuery, searchType: 'image', image, n_results? }
      - { userQuery, searchType: 'object', objectId, n_results? }
      - { userQuery, searchType: 'class', class_name, n_results? }
    Returns: { answer, retrieved: [...], model, latency_sec }
    """
    if ChatOpenAI is None:
        return jsonify({"error": "LangChain not installed on server"}), 500

    data = request.get_json(silent=True) or {}
    # Debug: log incoming payload keys and basic info (without sensitive data)
    try:
        print("[VRAG][REQ] keys=", list(data.keys()))
        print("[VRAG][REQ] has_api_key=", bool(data.get('api_key') or os.environ.get('OPENAI_API_KEY')))
        _img = data.get('image')
        if isinstance(_img, str):
            print("[VRAG][REQ] image_str_len=", len(_img), "prefix=", _img[:30] if len(_img) > 30 else _img)
        print("[VRAG][REQ] searchType=", data.get('searchType'), "objectId=", data.get('objectId'), "class_name=", data.get('class_name'))
    except Exception as _e:
        print("[VRAG][WARN] failed to log request payload:", _e)

    user_query = (data.get('userQuery') or '').strip()
    if not user_query:
        return jsonify({"error": "Missing 'userQuery'"}), 400

    api_key = data.get('api_key') or os.environ.get('OPENAI_API_KEY')
    if not api_key:
        return jsonify({"error": "Missing OpenAI API key. Provide in request or set OPENAI_API_KEY env."}), 400

    search_type = data.get('searchType', 'image')
    n_results = int(data.get('n_results', 5))
    print(f"[VRAG] user_query='{user_query}' | search_type={search_type} | n_results={n_results}")

    # Build query embedding or filtered fetch similar to /api/search-similar-objects
    results = None
    try:
        if search_type == 'image' and 'image' in data:
            image_data = data['image']
            if isinstance(image_data, str) and image_data.startswith('data:image'):
                image_data = image_data.split(',')[1]
            image = Image.open(BytesIO(base64.b64decode(image_data))).convert('RGB')
            query_embedding = generate_image_embedding(image)
            if query_embedding is None:
                return jsonify({"error": "Failed to generate image embedding"}), 500
            results = object_collection.query(
                query_embeddings=[query_embedding],
                n_results=n_results,
                include=["metadatas", "distances"]
            ) if object_collection is not None else None
        elif search_type == 'object' and 'objectId' in data:
            obj_id = data['objectId']
            base = object_collection.get(ids=[obj_id], include=["embeddings"]) if object_collection is not None else None
            emb = base["embeddings"][0] if base and "embeddings" in base and base["embeddings"] else None
            if emb is None:
                return jsonify({"error": "objectId not found or has no embedding"}), 400
            results = object_collection.query(
                query_embeddings=[emb],
                n_results=n_results,
                include=["metadatas", "distances"]
            )
        elif search_type == 'class' and 'class_name' in data:
            filter_query = {"class": {"$eq": data['class_name']}}
            results = object_collection.get(
                where=filter_query,
                limit=n_results,
                include=["metadatas", "embeddings", "documents"]
            ) if object_collection is not None else None
        else:
            return jsonify({"error": "Invalid search parameters"}), 400
    except Exception as e:
        return jsonify({"error": f"Retrieval failed: {str(e)}"}), 500

    # Format results using existing helper
    formatted = format_object_results(results) if results else []
    # Debug: log retrieval summary
    try:
        cnt = len(formatted)
        print(f"[VRAG][RETRIEVE] items={cnt}")
        if cnt:
            print("[VRAG][RETRIEVE] first_item=", {
                'id': formatted[0].get('id'),
                'distance': formatted[0].get('distance'),
                'meta_keys': list((formatted[0].get('metadata') or {}).keys())
            })
    except Exception as _e:
        print("[VRAG][WARN] failed to log retrieval summary:", _e)

    # Build concise context for LLM
    def _shorten(md):
        try:
            bbox = md.get('bbox') if isinstance(md, dict) else None
            if isinstance(bbox, dict):
                bbox = {k: round(float(v), 3) for k, v in bbox.items() if isinstance(v, (int, float))}
            return {
                'image_id': md.get('image_id'),
                'class': md.get('class'),
                'confidence': md.get('confidence'),
                'bbox': bbox,
            }
        except Exception:
            return {k: md.get(k) for k in ('image_id', 'class', 'confidence') if k in md}

    context_items = []
    for r in formatted[:n_results]:
        md = r.get('metadata', {})
        item = {
            'id': r.get('id'),
            'distance': r.get('distance'),
            'meta': _shorten(md)
        }
        context_items.append(item)

    # Compose prompt
    system_text = (
        "You are a vision assistant. Use ONLY the provided detected object context to answer. "
        "Be concise and state uncertainty if context is insufficient."
    )
    # Provide the minimal JSON-like context to the model
    context_text = json.dumps(context_items, ensure_ascii=False, indent=2)
    user_text = f"User question: {user_query}\n\nDetected context (top {len(context_items)}):\n{context_text}"

    # Debug: show compact context preview
    try:
        print("[VRAG][CTX] context_items_count=", len(context_items))
        if context_items:
            print("[VRAG][CTX] sample=", json.dumps(context_items[0], ensure_ascii=False))
    except Exception as _e:
        print("[VRAG][WARN] failed to log context:", _e)

    # Attempt multimodal call (text + top-1 image) if available; otherwise fallback to text-only LangChain.
    answer = None
    model_used = None
    try:
        start = time.time()
        top_data_url = None
        try:
            if formatted:
                md0 = (formatted[0] or {}).get('metadata') or {}
                img_b64 = md0.get('image_data')
                if isinstance(img_b64, str) and len(img_b64) > 50:
                    # Construct data URL without logging raw base64
                    top_data_url = 'data:image/jpeg;base64,' + img_b64
        except Exception:
            top_data_url = None

        # Prefer OpenAI SDK for multimodal if available and we have an image
        if OpenAI is not None and top_data_url is not None:
            client = OpenAI(api_key=api_key)
            model_used = os.environ.get('OPENAI_MODEL', 'gpt-5-mini')
            chat = client.chat.completions.create(
                model=model_used,
                messages=[
                    {"role": "system", "content": system_text},
                    {
                        "role": "user",
                        "content": [
                            {"type": "text", "text": user_text},
                            {"type": "image_url", "image_url": {"url": top_data_url}},
                        ],
                    },
                ],
            )
            answer = chat.choices[0].message.content if chat and chat.choices else ''
        else:
            # Fallback to existing LangChain text-only flow
            llm = ChatOpenAI(api_key=api_key, model=os.environ.get('OPENAI_MODEL', 'gpt-5-mini'))
            prompt = ChatPromptTemplate.from_messages([
                ("system", system_text),
                ("human", "{input}")
            ])
            chain = prompt | llm | StrOutputParser()
            answer = chain.invoke({"input": user_text})
            model_used = getattr(llm, 'model', None)

        latency = round(time.time() - start, 3)
    except Exception as e:
        return jsonify({"error": f"LLM call failed: {str(e)}"}), 502

    return jsonify({
        "answer": answer,
        "retrieved": context_items,
        "model": model_used,
        "latency_sec": latency
    })

@app.route('/api/status', methods=['GET'])
def status():
    # Public endpoint - no authentication required
    return jsonify({
        "status": "online",
        "models": {
            "yolo": yolo_model is not None,
            "detr": detr_model is not None and detr_processor is not None,
            "vit": vit_model is not None and vit_processor is not None
        },
        "device": "GPU" if torch.cuda.is_available() else "CPU"
    })

# Root route is now handled by serve_react function
# This route is removed to prevent conflicts

@app.route('/index')
@login_required
def index_page():
    # /index ๊ฒฝ๋กœ๋Š” index.html๋กœ ๋ฆฌ๋””๋ ‰์…˜
    print("Index route redirecting to index.html")
    return redirect('/index.html')


if __name__ == "__main__":
    # ํ—ˆ๊น…ํŽ˜์ด์Šค Space์—์„œ๋Š” PORT ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค
    port = int(os.environ.get("FLASK_PORT", os.environ.get("PORT", 7860)))
    app.run(debug=False, host='0.0.0.0', port=port)