File size: 102,469 Bytes
c46080e
 
 
 
 
 
 
 
 
 
 
8418b54
c46080e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8418b54
c46080e
 
 
 
8418b54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c46080e
 
 
 
 
 
8418b54
c46080e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8418b54
c46080e
 
 
 
 
8418b54
c46080e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8418b54
c46080e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8418b54
 
c46080e
8418b54
 
 
 
 
 
 
 
c46080e
 
8418b54
 
 
 
 
 
 
 
 
 
c46080e
 
 
8418b54
 
 
 
 
 
 
 
 
c46080e
8418b54
 
c46080e
8418b54
 
 
 
 
c46080e
8418b54
 
c46080e
8418b54
 
 
c46080e
 
 
8418b54
 
 
 
 
 
c46080e
 
8418b54
c46080e
8418b54
 
 
 
c46080e
8418b54
c46080e
8418b54
c46080e
 
 
8418b54
c46080e
8418b54
 
 
 
c46080e
8418b54
c46080e
8418b54
 
c46080e
8418b54
c46080e
8418b54
 
 
 
c46080e
8418b54
 
 
 
c46080e
8418b54
c46080e
8418b54
 
 
 
c46080e
 
8418b54
 
 
 
 
 
 
 
 
 
 
c46080e
 
8418b54
 
 
 
 
 
 
 
 
c46080e
 
 
8418b54
 
c46080e
 
 
8418b54
 
 
 
 
 
 
 
c46080e
8418b54
 
 
c46080e
 
 
 
8418b54
c46080e
 
8418b54
 
 
 
c46080e
8418b54
 
 
 
 
 
 
 
 
c46080e
8418b54
 
 
 
c46080e
8418b54
 
 
 
 
 
 
 
 
c46080e
 
8418b54
c46080e
8418b54
c46080e
 
8418b54
 
 
 
c46080e
8418b54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c46080e
 
8418b54
 
 
 
 
 
 
 
 
 
c46080e
8418b54
 
 
 
 
 
 
 
 
 
 
c46080e
8418b54
 
 
 
 
 
 
 
 
c46080e
 
8418b54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c46080e
 
 
8418b54
 
 
 
 
 
 
 
 
c46080e
8418b54
c46080e
 
8418b54
 
 
 
 
 
 
 
 
 
 
 
c46080e
 
8418b54
 
 
 
 
 
 
c46080e
8418b54
c46080e
8418b54
c46080e
8418b54
 
 
 
c46080e
8418b54
c46080e
8418b54
c46080e
8418b54
 
c46080e
8418b54
c46080e
8418b54
 
 
 
c46080e
8418b54
c46080e
8418b54
c46080e
8418b54
c46080e
8418b54
 
 
 
c46080e
8418b54
 
 
c46080e
8418b54
c46080e
8418b54
 
 
 
c46080e
 
 
8418b54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c46080e
 
 
 
8418b54
 
c46080e
8418b54
 
 
 
 
 
 
 
 
 
c46080e
8418b54
 
c46080e
8418b54
 
 
 
 
 
 
 
 
c46080e
 
8418b54
c46080e
8418b54
 
c46080e
8418b54
 
 
 
 
c46080e
8418b54
 
c46080e
8418b54
 
c46080e
8418b54
 
 
 
 
 
 
 
c46080e
8418b54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c46080e
 
8418b54
c46080e
 
 
 
8418b54
c46080e
 
 
8418b54
c46080e
 
 
8418b54
c46080e
8418b54
c46080e
 
 
 
 
 
 
8418b54
 
c46080e
 
8418b54
c46080e
 
8418b54
 
c46080e
8418b54
 
c46080e
8418b54
c46080e
 
 
8418b54
c46080e
 
8418b54
c46080e
 
 
 
8418b54
c46080e
 
 
8418b54
c46080e
8418b54
 
 
 
c46080e
 
8418b54
c46080e
8418b54
c46080e
 
 
8418b54
 
c46080e
 
8418b54
 
c46080e
 
8418b54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c46080e
 
8418b54
 
c46080e
8418b54
 
 
 
c46080e
 
8418b54
 
c46080e
8418b54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c46080e
8418b54
c46080e
 
8418b54
 
c46080e
8418b54
 
 
 
 
c46080e
 
8418b54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c46080e
8418b54
 
c46080e
8418b54
 
c46080e
 
8418b54
 
c46080e
8418b54
 
c46080e
8418b54
c46080e
8418b54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c46080e
8418b54
 
c46080e
8418b54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c46080e
8418b54
 
 
 
 
 
 
 
 
c46080e
 
 
 
 
 
 
 
8418b54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c46080e
 
 
 
8418b54
 
c46080e
8418b54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c46080e
 
8418b54
c46080e
 
 
 
 
8418b54
 
 
 
 
 
c46080e
 
 
 
8418b54
 
 
 
 
 
 
c46080e
 
8418b54
 
c46080e
 
8418b54
 
 
 
 
 
 
 
 
 
c46080e
 
8418b54
 
 
c46080e
 
8418b54
 
 
 
c46080e
8418b54
 
 
 
 
c46080e
 
 
8418b54
 
 
 
 
 
c46080e
 
8418b54
 
 
 
 
c46080e
8418b54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c46080e
 
8418b54
c46080e
 
8418b54
 
 
c46080e
8418b54
c46080e
 
8418b54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c46080e
8418b54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c46080e
 
 
8418b54
c46080e
 
 
8418b54
c46080e
 
 
 
8418b54
c46080e
 
 
 
 
 
 
 
 
 
 
8418b54
 
c46080e
 
 
 
 
 
 
 
 
8418b54
c46080e
 
 
 
 
 
8418b54
c46080e
 
 
8418b54
c46080e
 
 
 
 
 
 
 
 
 
 
 
8418b54
c46080e
 
 
 
 
 
 
 
8418b54
c46080e
 
 
 
 
 
 
8418b54
c46080e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8418b54
c46080e
8418b54
 
 
 
c46080e
 
 
 
 
 
 
 
8418b54
c46080e
 
 
8418b54
c46080e
 
 
 
 
 
 
 
 
 
 
 
 
 
8418b54
c46080e
 
 
 
 
 
 
 
 
 
 
 
 
 
8418b54
 
 
c46080e
 
 
 
 
 
 
 
 
 
 
 
 
8418b54
 
c46080e
 
 
 
 
 
8418b54
c46080e
 
 
 
 
 
 
8418b54
c46080e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8418b54
c46080e
8418b54
c46080e
 
 
 
 
 
 
 
 
 
 
8418b54
c46080e
 
8418b54
 
c46080e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8418b54
c46080e
 
 
8418b54
c46080e
 
 
 
 
8418b54
 
c46080e
 
 
8418b54
c46080e
 
 
 
8418b54
 
 
 
c46080e
 
8418b54
 
 
c46080e
8418b54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c46080e
8418b54
 
 
 
 
 
 
 
 
 
 
c46080e
8418b54
 
 
 
 
 
c46080e
 
8418b54
 
 
c46080e
8418b54
 
 
 
 
 
 
 
c46080e
8418b54
 
 
 
 
 
 
 
 
 
 
c46080e
8418b54
 
 
 
 
 
c46080e
8418b54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c46080e
8418b54
 
 
 
c46080e
8418b54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c46080e
8418b54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c46080e
8418b54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c46080e
8418b54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c46080e
8418b54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c46080e
8418b54
 
 
c46080e
 
 
8418b54
c46080e
 
 
 
8418b54
 
 
 
c46080e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8418b54
 
 
 
c46080e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8418b54
c46080e
8418b54
c46080e
 
8418b54
c46080e
8418b54
 
 
c46080e
 
 
 
8418b54
 
c46080e
8418b54
 
 
c46080e
8418b54
c46080e
 
8418b54
 
c46080e
8418b54
 
 
 
 
 
 
 
c46080e
 
8418b54
 
c46080e
8418b54
c46080e
 
8418b54
 
 
 
 
c46080e
 
8418b54
 
 
 
c46080e
 
8418b54
 
 
 
 
 
 
 
 
 
c46080e
 
8418b54
 
 
c46080e
8418b54
c46080e
 
8418b54
 
 
 
c46080e
 
8418b54
 
 
 
 
 
 
 
 
 
 
 
 
 
c46080e
8418b54
 
 
 
c46080e
 
 
8418b54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c46080e
8418b54
c46080e
 
 
 
 
 
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
import gradio as gr
import time
import json
import os
import subprocess
from datetime import datetime, timedelta
from typing import List, Tuple, Optional
from backend import (
    ALLOWED_LANGS, AUDIO_FORMATS, transcription_manager, 
    allowed_file, User
)
from ai_summary import ai_summary_manager

def format_status(status):
    """Convert status to user-friendly format"""
    status_map = {
        'pending': '⏳ Queued',
        'processing': '🔄 Processing',
        'completed': '✅ Done',
        'failed': '❌ Failed'
    }
    return status_map.get(status, status)

def format_processing_time(created_at, completed_at=None):
    """Calculate and format processing time"""
    try:
        start_time = datetime.fromisoformat(created_at)
        if completed_at:
            end_time = datetime.fromisoformat(completed_at)
            duration = end_time - start_time
        else:
            duration = datetime.now() - start_time
        
        total_seconds = int(duration.total_seconds())
        if total_seconds < 60:
            return f"{total_seconds}s"
        elif total_seconds < 3600:
            minutes = total_seconds // 60
            seconds = total_seconds % 60
            return f"{minutes}m {seconds}s"
        else:
            hours = total_seconds // 3600
            minutes = (total_seconds % 3600) // 60
            return f"{hours}h {minutes}m"
    except:
        return "Unknown"

def get_user_stats_display(user: User):
    """Get comprehensive user statistics for display"""
    if not user:
        return "👤 Please log in to view statistics"
    
    try:
        # Get transcript stats
        transcript_stats = transcription_manager.get_user_stats(user.user_id)
        
        # Get AI summary stats
        summary_stats = transcription_manager.get_user_summary_stats(user.user_id)
        
        total_transcripts = transcript_stats.get('total_jobs', 0)
        total_summaries = summary_stats.get('total_jobs', 0)
        
        stats_text = f"👤 {user.username} | 🎙️ Transcripts: {total_transcripts} | 🤖 AI Summaries: {total_summaries}"
        
        # Add processing status
        processing_transcripts = transcript_stats.get('by_status', {}).get('processing', 0)
        processing_summaries = summary_stats.get('by_status', {}).get('processing', 0)
        
        if processing_transcripts > 0:
            stats_text += f" | 🔄 Transcribing: {processing_transcripts}"
        if processing_summaries > 0:
            stats_text += f" | 🔄 Summarizing: {processing_summaries}"
            
        return stats_text
        
    except Exception as e:
        return f"👤 {user.username} | Stats error: {str(e)}"

# Authentication Functions (same as before)
def register_user(email, username, password, confirm_password, gdpr_consent, data_retention_consent, marketing_consent):
    """Register new user account"""
    try:
        print(f"📝 Registration attempt for: {username} ({email})")
        
        # Validate inputs
        if not email or not username or not password:
            return "❌ All fields are required", gr.update(visible=False)
        
        if password != confirm_password:
            return "❌ Passwords do not match", gr.update(visible=False)
        
        if not gdpr_consent:
            return "❌ GDPR consent is required to create an account", gr.update(visible=False)
        
        if not data_retention_consent:
            return "❌ Data retention agreement is required", gr.update(visible=False)
        
        # Attempt registration
        success, message, user_id = transcription_manager.register_user(
            email, username, password, gdpr_consent, data_retention_consent, marketing_consent
        )
        
        print(f"📝 Registration result: success={success}, message={message}")
        
        if success:
            print(f"✅ User registered successfully: {username}")
            return f"✅ {message}! Please log in with your credentials.", gr.update(visible=True)
        else:
            print(f"❌ Registration failed: {message}")
            return f"❌ {message}", gr.update(visible=False)
            
    except Exception as e:
        print(f"❌ Registration error: {str(e)}")
        return f"❌ Registration error: {str(e)}", gr.update(visible=False)

def login_user(login, password):
    """Login user"""
    try:
        print(f"🔐 Login attempt for: {login}")
        
        if not login or not password:
            return "❌ Please enter both username/email and password", None, gr.update(visible=True), gr.update(visible=False), "👤 Please log in to view your statistics..."
        
        success, message, user = transcription_manager.login_user(login, password)
        print(f"🔐 Login result: success={success}, message={message}")
        
        if success and user:
            print(f"✅ User logged in successfully: {user.username}")
            stats_display = get_user_stats_display(user)
            return f"✅ Welcome back, {user.username}!", user, gr.update(visible=False), gr.update(visible=True), stats_display
        else:
            print(f"❌ Login failed: {message}")
            return f"❌ {message}", None, gr.update(visible=True), gr.update(visible=False), "👤 Please log in to view your statistics..."
            
    except Exception as e:
        print(f"❌ Login error: {str(e)}")
        return f"❌ Login error: {str(e)}", None, gr.update(visible=True), gr.update(visible=False), "👤 Please log in to view your statistics..."

def logout_user():
    """Logout user"""
    print("👋 User logged out")
    return None, "👋 You have been logged out. Please log in to continue.", gr.update(visible=True), gr.update(visible=False), "👤 Please log in to view your statistics..."

# Transcription Functions
def submit_transcription(file, language, audio_format, diarization_enabled, speakers, 

                        profanity, punctuation, timestamps, lexical, user):
    """Submit transcription job - requires authenticated user"""
    if not user:
        return (
            "❌ Please log in to submit transcriptions",
            "",
            gr.update(visible=False),
            "",
            {},
            gr.update(visible=False),
            gr.update()
        )
    
    if file is None:
        return (
            "Please upload an audio or video file first.",
            "",
            gr.update(visible=False),
            "",
            {},
            gr.update(visible=False),
            gr.update()
        )
    
    try:
        # Get file data
        try:
            if isinstance(file, str):
                if os.path.exists(file):
                    with open(file, 'rb') as f:
                        file_bytes = f.read()
                    original_filename = os.path.basename(file)
                else:
                    return (
                        "File not found. Please try uploading again.",
                        "",
                        gr.update(visible=False),
                        "",
                        {},
                        gr.update(visible=False),
                        gr.update()
                    )
            else:
                file_path = str(file)
                if os.path.exists(file_path):
                    with open(file_path, 'rb') as f:
                        file_bytes = f.read()
                    original_filename = os.path.basename(file_path)
                else:
                    return (
                        "Unable to process file. Please try again.",
                        "",
                        gr.update(visible=False),
                        "",
                        {},
                        gr.update(visible=False),
                        gr.update()
                    )
        except Exception as e:
            return (
                f"Error reading file: {str(e)}",
                "",
                gr.update(visible=False),
                "",
                {},
                gr.update(visible=False),
                gr.update()
            )
        
        # Validate file
        file_extension = original_filename.split('.')[-1].lower() if '.' in original_filename else ""
        supported_extensions = set(AUDIO_FORMATS) | {
            'mp4', 'mov', 'avi', 'mkv', 'webm', 'm4a', '3gp', 'f4v', 
            'wmv', 'asf', 'rm', 'rmvb', 'flv', 'mpg', 'mpeg', 'mts', 'vob'
        }
        
        if file_extension not in supported_extensions and file_extension != "":
            return (
                f"Unsupported file format: .{file_extension}",
                "",
                gr.update(visible=False),
                "",
                {},
                gr.update(visible=False),
                gr.update()
            )
        
        # Basic file size check
        if len(file_bytes) > 500 * 1024 * 1024:  # 500MB limit
            return (
                "File too large. Please upload files smaller than 500MB.",
                "",
                gr.update(visible=False),
                "",
                {},
                gr.update(visible=False),
                gr.update()
            )
        
        # Prepare settings
        settings = {
            'audio_format': audio_format,
            'diarization_enabled': diarization_enabled,
            'speakers': speakers,
            'profanity': profanity,
            'punctuation': punctuation,
            'timestamps': timestamps,
            'lexical': lexical
        }
        
        # Submit job
        job_id = transcription_manager.submit_transcription(
            file_bytes, original_filename, user.user_id, language, settings
        )
        
        # Update job state
        job_state = {
            'current_job_id': job_id,
            'start_time': datetime.now().isoformat(),
            'auto_refresh_active': True,
            'last_status': 'pending'
        }
        
        # Get updated user stats
        stats_display = get_user_stats_display(user)
        
        return (
            f"🚀 Transcription started for: {original_filename}\n📡 Auto-refreshing every 10 seconds...",
            "",
            gr.update(visible=False),
            f"Job ID: {job_id}",
            job_state,
            gr.update(visible=True, value="🔄 Auto-refresh active"),
            stats_display
        )
        
    except Exception as e:
        print(f"❌ Error submitting transcription: {str(e)}")
        return (
            f"Error: {str(e)}",
            "",
            gr.update(visible=False),
            "",
            {},
            gr.update(visible=False),
            gr.update()
        )

def check_current_job_status(job_state, user):
    """Check status of current job with improved transcript handling"""
    if not user:
        return (
            "❌ Please log in to check status", 
            "", 
            gr.update(visible=False), 
            "",
            gr.update(visible=False),
            gr.update()
        )
    
    if not job_state or 'current_job_id' not in job_state:
        return (
            "No active job", 
            "", 
            gr.update(visible=False), 
            "",
            gr.update(visible=False),
            gr.update()
        )
    
    job_id = job_state['current_job_id']
    
    try:
        job = transcription_manager.get_job_status(job_id)
        if not job or job.user_id != user.user_id:
            return (
                "Job not found or access denied", 
                "", 
                gr.update(visible=False), 
                "",
                gr.update(visible=False),
                gr.update()
            )
        
        # Calculate processing time
        processing_time = format_processing_time(job.created_at, job.completed_at)
        
        # Enhanced status change logging
        last_status = job_state.get('last_status', '')
        if job.status != last_status:
            print(f"🔄 [{user.username}] Job status change: {last_status}{job.status} ({job.original_filename})")
            job_state['last_status'] = job.status
        
        # Get updated user stats
        stats_display = get_user_stats_display(user)
        
        # Handle completed status with better transcript detection
        if job.status == 'completed' and job.transcript_text and job.transcript_text.strip():
            # Job is complete and transcript is available, stop auto-refresh
            job_state['auto_refresh_active'] = False
            
            # Create downloadable file
            try:
                transcript_file = create_transcript_file(job.transcript_text, job_id)
                print(f"✅ [{user.username}] Transcription ready: {len(job.transcript_text)} characters")
            except Exception as e:
                print(f"⚠️ [{user.username}] Error creating transcript file: {str(e)}")
                transcript_file = None
            
            return (
                f"✅ Transcription completed in {processing_time}",
                job.transcript_text,
                gr.update(visible=True, value=transcript_file) if transcript_file else gr.update(visible=False),
                f"Processed: {job.original_filename}",
                gr.update(visible=False),  # Hide auto-refresh status
                stats_display
            )
        
        elif job.status == 'failed':
            # Job failed, stop auto-refresh
            job_state['auto_refresh_active'] = False
            error_msg = job.error_message[:100] + "..." if job.error_message and len(job.error_message) > 100 else job.error_message or "Unknown error"
            return (
                f"❌ Transcription failed after {processing_time}",
                "",
                gr.update(visible=False),
                f"Error: {error_msg}",
                gr.update(visible=False),
                stats_display
            )
        
        elif job.status == 'processing':
            # Still processing, continue auto-refresh
            auto_refresh_active = job_state.get('auto_refresh_active', False)
            return (
                f"🔄 Processing... ({processing_time} elapsed)\n📡 Auto-refreshing every 10 seconds...",
                "",
                gr.update(visible=False),
                f"Converting and analyzing: {job.original_filename}",
                gr.update(visible=True, value="🔄 Auto-refresh active") if auto_refresh_active else gr.update(visible=False),
                stats_display
            )
        
        elif job.status == 'completed' and (not job.transcript_text or not job.transcript_text.strip()):
            # Job marked as completed but transcript not yet available - keep refreshing
            auto_refresh_active = job_state.get('auto_refresh_active', False)
            return (
                f"🔄 Finalizing transcript... ({processing_time} elapsed)\n📡 Auto-refreshing every 10 seconds...",
                "",
                gr.update(visible=False),
                f"Retrieving results: {job.original_filename}",
                gr.update(visible=True, value="🔄 Auto-refresh active") if auto_refresh_active else gr.update(visible=False),
                stats_display
            )
        
        else:  # pending
            # Still pending, continue auto-refresh
            auto_refresh_active = job_state.get('auto_refresh_active', False)
            return (
                f"⏳ Queued for processing... ({processing_time} waiting)\n📡 Auto-refreshing every 10 seconds...",
                "",
                gr.update(visible=False),
                f"Waiting: {job.original_filename}",
                gr.update(visible=True, value="🔄 Auto-refresh active") if auto_refresh_active else gr.update(visible=False),
                stats_display
            )
        
    except Exception as e:
        print(f"❌ Error checking job status: {str(e)}")
        return (
            f"Error checking status: {str(e)}", 
            "", 
            gr.update(visible=False), 
            "",
            gr.update(visible=False),
            gr.update()
        )

# AI Summary Functions
def get_available_transcripts(user):
    """Get list of available transcripts for AI summarization"""
    if not user:
        return gr.update(choices=[], value=[])
    
    try:
        # Get completed transcripts
        completed_jobs = transcription_manager.get_user_history(user.user_id, limit=50)
        completed_transcripts = [
            job for job in completed_jobs 
            if job.status == 'completed' and job.transcript_text
        ]
        
        # Create choices list
        choices = []
        for job in completed_transcripts[:20]:  # Limit to recent 20
            label = f"{job.original_filename} ({job.created_at[:16]})"
            choices.append((label, job.job_id))
        
        return gr.update(choices=choices, value=[])
        
    except Exception as e:
        print(f"❌ Error getting available transcripts: {str(e)}")
        return gr.update(choices=[], value=[])

def submit_ai_summary_enhanced(existing_transcripts, new_audio_video_file, document_image_files,

                              ai_instructions, summary_format, output_language, focus_areas, 

                              include_timestamps, include_action_items, user):
    """Enhanced AI summary submission with immediate transcript processing"""
    if not user:
        return (
            "❌ Please log in to generate AI summaries",
            "",
            gr.update(visible=False),
            "",
            {},
            gr.update(visible=False),
            gr.update()
        )
    
    # Determine content type and validate inputs
    has_existing_transcripts = existing_transcripts and len(existing_transcripts) > 0
    has_new_audio_video = new_audio_video_file is not None
    has_document_images = document_image_files and len(document_image_files) > 0
    
    if not has_existing_transcripts and not has_new_audio_video and not has_document_images:
        return (
            "❌ Please provide content: select existing transcripts, upload audio/video file, or upload documents/images",
            "",
            gr.update(visible=False),
            "",
            {},
            gr.update(visible=False),
            gr.update()
        )
        
    if not ai_instructions.strip():
        return (
            "❌ Please provide AI instructions for the summary",
            "",
            gr.update(visible=False),
            "",
            {},
            gr.update(visible=False),
            gr.update()
        )
    
    try:
        # Handle new audio/video file - submit for transcription but don't create AI job yet
        transcription_job_id = None
        if has_new_audio_video:
            # Submit audio/video file to transcription service first
            try:
                # Read the uploaded file
                if isinstance(new_audio_video_file, str):
                    file_path = new_audio_video_file
                else:
                    file_path = str(new_audio_video_file)
                
                with open(file_path, 'rb') as f:
                    file_bytes = f.read()
                
                original_filename = os.path.basename(file_path)
                
                # Use default transcription settings optimized for AI Summary
                transcription_settings = {
                    'audio_format': 'wav',
                    'diarization_enabled': True,
                    'speakers': 5,  # Allow more speakers for conferences
                    'profanity': 'masked',
                    'punctuation': 'automatic',
                    'timestamps': True,
                    'lexical': False
                }
                
                # Submit to transcription service with Thai as default language
                transcription_job_id = transcription_manager.submit_transcription(
                    file_bytes, 
                    original_filename, 
                    user.user_id, 
                    "th-TH",  # Default to Thai
                    transcription_settings
                )
                
                print(f"🎙️ [{user.username}] Audio/video submitted for transcription: {transcription_job_id[:8]}...")
                
                # Create a special job state that will trigger AI summary when transcription completes
                summary_job_state = {
                    'waiting_for_transcription': True,
                    'transcription_job_id': transcription_job_id,
                    'start_time': datetime.now().isoformat(),
                    'auto_refresh_active': True,
                    'last_status': 'waiting_for_transcription',
                    'ai_instructions': ai_instructions,
                    'summary_format': summary_format,
                    'output_language': output_language,
                    'focus_areas': focus_areas,
                    'include_timestamps': include_timestamps,
                    'include_action_items': include_action_items,
                    'existing_transcripts': existing_transcripts if existing_transcripts else [],
                    'document_image_files': document_image_files if document_image_files else [],
                    'user_id': user.user_id
                }
                
                # Get updated user stats
                stats_display = get_user_stats_display(user)
                
                return (
                    f"🎙️ Audio/video submitted for transcription\n⏳ AI Summary will start automatically when transcription completes\n📡 Auto-refreshing every 10 seconds...",
                    "",
                    gr.update(visible=False),
                    f"Transcription Job: {transcription_job_id[:8]}... → Will auto-trigger AI Summary",
                    summary_job_state,
                    gr.update(visible=True, value="🔄 Waiting for transcription"),
                    stats_display
                )
                
            except Exception as e:
                print(f"❌ Error submitting audio/video for transcription: {str(e)}")
                return (
                    f"❌ Error processing audio/video file: {str(e)}",
                    "",
                    gr.update(visible=False),
                    "",
                    {},
                    gr.update(visible=False),
                    gr.update()
                )
        
        # For existing transcripts or documents only (no audio/video transcription needed)
        else:
            transcript_ids = existing_transcripts if existing_transcripts else []
            document_files = document_image_files if document_image_files else []
            
            # Determine content mode
            if has_existing_transcripts and not has_document_images:
                content_mode = "Existing Transcripts"
            elif has_document_images and not has_existing_transcripts:
                content_mode = "Text Documents"  
            else:
                content_mode = "Mixed Content"
            
            # Prepare settings
            settings = {
                'content_mode': content_mode,
                'format': summary_format,
                'output_language': output_language,
                'focus_areas': focus_areas,
                'include_timestamps': include_timestamps,
                'include_action_items': include_action_items,
                'language': "th-TH"
            }
            
            # Submit AI summary job immediately (no transcription needed)
            job_id = ai_summary_manager.submit_summary_job_enhanced(
                user_id=user.user_id,
                content_mode=content_mode,
                summary_type=summary_format,
                user_prompt=ai_instructions,
                existing_transcript_ids=transcript_ids,
                audio_video_files=[],
                document_files=document_files,
                settings=settings
            )
            
            # Update job state
            summary_job_state = {
                'current_summary_job_id': job_id,
                'start_time': datetime.now().isoformat(),
                'auto_refresh_active': True,
                'last_status': 'pending'
            }
            
            # Get updated user stats
            stats_display = get_user_stats_display(user)
            
            # Create source description
            source_parts = []
            if has_existing_transcripts:
                source_parts.append(f"{len(transcript_ids)} existing transcripts")
            if has_document_images:
                source_parts.append(f"{len(document_files)} document/image files")
            
            source_info = " + ".join(source_parts)
            
            return (
                f"🤖 AI Summary started with {source_info}\n📡 Auto-refreshing every 10 seconds...",
                "",
                gr.update(visible=False),
                f"AI Job ID: {job_id}",
                summary_job_state,
                gr.update(visible=True, value="🔄 AI Auto-refresh active"),
                stats_display
            )
        
    except Exception as e:
        print(f"❌ Error submitting enhanced AI summary: {str(e)}")
        return (
            f"❌ Error: {str(e)}",
            "",
            gr.update(visible=False),
            "",
            {},
            gr.update(visible=False),
            gr.update()
        )

def check_ai_summary_status(summary_job_state, user):
    """Check status of AI summary job with auto-trigger logic for completed transcriptions"""
    if not user:
        return (
            "❌ Please log in to check AI summary status", 
            "", 
            gr.update(visible=False), 
            "",
            gr.update(visible=False),
            gr.update()
        )
    
    if not summary_job_state:
        return (
            "No active AI summary job", 
            "", 
            gr.update(visible=False), 
            "",
            gr.update(visible=False),
            gr.update()
        )
    
    try:
        # Handle special case: waiting for transcription to complete
        if summary_job_state.get('waiting_for_transcription'):
            transcription_job_id = summary_job_state.get('transcription_job_id')
            if not transcription_job_id:
                return (
                    "❌ Error: Missing transcription job ID", 
                    "", 
                    gr.update(visible=False), 
                    "",
                    gr.update(visible=False),
                    gr.update()
                )
            
            # Check transcription status
            transcription_job = transcription_manager.get_job_status(transcription_job_id)
            if not transcription_job:
                return (
                    "❌ Transcription job not found", 
                    "", 
                    gr.update(visible=False), 
                    "",
                    gr.update(visible=False),
                    gr.update()
                )
            
            processing_time = format_processing_time(summary_job_state['start_time'])
            
            if transcription_job.status == 'pending':
                return (
                    f"⏳ Transcription queued... ({processing_time} elapsed)\n📡 Auto-refreshing every 10 seconds...",
                    "",
                    gr.update(visible=False),
                    f"Transcription: {transcription_job.original_filename}",
                    gr.update(visible=True, value="🔄 Waiting for transcription"),
                    get_user_stats_display(user)
                )
            
            elif transcription_job.status == 'processing':
                transcription_time = format_processing_time(transcription_job.created_at)
                return (
                    f"🎙️ Transcribing... ({transcription_time} transcribing, {processing_time} total)\n📡 Auto-refreshing every 10 seconds...",
                    "",
                    gr.update(visible=False),
                    f"Transcribing: {transcription_job.original_filename}",
                    gr.update(visible=True, value="🔄 Transcription in progress"),
                    get_user_stats_display(user)
                )
            
            elif transcription_job.status == 'failed':
                summary_job_state['auto_refresh_active'] = False
                return (
                    f"❌ Transcription failed - Cannot proceed\nError: {transcription_job.error_message or 'Unknown error'}",
                    "",
                    gr.update(visible=False),
                    f"Failed: {transcription_job.original_filename}",
                    gr.update(visible=False),
                    get_user_stats_display(user)
                )
            
            elif transcription_job.status == 'completed':
                if not transcription_job.transcript_text or not transcription_job.transcript_text.strip():
                    return (
                        f"🔄 Transcription completed, retrieving text... ({processing_time} elapsed)\n📡 Auto-refreshing every 10 seconds...",
                        "",
                        gr.update(visible=False),
                        f"Getting transcript: {transcription_job.original_filename}",
                        gr.update(visible=True, value="🔄 Getting transcript"),
                        get_user_stats_display(user)
                    )
                
                # TRANSCRIPTION IS COMPLETE! NOW TRIGGER AI SUMMARY IMMEDIATELY
                print(f"✅ Transcription completed, triggering AI summary immediately...")
                
                try:
                    # Prepare transcript IDs including the newly completed one
                    transcript_ids = summary_job_state.get('existing_transcripts', [])
                    transcript_ids.append(transcription_job_id)
                    
                    # Prepare settings
                    settings = {
                        'content_mode': "New Audio/Video Files",
                        'format': summary_job_state.get('summary_format', 'บทสรุปผู้บริหาร'),
                        'output_language': summary_job_state.get('output_language', 'Thai'),
                        'focus_areas': summary_job_state.get('focus_areas', ''),
                        'include_timestamps': summary_job_state.get('include_timestamps', True),
                        'include_action_items': summary_job_state.get('include_action_items', True),
                        'language': "th-TH"
                    }
                    
                    # Submit AI summary job NOW with completed transcript
                    job_id = ai_summary_manager.submit_summary_job_enhanced(
                        user_id=summary_job_state['user_id'],
                        content_mode="New Audio/Video Files",
                        summary_type=summary_job_state.get('summary_format', 'บทสรุปผู้บริหาร'),
                        user_prompt=summary_job_state.get('ai_instructions', ''),
                        existing_transcript_ids=transcript_ids,
                        audio_video_files=[],
                        document_files=summary_job_state.get('document_image_files', []),
                        settings=settings
                    )
                    
                    print(f"🤖 AI Summary job created immediately: {job_id}")
                    
                    # Update job state to track AI summary instead of transcription
                    summary_job_state.update({
                        'waiting_for_transcription': False,
                        'current_summary_job_id': job_id,
                        'transcription_completed_at': datetime.now().isoformat(),
                        'last_status': 'ai_started'
                    })
                    
                    return (
                        f"✅ Transcription done! 🤖 AI Summary started immediately\n📊 Using transcript: {len(transcription_job.transcript_text):,} characters\n📡 Auto-refreshing every 10 seconds...",
                        "",
                        gr.update(visible=False),
                        f"AI Processing: {transcription_job.original_filename}",
                        gr.update(visible=True, value="🔄 AI Summary active"),
                        get_user_stats_display(user)
                    )
                    
                except Exception as e:
                    print(f"❌ Error triggering AI summary: {str(e)}")
                    return (
                        f"❌ Transcription completed but AI summary failed to start: {str(e)}",
                        "",
                        gr.update(visible=False),
                        "AI Summary creation failed",
                        gr.update(visible=False),
                        get_user_stats_display(user)
                    )
        
        # Normal AI summary job monitoring
        if 'current_summary_job_id' not in summary_job_state:
            return (
                "No active AI summary job", 
                "", 
                gr.update(visible=False), 
                "",
                gr.update(visible=False),
                gr.update()
            )
        
        job_id = summary_job_state['current_summary_job_id']
        job = ai_summary_manager.get_summary_status(job_id)
        
        if not job or job.user_id != user.user_id:
            return (
                "AI summary job not found or access denied", 
                "", 
                gr.update(visible=False), 
                "",
                gr.update(visible=False),
                gr.update()
            )
        
        # Calculate processing time
        processing_time = format_processing_time(job.created_at, job.completed_at)
        
        # Enhanced status change logging
        last_status = summary_job_state.get('last_status', '')
        if job.status != last_status:
            print(f"🔄 [{user.username}] AI Summary status: {last_status}{job.status}")
            summary_job_state['last_status'] = job.status
        
        # Get updated user stats
        stats_display = get_user_stats_display(user)
        
        # Handle completed status
        if job.status == 'completed' and job.summary_text and job.summary_text.strip():
            # Job is complete, stop auto-refresh
            summary_job_state['auto_refresh_active'] = False
            
            # Create downloadable file
            try:
                summary_file = create_summary_file(job.summary_text, job_id)
                print(f"✅ [{user.username}] AI Summary ready: {len(job.summary_text)} characters")
            except Exception as e:
                print(f"⚠️ [{user.username}] Error creating summary file: {str(e)}")
                summary_file = None
            
            total_time = format_processing_time(summary_job_state['start_time'])
            return (
                f"✅ AI Summary completed! Total time: {total_time}\n📊 Generated: {len(job.summary_text):,} characters",
                job.summary_text,
                gr.update(visible=True, value=summary_file) if summary_file else gr.update(visible=False),
                f"Completed: {', '.join(job.original_files)}",
                gr.update(visible=False),
                stats_display
            )
        
        elif job.status == 'failed':
            # Job failed, stop auto-refresh
            summary_job_state['auto_refresh_active'] = False
            error_msg = job.error_message[:100] + "..." if job.error_message else "Unknown error"
            total_time = format_processing_time(summary_job_state['start_time'])
            return (
                f"❌ AI Summary failed after {total_time}",
                "",
                gr.update(visible=False),
                f"Error: {error_msg}",
                gr.update(visible=False),
                stats_display
            )
        
        elif job.status == 'processing':
            # Still processing, continue auto-refresh
            return (
                f"🤖 AI analyzing and generating summary... ({processing_time} AI processing)\n📊 Creating comprehensive analysis\n📡 Auto-refreshing every 10 seconds...",
                "",
                gr.update(visible=False),
                f"AI Processing: {', '.join(job.original_files[:2])}{'...' if len(job.original_files) > 2 else ''}",
                gr.update(visible=True, value="🔄 AI generating summary"),
                stats_display
            )
        
        else:  # pending
            # Still pending, continue auto-refresh
            return (
                f"⏳ AI Summary queued... ({processing_time} waiting)\n📡 Auto-refreshing every 10 seconds...",
                "",
                gr.update(visible=False),
                f"Queued: {', '.join(job.original_files[:2])}{'...' if len(job.original_files) > 2 else ''}",
                gr.update(visible=True, value="🔄 AI queued"),
                stats_display
            )
        
    except Exception as e:
        print(f"❌ Error checking AI summary status: {str(e)}")
        return (
            f"Error checking AI summary status: {str(e)}", 
            "", 
            gr.update(visible=False), 
            "",
            gr.update(visible=False),
            gr.update()
        )

def should_auto_refresh(job_state, user):
    """Check if auto-refresh should be active"""
    if not user or not job_state or not job_state.get('auto_refresh_active', False):
        return False
    
    if 'current_job_id' not in job_state:
        return False
    
    job_id = job_state['current_job_id']
    
    try:
        job = transcription_manager.get_job_status(job_id)
        
        if not job or job.user_id != user.user_id:
            return False
        
        if job.status == 'failed':
            return False
        elif job.status == 'completed':
            if job.transcript_text and job.transcript_text.strip():
                return False
            else:
                return True
        else:
            return True
            
    except Exception as e:
        print(f"❌ Error in should_auto_refresh: {str(e)}")
        return True

def should_auto_refresh_summary(summary_job_state, user):
    """Check if AI summary auto-refresh should be active"""
    if not user or not summary_job_state or not summary_job_state.get('auto_refresh_active', False):
        return False
    
    if 'current_summary_job_id' not in summary_job_state:
        return False
    
    job_id = summary_job_state['current_summary_job_id']
    
    try:
        job = ai_summary_manager.get_summary_status(job_id)
        
        if not job or job.user_id != user.user_id:
            return False
        
        if job.status in ['failed', 'completed']:
            return False
        else:
            return True
            
    except Exception as e:
        print(f"❌ Error in should_auto_refresh_summary: {str(e)}")
        return True

def auto_refresh_status(job_state, user):
    """Auto-refresh function for transcription"""
    if not user:
        return (
            gr.update(),
            gr.update(),
            gr.update(),
            gr.update(),
            gr.update(visible=False),
            gr.update()
        )
    
    if should_auto_refresh(job_state, user):
        return check_current_job_status(job_state, user)
    else:
        return (
            gr.update(),
            gr.update(),
            gr.update(),
            gr.update(),
            gr.update(visible=False),
            gr.update()
        )

def auto_refresh_ai_summary(summary_job_state, user):
    """Auto-refresh function for AI summary"""
    if not user:
        return (
            gr.update(),
            gr.update(),
            gr.update(),
            gr.update(),
            gr.update(visible=False),
            gr.update()
        )
    
    if should_auto_refresh_summary(summary_job_state, user):
        return check_ai_summary_status(summary_job_state, user)
    else:
        return (
            gr.update(),
            gr.update(),
            gr.update(),
            gr.update(),
            gr.update(visible=False),
            gr.update()
        )

# History Functions
def get_transcription_history_table(user, show_all=False):
    """Get transcription history table"""
    if not user:
        return []
    
    try:
        limit = 100 if show_all else 20
        transcript_jobs = transcription_manager.get_user_history(user.user_id, limit=limit)
        
        table_data = []
        for job in transcript_jobs:
            try:
                created_time = datetime.fromisoformat(job.created_at)
                formatted_date = created_time.strftime("%Y-%m-%d %H:%M")
            except:
                formatted_date = job.created_at[:16]
            
            status_display = format_status(job.status)
            time_display = format_processing_time(job.created_at, job.completed_at)
            job_id_display = job.job_id[:8] + "..." if len(job.job_id) > 8 else job.job_id
            language_display = ALLOWED_LANGS.get(job.language, job.language)
            
            if job.status == 'completed' and job.transcript_text:
                download_status = "Available"
            else:
                download_status = status_display
            
            table_data.append([
                formatted_date,
                job.original_filename,
                language_display,
                status_display,
                time_display,
                job_id_display,
                download_status
            ])
        
        return table_data
        
    except Exception as e:
        print(f"❌ Error loading transcription history: {str(e)}")
        return []

def get_ai_summary_history_table(user, show_all=False):
    """Get AI summary history table"""
    if not user:
        return []
    
    try:
        limit = 100 if show_all else 20
        summary_jobs = ai_summary_manager.get_user_summary_history(user.user_id, limit=limit)
        
        table_data = []
        for job in summary_jobs:
            try:
                created_time = datetime.fromisoformat(job.created_at)
                formatted_date = created_time.strftime("%Y-%m-%d %H:%M")
            except:
                formatted_date = job.created_at[:16]
            
            status_display = format_status(job.status)
            time_display = format_processing_time(job.created_at, job.completed_at)
            job_id_display = job.job_id[:8] + "..." if len(job.job_id) > 8 else job.job_id
            
            # Get source summary
            source_summary = f"{len(job.original_files)} sources"
            if len(job.original_files) <= 2:
                source_summary = ", ".join([f[:20] + "..." if len(f) > 20 else f for f in job.original_files])
            
            if job.status == 'completed' and job.summary_text:
                download_status = "Available"
            else:
                download_status = status_display
            
            table_data.append([
                formatted_date,
                source_summary,
                job.settings.get('output_language', 'Thai') if job.settings else 'Thai',
                status_display,
                time_display,
                job_id_display,
                download_status
            ])
        
        return table_data
        
    except Exception as e:
        print(f"❌ Error loading AI summary history: {str(e)}")
        return []

def refresh_transcription_history(user, show_all=False):
    """Refresh transcription history and create download files"""
    if not user:
        return [], gr.update(), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
    
    try:
        table_data = get_transcription_history_table(user, show_all)
        stats_display = get_user_stats_display(user)
        
        # Get completed transcription jobs for downloads
        completed_jobs = transcription_manager.get_user_history(user.user_id, limit=50)
        completed_transcripts = [
            job for job in completed_jobs 
            if job.status == 'completed' and job.transcript_text
        ]
        
        # Create download files
        download_updates = []
        for i in range(5):
            if i < len(completed_transcripts):
                job = completed_transcripts[i]
                try:
                    file_path = create_transcript_file(job.transcript_text, job.job_id)
                    label = f"📄 {job.original_filename} ({job.created_at[:10]})"
                    download_updates.append(gr.update(visible=True, value=file_path, label=label))
                except Exception as e:
                    print(f"Error creating transcript download: {e}")
                    download_updates.append(gr.update(visible=False))
            else:
                download_updates.append(gr.update(visible=False))
        
        return [table_data, stats_display] + download_updates
        
    except Exception as e:
        print(f"❌ Error refreshing transcription history: {str(e)}")
        return [], gr.update(), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)

def refresh_ai_summary_history(user, show_all=False):
    """Refresh AI summary history and create download files"""
    if not user:
        return [], gr.update(), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
    
    try:
        table_data = get_ai_summary_history_table(user, show_all)
        stats_display = get_user_stats_display(user)
        
        # Get completed AI summary jobs for downloads
        completed_jobs = ai_summary_manager.get_user_summary_history(user.user_id, limit=50)
        completed_summaries = [
            job for job in completed_jobs 
            if job.status == 'completed' and job.summary_text
        ]
        
        # Create download files
        download_updates = []
        for i in range(5):
            if i < len(completed_summaries):
                job = completed_summaries[i]
                try:
                    file_path = create_summary_file(job.summary_text, job.job_id)
                    source_name = job.original_files[0][:30] if job.original_files else "AI Summary"
                    label = f"🤖 {source_name} ({job.created_at[:10]})"
                    download_updates.append(gr.update(visible=True, value=file_path, label=label))
                except Exception as e:
                    print(f"Error creating summary download: {e}")
                    download_updates.append(gr.update(visible=False))
            else:
                download_updates.append(gr.update(visible=False))
        
        return [table_data, stats_display] + download_updates
        
    except Exception as e:
        print(f"❌ Error refreshing AI summary history: {str(e)}")
        return [], gr.update(), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)

# PDPA Compliance Functions (same as before, but updated for summaries too)
def export_user_data(user):
    """Export comprehensive user data including summaries"""
    if not user:
        return "❌ Please log in to export your data", gr.update(visible=False)
    
    try:
        # Export transcript data
        transcript_export = transcription_manager.export_user_data(user.user_id)
        
        # Export AI summary data (if available)
        try:
            summary_export = {
                'ai_summaries': [job.__dict__ for job in ai_summary_manager.get_user_summary_history(user.user_id, limit=1000)],
                'summary_stats': transcription_manager.get_user_summary_stats(user.user_id)
            }
        except:
            summary_export = {'ai_summaries': [], 'summary_stats': {}}
        
        # Combine exports
        combined_export = {
            **transcript_export,
            **summary_export,
            'export_type': 'comprehensive_azure_ai_service',
            'services': ['transcription', 'ai_summarization']
        }
        
        # Create export file
        os.makedirs("temp", exist_ok=True)
        filename = f"temp/user_data_export_{user.user_id}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
        
        with open(filename, "w", encoding="utf-8") as f:
            json.dump(combined_export, f, indent=2, ensure_ascii=False, default=str)
        
        print(f"📦 [{user.username}] Comprehensive data export created")
        return "✅ Your complete data (transcripts + AI summaries) has been exported successfully", gr.update(visible=True, value=filename, label="Download Your Complete Data Export")
        
    except Exception as e:
        print(f"❌ Error exporting comprehensive user data: {str(e)}")
        return f"❌ Export failed: {str(e)}", gr.update(visible=False)

def update_marketing_consent(user, marketing_consent):
    """Update user's marketing consent"""
    if not user:
        return "❌ Please log in to update consent"
    
    try:
        success = transcription_manager.update_user_consent(user.user_id, marketing_consent)
        if success:
            user.marketing_consent = marketing_consent
            print(f"📧 [{user.username}] Marketing consent updated: {marketing_consent}")
            return f"✅ Marketing consent updated successfully"
        else:
            return "❌ Failed to update consent"
    except Exception as e:
        return f"❌ Error: {str(e)}"

def delete_user_account(user, confirmation_text):
    """Delete user account and all data (transcripts + summaries)"""
    if not user:
        return "❌ Please log in to delete account", None, gr.update(visible=True), gr.update(visible=False)
    
    if confirmation_text != "DELETE MY ACCOUNT":
        return "❌ Please type 'DELETE MY ACCOUNT' to confirm", user, gr.update(visible=False), gr.update(visible=True)
    
    try:
        # Delete transcript data
        success = transcription_manager.delete_user_account(user.user_id)
        
        # Delete AI summary data (if backend supports it)
        try:
            transcription_manager.delete_user_summary_data(user.user_id)
        except Exception as e:
            print(f"⚠️ Warning: Could not delete AI summary data: {e}")
        
        if success:
            print(f"🗑️ [{user.username}] Complete account deleted (transcripts + AI summaries)")
            return "✅ Your account and all data (transcripts + AI summaries) have been permanently deleted", None, gr.update(visible=True), gr.update(visible=False)
        else:
            return "❌ Failed to delete account", user, gr.update(visible=False), gr.update(visible=True)
    except Exception as e:
        return f"❌ Error: {str(e)}", user, gr.update(visible=False), gr.update(visible=True)

def on_user_login(user):
    """Update UI components when user logs in"""
    if user:
        return gr.update(value=user.marketing_consent)
    else:
        return gr.update(value=False)

def create_transcript_file(transcript_text, job_id):
    """Create a downloadable transcript file"""
    os.makedirs("temp", exist_ok=True)
    filename = f"temp/transcript_{job_id}.txt"
    with open(filename, "w", encoding="utf-8") as f:
        f.write(transcript_text)
    return filename

def create_summary_file(summary_text, job_id):
    """Create a downloadable AI summary file"""
    os.makedirs("temp", exist_ok=True)
    filename = f"temp/ai_summary_{job_id}.txt"
    with open(filename, "w", encoding="utf-8") as f:
        f.write(summary_text)
    return filename

# Enhanced CSS with AI Summary styling
enhanced_css = """

/* Main container styling */

.gradio-container {

    background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);

    font-family: 'Segoe UI', system-ui, sans-serif;

    color: #212529;

}



/* Enhanced header styling */

.main-header {

    background: linear-gradient(135deg, #007bff, #0056b3);

    color: white;

    border: none;

    border-radius: 12px;

    padding: 30px;

    text-align: center;

    margin-bottom: 20px;

    box-shadow: 0 4px 12px rgba(0,123,255,0.3);

}



.main-header h1 {

    color: white;

    margin-bottom: 10px;

    font-size: 2.5em;

    font-weight: 700;

    text-shadow: 0 2px 4px rgba(0,0,0,0.3);

}



.main-header p {

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

    font-size: 1.2em;

    margin: 0;

}



/* Card styling with enhanced AI theme */

.gr-box {

    background: white;

    border: 1px solid #dee2e6;

    border-radius: 12px;

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

    padding: 25px;

    margin: 10px 0;

    transition: all 0.3s ease;

}



.gr-box:hover {

    box-shadow: 0 6px 16px rgba(0,0,0,0.12);

    transform: translateY(-2px);

}



/* AI-specific card styling */

.ai-summary-card {

    background: linear-gradient(135deg, #f8f9ff, #e8f2ff);

    border: 2px solid #007bff;

    border-radius: 12px;

    padding: 25px;

    margin: 15px 0;

}



/* Button styling with AI enhancements */

.gr-button {

    background: linear-gradient(135deg, #007bff, #0056b3);

    border: none;

    border-radius: 8px;

    color: white;

    font-weight: 600;

    padding: 14px 28px;

    transition: all 0.3s ease;

    box-shadow: 0 3px 6px rgba(0,123,255,0.3);

    text-transform: uppercase;

    letter-spacing: 0.5px;

}



.gr-button:hover {

    background: linear-gradient(135deg, #0056b3, #004085);

    transform: translateY(-2px);

    box-shadow: 0 5px 10px rgba(0,123,255,0.4);

}



/* AI Summary specific buttons */

.ai-button {

    background: linear-gradient(135deg, #28a745, #1e7e34);

}



.ai-button:hover {

    background: linear-gradient(135deg, #1e7e34, #155724);

}



/* Status displays with enhanced styling */

.status-display {

    background: linear-gradient(135deg, #e3f2fd, #bbdefb);

    border-left: 4px solid #2196f3;

    padding: 20px;

    border-radius: 0 12px 12px 0;

    margin: 15px 0;

    font-family: 'Monaco', 'Consolas', monospace;

    font-size: 14px;

    line-height: 1.6;

}



.ai-status-display {

    background: linear-gradient(135deg, #e8f5e8, #c8e6c9);

    border-left: 4px solid #28a745;

}



/* Auto-refresh indicators */

.auto-refresh-indicator {

    background: linear-gradient(135deg, #fff3cd, #ffeaa7);

    border: 2px solid #ffc107;

    border-radius: 8px;

    padding: 10px 16px;

    font-size: 12px;

    color: #856404;

    text-align: center;

    animation: pulse 2s infinite;

    font-weight: 600;

}



@keyframes pulse {

    0%, 100% { opacity: 1; box-shadow: 0 0 0 0 rgba(255, 193, 7, 0.4); }

    50% { opacity: 0.8; box-shadow: 0 0 0 10px rgba(255, 193, 7, 0); }

}



/* User stats with enhanced design */

.user-stats {

    background: linear-gradient(135deg, #e8f5e8, #c8e6c9);

    border: 2px solid #28a745;

    border-radius: 8px;

    padding: 12px 16px;

    font-size: 13px;

    color: #155724;

    text-align: center;

    font-weight: 600;

    box-shadow: 0 2px 8px rgba(40,167,69,0.2);

}



/* Enhanced input styling */

.gr-textbox, .gr-dropdown, .gr-file {

    border: 2px solid #e9ecef;

    border-radius: 10px;

    background: white;

    color: #212529;

    transition: all 0.3s ease;

    padding: 12px;

    font-size: 14px;

}



.gr-textbox:focus, .gr-dropdown:focus {

    border-color: #007bff;

    box-shadow: 0 0 0 4px rgba(0,123,255,0.1);

    background: #f8f9ff;

}



/* Tab styling with AI theme */

.tab-nav {

    background: white;

    border-bottom: 3px solid #007bff;

    border-radius: 12px 12px 0 0;

    box-shadow: 0 2px 8px rgba(0,0,0,0.1);

}



.tab-nav .tab-button {

    padding: 15px 25px;

    font-weight: 600;

    transition: all 0.3s ease;

}



.tab-nav .tab-button.selected {

    background: linear-gradient(135deg, #007bff, #0056b3);

    color: white;

}



/* History table with enhanced design and black text */

.history-table {

    background: white;

    border: 2px solid #dee2e6;

    border-radius: 12px;

    font-size: 14px;

    overflow: hidden;

    color: #000000; /* Force black text */

}



.history-table thead th {

    background: linear-gradient(135deg, #343a40, #495057);

    color: white;

    font-weight: 700;

    padding: 16px 12px;

    text-align: center;

    border: none;

}



.history-table tbody tr {

    transition: all 0.2s ease;

    border-bottom: 1px solid #dee2e6;

    color: #000000; /* Force black text for rows */

}



.history-table tbody tr:hover {

    background: linear-gradient(135deg, #f8f9ff, #e8f2ff);

    transform: scale(1.01);

    color: #000000; /* Ensure text stays black on hover */

}



.history-table tbody td {

    padding: 12px;

    vertical-align: middle;

    text-align: center;

    border-right: 1px solid #dee2e6;

    color: #000000 !important; /* Force black text with !important */

    font-weight: 500;

}



.history-table tbody td:last-child {

    border-right: none;

}



/* AI Summary specific elements */

.ai-content-sources {

    background: linear-gradient(135deg, #fff8e1, #ffecb3);

    border: 2px solid #ffa000;

    border-radius: 12px;

    padding: 20px;

    margin: 15px 0;

}



.ai-instructions {

    background: linear-gradient(135deg, #e8f5e8, #c8e6c9);

    border: 2px solid #4caf50;

    border-radius: 12px;

    padding: 20px;

    margin: 15px 0;

}



.ai-results {

    background: linear-gradient(135deg, #f3e5f5, #e1bee7);

    border: 2px solid #9c27b0;

    border-radius: 12px;

    padding: 20px;

    margin: 15px 0;

}



/* Enhanced file upload areas */

.file-upload-area {

    border: 3px dashed #007bff;

    border-radius: 12px;

    padding: 30px;

    text-align: center;

    background: linear-gradient(135deg, #f8f9ff, #e8f2ff);

    transition: all 0.3s ease;

}



.file-upload-area:hover {

    border-color: #0056b3;

    background: linear-gradient(135deg, #e8f2ff, #d3e8ff);

}



/* Progress indicators */

.progress-indicator {

    background: linear-gradient(90deg, #007bff, #28a745, #ffc107);

    height: 4px;

    border-radius: 2px;

    animation: progress 2s linear infinite;

}



@keyframes progress {

    0% { width: 0%; }

    50% { width: 70%; }

    100% { width: 100%; }

}



/* Enhanced tooltips and help text */

.help-text {

    color: #6c757d;

    font-style: italic;

    font-size: 12px;

    margin-top: 5px;

}



/* Responsive design improvements */

@media (max-width: 768px) {

    .main-header h1 { font-size: 2em; }

    .gr-button { padding: 10px 20px; font-size: 14px; }

    .gr-box { padding: 15px; margin: 5px 0; }

}

"""

# Create the main interface
with gr.Blocks(
    theme=gr.themes.Soft(
        primary_hue="blue",
        secondary_hue="green",
        neutral_hue="gray",
        font=["system-ui", "sans-serif"]
    ),
    css=enhanced_css,
    title="🎙️🤖 Azure-Powered AI Conference Service - Advanced Transcription & Intelligent Summarization"
) as demo:
    
    # Global state
    current_user = gr.State(None)
    job_state = gr.State({})
    summary_job_state = gr.State({})
    
    # Header
    with gr.Row():
        gr.HTML("""

        <div class="main-header">

            <h1>🎙️🤖 Azure-Powered AI Conference Service</h1>

            <p>Advanced AI-powered conference analysis with transcription, computer vision, and intelligent summarization using Azure AI Foundry</p>

        </div>

        """)
    
    # User stats display
    user_stats_display = gr.Textbox(
        label="",
        lines=1,
        interactive=False,
        show_label=False,
        placeholder="👤 Please log in to view your comprehensive statistics...",
        elem_classes=["user-stats"]
    )
    
    # Authentication Section
    with gr.Column(visible=True, elem_classes=["auth-form"]) as auth_section:
        gr.Markdown("## 🔐 Authentication Required")
        gr.Markdown("Please log in or create an account to use the advanced AI-powered conference analysis service.")
        
        with gr.Tabs() as auth_tabs:
            # Login Tab
            with gr.Tab("🔒 Login") as login_tab:
                with gr.Column():
                    login_email = gr.Textbox(
                        label="Email or Username",
                        placeholder="Enter your email or username"
                    )
                    login_password = gr.Textbox(
                        label="Password",
                        type="password",
                        placeholder="Enter your password"
                    )
                    
                    with gr.Row():
                        login_btn = gr.Button("🔒 Login", variant="primary", elem_classes=["auth-button"])
                    
                    login_status = gr.Textbox(
                        label="",
                        show_label=False,
                        interactive=False,
                        placeholder="Enter your credentials and click Login"
                    )
            
            # Register Tab  
            with gr.Tab("📝 Register") as register_tab:
                with gr.Column():
                    reg_email = gr.Textbox(
                        label="Email",
                        placeholder="Enter your email address"
                    )
                    reg_username = gr.Textbox(
                        label="Username",
                        placeholder="Choose a username (3-30 characters, alphanumeric and underscore)"
                    )
                    reg_password = gr.Textbox(
                        label="Password",
                        type="password",
                        placeholder="Create a strong password (min 8 chars, mixed case, numbers)"
                    )
                    reg_confirm_password = gr.Textbox(
                        label="Confirm Password",
                        type="password",
                        placeholder="Confirm your password"
                    )
                    
                    gr.Markdown("### 📋 Privacy & Data Consent")
                    
                    with gr.Column(elem_classes=["privacy-notice"]):
                        gr.Markdown("""

                        **Enhanced Privacy Notice**: By creating an account, you acknowledge that:

                        - Your data will be stored securely in user-separated Azure Blob Storage

                        - Transcriptions are processed using Azure Speech Services

                        - AI summaries are generated using Azure OpenAI with advanced privacy protection

                        - Computer vision analysis may be performed on uploaded images/videos

                        - You can export or delete all your data (transcripts + AI summaries) at any time

                        - We comply with GDPR and data protection regulations

                        """)
                    
                    gdpr_consent = gr.Checkbox(
                        label="I consent to the processing of my personal data as described in the Privacy Notice (Required)",
                        value=False
                    )
                    data_retention_consent = gr.Checkbox(
                        label="I agree to data retention for transcription and AI analysis service functionality (Required)",
                        value=False
                    )
                    marketing_consent = gr.Checkbox(
                        label="I consent to receiving marketing communications about new AI features (Optional)",
                        value=False
                    )
                    
                    with gr.Row():
                        register_btn = gr.Button("📝 Create Account", variant="primary", elem_classes=["auth-button"])
                    
                    register_status = gr.Textbox(
                        label="",
                        show_label=False,
                        interactive=False,
                        placeholder="Fill out the form and agree to the required consents to create your account"
                    )
                    
                    login_after_register = gr.Button(
                        "🔒 Go to Login",
                        visible=False,
                        variant="secondary"
                    )
    
    # Main Application (visible only when logged in)
    with gr.Column(visible=False) as main_app:
        
        # Logout button
        with gr.Row():
            with gr.Column(scale=3):
                pass
            with gr.Column(scale=1):
                logout_btn = gr.Button("👋 Logout", variant="secondary")
        
        # Enhanced tabs with AI Summary
        with gr.Tabs():
            # Transcription tab
            with gr.Tab("🎙️ Transcribe"):
                with gr.Row():
                    # Left column - Input settings
                    with gr.Column(scale=1):
                        gr.Markdown("### 📁 Upload File")
                        
                        file_upload = gr.File(
                            label="Audio or Video File",
                            type="filepath",
                            file_types=[
                                ".wav", ".mp3", ".ogg", ".opus", ".flac", ".wma", ".aac", 
                                ".m4a", ".amr", ".webm", ".speex",
                                ".mp4", ".mov", ".avi", ".mkv", ".wmv", ".flv", ".3gp"
                            ],
                            elem_classes=["file-upload-area"]
                        )
                        
                        with gr.Row():
                            language = gr.Dropdown(
                                choices=[(v, k) for k, v in ALLOWED_LANGS.items()],
                                label="Language",
                                value="th-TH"  # Default to Thai
                            )
                            audio_format = gr.Dropdown(
                                choices=AUDIO_FORMATS,
                                value="wav",
                                label="Output Format"
                            )
                        
                        gr.Markdown("### ⚙️ Advanced Settings")
                        
                        with gr.Row():
                            diarization_enabled = gr.Checkbox(
                                label="Speaker Identification",
                                value=True
                            )
                            speakers = gr.Slider(
                                minimum=1,
                                maximum=10,
                                step=1,
                                value=2,
                                label="Max Speakers"
                            )
                        
                        with gr.Row():
                            timestamps = gr.Checkbox(
                                label="Timestamps",
                                value=True
                            )
                            profanity = gr.Dropdown(
                                choices=["masked", "removed", "raw"],
                                value="masked",
                                label="Profanity Filter"
                            )
                        
                        with gr.Row():
                            punctuation = gr.Dropdown(
                                choices=["automatic", "dictated", "none"],
                                value="automatic",
                                label="Punctuation"
                            )
                            lexical = gr.Checkbox(
                                label="Lexical Form",
                                value=False
                            )
                        
                        submit_btn = gr.Button(
                            "🚀 Start Transcription",
                            variant="primary",
                            size="lg"
                        )
                    
                    # Right column - Results
                    with gr.Column(scale=1):
                        gr.Markdown("### 📊 Status & Results")
                        
                        # Auto-refresh indicator
                        auto_refresh_status_display = gr.Textbox(
                            label="",
                            lines=1,
                            interactive=False,
                            show_label=False,
                            visible=False,
                            elem_classes=["auto-refresh-indicator"]
                        )
                        
                        status_display = gr.Textbox(
                            label="",
                            lines=4,
                            interactive=False,
                            show_label=False,
                            placeholder="Upload a file and click 'Start Transcription' to begin...\nStatus will auto-refresh every 10 seconds during processing.\nYour data is stored in your private user folder for PDPA compliance.\nCompleted transcripts can be used for AI summarization.",
                            elem_classes=["status-display"]
                        )
                        
                        job_info = gr.Textbox(
                            label="",
                            lines=1,
                            interactive=False,
                            show_label=False,
                            placeholder=""
                        )
                        
                        with gr.Row():
                            refresh_btn = gr.Button(
                                "🔄 Check Status",
                                variant="secondary"
                            )
                            stop_refresh_btn = gr.Button(
                                "⏹️ Stop Auto-Refresh",
                                variant="secondary"
                            )
                        
                        gr.Markdown("### 📄 Transcript Results")
                        
                        transcript_output = gr.Textbox(
                            label="Transcript",
                            lines=12,
                            interactive=False,
                            placeholder="Your transcript with speaker identification and precise timestamps (HH:MM:SS) will appear here...\nThis transcript will be available for AI-powered summarization.",
                            elem_classes=["status-display"]
                        )
                        
                        download_file = gr.File(
                            label="Download Transcript",
                            interactive=False,
                            visible=False
                        )
            
            # AI Summary tab with proper structure
            with gr.Tab("🤖 AI Summary Conference"):
                gr.Markdown("### 🎯 AI-Powered Conference Summarization")
                gr.Markdown("*Generate intelligent summaries from transcripts, documents, audio/video files, and visual content using Azure AI Foundry*")
                
                with gr.Row():
                    # INPUT CONTENT Block
                    with gr.Column(scale=1, elem_classes=["ai-content-sources"]):
                        gr.Markdown("## 📂 INPUT CONTENT")
                        
                        # Part 1: Audio/Video Content with Tabs
                        gr.Markdown("#### Audio/Video Content")
                        with gr.Tabs():
                            with gr.Tab("📜 Existing Transcripts"):
                                available_transcripts = gr.Dropdown(
                                    label="Select Transcript",
                                    choices=[],
                                    value=None,
                                    multiselect=True
                                )
                                refresh_transcripts_btn = gr.Button(
                                    "🔄 Refresh Transcripts",
                                    variant="secondary",
                                    size="sm"
                                )
                                
                            with gr.Tab("🎥 New Audio/Video Files"):
                                new_audio_video_file = gr.File(
                                    label="Upload Audio/Video File",
                                    file_count="single",
                                    file_types=[
                                        ".mp4", ".mov", ".avi", ".mkv", ".webm", ".flv", ".3gp", ".wmv",
                                        ".wav", ".mp3", ".ogg", ".opus", ".flac", ".wma", ".aac", ".m4a", ".amr", ".speex"
                                    ],
                                    elem_classes=["file-upload-area"]
                                )
                        
                        # Part 2: Document/Image Uploads for OCR
                        gr.Markdown("#### Document/Image Files (OCR Processing)")
                        document_image_files = gr.File(
                            label="Upload Documents/Images for OCR Text Extraction",
                            file_count="multiple",
                            file_types=[
                                ".pdf", ".docx", ".doc", ".pptx", ".ppt", ".xlsx", ".xls", ".txt", ".csv", ".json",
                                ".jpg", ".jpeg", ".png", ".bmp", ".gif", ".tiff", ".webp"
                            ],
                            elem_classes=["file-upload-area"]
                        )
                        
                        gr.HTML("""

                        <div class="help-text">

                            <p><strong>Documents:</strong> PDF, DOCX, DOC, PPTX, PPT, XLSX, XLS, TXT, CSV, JSON</p>

                            <p><strong>Images:</strong> JPG, JPEG, PNG, BMP, GIF, TIFF, WebP</p>

                            <p><strong>OCR Process:</strong> Extract text from documents and images for AI analysis</p>

                        </div>

                        """)
                    
                    # AI Instructions
                    with gr.Column(scale=1, elem_classes=["ai-instructions"]):
                        gr.Markdown("## 🧠 AI Instructions")
                        
                        gr.Markdown("#### Instructions for AI")
                        ai_instructions = gr.Textbox(
                            label="",
                            lines=6,
                            placeholder="Describe the conference type, desired format, and any corrections...\n\nExample: 'สรุปการประชุมรายไตรมาสนี้ โฟกัสที่ผลการเงิน การตัดสินใจสำคัญ และแผนงาน สร้างบทสรุปผู้บริหารพร้อมจุดสำคัญ'",
                            value="สรุปเนื้อหาการประชุมหรือเอกสารนี้ โดยจัดรูปแบบเป็นหัวข้อและรายละเอียดสำคัญ พร้อมระบุประเด็นที่ต้องติดตาม",  # Default Thai instructions
                            show_label=False
                        )
                        
                        with gr.Row():
                            summary_format = gr.Dropdown(
                                choices=["บทสรุปผู้บริหาร", "รายงานการประชุม", "แผนงานและภารกิจ", "ประเด็นสำคัญ", "การวิเคราะห์เชิงลึก"],
                                value="บทสรุปผู้บริหาร",
                                label="Format"
                            )
                            output_language = gr.Dropdown(
                                choices=["Thai", "English", "Spanish", "French", "German", "Chinese", "Japanese"],
                                value="Thai",  # Default to Thai
                                label="Language"
                            )
                        
                        gr.Markdown("#### Focus Areas")
                        focus_areas = gr.Textbox(
                            label="",
                            placeholder="เช่น ผลการเงิน การตัดสินใจ การพูดคุยด้านเทคนิค",
                            show_label=False
                        )
                        
                        with gr.Row():
                            include_timestamps = gr.Checkbox(
                                label="เวลา (Timestamps)",
                                value=True
                            )
                            include_action_items = gr.Checkbox(
                                label="รายการภารกิจ",
                                value=True
                            )
                        
                        generate_summary_btn = gr.Button(
                            "🚀 สร้าง AI Summary",
                            variant="primary",
                            size="lg",
                            elem_classes=["ai-button"]
                        )
                    
                    # Status & Results
                    with gr.Column(scale=1, elem_classes=["ai-results"]):
                        gr.Markdown("## 📊 Status & Results")
                        
                        ai_status_display = gr.Textbox(
                            label="",
                            lines=3,
                            interactive=False,
                            show_label=False,
                            placeholder="ไม่มีงาน AI Summary ที่กำลังดำเนินการ",
                            elem_classes=["ai-status-display"]
                        )
                        
                        # Auto-refresh indicator for AI
                        ai_auto_refresh_status = gr.Textbox(
                            label="",
                            lines=1,
                            interactive=False,
                            show_label=False,
                            visible=False,
                            elem_classes=["auto-refresh-indicator"]
                        )
                        
                        ai_job_info = gr.Textbox(
                            label="",
                            lines=1,
                            interactive=False,
                            show_label=False,
                            placeholder=""
                        )
                        
                        check_ai_status_btn = gr.Button(
                            "🔄 Check Status",
                            variant="secondary"
                        )
                        
                        gr.Markdown("#### AI Summary Results")
                        ai_summary_output = gr.Textbox(
                            label="",
                            lines=12,
                            interactive=False,
                            show_label=False,
                            placeholder="ผลลัพธ์ AI Summary พร้อมข้อมูลเชิงลึก ประเด็นสำคัญ และคำแนะนำที่สามารถนำไปปฏิบัติได้จะแสดงที่นี่...",
                            elem_classes=["ai-status-display"]
                        )
                        
                        ai_download_file = gr.File(
                            label="Download AI Summary",
                            interactive=False,
                            visible=False
                        )
            
            # Enhanced History tab with separate services
            with gr.Tab("📚 My History"):
                gr.Markdown("### 📋 Your Service History")
                gr.Markdown("*View and download your transcription and AI summarization history (PDPA compliant - only your data)*")
                
                # Service History Tabs
                with gr.Tabs():
                    # Transcription History Tab
                    with gr.Tab("🎙️ Transcription History"):
                        with gr.Row():
                            refresh_transcription_history_btn = gr.Button(
                                "🔄 Refresh Transcription History",
                                variant="primary"
                            )
                            show_all_transcriptions_checkbox = gr.Checkbox(
                                label="Show All Transcription Records (not just recent 20)",
                                value=False
                            )
                        
                        transcription_history_table = gr.Dataframe(
                            headers=["Date", "Filename", "Language", "Status", "Duration", "Job ID", "Download"],
                            datatype=["str", "str", "str", "str", "str", "str", "str"],
                            col_count=(7, "fixed"),
                            row_count=(20, "dynamic"),
                            wrap=True,
                            interactive=False,
                            elem_classes=["history-table"]
                        )
                        
                        # Download Files Section for Transcriptions
                        gr.Markdown("### 📥 Download Your Transcripts")
                        gr.Markdown("*Your available transcript downloads will appear below after refreshing*")
                        
                        with gr.Column():
                            transcript_download_1 = gr.File(label="", visible=False, interactive=False)
                            transcript_download_2 = gr.File(label="", visible=False, interactive=False)
                            transcript_download_3 = gr.File(label="", visible=False, interactive=False)
                            transcript_download_4 = gr.File(label="", visible=False, interactive=False)
                            transcript_download_5 = gr.File(label="", visible=False, interactive=False)
                    
                    # AI Summary History Tab
                    with gr.Tab("🤖 AI Summary History"):
                        with gr.Row():
                            refresh_ai_summary_history_btn = gr.Button(
                                "🔄 Refresh AI Summary History",
                                variant="primary"
                            )
                            show_all_summaries_checkbox = gr.Checkbox(
                                label="Show All AI Summary Records (not just recent 20)",
                                value=False
                            )
                        
                        ai_summary_history_table = gr.Dataframe(
                            headers=["Date", "Sources", "Language", "Status", "Duration", "Job ID", "Download"],
                            datatype=["str", "str", "str", "str", "str", "str", "str"],
                            col_count=(7, "fixed"),
                            row_count=(20, "dynamic"),
                            wrap=True,
                            interactive=False,
                            elem_classes=["history-table"]
                        )
                        
                        # Download Files Section for AI Summaries
                        gr.Markdown("### 📥 Download Your AI Summaries")
                        gr.Markdown("*Your available AI summary downloads will appear below after refreshing*")
                        
                        with gr.Column():
                            summary_download_1 = gr.File(label="", visible=False, interactive=False)
                            summary_download_2 = gr.File(label="", visible=False, interactive=False)
                            summary_download_3 = gr.File(label="", visible=False, interactive=False)
                            summary_download_4 = gr.File(label="", visible=False, interactive=False)
                            summary_download_5 = gr.File(label="", visible=False, interactive=False)
            
            # Enhanced Privacy & Data tab
            with gr.Tab("🔒 Privacy & Data"):
                gr.Markdown("### 🔒 Enhanced GDPR & Data Protection")
                gr.Markdown("Manage your personal data and privacy settings for both transcription and AI services in compliance with data protection regulations.")
                
                with gr.Column(elem_classes=["pdpa-section"]):
                    gr.Markdown("#### 📊 Complete Data Export")
                    gr.Markdown("Download all your personal data including transcriptions, AI summaries, account info, and usage statistics.")
                    
                    export_btn = gr.Button("📦 Export My Complete Data", variant="primary")
                    export_status = gr.Textbox(
                        label="",
                        show_label=False,
                        interactive=False,
                        placeholder="Click 'Export My Complete Data' to download your comprehensive data archive (transcripts + AI summaries)"
                    )
                    export_file = gr.File(
                        label="Your Complete Data Export",
                        visible=False,
                        interactive=False
                    )
                
                with gr.Column(elem_classes=["pdpa-section"]):
                    gr.Markdown("#### 📧 Marketing Consent")
                    gr.Markdown("Update your preferences for receiving marketing communications about new AI features.")
                    
                    marketing_consent_checkbox = gr.Checkbox(
                        label="I consent to receiving marketing communications about new AI features",
                        value=False
                    )
                    update_consent_btn = gr.Button("✅ Update Consent", variant="secondary")
                    consent_status = gr.Textbox(
                        label="",
                        show_label=False,
                        interactive=False,
                        placeholder="Update your marketing consent preferences"
                    )
                
                with gr.Column(elem_classes=["pdpa-section"]):
                    gr.Markdown("#### ⚠️ Complete Account Deletion")
                    gr.Markdown("""

                    **Warning**: This action is irreversible and will permanently delete:

                    - Your user account and profile

                    - All transcription history and files

                    - All AI summary history and results

                    - All data stored in Azure Blob Storage

                    - Usage statistics and preferences

                    - Any stored AI model interactions

                    """)
                    
                    deletion_confirmation = gr.Textbox(
                        label="Type 'DELETE MY ACCOUNT' to confirm",
                        placeholder="Type the exact phrase to confirm complete account deletion"
                    )
                    delete_account_btn = gr.Button(
                        "🗑️ Delete My Complete Account",
                        variant="stop",
                        elem_classes=["danger-button"]
                    )
                    deletion_status = gr.Textbox(
                        label="",
                        show_label=False,
                        interactive=False,
                        placeholder="Complete account deletion requires confirmation text"
                    )
    
    # Auto-refresh timers
    transcript_timer = gr.Timer(10.0)
    ai_timer = gr.Timer(10.0)
    
    # Event handlers
    
    # Authentication events (same as before)
    login_btn.click(
        login_user,
        inputs=[login_email, login_password],
        outputs=[login_status, current_user, auth_section, main_app, user_stats_display]
    ).then(
        on_user_login,
        inputs=[current_user],
        outputs=[marketing_consent_checkbox]
    ).then(
        lambda user: ("", "") if user else (gr.update(), gr.update()),
        inputs=[current_user],
        outputs=[login_email, login_password]
    ).then(
        get_available_transcripts,
        inputs=[current_user],
        outputs=[available_transcripts]
    )
    
    register_btn.click(
        register_user,
        inputs=[reg_email, reg_username, reg_password, reg_confirm_password, 
                gdpr_consent, data_retention_consent, marketing_consent],
        outputs=[register_status, login_after_register]
    ).then(
        lambda status: ("", "", "", "", False, False, False) if "✅" in status else (gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update()),
        inputs=[register_status],
        outputs=[reg_email, reg_username, reg_password, reg_confirm_password, gdpr_consent, data_retention_consent, marketing_consent]
    )
    
    login_after_register.click(
        lambda: (gr.update(selected=0), ""),
        outputs=[auth_tabs, register_status]
    )
    
    logout_btn.click(
        logout_user,
        outputs=[current_user, login_status, auth_section, main_app, user_stats_display]
    )
    
    # Transcription events
    submit_btn.click(
        submit_transcription,
        inputs=[
            file_upload, language, audio_format, diarization_enabled,
            speakers, profanity, punctuation, timestamps, lexical, current_user
        ],
        outputs=[status_display, transcript_output, download_file, job_info, job_state, auto_refresh_status_display, user_stats_display]
    )
    
    refresh_btn.click(
        check_current_job_status,
        inputs=[job_state, current_user],
        outputs=[status_display, transcript_output, download_file, job_info, auto_refresh_status_display, user_stats_display]
    )
    
    # AI Summary events - simplified structure
    refresh_transcripts_btn.click(
        lambda user: gr.update(choices=[(job.original_filename + f" ({job.created_at[:16]})", job.job_id) 
                                       for job in transcription_manager.get_user_history(user.user_id, limit=50) 
                                       if job.status == 'completed' and job.transcript_text] if user else []),
        inputs=[current_user],
        outputs=[available_transcripts]
    )
    
    generate_summary_btn.click(
        submit_ai_summary_enhanced,
        inputs=[
            available_transcripts, new_audio_video_file, document_image_files,
            ai_instructions, summary_format, output_language, focus_areas, 
            include_timestamps, include_action_items, current_user
        ],
        outputs=[ai_status_display, ai_summary_output, ai_download_file, ai_job_info, summary_job_state, ai_auto_refresh_status, user_stats_display]
    )
    
    check_ai_status_btn.click(
        check_ai_summary_status,
        inputs=[summary_job_state, current_user],
        outputs=[ai_status_display, ai_summary_output, ai_download_file, ai_job_info, ai_auto_refresh_status, user_stats_display]
    )
    
    # Auto-refresh timer events
    transcript_timer.tick(
        auto_refresh_status,
        inputs=[job_state, current_user],
        outputs=[status_display, transcript_output, download_file, job_info, auto_refresh_status_display, user_stats_display]
    )
    
    ai_timer.tick(
        auto_refresh_ai_summary,
        inputs=[summary_job_state, current_user],
        outputs=[ai_status_display, ai_summary_output, ai_download_file, ai_job_info, ai_auto_refresh_status, user_stats_display]
    )
    
    # History events - Separate for each service with downloads
    refresh_transcription_history_btn.click(
        refresh_transcription_history,
        inputs=[current_user, show_all_transcriptions_checkbox],
        outputs=[transcription_history_table, user_stats_display, transcript_download_1, transcript_download_2, transcript_download_3, transcript_download_4, transcript_download_5]
    )
    
    show_all_transcriptions_checkbox.change(
        refresh_transcription_history,
        inputs=[current_user, show_all_transcriptions_checkbox],
        outputs=[transcription_history_table, user_stats_display, transcript_download_1, transcript_download_2, transcript_download_3, transcript_download_4, transcript_download_5]
    )
    
    refresh_ai_summary_history_btn.click(
        refresh_ai_summary_history,
        inputs=[current_user, show_all_summaries_checkbox],
        outputs=[ai_summary_history_table, user_stats_display, summary_download_1, summary_download_2, summary_download_3, summary_download_4, summary_download_5]
    )
    
    show_all_summaries_checkbox.change(
        refresh_ai_summary_history,
        inputs=[current_user, show_all_summaries_checkbox],
        outputs=[ai_summary_history_table, user_stats_display, summary_download_1, summary_download_2, summary_download_3, summary_download_4, summary_download_5]
    )
    
    # PDPA compliance events
    export_btn.click(
        export_user_data,
        inputs=[current_user],
        outputs=[export_status, export_file]
    )
    
    update_consent_btn.click(
        update_marketing_consent,
        inputs=[current_user, marketing_consent_checkbox],
        outputs=[consent_status]
    )
    
    delete_account_btn.click(
        delete_user_account,
        inputs=[current_user, deletion_confirmation],
        outputs=[deletion_status, current_user, auth_section, main_app]
    )
    
    # Auto-hide/show speakers slider
    diarization_enabled.change(
        lambda enabled: gr.update(visible=enabled),
        inputs=[diarization_enabled],
        outputs=[speakers]
    )
    
    # Load initial data
    demo.load(
        lambda: (
            print("🚀 Azure-Powered AI Conference Service Started..."),
            get_user_stats_display(None)
        )[1],
        outputs=[user_stats_display]
    )

# Enhanced info section
with demo:
    gr.HTML("""

    <div style="background: linear-gradient(135deg, #ffffff, #f8f9fa); border: 2px solid #007bff; border-radius: 16px; padding: 25px; margin-top: 20px; color: #212529; box-shadow: 0 4px 12px rgba(0,123,255,0.1);">

        <h3 style="color: #007bff; margin-top: 0; font-size: 1.5em;">📋 How to Use the Advanced AI Service</h3>

        <ol style="line-height: 1.8; font-size: 14px;">

            <li><strong>🔐 Register/Login:</strong> Create an account or log in with existing credentials</li>

            <li><strong>🎙️ Transcribe:</strong> Upload audio/video files for high-quality transcription with speaker identification</li>

            <li><strong>🤖 AI Analysis:</strong> Choose from 3 content types: existing transcripts, new audio/video, or OCR images</li>

            <li><strong>📊 Monitor:</strong> Status auto-updates every 10 seconds with real-time progress</li>

            <li><strong>📥 Download:</strong> Get transcripts and AI summaries with comprehensive insights</li>

            <li><strong>🔒 Manage:</strong> Use Privacy & Data tab to export or delete all your data</li>

        </ol>

        

        <h3 style="color: #007bff; font-size: 1.4em;">🆕 Enhanced AI Summary Features</h3>

        <div style="display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 15px; margin: 15px 0;">

            <div>

                <p><strong>📜 Existing Transcripts:</strong> Select from your completed transcriptions</p>

                <p><strong>🎥 New Audio/Video:</strong> Upload files for transcription + AI analysis</p>

            </div>

            <div>

                <p><strong>🖼️ OCR Images:</strong> Extract text from images using computer vision</p>

                <p><strong>🇹🇭 Thai Default:</strong> Optimized for Thai language processing</p>

            </div>

            <div>

                <p><strong>📚 Separate History:</strong> Independent tracking for each service</p>

                <p><strong>🔄 Auto-Refresh:</strong> Real-time status updates for all processes</p>

            </div>

        </div>

        

        <h3 style="color: #007bff; font-size: 1.4em;">🎵 Enhanced File Support</h3>

        <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 15px; margin: 15px 0;">

            <div>

                <p><strong>🔹 Video:</strong> MP4, MOV, AVI, MKV, WMV, FLV, 3GP, WebM</p>

                <p><strong>🎵 Audio:</strong> WAV, MP3, OGG, OPUS, FLAC, WMA, AAC, M4A</p>

            </div>

            <div>

                <p><strong>📄 Documents:</strong> PDF, DOCX, DOC, PPTX, PPT, XLSX, XLS, CSV, TXT</p>

                <p><strong>🖼️ Images:</strong> JPG, JPEG, PNG, BMP, GIF, TIFF, WebP (with OCR)</p>

            </div>

        </div>

        

        <h3 style="color: #007bff; font-size: 1.4em;">💡 Pro Tips for Best Results</h3>

        <div style="background: linear-gradient(135deg, #e8f4f8, #d4e8fc); border-left: 4px solid #007bff; padding: 15px; border-radius: 8px; margin: 15px 0;">

            <ul style="line-height: 1.7; font-size: 13px; margin: 0;">

                <li><strong>🎙️ Transcription:</strong> WAV files process fastest, enable speaker ID for meetings</li>

                <li><strong>🤖 AI Summaries:</strong> Provide detailed Thai instructions for better results</li>

                <li><strong>📜 Use Tabs:</strong> Switch between content types easily with the new tab interface</li>

                <li><strong>🖼️ OCR Processing:</strong> Upload clear images for best text extraction results</li>

                <li><strong>📚 History:</strong> Use separate tabs to track transcription and AI summary history</li>

                <li><strong>🇹🇭 Thai Language:</strong> Both services now default to Thai for optimal local use</li>

                <li><strong>📊 Auto-Updates:</strong> Let the system update automatically during processing</li>

                <li><strong>🔒 Privacy:</strong> Export your data regularly and manage consent preferences</li>

            </ul>

        </div>

        

        <div style="text-align: center; margin-top: 20px; padding: 15px; background: linear-gradient(135deg, #007bff, #28a745); color: white; border-radius: 8px;">

            <p style="margin: 0; font-weight: 600; font-size: 14px;">🚀 Powered by Azure AI Foundry | 🔒 Enterprise-Grade Security | 🌐 GDPR Compliant | 🇹🇭 Thai Language Optimized</p>

        </div>

    </div>

    """)

if __name__ == "__main__":
    print("🚀 Starting Advanced Azure-Powered AI Conference Service...")
    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=False,
        show_error=True
    )