File size: 160,648 Bytes
160aacb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
"""
🎨 Agent UX (Interface & API Gateway)
-------------------------------------------------
This agent acts as the central controller and router. It manages the Gradio 
Lab Admin dashboard, provides headless API endpoints for the React frontend games, 
and seamlessly orchestrates data flow between the users, the Brain, and the Trust agents.
"""
import os
import time
import uuid
import glob
import hmac
import inspect
import json
import re
import shutil
import threading
import traceback
import zipfile
from datetime import datetime

import gradio as gr
import pandas as pd
from config import AppConfig 
from huggingface_hub import HfApi, hf_hub_download
try:
    import spaces
except Exception:
    spaces = None

from src import prepare_training_data  # 🟢 Import your new script
from src.dialect_rules import (
    hausa_variety_instruction,
    nigerian_variety_instruction,
    nigerian_variety_retry_prompt,
    nigerian_variety_retry_reason,
)
from src.asr_language import resolve_asr_language_code

def zero_gpu_optional(duration=60):
    def decorator(fn):
        if spaces is not None and hasattr(spaces, "GPU"):
            return spaces.GPU(duration=duration)(fn)
        return fn
    return decorator

# ==========================================
# ☁️ CLOUD SYNC CHECK
# ==========================================
def check_cloud_sync_status():
    """Checks Hugging Face for the last commit time and returns a HUD status string."""
    try:
        # 🟢 FIX: Use AppConfig to guarantee the token is found, instead of relying on os.environ
        hf_token = AppConfig.HF_TOKEN
        
        if not hf_token:
            print("Sync check warning: HF_TOKEN is None")
            return "🔴 SYNC OFFLINE (NO TOKEN)"

        api = HfApi(token=hf_token) 
        repo_id = "toecm/IEDID"
        
        commits = api.list_repo_commits(repo_id=repo_id, repo_type="dataset")
        if commits:
            last_sync = commits[0].created_at.strftime("%H:%M")
            return f"🟢 CLOUD SYNCED (Last: {last_sync})"
        return "🟡 REPO EMPTY"
    except Exception as e:
        print(f"Sync check error: {e}")
        return "🔴 SYNC OFFLINE"
        
class AgentUX:
    def __init__(self, input_agent, brain_agent, trust_agent, acoustic_agent=None):
        print("\n" + "="*40)
        print("🕵️‍♂️ UX AGENT: SECRETS AUDIT")
        pk = os.environ.get("PRIVATE_KEY")
        rpc = os.environ.get("PURECHAIN_RPC_URL")
        
        print(f"🔑 PRIVATE_KEY    : {'✅ LOADED' if pk else '❌ MISSING'}")
        print(f"🌐 RPC_URL        : {rpc if rpc else '❌ MISSING'}")
        print(f"🧠 GEMINI_KEY     : {'✅ LOADED' if os.environ.get('GOOGLE_API_KEY') else '❌ MISSING'}")
        print(f"☁️  HF_TOKEN       : {'✅ LOADED' if os.environ.get('HF_TOKEN') else '❌ MISSING'}")
        print(f"🛡️ REVIEW_KEY     : {'✅ LOADED' if (os.environ.get('PURE_REVIEW_KEY') or os.environ.get('PURE_ADMIN_KEY') or os.environ.get('ADMIN_PASSCODE')) else '❌ MISSING - review/admin mutations disabled'}")
        print("="*40 + "\n")
        
        self.input = input_agent
        self.brain = brain_agent
        self.trust = trust_agent
        self.acoustic = acoustic_agent
        if self.acoustic and hasattr(self.acoustic, "attach_input_agent"):
            self.acoustic.attach_input_agent(self.input)
        self.TONES = ["Neutral / Conversational", "Casual / Slang", "Formal / Professional", "Proverb / Idiom"]
        self.PENDING_FILE = "/app/pending_approvals.csv"
        self.ADMIN_ACTION_LOG = os.environ.get("ADMIN_ACTION_LOG_FILE", "/app/admin_action_log.jsonl")
        self.last_audio_path = None
        self.last_pending_count = 0
        
        # 🟢 NEW: Matchmaking & Live Room Memory
        self.waiting_pool = []       # List of operators looking for a match
        self.active_matches = {}     # Maps Operator ID -> Room Code
        self.live_rooms = {}         # Stores the actual chat logs per room
        
        self.alert_sound = None
        
        print("🎨 Agent UX Online: PhD Research Hub Ready.")
        self.sync_pending_queue(direction="down")
        self.sync_admin_action_log(direction="down")

    def get_quota_status(self):
        if hasattr(self.brain, 'check_quota'): return self.brain.check_quota()
        if hasattr(self.brain.gemini_manager, 'get_status_string'): return self.brain.gemini_manager.get_status_string()
        return "Active"

    def _resolve_ai_model_label(self):
        manager = getattr(getattr(self, "brain", None), "gemini_manager", None)
        model = getattr(manager, "model_flash", None) or getattr(manager, "last_used_model", None)
        if model:
            return str(model)
        if os.environ.get("QWEN_MODEL_NAME"):
            return os.environ.get("QWEN_MODEL_NAME")
        if os.environ.get("GROQ_API_KEY"):
            return "llama-3.3-70b-versatile"
        return "Dataset/Persona/Fallback"

    def _resolve_asr_model_label(self):
        model_name = getattr(getattr(self, "input", None), "model_name", None)
        return f"Whisper {model_name}" if model_name else "Whisper tiny"

    def get_blockchain_health(self):
        try:
            if hasattr(self.trust, 'w3') and self.trust.w3 and self.trust.w3.is_connected():
                return "<div style='padding: 10px; border-radius: 8px; background-color: rgba(46, 204, 113, 0.1); border: 1px solid #2ecc71; text-align: center; color: #2ecc71; font-size: 16px;'><strong>🟢 PureChain Network: ONLINE & SYNCED</strong></div>"
        except Exception:
            pass
        return "<div style='padding: 10px; border-radius: 8px; background-color: rgba(231, 76, 60, 0.1); border: 1px solid #e74c3c; text-align: center; color: #e74c3c; font-size: 16px;'><strong>🔴 PureChain Network: OFFLINE / DISCONNECTED</strong></div>"

    def check_background_status(self):
        tasks = self.trust.active_tasks
        current_time = datetime.now().strftime('%H:%M:%S')
        if tasks > 0:
            return f"🔄 Processing {tasks} background task(s)... | {current_time}"
        return f"✅ System Active (Ready) | {current_time}"

    def _review_key(self):
        return (os.environ.get("PURE_REVIEW_KEY") or os.environ.get("PURE_ADMIN_KEY") or os.environ.get("ADMIN_PASSCODE") or "").strip()

    def _review_scope(self, review_key):
        provided = str(review_key or "").strip()
        if not provided:
            return None
        for env_name in ["PURE_REVIEW_KEY", "PURE_ADMIN_KEY", "ADMIN_PASSCODE"]:
            expected = os.environ.get(env_name, "").strip()
            if expected and hmac.compare_digest(provided, expected):
                return {"kind": "full", "label": "Full admin"}
        scoped = [
            ("PURE_ADMIN_KEY_CH", "Chinese pending reviewer", ["chinese"], ["mandarin", "cantonese", "hakka", "gan", "jin", "wu", "yue", "min", "xiang", "gan chinese", "jin chinese", "mandarin chinese", "yue chinese", "hakka chinese", "wu chinese", "xiang chinese", "min nan chinese", "min dong chinese", "min bei chinese", "standard mandarin", "beijing dialect", "taiwanese mandarin", "singaporean mandarin", "guangzhou cantonese", "hong kong cantonese", "macau cantonese", "nanchang", "ji'an", "jian", "jian'ou", "yichun", "zhangjiakou-hohhot", "lüliang", "luliang", "bingzhou", "fuzhou", "hainanese", "hokkien", "teochew", "hailu", "meixian", "sixian", "shanghainese", "suzhounese", "wenzhounese", "loushao", "chenxü", "chenxu", "changyi"]),
            ("PURE_ADMIN_KEY_KO", "Korean pending reviewer", ["korean"], ["satoori", "jeju", "gyeongsang", "jeolla", "chungcheong", "gangwon", "hamgyong", "pyongan", "hwanghae"]),
        ]
        for env_name, label, language_terms, dialect_terms in scoped:
            expected = os.environ.get(env_name, "").strip()
            if expected and hmac.compare_digest(provided, expected):
                return {
                    "kind": "scoped",
                    "label": label,
                    "language_terms": language_terms,
                    "dialect_terms": dialect_terms,
                }
        return None

    def _is_review_authorized(self, review_key):
        return self._review_scope(review_key) is not None

    def _is_full_admin_authorized(self, review_key):
        scope = self._review_scope(review_key)
        return bool(scope and scope.get("kind") == "full")

    def _scope_allows_language_dialect(self, scope, language="", dialect="", *extra_texts):
        if not scope:
            return False
        if scope.get("kind") == "full":
            return True
        language_text = str(language or "").lower()
        dialect_text = str(dialect or "").lower()
        extra_text = " ".join(str(value or "").lower() for value in extra_texts)
        combined = f"{language_text} {dialect_text} {extra_text}"
        if any(self._scope_term_matches(combined, term) for term in scope.get("language_terms", [])):
            return True
        if any(self._scope_term_matches(combined, term) for term in scope.get("dialect_terms", [])):
            return True
        return False

    def _scope_term_matches(self, text, term):
        text = str(text or "").lower()
        term = str(term or "").lower()
        if not term:
            return False
        if len(term) <= 3:
            import re
            return re.search(r"(?<![a-z0-9])" + re.escape(term) + r"(?![a-z0-9])", text) is not None
        return term in text

    def _row_value_case_insensitive(self, row, *names):
        if row is None:
            return ""
        for name in names:
            try:
                value = row.get(name, "")
            except Exception:
                value = ""
            if value is not None and str(value).strip():
                return value
        try:
            keys = list(row.keys())
        except Exception:
            return ""
        wanted = {str(name).lower() for name in names}
        for key in keys:
            if str(key).lower() in wanted:
                try:
                    value = row.get(key, "")
                except Exception:
                    value = ""
                if value is not None and str(value).strip():
                    return value
        return ""

    def _infer_pending_languages(self, df):
        if df is None or df.empty:
            return df
        try:
            hierarchy = json.loads(self.api_get_language_hierarchy() or "{}")
        except Exception:
            hierarchy = {}
        reverse_hierarchy = {}
        for lang, dialects in hierarchy.items():
            for dialect in dialects:
                reverse_hierarchy[str(dialect)] = str(lang)
        lower_cols = {str(col).lower(): col for col in df.columns}
        if "Language" not in df.columns:
            source_col = lower_cols.get("language")
            df["Language"] = df[source_col] if source_col is not None else ""
        if "Dialect" not in df.columns:
            source_col = lower_cols.get("dialect")
            df["Dialect"] = df[source_col] if source_col is not None else ""
        df["Language"] = df.apply(
            lambda row: reverse_hierarchy.get(str(row.get("Dialect", "")), "Other")
            if pd.isna(row.get("Language")) or not str(row.get("Language", "")).strip()
            else row.get("Language", ""),
            axis=1,
        )
        return df

    def _filter_dataframe_by_review_scope(self, df, scope):
        if scope and scope.get("kind") == "full":
            return df
        if df is None or df.empty:
            return df
        df = self._infer_pending_languages(df.copy())
        return df[df.apply(
            lambda row: self._scope_allows_language_dialect(
                scope,
                self._row_value_case_insensitive(row, "Language", "language"),
                self._row_value_case_insensitive(row, "Dialect", "dialect"),
                self._row_value_case_insensitive(row, "Data_Origin", "data_origin", "Game", "game"),
            ),
            axis=1,
        )]

    def _pending_row_allowed_by_scope(self, row, scope):
        return self._scope_allows_language_dialect(
            scope,
            self._row_value_case_insensitive(row, "Language", "language"),
            self._row_value_case_insensitive(row, "Dialect", "dialect"),
            self._row_value_case_insensitive(row, "Data_Origin", "data_origin", "Game", "game"),
        )

    def _full_admin_required_message(self):
        return "🔒 Full admin passcode required. Scoped language keys can only manage their Pending Audit rows."

    def _review_unauthorized_json(self):
        return json.dumps({
            "status": "unauthorized",
            "message": "Reviewer/admin passcode required. Set PURE_ADMIN_KEY, PURE_REVIEW_KEY, PURE_ADMIN_KEY_CH, or PURE_ADMIN_KEY_KO in the Space secrets."
        })

    def _review_unauthorized_message(self):
        return "🔒 Reviewer/admin passcode required. Set PURE_ADMIN_KEY, PURE_REVIEW_KEY, PURE_ADMIN_KEY_CH, or PURE_ADMIN_KEY_KO in the Space secrets."

    def _review_empty_feedback_dataframe(self):
        return pd.DataFrame(columns=["Timestamp", "Operator_ID", "Feedback_Text", "Image_Reference"])

    def _review_empty_history_dataframe(self):
        return pd.DataFrame(columns=["Timestamp", "Utterance", "Dialect", "Data_Origin", "Data_Approved", "Block", "TX Hash"])

    def _voice_privacy_metadata(self, source_tag="", pragmatics="", context="", clar_source=""):
        combined = "\n".join(str(value or "") for value in [source_tag, pragmatics, context, clar_source]).lower()
        mode = "transcript_only"
        if "voice_privacy_mode=raw_consent" in combined or '"voice_privacy_mode": "raw_consent"' in combined:
            mode = "raw_consent"
        elif "voice_privacy_mode=altered_audio" in combined or '"voice_privacy_mode": "altered_audio"' in combined:
            mode = "altered_audio"

        consent = any(token in combined for token in [
            "voice_audio_consent=true",
            "voice_audio_consent=yes",
            '"voice_audio_consent": true',
            '"voice_audio_consent": "true"',
        ])
        raw_audio_allowed = mode == "raw_consent" and consent
        return {
            "voice_privacy_mode": mode,
            "voice_audio_consent": consent,
            "raw_audio_uploaded": raw_audio_allowed,
            "dataset_audio_policy": "explicit_raw_audio_consent" if raw_audio_allowed else "transcript_only_no_raw_audio",
        }

    # ==========================================
    #            REACT API ENDPOINTS
    # ==========================================
    # 🟢 FIX: Added 'dummy_trigger' to fix the React Zero-Input bug
    def api_get_dialects(self):
        
        print("\n" + "="*40)
        print("📡 REACT API WAKEUP: Requesting Dialects...")
        
        dialects = set()
        ignore_list = ["minted_history", "system_feedback", "pending_approvals", "train", "dataset_export", "eeqs_events"] 
        
        try:
            # 🟢 Directly target the IEDID directory
            dataset_dir = getattr(self.brain.config, 'DATASET_DIR', "/app/IEDID")
            print(f"📂 Scanning directory: {dataset_dir}")
            
            if os.path.exists(dataset_dir):
                files = glob.glob(os.path.join(dataset_dir, "**", "*.csv"), recursive=True)
                print(f"📄 Found {len(files)} CSV files in folder.")
                for f in files:
                    name = os.path.basename(f).replace(".csv", "")
                    if name not in ignore_list:
                        dialects.add(name)
            else:
                print(f"⚠️ Directory {dataset_dir} does not exist!")

            if not dialects:
                print("⚠️ No valid dialects found. Using defaults.")
                dialects = {"American English", "British English", "Nigerian Pidgin English"}

            final_list = sorted(list(dialects)) + ["+ Add New Dialect"]
            print(f"✅ SUCCESS: Sending to React -> {final_list}")
            print("="*40 + "\n")
            
            return json.dumps(final_list)
            
        except Exception as e:
            print(f"🚨 CRITICAL API ERROR: {e}")
            return json.dumps(["American English", "British English", "+ Add New Dialect"])

    def api_get_language_hierarchy(self):
        print("\n" + "="*40)
        print("📡 REACT API WAKEUP: Requesting Language Hierarchy...")
        
        hierarchy = {}
        ignore_list = ["minted_history", "system_feedback", "pending_approvals", "train", "dataset_export", "rejected_graveyard", "eeqs_events"] 
        
        try:
            dataset_dir = getattr(self.brain.config, 'DATASET_DIR', "/app/IEDID")
            print(f"📂 Scanning directory for hierarchy: {dataset_dir}")
            
            if os.path.exists(dataset_dir):
                files = glob.glob(os.path.join(dataset_dir, "**", "*.csv"), recursive=True)
                for f in files:
                    name = os.path.basename(f).replace(".csv", "")
                    if name in ignore_list:
                        continue
                    
                    parent_dir = os.path.dirname(f)
                    grandparent_dir = os.path.dirname(parent_dir)
                    if os.path.basename(parent_dir) == "data":
                        language = os.path.basename(grandparent_dir)
                    else:
                        language = os.path.basename(parent_dir)
                    
                    if language == os.path.basename(dataset_dir):
                        language = "Other"
                        
                    if language not in hierarchy:
                        hierarchy[language] = set()
                    hierarchy[language].add(name)
            else:
                print(f"⚠️ Directory {dataset_dir} does not exist!")

            final_hierarchy = {}
            for lang, dialects in hierarchy.items():
                final_hierarchy[lang] = sorted(list(dialects)) + ["+ Add New Dialect"]
            
            if not final_hierarchy:
                final_hierarchy = {"English": ["American English", "British English", "+ Add New Dialect"]}
                
            print(f"✅ SUCCESS: Sending Hierarchy to React")
            print("="*40 + "\n")
            
            return json.dumps(final_hierarchy)
            
        except Exception as e:
            print(f"🚨 CRITICAL API ERROR: {e}")
            return json.dumps({"English": ["American English", "British English", "+ Add New Dialect"]})

    def api_generate_mission(self, topic):
        topic_str = str(topic).strip("['\"]")
        if hasattr(self.brain, 'generate_conversation_starter'):
            return self.brain.generate_conversation_starter(topic_str)
        return json.dumps({"text": f"Let's talk about {topic_str}."})

    def api_transcribe(self, audio_path, dialect):
        return self.api_transcribe_with_model(audio_path, dialect, "auto", "on")

    def api_acoustic_models(self):
        if self.acoustic and hasattr(self.acoustic, "models"):
            return json.dumps(self.acoustic.models())
        return json.dumps({"ok": False, "error": "Acoustic agent unavailable."})

    def api_acoustic_tts(self, text, language="", dialect="", voice="browser-native"):
        if self.acoustic and hasattr(self.acoustic, "tts"):
            return json.dumps(self.acoustic.tts(text, language=language, dialect=dialect, voice=voice))
        return json.dumps({"ok": False, "error": "Acoustic agent unavailable.", "text": text or ""})

    @zero_gpu_optional(duration=60)
    def api_acoustic_transcribe(self, audio_path, language="", dialect="", speech_model="auto", audio_sanitation="on"):
        if not self.acoustic or not hasattr(self.acoustic, "transcribe"):
            return json.dumps({"ok": False, "text": "", "error": "Acoustic agent unavailable."})
        try:
            result = self.acoustic.transcribe(
                audio_path,
                language=language,
                dialect=dialect,
                speech_model=speech_model,
                audio_sanitation=audio_sanitation,
            )
            return json.dumps(result)
        except Exception as e:
            return json.dumps({"ok": False, "text": "", "error": str(e)})

    @zero_gpu_optional(duration=60)
    def api_transcribe_with_model(self, audio_path, dialect, speech_model="auto", audio_sanitation="on"):
        if not audio_path: return ""
        try:
            parsed_language = ""
            parsed_dialect = str(dialect or "")
            if "|||" in parsed_dialect:
                parsed_language, parsed_dialect = (parsed_dialect.split("|||", 1) + [""])[:2]
            if self.acoustic and hasattr(self.acoustic, "transcribe"):
                result = self.acoustic.transcribe(
                    audio_path,
                    language=parsed_language,
                    dialect=parsed_dialect,
                    speech_model=speech_model,
                    audio_sanitation=audio_sanitation,
                )
                return result.get("text", "") if isinstance(result, dict) else str(result)
            if hasattr(self.input, 'transcribe'):
                asr_code, source_language, source_dialect = resolve_asr_language_code(dialect)
                speech_model_choice = str(speech_model or "auto").strip() or "auto"
                sanitation_choice = str(audio_sanitation or "on").strip().lower() or "on"
                print(f"🎧 ASR hint resolved: language={source_language or 'auto'} dialect={source_dialect or 'auto'} code={asr_code or 'auto'} speech_model={speech_model_choice} clean_audio={sanitation_choice}")
                res = self.input.transcribe(
                    audio_path,
                    language=asr_code,
                    model_choice=speech_model_choice,
                    dialect_hint=f"{source_language or ''} {source_dialect or ''} {dialect or ''}",
                    sanitize_audio=sanitation_choice
                )
                return res[0]['text'] if isinstance(res, list) else str(res)
        except Exception as e:
            return f"Transcription error: {str(e)}"
        return ""

    def api_clarify(self, text, dialect):
        try:
            if hasattr(self.brain, 'analyze_dialect_single'):
                res = self.brain.analyze_dialect_single(text, dialect)
                clarification = res.get("clarification", text)
                return json.dumps({"clarification": clarification})
        except: pass
        return json.dumps({"clarification": text})

    def _ai_model_hint(self, ai_model):
        choice = str(ai_model or "auto").strip().lower()
        hints = {
            "auto": "Use the system's smart default model for this language and task.",
            "llama": "Prefer the Llama/Groq route when available.",
            "qwen": "Prefer the Qwen route when available, especially for multilingual and non-Latin-script handling.",
            "nemotron": "Prefer the free OpenRouter Nemotron route when available, especially for reasoning and ambiguity handling.",
            "gpt-oss": "Prefer the free OpenRouter GPT-OSS route for general multilingual text work.",
            "lfm": "Prefer the free OpenRouter LFM tiny route when speed is more important than depth.",
            "openrouter-free": "Prefer OpenRouter's free auto route as a cost-free fallback.",
            "deepseek": "Prefer the paid DeepSeek route when configured, especially for careful reasoning and ambiguity handling.",
            "gemini": "Prefer the Gemini route when available.",
        }
        return hints.get(choice, hints["auto"])

    def api_clarify_with_model(self, text, dialect, ai_model="auto"):
        if not text:
            return json.dumps({"clarification": ""})
        if str(ai_model or "auto").strip().lower() == "auto":
            return self.api_clarify(text, dialect)
        try:
            if hasattr(self.brain, "_safe_generate"):
                prompt = f"""
                {self._ai_model_hint(ai_model)}
                Explain the meaning of this utterance for PureHumCom data review.
                Dialect/language: {dialect}
                Utterance: "{text}"
                Return only concise JSON with a clarification field.
                """
                raw = self.brain._safe_generate(prompt)
                cleaned = self._clean_peer_translation(raw)
                try:
                    parsed = json.loads(cleaned)
                    clarification = parsed.get("clarification") or parsed.get("text") or parsed.get("Meaning") or cleaned
                except Exception:
                    clarification = cleaned
                return json.dumps({"clarification": clarification or text})
        except Exception as e:
            print(f"Selector-aware clarify Error: {e}")
        return self.api_clarify(text, dialect)

    def _target_script_rule(self, dialect):
        d = (dialect or "").lower()
        rules = [
            (("korean", "seoul", "satoori", "jeju", "gyeongsang", "chungcheong", "jeolla", "busan"), "Hangul", r"[\uac00-\ud7af\u1100-\u11ff\u3130-\u318f]", "Write natural Korean Hangul syllables only. Do not use romanization, Chinese characters, Japanese kana, or punctuation-only placeholders."),
            (("arabic", "urdu", "uyghur", "persian", "farsi", "dari", "pashto", "shahmukhi"), "Arabic", r"[\u0600-\u06ff]", "Use Arabic-script characters appropriate for the target language or dialect."),
            (("greek", "cretan"), "Greek", r"[\u0370-\u03ff]", "Use Greek script characters."),
            (("chinese", "mandarin", "cantonese", "yue", "wu ", "xiang"), "Han", r"[\u4e00-\u9fff]", "Use Chinese Han characters where natural for the target variety."),
            (("japanese", "kansai"), "Japanese", r"[\u3040-\u30ff\u4e00-\u9fff]", "Use Japanese kana/kanji, not romanization."),
            (("thai",), "Thai", r"[\u0e00-\u0e7f]", "Use Thai script."),
            (("russian", "ukrainian", "bulgarian", "serbian", "cyrillic"), "Cyrillic", r"[\u0400-\u04ff]", "Use Cyrillic script."),
            (("hindi", "marathi", "nepali", "sanskrit"), "Devanagari", r"[\u0900-\u097f]", "Use Devanagari script."),
            (("bengali", "bangla"), "Bengali", r"[\u0980-\u09ff]", "Use Bengali script."),
            (("tamil",), "Tamil", r"[\u0b80-\u0bff]", "Use Tamil script."),
            (("telugu",), "Telugu", r"[\u0c00-\u0c7f]", "Use Telugu script."),
        ]
        for tokens, name, pattern, instruction in rules:
            if any(token in d for token in tokens):
                return {"name": name, "pattern": pattern, "instruction": instruction}
        return None

    def _clean_peer_translation(self, response):
        raw_text = getattr(response, "text", response)
        cleaned = str(raw_text or "").replace("```json", "").replace("```", "").strip()
        try:
            parsed = json.loads(cleaned)
            if isinstance(parsed, dict):
                cleaned = parsed.get("translation") or parsed.get("text") or parsed.get("translated_text") or cleaned
        except Exception:
            pass
        for prefix in ("Translation:", "Translated text:", "Output:"):
            if cleaned.lower().startswith(prefix.lower()):
                cleaned = cleaned[len(prefix):].strip()
        return cleaned.strip().strip('"').strip("'").strip()

    def _translation_has_required_script(self, translated, target_dialect):
        if not translated or re.fullmatch(r"[\s\W_]+", translated, flags=re.UNICODE):
            return False
        rule = self._target_script_rule(target_dialect)
        if not rule:
            return True
        return bool(re.search(rule["pattern"], translated))

    def _fallback_script_translation(self, text, target_dialect):
        rule = self._target_script_rule(target_dialect)
        if not rule:
            return ""

        d = (target_dialect or "").lower()
        lower_text = str(text or "").lower()

        if rule["name"] == "Hangul":
            if "echo" in lower_text or "ai mock peer" in lower_text:
                if "gyeongsang" in d or "busan" in d:
                    return "안녕하이소! 나는 에코라예. 니 AI 대화 상대라예. 경상도 사투리로 얘기할끼라예. 같이 얘기해보입시더!"
                if "jeju" in d:
                    return "안녕허우꽈! 나는 에코우다. 네 AI 대화 상대우다. 제주 사투리로 말해보쿠다. 같이 이야기해봅서!"
                if "chungcheong" in d:
                    return "안녕하세유! 나는 에코여유. 네 AI 대화 상대여유. 충청도 사투리로 얘기할게유. 같이 얘기해봐유!"
                if "jeolla" in d:
                    return "안녕허요! 나는 에코여라. 네 AI 대화 상대여라. 전라도 사투리로 말할랑께 같이 얘기해보자잉!"
                if "seoul" in d:
                    return "안녕! 나는 에코야. 네 AI 모의 대화 상대야. 서울말로 이야기할게. 같이 얘기하자!"
                return "안녕하세요! 저는 에코예요. 당신의 AI 모의 대화 상대입니다. 한국어로 이야기할게요. 함께 대화해요!"

            common_phrases = [
                (("no wahala", "no problem", "it's okay", "it is okay"), "괜찮아요."),
                (("i dey alright", "i am alright", "i'm alright", "i am fine"), "저는 괜찮아요."),
                (("you still dey", "are you still there", "you still there"), "아직 거기 있어요?"),
                (("sleep", "slept"), "벌써 잤어요?"),
                (("what happened", "wetin dey happen"), "무슨 일이에요?"),
            ]
            for needles, phrase in common_phrases:
                if any(needle in lower_text for needle in needles):
                    return phrase
            return "다시 한 번 말해 주세요."

        fallback_by_script = {
            "Arabic": "من فضلك أعد إرسال الرسالة.",
            "Greek": "Παρακαλώ στείλτε ξανά το μήνυμα.",
            "Han": "请再说一遍。",
            "Japanese": "もう一度言ってください。",
            "Thai": "กรุณาพูดอีกครั้ง",
            "Cyrillic": "Повторите, пожалуйста.",
            "Devanagari": "कृपया फिर से कहें।",
            "Bengali": "অনুগ্রহ করে আবার বলুন।",
            "Tamil": "மீண்டும் சொல்லுங்கள்.",
            "Telugu": "దయచేసి మళ్లీ చెప్పండి.",
        }
        return fallback_by_script.get(rule["name"], "")

    def _request_peer_translation(self, prompt):
        if hasattr(self.brain, '_safe_generate'):
            try:
                return self.brain._safe_generate(prompt)
            except Exception as e:
                print(f"Peer Translation safe-generate Error: {e}")

        manager = getattr(self.brain, "gemini_manager", None)
        client = getattr(manager, "client", None)
        if client and hasattr(client, "models"):
            return client.models.generate_content(model='gemini-2.0-flash', contents=prompt)
        if manager and hasattr(manager, "generate_fast") and not inspect.iscoroutinefunction(manager.generate_fast):
            try:
                return manager.generate_fast(prompt)
            except Exception as e:
                print(f"Peer Translation manager Error: {e}")
        return None

    def api_translate_peer(self, text, source_dialect, target_dialect, ai_model="auto"):
        """Translates an utterance directly from one dialect to another."""
        if not text: return ""
        script_rule = self._target_script_rule(target_dialect)
        script_instruction = script_rule["instruction"] if script_rule else "Use the target language or dialect's normal writing system. For Igbo, keep proper Igbo letters and tone/dot marks such as ị, ụ, ọ, ṅ, ẹ, á, and à where natural."
        variety_instruction = "\n".join(filter(None, [
            nigerian_variety_instruction(source_dialect, target_dialect),
            hausa_variety_instruction(source_dialect, target_dialect),
        ]))

        prompt = f"""
        MODEL ROUTING PREFERENCE: {self._ai_model_hint(ai_model)}
        You are a careful translation engine for PureHumCom.
        Translate the following utterance from {source_dialect} to {target_dialect}.
        Utterance: "{text}"
        
        CRITICAL INSTRUCTIONS:
        1. Output ONLY the translated text. No conversational filler.
        2. Preserve the cultural pragmatics and emotional tone.
        3. Do not explain the translation, just provide the direct equivalent in {target_dialect}.
        4. SCRIPT FIDELITY: {script_instruction}
        5. LANGUAGE CONSISTENCY: Once a language or dialect is established, strictly maintain translations in that dialect. Do not randomly switch to another language mid-conversation.
        6. Never answer with punctuation-only text or romanization when a native script is required.
        7. {variety_instruction}
        """
        try:
            translated = self._clean_peer_translation(self._request_peer_translation(prompt))
            boundary_reason = nigerian_variety_retry_reason(translated, target_dialect)
            if self._translation_has_required_script(translated, target_dialect) and not boundary_reason:
                return translated

            if boundary_reason:
                retry_prompt = nigerian_variety_retry_prompt(
                    text, source_dialect, target_dialect, translated, boundary_reason
                )
                retry = self._clean_peer_translation(self._request_peer_translation(retry_prompt))
                if self._translation_has_required_script(retry, target_dialect) and not nigerian_variety_retry_reason(retry, target_dialect):
                    return retry

            if script_rule:
                retry_prompt = f"""
                Your previous output failed the script requirement for {target_dialect}.
                Previous invalid output: "{translated}"

                Rewrite this utterance from {source_dialect} to {target_dialect}.
                Utterance: "{text}"

                Output ONLY the corrected translation.
                Required script: {script_rule["instruction"]}
                """
                retry = self._clean_peer_translation(self._request_peer_translation(retry_prompt))
                if self._translation_has_required_script(retry, target_dialect):
                    return retry

                fallback = self._fallback_script_translation(text, target_dialect)
                if fallback:
                    return fallback

            return translated or text
        except Exception as e:
            print(f"Peer Translation Error: {e}")
        return self._fallback_script_translation(text, target_dialect) or text

    # ==========================================
    #      DIALECT RELAY (MATCHMAKING & CHAT)
    # ==========================================
    def api_join_queue(self, operator_id, dialect, target_partner_id=""):
        import uuid, json, time
        # Clean up any old ghost matches
        if operator_id in self.active_matches:
            del self.active_matches[operator_id]
            
        # 1. Check if a specific target partner is requested and available
        partner = None
        if target_partner_id:
            for p in self.waiting_pool:
                if p['operator_id'] == target_partner_id:
                    partner = p
                    break
        
        # If targeting someone and they exist, or just picking someone (if we wanted auto)
        if partner:
            # 2. Match found! Create a room.
            self.waiting_pool.remove(partner)
            room_id = f"FREQ-{uuid.uuid4().hex[:6].upper()}"
            
            self.active_matches[operator_id] = {"room_id": room_id, "partner_dialect": partner['dialect']}
            self.active_matches[partner['operator_id']] = {"room_id": room_id, "partner_dialect": dialect}
            self.live_rooms[room_id] = []
            
            return json.dumps({"status": "matched", "room_id": room_id, "partner_dialect": partner['dialect']})
        else:
            # 3. Add self to pool to wait.
            self.waiting_pool = [p for p in self.waiting_pool if p['operator_id'] != operator_id] # Prevent duplicates
            self.waiting_pool.append({"operator_id": operator_id, "dialect": dialect, "time": time.time()})
            return json.dumps({"status": "waiting"})

    def api_get_lobby(self):
        import json
        # Return only dialect and masked operator ID for privacy/display
        lobby_data = [{"id": p["operator_id"], "dialect": p["dialect"]} for p in self.waiting_pool]
        return json.dumps(lobby_data)

    def api_check_match(self, operator_id):
        import json
        if operator_id in self.active_matches:
            match = self.active_matches[operator_id]
            # Consume the match so it cannot be re-used on a second poll (prevents ghost room reconnection)
            del self.active_matches[operator_id]
            return json.dumps({"status": "matched", "room_id": match["room_id"], "partner_dialect": match["partner_dialect"]})
        return json.dumps({"status": "waiting"})
        
    def api_leave_queue(self, operator_id):
        import json
        self.waiting_pool = [p for p in self.waiting_pool if p['operator_id'] != operator_id]
        if operator_id in self.active_matches:
            del self.active_matches[operator_id]
        return json.dumps({"status": "left"})

    def api_remote_eval_and_send(self, room_code, sender_id, text, source_dialect, target_dialect, meaning_to_send=""):
        import json, os, re, uuid, threading
        import pandas as pd
        
        if not text: return json.dumps({"status": "error", "msg": "Empty text"})
        
        standard_meaning = meaning_to_send.strip() if meaning_to_send and meaning_to_send.strip() else text
        
        # 1. Forward Lookup (Understand Source - used for dataset clarity if meaning wasn't provided)
        if not meaning_to_send or not meaning_to_send.strip():
            eval_result = self.brain.search_local_dataset(text)
            if not eval_result: eval_result = self.brain.search_personas(text)
            if eval_result:
                standard_meaning = eval_result.get("clarification", text)

        # 🟢 THE DATA COLLECTION HOOK (Silent Background Saving)
        clarification_to_save = standard_meaning
        try:
            threading.Thread(
                target=self.check_and_submit_logic,
                args=(text, source_dialect, "", clarification_to_save, "Conversational", "Relay Peer-to-Peer Chat", "Automated Relay Extraction", "Game: Relay Pair", "AI Relay Bouncer", sender_id, None, False),
                daemon=True
            ).start()
        except Exception as save_err:
            print(f"Data Hook Error: {save_err}")
            
        target_translation = standard_meaning
        target_label = target_dialect or "Standard English"
        if target_dialect and target_dialect != "Standard English":
            try:
                target_translation = self.api_translate_peer(standard_meaning, "Standard English", target_dialect)
            except Exception as translate_err:
                print(f"Relay target translation failed: {translate_err}")

        # 4. Route to Room
        if room_code not in self.live_rooms: self.live_rooms[room_code] = []
        msg = {"sender": sender_id, "original": text, "translation": target_translation, "dialect": source_dialect, "target_dialect": target_label, "standard_meaning": standard_meaning, "id": str(uuid.uuid4())[:8]}
        self.live_rooms[room_code].append(msg)
        return json.dumps({"status": "success", "message": msg})

    def api_remote_poll(self, room_code, last_index):
        import json
        try:
            idx = int(float(last_index or 0))
        except Exception:
            idx = 0
        return json.dumps(self.live_rooms.get(room_code, [])[idx:])
        
    def api_get_user_xp(self, operator_id, dialect):
        import json, os
        try:
            w3 = self.trust.w3
            contract_addr = os.environ.get("PURECHAIN_CONTRACT_ADDRESS", getattr(self.trust.config, 'PURECHAIN_CONTRACT_ADDRESS', None))
            if not contract_addr or not w3: return json.dumps({"xp": 0})
            
            raw_operator = str(operator_id).strip().lower()
            try:
                final_operator_id = w3.to_checksum_address(raw_operator)
            except:
                return json.dumps({"xp": 0})
                
            contract = w3.eth.contract(address=contract_addr, abi=self.trust.config.CONTRACT_ABI)
            xp = contract.functions.getXP(final_operator_id, dialect).call()
            return json.dumps({"xp": xp})
        except Exception as e:
            print(f"XP Fetch Error: {e}")
            return json.dumps({"xp": 0})

    def api_get_oracle_task(self, operator_id, dialect, review_key=""):
        import json, pandas as pd, os
        scope = self._review_scope(review_key)
        if not scope:
            return self._review_unauthorized_json()
        if not self._scope_allows_language_dialect(scope, "", dialect):
            return self._review_unauthorized_json()
        if not os.path.exists(self.PENDING_FILE): return json.dumps({"status": "empty"})
        try:
            df = pd.read_csv(self.PENDING_FILE)
            df = self._infer_pending_languages(df)
            if "Dialect" not in df.columns or "Chain_ID" not in df.columns: return json.dumps({"status": "empty"})
            target_df = df[(df["Dialect"] == dialect) & (df["Chain_ID"].notna()) & (df["Chain_ID"] != "")]
            target_df = self._filter_dataframe_by_review_scope(target_df, scope)
            for idx, row in target_df.iterrows():
                approvers = str(row.get("Approvers", "")).split(",")
                if operator_id not in approvers and row.get("User", "") != operator_id:
                    return json.dumps({
                        "status": "success",
                        "row_id": int(idx),
                        "chain_id": str(row["Chain_ID"]),
                        "phrase": row.get("Utterance", ""),
                        "clarification": row.get("Clarification", ""),
                        "audio": row.get("Audio", "")
                    })
            return json.dumps({"status": "empty"})
        except Exception as e:
            print(f"Get Oracle Task Error: {e}")
            return json.dumps({"status": "empty"})

    def api_submit_oracle_review(self, row_id, chain_id, operator_id, dialect, is_approve, reason="Oracle Peer Review Failed", review_key=""):
        import json, pandas as pd, os, threading
        if not self._is_review_authorized(review_key) and self._is_review_authorized(reason):
            review_key, reason = reason, "Oracle Peer Review Failed"
        scope = self._review_scope(review_key)
        if not scope:
            return self._review_unauthorized_json()
        try:
            df = pd.read_csv(self.PENDING_FILE)
            df = self._infer_pending_languages(df)
            if int(row_id) not in df.index: return json.dumps({"status": "error"})
            if not self._pending_row_allowed_by_scope(df.loc[int(row_id)], scope):
                return self._review_unauthorized_json()
            
            approvers = str(df.at[int(row_id), "Approvers"]).split(",")
            approvers.append(operator_id)
            df.at[int(row_id), "Approvers"] = ",".join(filter(None, approvers))
            df.to_csv(self.PENDING_FILE, index=False)
            self.sync_pending_queue(direction="up")
            
            def run_chain_verify():
                try:
                    w3 = self.trust.w3
                    contract_addr = os.environ.get("PURECHAIN_CONTRACT_ADDRESS", getattr(self.trust.config, 'PURECHAIN_CONTRACT_ADDRESS', None))
                    contract = w3.eth.contract(address=contract_addr, abi=self.trust.config.CONTRACT_ABI)
                    final_operator = w3.to_checksum_address(operator_id)
                    nonce = w3.eth.get_transaction_count(self.trust.account.address)
                    
                    approve_bool = str(is_approve).lower() == "true"
                    if approve_bool:
                        tx = contract.functions.verifyEntry(int(chain_id), final_operator).build_transaction({
                            'chainId': getattr(self.trust.config, 'PURECHAIN_ID', 900520900520),
                            'gas': 2000000, 'gasPrice': 0, 'nonce': nonce
                        })
                    else:
                        tx = contract.functions.rejectEntry(int(chain_id), final_operator, reason).build_transaction({
                            'chainId': getattr(self.trust.config, 'PURECHAIN_ID', 900520900520),
                            'gas': 2000000, 'gasPrice': 0, 'nonce': nonce
                        })
                    
                    signed_tx = w3.eth.account.sign_transaction(tx, self.trust.config.PRIVATE_KEY)
                    tx_hash = w3.eth.send_raw_transaction(signed_tx.raw_transaction)
                    receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
                    
                    if approve_bool:
                        events = contract.events.EntryVerified().process_receipt(receipt)
                        if events:
                            print("🎉 Oracle consensus reached (2 approvals). Moving to IEDID!")
                            row = df.loc[int(row_id)]
                            self.trust.update_dataset_csv(
                                dialect=row["Dialect"], utterance=row["Utterance"], clarification=row["Clarification"],
                                tone=row["Tone"], context=row.get("Context", ""), syntax="", audio_path=row.get("Audio", ""),
                                pragmatics=row.get("Pragmatic_Analysis", ""), sourceTag="Game (Verified)", clar_source="User",
                                userKey=row.get("User", "")
                            )
                    else:
                        row = df.loc[int(row_id)]
                        self.trust.add_to_graveyard(
                            dialect=row.get("Dialect", ""), utterance=row.get("Utterance", ""), clarification=row.get("Clarification", ""),
                            tone=row.get("Tone", ""), context=row.get("Context", ""), syntax="", audio_path=row.get("Audio", ""),
                            pragmatics=row.get("Pragmatic_Analysis", ""), sourceTag="Game (Rejected)", clar_source="User",
                            userKey=row.get("User", ""), reason=reason, rejector=operator_id, chain_id=chain_id
                        )
                        print("🔥 Oracle rejected entry. Added to Burn Ward.")
                except Exception as e:
                    print(f"Blockchain verify error: {e}")
            
            threading.Thread(target=run_chain_verify, daemon=True).start()
            return json.dumps({"status": "success"})
        except Exception as e:
            print(f"Submit Oracle Error: {e}")
            return json.dumps({"status": "error"})

    def api_get_graveyard_task(self, operator_id, dialect, review_key=""):
        import json, pandas as pd, os
        scope = self._review_scope(review_key)
        if not scope:
            return self._review_unauthorized_json()
        if not self._scope_allows_language_dialect(scope, "", dialect):
            return self._review_unauthorized_json()
        graveyard_path = os.path.join(self.trust.config.DATASET_DIR, "..", "rejected_graveyard.csv")
        if not os.path.exists(graveyard_path): return json.dumps({"status": "empty"})
        try:
            df = pd.read_csv(graveyard_path, encoding='utf-8-sig', on_bad_lines='skip')
            if "Dialect" not in df.columns or "Chain_ID" not in df.columns: return json.dumps({"status": "empty"})
            target_df = df[(df["Dialect"] == dialect) & (df["Chain_ID"].notna()) & (df["Chain_ID"] != "")]
            for idx, row in target_df.iterrows():
                if str(row.get("User", "")) != operator_id:
                    return json.dumps({
                        "status": "success",
                        "row_id": int(idx),
                        "chain_id": str(row["Chain_ID"]),
                        "phrase": str(row.get("Utterance", "")),
                        "clarification": str(row.get("Clarification", "")),
                        "audio": str(row.get("audio_file_name", "")),
                        "reason": str(row.get("Rejection_Reason", "Unknown")),
                        "rejector": str(row.get("Rejector", "Unknown"))
                    })
            return json.dumps({"status": "empty"})
        except Exception as e:
            print(f"Get Graveyard Task Error: {e}")
            return json.dumps({"status": "empty"})

    def api_submit_appeal(self, chain_id, operator_id, dialect, review_key=""):
        import json, os, threading, pandas as pd
        scope = self._review_scope(review_key)
        if not scope:
            return self._review_unauthorized_json()
        if not self._scope_allows_language_dialect(scope, "", dialect):
            return self._review_unauthorized_json()
        try:
            graveyard_path = os.path.join(self.trust.config.DATASET_DIR, "..", "rejected_graveyard.csv")
            if os.path.exists(graveyard_path):
                try:
                    df = pd.read_csv(graveyard_path, encoding='utf-8-sig', on_bad_lines='skip')
                    df['Chain_ID'] = df['Chain_ID'].astype(str)
                    df = df[df["Chain_ID"] != str(chain_id)]
                    df.to_csv(graveyard_path, index=False, encoding='utf-8-sig')
                except Exception as e:
                    print(f"Graveyard update error: {e}")

            def run_chain_appeal():
                try:
                    w3 = self.trust.w3
                    contract_addr = os.environ.get("PURECHAIN_CONTRACT_ADDRESS", getattr(self.trust.config, 'PURECHAIN_CONTRACT_ADDRESS', None))
                    contract = w3.eth.contract(address=contract_addr, abi=self.trust.config.CONTRACT_ABI)
                    final_operator = w3.to_checksum_address(operator_id)
                    nonce = w3.eth.get_transaction_count(self.trust.account.address)
                    
                    tx = contract.functions.appealEntry(int(chain_id), final_operator).build_transaction({
                        'chainId': getattr(self.trust.config, 'PURECHAIN_ID', 900520900520),
                        'gas': 2000000, 'gasPrice': 0, 'nonce': nonce
                    })
                    
                    signed_tx = w3.eth.account.sign_transaction(tx, self.trust.config.PRIVATE_KEY)
                    tx_hash = w3.eth.send_raw_transaction(signed_tx.raw_transaction)
                    w3.eth.wait_for_transaction_receipt(tx_hash)
                    print(f"⚖️ Appeal filed for Chain ID: {chain_id} by {final_operator}")
                except Exception as e:
                    print(f"Blockchain appeal error: {e}")
            
            threading.Thread(target=run_chain_appeal, daemon=True).start()
            return json.dumps({"status": "success"})
        except Exception as e:
            print(f"Submit Appeal Error: {e}")
            return json.dumps({"status": "error"})

    def api_get_room_messages(self, room_code, last_index):
        idx = int(last_index)
        return json.dumps(self.live_rooms[room_code][idx:])
    # ==========================================
    #            SOCIOLINGUISTIC PIPELINE
    # ==========================================
    def automated_pipeline(self, audio_path, language_code, request: gr.Request = None):
        client_ip = request.headers.get("x-forwarded-for") or request.client.host if request else "Unknown_IP"
        source_tag = f"Lab_Admin_{client_ip}"

        print(f"🚀 PIPELINE TRIGGERED by {source_tag}")
        headers = ["Source", "Speaker", "Utterance", "Dialect", "Clarification", "Tone", "Context", "Pragmatic Analysis"]
        empty_df = pd.DataFrame(columns=headers)
        
        if not audio_path:
            yield empty_df, "Waiting for input...", self.get_quota_status(), "", None, "Neutral / Conversational", "", ""
            return
        
        print("\n" + "="*40)
        print(f"🚀 PIPELINE TRIGGERED for {language_code}!")
        print(f"Audio Path: {audio_path}")
        
        self.last_audio_path = audio_path
        status_log = "🎧 Transcribing Audio...\n"

        try:
            print("⏳ STEP 1: Calling Whisper Transcription...")
            if hasattr(self.input, 'transcribe'):
                asr_code, _, _ = resolve_asr_language_code(language_code)
                transcription_result = self.input.transcribe(audio_path, language=asr_code)
                print(f"✅ Whisper Result: {transcription_result}")
                
                if isinstance(transcription_result, list) and len(transcription_result) > 0:
                    transcribed_text = transcription_result[0].get('text', str(transcription_result))
                else:
                    transcribed_text = str(transcription_result)   
            else:
                transcribed_text = "Audio Received."
                
            status_log += f"🗣️ Heard: '{transcribed_text}'\n\n"

            print(f"⏳ STEP 2: Checking Local Dataset...")
            status_log += "🗄️ Checking Local Dataset (Fast Match)...\n"
            final_result = self.brain.search_local_dataset(transcribed_text)

            if not final_result:
                print("⏳ STEP 3: Checking Persona...")
                status_log += "❌ No local match. 🎭 Checking Persona Context...\n"
                final_result = self.brain.search_personas(transcribed_text)

            if not final_result:
                print("⏳ STEP 4: Sending to Gemini API...")
                status_log += "❌ No persona hit. 🧠 Generating AI interpretation...\n"
                if hasattr(self.brain, 'analyze_dialect_single'):
                    final_result = self.brain.analyze_dialect_single(transcribed_text, language_code)
                    print(f"✅ Gemini Result: {final_result}")
                else:
                    raise Exception("analyze_dialect_single missing from brain_agent.")

            status_log += f"\n✅ Analysis Complete via {final_result.get('Source', 'Unknown')}."
            
            def safe_str(val, default=""):
                return default if pd.isna(val) or val is None else str(val)

            df_data = [[
                safe_str(final_result.get("Source", "Unknown")), 
                source_tag, 
                safe_str(transcribed_text), 
                safe_str(final_result.get("dialect", "")), 
                safe_str(final_result.get("clarification", "")), 
                safe_str(final_result.get("tone", "")), 
                safe_str(final_result.get("context", "")), 
                safe_str(final_result.get("pragmatics", ""))
            ]]
            df = pd.DataFrame(df_data, columns=headers)

            print("✅ PIPELINE COMPLETED SUCCESSFULLY.")
            print("="*40 + "\n")

            yield (
                df, safe_str(status_log), self.get_quota_status(), safe_str(transcribed_text), 
                safe_str(final_result.get("dialect")), safe_str(final_result.get("clarification")), 
                safe_str(final_result.get("tone", "Neutral / Conversational")), 
                safe_str(final_result.get("context")), safe_str(final_result.get("pragmatics"))
            )
            return

        except Exception as e:
            print("\n🚨 CRITICAL PIPELINE ERROR 🚨")
            traceback.print_exc()
            print("="*40 + "\n")
            status_log += f"\n❌ System Error: {e}\n"
            yield empty_df, status_log, self.get_quota_status(), "", None, "Neutral / Conversational", "", ""
            return

    # ==========================================
    #            RESEARCH DATA SUBMISSION
    # ==========================================
    def check_and_submit_logic(
        self, transcribed, dialect, customD, clarification, tone, context, pragmatics, 
        sourceTag="Web", clar_source="User", userKey="", blob=None, confirm=False,
        language="", request: gr.Request = None 
    ):
        print("\n" + "="*40)
        print(f"📥 API HIT: /check_and_submit_logic")
        print(f"   Source: {sourceTag}")
        print(f"   Text: '{transcribed}'")
        print(f"   Dialect: {dialect}")
        print("="*40)
        
        clean_key = str(userKey).strip()
        if not clean_key.startswith("0x"):
            clean_key = "0x" + uuid.uuid4().hex + uuid.uuid4().hex[:8]
            
        final_user = clean_key
        
        if sourceTag == "Web" or sourceTag == "":
            final_origin = "Gradio Admin UI"
        else:
            final_origin = sourceTag 

        if not transcribed or not clarification:
            return "⚠️ Cannot submit empty analysis.", gr.update(visible=False)

        final_d = customD if (dialect == "+ Add New Dialect" and customD) else dialect
        if not final_d or final_d == "+ Add New Dialect":
            return "⚠️ Select a dialect first.", gr.update(visible=False)

        voice_privacy = self._voice_privacy_metadata(final_origin, pragmatics, context, clar_source)
        voice_acoustic_profile = json.dumps({"voice_privacy": voice_privacy}, ensure_ascii=False)

        permanent_audio_path = ""
        if voice_privacy.get("raw_audio_uploaded") and blob is not None:
            actual_path = None
            if isinstance(blob, str): actual_path = blob
            elif isinstance(blob, dict) and 'path' in blob: actual_path = blob['path']
            elif hasattr(blob, 'name'): actual_path = blob.name
                
            if actual_path and os.path.exists(actual_path):
                save_dir = os.path.join(self.brain.config.DATASET_DIR, "audio")
                os.makedirs(save_dir, exist_ok=True)
                unique_name = f"rec_{int(time.time())}_{uuid.uuid4().hex[:6]}.wav"
                permanent_audio_path = os.path.join(save_dir, unique_name)
                
                shutil.copy(actual_path, permanent_audio_path)
                print(f"✅ Audio securely extracted to: {permanent_audio_path}")
            else:
                print(f"⚠️ Audio skipped! Blob invalid: {blob}")
        elif voice_privacy.get("raw_audio_uploaded") and self.last_audio_path:
            permanent_audio_path = self.last_audio_path
        elif blob is not None or self.last_audio_path:
            print("🔒 Voice privacy active: raw audio was not persisted; transcript-only metadata will be stored.")

        is_game_submission = ("Game" in final_origin)
        
        if not is_game_submission:
            success = self.trust.update_dataset_csv(
                final_d, transcribed, clarification, tone, context, "", 
                permanent_audio_path, pragmatics, final_origin, clar_source, final_user,
                acoustic_profile=voice_acoustic_profile, language=language
            )
            
            if success:
                payload = { "original": transcribed, "dialect": final_d, "clarification": clarification, "tone": tone, "user": final_user }
                threading.Thread(target=self.trust.stamp_on_chain, args=(payload,), daemon=True).start()
                
            return f"🚀 Approved and Minted to {final_d}", gr.update(visible=False)

        else:
            new_entry = {
                "User": final_user,               
                "Data_Origin": final_origin,      
                "Utterance": transcribed,
                "Dialect": final_d,
                "Clarification": clarification,
                "Clarification_Source": clar_source,
                "Tone": tone,
                "Context": context,
                "Pragmatic_Analysis": pragmatics,
                "Acoustic_Profile": voice_acoustic_profile,
                "Audio": permanent_audio_path,    
                "Timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
                "Chain_ID": "",
                "Approvers": "",
                "Language": language,
                "Voice_Privacy_Mode": voice_privacy.get("voice_privacy_mode", "transcript_only"),
                "Voice_Audio_Consent": str(bool(voice_privacy.get("voice_audio_consent", False))),
                "Raw_Audio_Uploaded": str(bool(voice_privacy.get("raw_audio_uploaded", False))),
            }
            
            # Start a thread to stamp on chain and then save to CSV
            def process_game_submission():
                payload = { "original": transcribed, "dialect": final_d, "clarification": clarification, "tone": tone, "user": final_user }
                entry_id = self.trust.stamp_on_chain(payload)
                if str(entry_id).isdigit():
                    new_entry["Chain_ID"] = str(entry_id)
                
                try:
                    if os.path.exists(self.PENDING_FILE): 
                        df = pd.read_csv(self.PENDING_FILE)
                    else: 
                        df = pd.DataFrame(columns=new_entry.keys())
                        
                    df = pd.concat([df, pd.DataFrame([new_entry])], ignore_index=True)
                    df.to_csv(self.PENDING_FILE, index=False)
                    self.sync_pending_queue(direction="up")
                    
                    if permanent_audio_path and os.path.exists(permanent_audio_path):
                        try:
                            api = HfApi(token=os.environ.get("HF_TOKEN"))
                            api.upload_file(
                                path_or_fileobj=permanent_audio_path,
                                path_in_repo=f"pending_audio/{os.path.basename(permanent_audio_path)}",
                                repo_id="toecm/PureChain_Dataset",
                                repo_type="dataset",
                                commit_message=f"🎙️ Staging pending audio from {final_origin}"
                            )
                        except Exception as e:
                            print(f"⚠️ Failed to stage audio: {e}")
                            
                except Exception as e:
                    print(f"Failed to save game submission: {e}")
            
            threading.Thread(target=process_game_submission, daemon=True).start()
            
            return "📥 Submitted for Peer Review (XP pending approval)", gr.update(visible=False)
                
    def force_overwrite_logic(self, *args):
        return self.check_and_submit_logic(*args, confirm=True)

    def admin_check_and_submit_logic(self, *args):
        review_key = args[-1] if args else ""
        if not self._is_full_admin_authorized(review_key):
            return self._full_admin_required_message(), gr.update(visible=False)
        return self.check_and_submit_logic(*args[:-1])

    def admin_force_overwrite_logic(self, *args):
        review_key = args[-1] if args else ""
        if not self._is_full_admin_authorized(review_key):
            return self._full_admin_required_message(), gr.update(visible=False)
        return self.check_and_submit_logic(*args[:-1], confirm=True)

    # ==========================================
    #            FEEDBACK & AUDIT HELPERS
    # ==========================================
    def handle_feedback_submission(self, op_id, text, img_blob):
        """Catches secure feedback from React games and logs it with images."""
        feedback_file = "/app/system_feedback.csv"
        image_path = ""
        
        print("\n" + "="*40)
        print("🛡️ SECURE FEEDBACK RECEIVED")
        print(f"Operator: {op_id}")
        
        # 1. Process Image if attached
        if img_blob is not None:
            actual_path = None
            if isinstance(img_blob, str): actual_path = img_blob
            elif hasattr(img_blob, 'name'): actual_path = img_blob.name
            
            if actual_path and os.path.exists(actual_path):
                unique_name = f"bug_img_{int(time.time())}.png"
                image_path = os.path.join("/app", unique_name)
                shutil.copy(actual_path, image_path)
                
                try:
                    api = HfApi(token=os.environ.get("HF_TOKEN"))
                    api.upload_file(
                        path_or_fileobj=image_path,
                        path_in_repo=f"feedback_images/{unique_name}",
                        repo_id="toecm/PureChain_Dataset",
                        repo_type="dataset",
                        commit_message="📸 New Bug Report Image"
                    )
                except Exception as e:
                    print(f"Image upload failed: {e}")

        # 2. Log to CSV
        new_row = {
            "Timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
            "Operator_ID": op_id,
            "Feedback_Text": text,
            "Image_Reference": image_path
        }
        try:
            if os.path.exists(feedback_file):
                df = pd.read_csv(feedback_file)
            else:
                df = pd.DataFrame(columns=new_row.keys())
            df = pd.concat([df, pd.DataFrame([new_row])], ignore_index=True)
            df.to_csv(feedback_file, index=False)
            
            try:
                api = HfApi(token=os.environ.get("HF_TOKEN"))
                api.upload_file(
                    path_or_fileobj=feedback_file,
                    path_in_repo="system_feedback.csv",
                    repo_id="toecm/PureChain_Dataset",
                    repo_type="dataset",
                    commit_message="📝 Updated System Feedback Log"
                )
            except: pass
            
        except Exception as e:
            print(f"Feedback save error: {e}")
            
        return "Success"
    
    def get_feedback_dataframe(self, review_key=""):
        if not self._is_full_admin_authorized(review_key):
            return self._review_empty_feedback_dataframe()
        feedback_file = "/app/system_feedback.csv"
        hf_token = os.environ.get("HF_TOKEN")
        repo_id = "toecm/PureChain_Dataset"
        
        if hf_token:
            try:
                downloaded = hf_hub_download(repo_id=repo_id, filename="system_feedback.csv", repo_type="dataset", token=hf_token)
                shutil.copy(downloaded, feedback_file)
            except Exception as e:
                pass 
                
        if os.path.exists(feedback_file):
            try:
                df = pd.read_csv(feedback_file)
                for col in ["Timestamp", "Operator_ID", "Feedback_Text", "Image_Reference"]:
                    if col not in df.columns: df[col] = ""
                return df.sort_values(by="Timestamp", ascending=False)
            except:
                pass
                
        return self._review_empty_feedback_dataframe()

    def _eeqs_local_file(self):
        base_dir = "/app" if os.path.isdir("/app") else os.getcwd()
        return os.path.join(base_dir, "eeqs_events.csv")

    def _manual_eeqs_local_file(self):
        base_dir = "/app" if os.path.isdir("/app") else os.getcwd()
        return os.path.join(base_dir, "eeqs_manual_events.csv")

    def _empty_eeqs_dashboard(self):
        return pd.DataFrame(columns=[
            "Game", "AI_Model", "ASR_Model", "Source_Language", "Source_Dialect",
            "Target_Language", "Target_Dialect", "Interaction_Type", "Event_Count",
            "Mean_EEQ", "Last_EEQ", "Last_Seen"
        ])

    def _load_eeqs_events(self):
        eeqs_file = self._eeqs_local_file()
        hf_token = os.environ.get("HF_TOKEN")
        repo_id = "toecm/PureChain_Dataset"

        if hf_token:
            try:
                downloaded = hf_hub_download(
                    repo_id=repo_id,
                    filename="eeqs_events.csv",
                    repo_type="dataset",
                    token=hf_token
                )
                shutil.copy(downloaded, eeqs_file)
            except Exception:
                pass

        if os.path.exists(eeqs_file):
            try:
                return pd.read_csv(eeqs_file)
            except Exception:
                pass
        return pd.DataFrame()

    def _load_manual_eeqs_events(self):
        manual_file = self._manual_eeqs_local_file()
        hf_token = os.environ.get("HF_TOKEN")
        repo_id = "toecm/PureChain_Dataset"

        if hf_token:
            try:
                downloaded = hf_hub_download(
                    repo_id=repo_id,
                    filename="eeqs_manual_events.csv",
                    repo_type="dataset",
                    token=hf_token
                )
                shutil.copy(downloaded, manual_file)
            except Exception:
                pass

        if os.path.exists(manual_file):
            try:
                return pd.read_csv(manual_file)
            except Exception:
                pass
        return pd.DataFrame()

    def handle_eeqs_submission(self, op_id, payload_json):
        """Stores user-synced passive and manual EEQ-s events for audit dashboards."""
        eeqs_file = self._eeqs_local_file()
        manual_file = self._manual_eeqs_local_file()
        hf_token = os.environ.get("HF_TOKEN")
        repo_id = "toecm/PureChain_Dataset"

        try:
            payload = json.loads(payload_json or "{}")
            events = payload.get("events", [])
            manual_events = payload.get("manual_events", [])
            if not isinstance(events, list):
                events = []
            if not isinstance(manual_events, list):
                manual_events = []
            if not events and not manual_events:
                return "No EEQ-s events received."

            rows = []
            manual_rows = []
            sync_ts = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
            for event in events:
                if not isinstance(event, dict):
                    continue
                items = event.get("items") if isinstance(event.get("items"), dict) else {}
                row = {
                    "Sync_Timestamp": sync_ts,
                    "Timestamp": event.get("timestamp", ""),
                    "Operator_ID": op_id or "Anonymous",
                    "App": payload.get("app") or event.get("app", ""),
                    "Host": payload.get("host") or event.get("host", ""),
                    "AI_Model": event.get("aiModel") or payload.get("ai_model") or self._resolve_ai_model_label(),
                    "ASR_Model": event.get("asrModel") or payload.get("asr_model") or self._resolve_asr_model_label(),
                    "Event_ID": event.get("id", ""),
                    "Game": event.get("game", ""),
                    "Interaction_Type": event.get("interaction", ""),
                    "Source_Language": event.get("sourceLanguage", ""),
                    "Source_Dialect": event.get("sourceDialect", ""),
                    "Target_Language": event.get("targetLanguage", ""),
                    "Target_Dialect": event.get("targetDialect", ""),
                    "Index": event.get("index", ""),
                    "Accepted": event.get("accepted", ""),
                    "Error": event.get("error", ""),
                    "Regenerated": event.get("regenerated", ""),
                    "Explained": event.get("explained", ""),
                    "Played_Audio": event.get("playedAudio", ""),
                    "Sent": event.get("sent", ""),
                    "Transcript_Edit_Ratio": event.get("transcriptEditRatio", ""),
                    "Meaning_Edit_Ratio": event.get("meaningEditRatio", ""),
                    "Translation_Edit_Ratio": event.get("translationEditRatio", ""),
                }
                for key, value in items.items():
                    row[key] = value
                rows.append(row)

            for event in manual_events:
                if not isinstance(event, dict):
                    continue
                scores = event.get("scores") if isinstance(event.get("scores"), dict) else {}
                manual_rows.append({
                    "Sync_Timestamp": sync_ts,
                    "Timestamp": event.get("timestamp", ""),
                    "Operator_ID": op_id or "Anonymous",
                    "App": payload.get("app") or event.get("app", ""),
                    "Host": payload.get("host") or event.get("host", ""),
                    "Event_ID": event.get("id", ""),
                    "Source_Event_ID": event.get("source_event_id", ""),
                    "Game": event.get("game", ""),
                    "Interaction_Type": event.get("interaction", ""),
                    "Source_Language": event.get("sourceLanguage", ""),
                    "Source_Dialect": event.get("sourceDialect", ""),
                    "Target_Language": event.get("targetLanguage", ""),
                    "Target_Dialect": event.get("targetDialect", ""),
                    "Manual_Index": event.get("manual_index", ""),
                    "Passive_Index": event.get("passive_index", ""),
                    "Score_Understood": scores.get("understood", ""),
                    "Score_Tone": scores.get("tone", ""),
                    "Score_Dialect": scores.get("dialect", ""),
                    "Score_Repair": scores.get("repair", ""),
                    "Note": event.get("note", ""),
                })

            if not rows and not manual_rows:
                return "No valid EEQ-s events received."

            if rows:
                existing = self._load_eeqs_events()
                new_df = pd.DataFrame(rows)
                df = pd.concat([existing, new_df], ignore_index=True)
                if "Event_ID" in df.columns:
                    df = df.drop_duplicates(subset=["Event_ID"], keep="last")
                df.to_csv(eeqs_file, index=False)

            if manual_rows:
                existing_manual = self._load_manual_eeqs_events()
                new_manual_df = pd.DataFrame(manual_rows)
                manual_df = pd.concat([existing_manual, new_manual_df], ignore_index=True)
                if "Event_ID" in manual_df.columns:
                    manual_df = manual_df.drop_duplicates(subset=["Event_ID"], keep="last")
                manual_df.to_csv(manual_file, index=False)

            if hf_token:
                api = HfApi(token=hf_token)
                if rows:
                    api.upload_file(
                        path_or_fileobj=eeqs_file,
                        path_in_repo="eeqs_events.csv",
                        repo_id=repo_id,
                        repo_type="dataset",
                        commit_message="Update Passive EEQ-s dashboard events"
                    )
                if manual_rows:
                    api.upload_file(
                        path_or_fileobj=manual_file,
                        path_in_repo="eeqs_manual_events.csv",
                        repo_id=repo_id,
                        repo_type="dataset",
                        commit_message="Update Manual EEQ-s audit events"
                    )
                return f"EEQ-s synced: {len(rows)} passive and {len(manual_rows)} manual events saved to global dashboard."

            return f"EEQ-s saved locally on backend: {len(rows)} passive and {len(manual_rows)} manual events. HF_TOKEN is not configured."
        except Exception as e:
            print(f"EEQ-s save error: {e}")
            return f"EEQ-s sync error: {e}"

    def get_eeqs_dashboard_dataframe(self, review_key=""):
        if not self._is_full_admin_authorized(review_key):
            return self._empty_eeqs_dashboard()
        df = self._load_eeqs_events()
        if df.empty:
            return self._empty_eeqs_dashboard()

        required = [
            "Game", "AI_Model", "ASR_Model", "Source_Language", "Source_Dialect",
            "Target_Language", "Target_Dialect", "Interaction_Type", "Index", "Timestamp"
        ]
        for col in required:
            if col not in df.columns:
                df[col] = ""

        df["Index"] = pd.to_numeric(df["Index"], errors="coerce")
        df = df.dropna(subset=["Index"])
        if df.empty:
            return self._empty_eeqs_dashboard()

        group_cols = [
            "Game", "AI_Model", "ASR_Model", "Source_Language", "Source_Dialect",
            "Target_Language", "Target_Dialect", "Interaction_Type"
        ]
        grouped = df.groupby(group_cols, dropna=False).agg(
            Event_Count=("Index", "count"),
            Mean_EEQ=("Index", "mean"),
            Last_EEQ=("Index", "last"),
            Last_Seen=("Timestamp", "last")
        ).reset_index()
        grouped["Mean_EEQ"] = grouped["Mean_EEQ"].round(1)
        grouped["Last_EEQ"] = grouped["Last_EEQ"].round(1)
        return grouped.sort_values(by=["Event_Count", "Mean_EEQ"], ascending=[False, False])

    def get_eeqs_download_file(self, review_key=""):
        if not self._is_full_admin_authorized(review_key):
            return None
        eeqs_file = self._eeqs_local_file()
        manual_file = self._manual_eeqs_local_file()
        base_dir = "/app" if os.path.isdir("/app") else os.getcwd()
        export_zip = os.path.join(base_dir, "eeqs_audit_export.zip")
        df = self._load_eeqs_events()
        if df.empty:
            df = pd.DataFrame(columns=[
                "Sync_Timestamp", "Timestamp", "Operator_ID", "App", "Host",
                "AI_Model", "ASR_Model", "Event_ID", "Game", "Interaction_Type",
                "Source_Language", "Source_Dialect", "Target_Language",
                "Target_Dialect", "Index", "Accepted", "Error", "Regenerated",
                "Explained", "Played_Audio", "Sent", "Transcript_Edit_Ratio",
                "Meaning_Edit_Ratio", "Translation_Edit_Ratio"
            ])
            df.to_csv(eeqs_file, index=False)
        manual_df = self._load_manual_eeqs_events()
        if manual_df.empty:
            manual_df = pd.DataFrame(columns=[
                "Sync_Timestamp", "Timestamp", "Operator_ID", "App", "Host",
                "Event_ID", "Source_Event_ID", "Game", "Interaction_Type",
                "Source_Language", "Source_Dialect", "Target_Language",
                "Target_Dialect", "Manual_Index", "Passive_Index",
                "Score_Understood", "Score_Tone", "Score_Dialect",
                "Score_Repair", "Note"
            ])
            manual_df.to_csv(manual_file, index=False)
        with zipfile.ZipFile(export_zip, "w", zipfile.ZIP_DEFLATED) as zf:
            zf.write(eeqs_file, arcname="eeqs_passive_events.csv")
            zf.write(manual_file, arcname="eeqs_manual_events.csv")
        return export_zip

    def get_pending_dataframe(self, language_filter="All", dialect_filter="All", review_key=""):
        cols = [
            "User", "Data_Origin", "Utterance", "Language", "Dialect",
            "Clarification", "Clarification_Source", "Tone", "Audio", "Timestamp",
            "App_Source", "Source_Language", "Source_Dialect", "Source_Input_Mode",
            "Machine_Transcript_Initial", "User_Transcript_Final",
            "Transcript_Edit_Distance", "Machine_Translation_Initial",
            "User_Translation_Final", "Translation_Edit_Distance", "ASR_Model",
            "AI_Model", "Audio_Sanitation", "Consent_Confirmed", "Queue_ID"
        ]
        scope = self._review_scope(review_key)
        if not scope:
            return pd.DataFrame(columns=cols)
        if os.path.exists(self.PENDING_FILE): 
            df = pd.read_csv(self.PENDING_FILE)
            for c in cols:
                if c not in df.columns: df[c] = ""
                
            df = self._infer_pending_languages(df)
            df = self._filter_dataframe_by_review_scope(df, scope)

            if language_filter and language_filter != "All":
                df = df[df["Language"] == language_filter]
            if dialect_filter and dialect_filter != "All":
                df = df[df["Dialect"] == dialect_filter]
            return df[cols]
        return pd.DataFrame(columns=cols)

    def sync_pending_queue(self, direction="up"):
        hf_token = os.environ.get("HF_TOKEN")
        repo_id = "toecm/PureChain_Dataset"  
        
        if not hf_token:
            print("⚠️ Skipping Pending Sync: No HF_TOKEN found.")
            return

        api = HfApi(token=hf_token)

        if direction == "up":
            if os.path.exists(self.PENDING_FILE):
                try:
                    api.upload_file(
                        path_or_fileobj=self.PENDING_FILE,
                        path_in_repo="pending_approvals.csv",
                        repo_id=repo_id,
                        repo_type="dataset",
                        commit_message="🔄 Auto-sync: Updated pending approvals queue"
                    )
                    print("☁️ Pending queue backed up to PureChain_Dataset.")
                except Exception as e:
                    print(f"⚠️ Failed to upload pending queue: {e}")

        elif direction == "down":
            try:
                downloaded_path = hf_hub_download(
                    repo_id=repo_id, 
                    filename="pending_approvals.csv", 
                    repo_type="dataset", 
                    token=hf_token
                )
                shutil.copy(downloaded_path, self.PENDING_FILE)
                print("☁️ Pending queue CSV restored from PureChain_Dataset.")
                
                df = pd.read_csv(self.PENDING_FILE)
                for audio_path in df['Audio'].dropna():
                    if audio_path and not os.path.exists(audio_path):
                        try:
                            audio_filename = os.path.basename(audio_path)
                            print(f"☁️ Recovering missing audio: {audio_filename}...")
                            audio_dl = hf_hub_download(
                                repo_id=repo_id,
                                filename=f"pending_audio/{audio_filename}",
                                repo_type="dataset",
                                token=hf_token
                            )
                            os.makedirs(os.path.dirname(audio_path), exist_ok=True)
                            shutil.copy(audio_dl, audio_path)
                        except Exception as dl_err:
                            print(f"⚠️ Could not recover {audio_filename}: {dl_err}")
                            
            except Exception as e:
                print("ℹ️ No remote pending queue found. Starting fresh.")

    def _admin_action_log_path(self):
        try:
            os.makedirs(os.path.dirname(self.ADMIN_ACTION_LOG), exist_ok=True)
            return self.ADMIN_ACTION_LOG
        except Exception:
            fallback = os.path.join(os.getcwd(), "admin_action_log.jsonl")
            os.makedirs(os.path.dirname(fallback), exist_ok=True)
            self.ADMIN_ACTION_LOG = fallback
            return fallback

    def _clean_cell(self, value):
        if value is None:
            return ""
        try:
            if pd.isna(value):
                return ""
        except Exception:
            pass
        return str(value)

    def _row_to_action_dict(self, row):
        return {str(k): self._clean_cell(v) for k, v in row.to_dict().items()}

    def sync_admin_action_log(self, direction="up"):
        hf_token = os.environ.get("HF_TOKEN")
        repo_id = "toecm/PureChain_Dataset"
        if not hf_token:
            return

        api = HfApi(token=hf_token)
        log_path = self._admin_action_log_path()

        if direction == "up":
            if os.path.exists(log_path):
                try:
                    api.upload_file(
                        path_or_fileobj=log_path,
                        path_in_repo="admin_action_log.jsonl",
                        repo_id=repo_id,
                        repo_type="dataset",
                        commit_message="Sync admin approval/rejection action log"
                    )
                except Exception as e:
                    print(f"Failed to upload admin action log: {e}")
        elif direction == "down":
            try:
                downloaded_path = hf_hub_download(
                    repo_id=repo_id,
                    filename="admin_action_log.jsonl",
                    repo_type="dataset",
                    token=hf_token
                )
                shutil.copy(downloaded_path, log_path)
                print("Admin action log restored from PureChain_Dataset.")
            except Exception:
                pass

    def _load_admin_actions(self):
        log_path = self._admin_action_log_path()
        if not os.path.exists(log_path):
            return []

        actions = []
        with open(log_path, "r", encoding="utf-8") as f:
            for line in f:
                line = line.strip()
                if not line:
                    continue
                try:
                    actions.append(json.loads(line))
                except Exception:
                    continue
        return actions

    def _write_admin_actions(self, actions):
        log_path = self._admin_action_log_path()
        with open(log_path, "w", encoding="utf-8") as f:
            for action in actions:
                f.write(json.dumps(action, ensure_ascii=False) + "\n")
        self.sync_admin_action_log(direction="up")

    def _append_admin_action(self, action):
        action.setdefault("action_id", uuid.uuid4().hex)
        action.setdefault("created_at", datetime.now().isoformat())
        action.setdefault("undone", False)
        log_path = self._admin_action_log_path()
        with open(log_path, "a", encoding="utf-8") as f:
            f.write(json.dumps(action, ensure_ascii=False) + "\n")
        self.sync_admin_action_log(direction="up")
        return action["action_id"]

    def _mark_admin_action_undone(self, action_id):
        actions = self._load_admin_actions()
        for action in reversed(actions):
            if action.get("action_id") == action_id:
                action["undone"] = True
                action["undone_at"] = datetime.now().isoformat()
                break
        self._write_admin_actions(actions)

    def _restore_pending_row(self, row_data):
        row_data = {str(k): self._clean_cell(v) for k, v in dict(row_data or {}).items()}
        if not row_data:
            return False, "No saved pending row was found."

        if os.path.exists(self.PENDING_FILE):
            df = pd.read_csv(self.PENDING_FILE, dtype=str).fillna("")
        else:
            df = pd.DataFrame()

        for col in row_data.keys():
            if col not in df.columns:
                df[col] = ""
        for col in df.columns:
            row_data.setdefault(col, "")

        timestamp = row_data.get("Timestamp", "")
        utterance = row_data.get("Utterance", "")
        if not df.empty and "Timestamp" in df.columns and "Utterance" in df.columns:
            duplicate = df[
                (df["Timestamp"].astype(str) == str(timestamp)) &
                (df["Utterance"].astype(str) == str(utterance))
            ]
            if not duplicate.empty:
                return True, "Entry was already back in pending."

        df = pd.concat([df, pd.DataFrame([{c: row_data.get(c, "") for c in df.columns}])], ignore_index=True)
        df.to_csv(self.PENDING_FILE, index=False)
        self.sync_pending_queue(direction="up")
        return True, "Entry restored to pending."

    def _approval_csv_path(self, language, dialect):
        dialect_str = "" if pd.isna(dialect) else str(dialect)
        clean_dialect = dialect_str.strip().title()
        if "Pidgin" in clean_dialect and not clean_dialect.endswith("English") and not clean_dialect.endswith("Dialect"):
            clean_dialect += " English"

        def infer_language(name):
            name_lower = name.lower()
            if "patois" in name_lower or "pidgin" in name_lower or "creole" in name_lower:
                return "Creole"
            if "english" in name_lower or "american" in name_lower or "british" in name_lower or "south african" in name_lower:
                return "English"
            if "korean" in name_lower:
                return "Korean"
            if "igbo" in name_lower:
                return "Asusu Igbo"
            if "tagalog" in name_lower or "filipino" in name_lower:
                return "Filipino"
            if "indonesian" in name_lower:
                return "Indonesian"
            return "Other"

        if language is not None and not pd.isna(language) and str(language).strip():
            lang = str(language).strip().title()
        else:
            lang = infer_language(clean_dialect)
        return os.path.join(self.trust.config.DATASET_DIR, lang, "data", f"{clean_dialect}.csv"), clean_dialect

    def _upload_file_to_hf(self, local_path, path_in_repo, repo_id, message):
        hf_token = os.environ.get("HF_TOKEN")
        if not hf_token or not os.path.exists(local_path):
            return
        try:
            HfApi(token=hf_token).upload_file(
                path_or_fileobj=local_path,
                path_in_repo=path_in_repo,
                repo_id=repo_id,
                repo_type="dataset",
                commit_message=message
            )
        except Exception as e:
            print(f"HF rollback sync warning: {e}")

    def _remove_approved_dataset_row(self, action):
        import csv

        approved = action.get("approved_row", {})
        csv_path, clean_dialect = self._approval_csv_path(approved.get("Language", ""), approved.get("Dialect", ""))
        if not os.path.exists(csv_path):
            return False, "Approved CSV was not found locally."

        df = pd.read_csv(csv_path, dtype=str, encoding="utf-8-sig", on_bad_lines="skip").fillna("")
        if df.empty:
            return False, "Approved CSV was empty."

        def series(col):
            if col not in df.columns:
                return pd.Series([""] * len(df), index=df.index)
            return df[col].astype(str).str.strip()

        mask = (
            (series("Utterance") == str(approved.get("Utterance", "")).strip()) &
            (series("Dialect") == str(clean_dialect).strip()) &
            (series("Clarification") == str(approved.get("Clarification", "")).strip()) &
            (series("Data_Origin") == str(approved.get("Data_Origin", "")).strip()) &
            (series("User") == str(approved.get("User", "")).strip())
        )
        matches = df[mask]
        if matches.empty:
            mask = (
                (series("Utterance") == str(approved.get("Utterance", "")).strip()) &
                (series("Dialect") == str(clean_dialect).strip()) &
                (series("Clarification") == str(approved.get("Clarification", "")).strip())
            )
            matches = df[mask]
        if matches.empty:
            return False, "Could not find the matching approved CSV row."

        df = df.drop(matches.index[-1])
        df.to_csv(csv_path, index=False, encoding="utf-8-sig", quoting=csv.QUOTE_ALL)
        relative_path = os.path.relpath(csv_path, self.trust.config.DATASET_DIR).replace(os.sep, "/")
        self._upload_file_to_hf(
            csv_path,
            relative_path,
            getattr(self.trust.config, "HF_REPO_ID", "toecm/IEDID"),
            "Undo admin approval CSV row"
        )
        return True, "Approved CSV row removed."

    def _remove_rejected_graveyard_row(self, action):
        import csv

        row = action.get("pending_row", {})
        graveyard_path = os.path.join(self.trust.config.DATASET_DIR, "..", "rejected_graveyard.csv")
        if not os.path.exists(graveyard_path):
            return False, "Rejected graveyard CSV was not found locally."

        df = pd.read_csv(graveyard_path, dtype=str, encoding="utf-8-sig", on_bad_lines="skip").fillna("")
        if df.empty:
            return False, "Rejected graveyard CSV was empty."

        def series(col):
            if col not in df.columns:
                return pd.Series([""] * len(df), index=df.index)
            return df[col].astype(str).str.strip()

        mask = (
            (series("Utterance") == str(row.get("Utterance", "")).strip()) &
            (series("Dialect") == str(row.get("Dialect", "")).strip()) &
            (series("Clarification") == str(row.get("Clarification", "")).strip()) &
            (series("Data_Origin") == str(row.get("Data_Origin", "")).strip()) &
            (series("User") == str(row.get("User", "")).strip()) &
            (series("Rejection_Reason") == str(action.get("reject_reason", "")).strip())
        )
        matches = df[mask]
        if matches.empty:
            mask = (
                (series("Utterance") == str(row.get("Utterance", "")).strip()) &
                (series("Dialect") == str(row.get("Dialect", "")).strip()) &
                (series("Rejection_Reason") == str(action.get("reject_reason", "")).strip())
            )
            matches = df[mask]
        if matches.empty:
            return False, "Could not find the matching rejected graveyard row."

        df = df.drop(matches.index[-1])
        df.to_csv(graveyard_path, index=False, encoding="utf-8-sig", quoting=csv.QUOTE_ALL)
        self._upload_file_to_hf(
            graveyard_path,
            "rejected_graveyard.csv",
            getattr(self.trust.config, "HF_REPO_ID", "toecm/IEDID"),
            "Undo admin rejection graveyard row"
        )
        return True, "Rejected graveyard row removed."

    def _restore_pending_audio(self, action):
        row = action.get("pending_row", {})
        audio_path = str(row.get("Audio", "") or "")
        if not audio_path or audio_path == "nan":
            return "No pending audio path to restore."

        audio_name = os.path.basename(audio_path)
        if not audio_name:
            return "No pending audio filename to restore."

        if not os.path.exists(audio_path):
            hf_token = os.environ.get("HF_TOKEN")
            if hf_token:
                candidates = []
                if action.get("action_type") == "reject":
                    candidates.append(("toecm/PureChain_Dataset", f"rejected_audio/{audio_name}"))
                    candidates.append((getattr(self.trust.config, "HF_REPO_ID", "toecm/IEDID"), f"rejected_audio/{audio_name}"))
                candidates.append((getattr(self.trust.config, "HF_REPO_ID", "toecm/IEDID"), f"audio/{audio_name}"))
                for repo_id, filename in candidates:
                    try:
                        downloaded = hf_hub_download(repo_id=repo_id, filename=filename, repo_type="dataset", token=hf_token)
                        os.makedirs(os.path.dirname(audio_path), exist_ok=True)
                        shutil.copy(downloaded, audio_path)
                        break
                    except Exception:
                        continue

        if os.path.exists(audio_path):
            self._upload_file_to_hf(
                audio_path,
                f"pending_audio/{audio_name}",
                "toecm/PureChain_Dataset",
                "Restore pending audio after admin undo"
            )
            return "Pending audio restored."
        return "Pending audio could not be restored automatically."

    def admin_undo_last_action(self, review_key=""):
        if not self._is_full_admin_authorized(review_key):
            return self._full_admin_required_message()
        try:
            actions = self._load_admin_actions()
            action = next((a for a in reversed(actions) if not a.get("undone") and a.get("action_type") in ["approve", "reject"]), None)
            if not action:
                return "No approval/rejection action is available to undo."

            status_parts = []
            restored, msg = self._restore_pending_row(action.get("pending_row", {}))
            status_parts.append(msg)

            if action.get("action_type") == "approve":
                ok, msg = self._remove_approved_dataset_row(action)
                status_parts.append(msg)
            elif action.get("action_type") == "reject":
                ok, msg = self._remove_rejected_graveyard_row(action)
                status_parts.append(msg)

            status_parts.append(self._restore_pending_audio(action))
            self._mark_admin_action_undone(action.get("action_id"))

            undone_label = "approval" if action.get("action_type") == "approve" else "rejection"
            return f"Undo complete for last {undone_label}: " + " ".join(status_parts)
        except Exception as e:
            return f"Undo failed: {e}"
                
    def get_pending_label(self):
        if os.path.exists(self.PENDING_FILE):
            count = len(pd.read_csv(self.PENDING_FILE))
            if count > 0:
                return f"👮 Pending ({count})", count
        return "👮 Pending", 0

    def monitor_pending_state(self):
        label, count = self.get_pending_label()
        if count > self.last_pending_count and self.alert_sound:
            sound = gr.update(value=self.alert_sound, autoplay=True)
        else:
            sound = gr.skip()
        self.last_pending_count = count
        return f"### {label} - Review Submissions from React Games", sound

    def admin_approve_pending(self, timestamp, orig_utt, edited_utt, edited_lang, edited_dialect, edited_clar, edited_tone, trimmed_audio_path, review_key=""):
        scope = self._review_scope(review_key)
        if not scope:
            return self._review_unauthorized_message()
        try:
            df = pd.read_csv(self.PENDING_FILE)
            df = self._infer_pending_languages(df)
            match = df[(df["Timestamp"] == timestamp) & (df["Utterance"] == orig_utt)]
            if len(match) == 0:
                return "❌ Approval failed: Entry not found in pending database."
            
            index_in_csv = match.index[0]
            row = df.loc[index_in_csv]
            if not self._pending_row_allowed_by_scope(row, scope):
                return "🔒 Scoped reviewer passcode cannot approve this language/dialect."
            
            # 🟢 Use the edited text instead of the original row data
            final_utt = edited_utt if edited_utt else row["Utterance"]
            final_lang = edited_lang if edited_lang else row.get("Language", "")
            final_dialect = edited_dialect if edited_dialect else row["Dialect"]
            final_clar = edited_clar if edited_clar else row["Clarification"]
            final_tone = edited_tone if edited_tone else row["Tone"]
            if not self._scope_allows_language_dialect(scope, final_lang, final_dialect):
                return "🔒 Scoped reviewer passcode cannot approve edits outside its language/dialect scope."
            
            final_audio = trimmed_audio_path if isinstance(trimmed_audio_path, str) and os.path.exists(trimmed_audio_path) else row.get("Audio", "")
            review_metadata = {
                column: self._clean_cell(row.get(column, ""))
                for column in [
                    "Queue_ID", "Interaction_ID", "Supersedes_Interaction_ID",
                    "App_Source", "Source_Input_Mode", "Machine_Transcript_Initial",
                    "User_Transcript_Final", "Transcript_Edit_Distance",
                    "ASR_Model", "Audio_Sanitation", "Audio_Retained",
                    "Machine_Translation_Initial",
                    "User_Translation_Final", "Translation_Edit_Distance",
                    "AI_Model", "Translation_Route", "Consent_Confirmed",
                    "Consent_Version", "Source_Language", "Source_Dialect",
                    "Target_Language", "Target_Dialect", "Review_Submitted_At"
                ]
            }
            review_metadata["Review_Status"] = "Approved"
            
            self.trust.update_dataset_csv(
                final_dialect, final_utt, final_clar, final_tone,
                row.get("Context", ""), "", final_audio, row.get("Pragmatic_Analysis", ""),
                row.get("Data_Origin", ""), "Admin Edit", row.get("User", ""),
                language=final_lang, review_metadata=review_metadata
            )

            self._append_admin_action({
                "action_type": "approve",
                "pending_row": self._row_to_action_dict(row),
                "approved_row": {
                    "Utterance": self._clean_cell(final_utt),
                    "Dialect": self._clean_cell(final_dialect),
                    "Language": self._clean_cell(final_lang),
                    "Clarification": self._clean_cell(final_clar),
                    "Tone_Category": self._clean_cell(final_tone),
                    "Linguistic_Context": self._clean_cell(row.get("Context", "")),
                    "Pragmatic_Analysis": self._clean_cell(row.get("Pragmatic_Analysis", "")),
                    "Data_Origin": self._clean_cell(row.get("Data_Origin", "")),
                    "Clarification_Source": "Admin Edit",
                    "User": self._clean_cell(row.get("User", "")),
                    "Audio": self._clean_cell(final_audio)
                }
            })
            
            payload = {
                "original": final_utt,
                "dialect": final_dialect,
                "clarification": final_clar,
                "tone": final_tone,
                "user": str(row.get("User", "")),
                "Data_Origin": str(row.get("Data_Origin", ""))
            }
            threading.Thread(target=self.trust.stamp_on_chain, args=(payload,), daemon=True).start()
            
            df.drop(index_in_csv).to_csv(self.PENDING_FILE, index=False)
            self.sync_pending_queue(direction="up")
            
            audio_to_delete = row.get("Audio")
            if audio_to_delete and str(audio_to_delete) != "nan":
                try:
                    api = HfApi(token=os.environ.get("HF_TOKEN"))
                    api.delete_file(
                        path_in_repo=f"pending_audio/{os.path.basename(audio_to_delete)}",
                        repo_id="toecm/PureChain_Dataset",
                        repo_type="dataset",
                        commit_message="🗑️ Cleaned up processed pending audio"
                    )
                except Exception as e:
                    pass 
            
            return f"✅ Approved & Minted: {final_utt[:20]}..."
        except Exception as e: 
            return f"❌ Approval failed: {e}"

    def admin_reject_pending(self, timestamp, orig_utt, reject_reason, custom_reason, review_key=""):
        scope = self._review_scope(review_key)
        if not scope:
            return self._review_unauthorized_message()
        try:
            df = pd.read_csv(self.PENDING_FILE)
            df = self._infer_pending_languages(df)
            match = df[(df["Timestamp"] == timestamp) & (df["Utterance"] == orig_utt)]
            if len(match) == 0:
                return "❌ Rejection failed: Entry not found."
            
            index_in_csv = match.index[0]
            row = df.loc[index_in_csv] 
            if not self._pending_row_allowed_by_scope(row, scope):
                return "🔒 Scoped reviewer passcode cannot reject this language/dialect."
            
            final_reason = custom_reason if reject_reason == "Other" else reject_reason
            if not final_reason:
                final_reason = "Admin Override"

            # If it has a Chain_ID, it was proposed. Reject it on-chain!
            chain_id = str(row.get("Chain_ID", ""))

            # Log to Graveyard
            self.trust.add_to_graveyard(
                dialect=row.get("Dialect", ""),
                utterance=row.get("Utterance", ""),
                clarification=row.get("Clarification", ""),
                tone=row.get("Tone", ""),
                context=row.get("Context", ""),
                syntax="",
                audio_path=row.get("Audio", ""),
                pragmatics=row.get("Pragmatic_Analysis", ""),
                sourceTag=row.get("Data_Origin", "Pending"),
                clar_source="Lab Admin",
                userKey=row.get("User", ""),
                reason=final_reason,
                rejector="Admin",
                chain_id=chain_id
            )
            self._append_admin_action({
                "action_type": "reject",
                "pending_row": self._row_to_action_dict(row),
                "reject_reason": self._clean_cell(final_reason),
                "rejector": "Admin"
            })
            if chain_id.isdigit():
                try:
                    w3 = self.trust.w3
                    contract_addr = os.environ.get("PURECHAIN_CONTRACT_ADDRESS", getattr(self.trust.config, 'PURECHAIN_CONTRACT_ADDRESS', None))
                    contract = w3.eth.contract(address=contract_addr, abi=self.trust.config.CONTRACT_ABI)
                    nonce = w3.eth.get_transaction_count(self.trust.account.address)
                    tx = contract.functions.rejectEntry(int(chain_id), self.trust.account.address, final_reason).build_transaction({
                        'chainId': getattr(self.trust.config, 'PURECHAIN_ID', 900520900520),
                        'gas': 2000000, 'gasPrice': 0, 'nonce': nonce
                    })
                    signed_tx = w3.eth.account.sign_transaction(tx, self.trust.config.PRIVATE_KEY)
                    tx_hash = w3.eth.send_raw_transaction(signed_tx.raw_transaction)
                    w3.eth.wait_for_transaction_receipt(tx_hash)
                    print(f"🔥 On-chain burn applied for {chain_id} with reason: {final_reason}")
                except Exception as e:
                    print(f"Failed to burn on-chain: {e}")
            
            df.drop(index_in_csv).to_csv(self.PENDING_FILE, index=False)
            self.sync_pending_queue(direction="up")
            
            audio_to_delete = row.get("Audio")
            if audio_to_delete and str(audio_to_delete) != "nan":
                try:
                    api = HfApi(token=os.environ.get("HF_TOKEN"))
                    if os.path.exists(audio_to_delete):
                        api.upload_file(
                            path_or_fileobj=audio_to_delete, # Local file
                            path_in_repo=f"rejected_audio/{os.path.basename(audio_to_delete)}",
                            repo_id="toecm/PureChain_Dataset",
                            repo_type="dataset",
                            commit_message=f"🚫 Moved rejected audio: {final_reason}"
                        )
                    api.delete_file(
                        path_in_repo=f"pending_audio/{os.path.basename(audio_to_delete)}",
                        repo_id="toecm/PureChain_Dataset",
                        repo_type="dataset",
                        commit_message="🗑️ Cleaned up rejected pending audio"
                    )
                except Exception as e:
                    print(f"HF Audio Move Error: {e}") 
                    
            return f"🗑️ Entry Rejected ({final_reason}) & Moved to Burn Ward."
        except Exception as e: 
            return f"❌ Rejection Error: {e}"

    def admin_clear_all_pending(self, review_key=""):
        if not self._is_full_admin_authorized(review_key):
            return self._full_admin_required_message()
        try:
            if os.path.exists(self.PENDING_FILE):
                os.remove(self.PENDING_FILE)
                self.sync_pending_queue(direction="up")
            return "🧹 All pending entries swept!"
        except Exception as e: return f"❌ Clear failed: {e}"

    def export_analysis_to_csv(self, df, review_key=""):
        if not self._is_full_admin_authorized(review_key):
            return None
        if df is None or not hasattr(df, 'columns') or df.empty: 
            return None
        path = "/app/sociolinguistic_export.csv"
        df.to_csv(path, index=False, encoding='utf-8-sig')
        return path

    def api_generate_training_data(self, review_key=""):
        if not self._is_full_admin_authorized(review_key):
            return self._full_admin_required_message()
        try:
            prepare_training_data.main()
            return f"✅ Success! 'train.csv' created."
        except Exception as e:
            return f"❌ Error generating data: {e}"

    def api_get_full_dataset_zip(self, review_key=""):
        if not self._is_full_admin_authorized(review_key):
            return None
        try:
            shutil.make_archive("/app/dataset_export", 'zip', self.brain.config.DATASET_DIR)
            return "/app/dataset_export.zip"
        except Exception as e:
            return f"Error zipping: {e}"

    def auto_regenerate_analysis(self, text, clar, tone, ctx, prag, new_dialect):
        show_new = (new_dialect == "+ Add New Dialect")
        # Guard: don't fire heavy AI calls if there is no utterance text (e.g. tab-switching with empty field)
        if not text or not text.strip() or show_new or not new_dialect:
            return clar, tone, ctx, prag, gr.update(visible=show_new)
            
        print(f"🔄 UI Trigger: Re-analyzing '{text}' for dialect: {new_dialect}")
        
        try:
            if hasattr(self.brain, 'analyze_dialect_single'):
                res = self.brain.analyze_dialect_single(text, new_dialect)
                return (
                    res.get("clarification", clar),
                    res.get("tone", tone),
                    res.get("context", ctx),
                    res.get("pragmatics", prag),
                    gr.update(visible=show_new)
                )
        except Exception as e:
            print(f"Auto-regenerate error: {e}")
            
        return clar, tone, ctx, prag, gr.update(visible=show_new)

    def lab_analyze_and_mint(self, text, dialect, force_ai, userKey, review_key="", request: gr.Request = None):
        if not self._is_full_admin_authorized(review_key):
            return {"error": "unauthorized"}, self._full_admin_required_message()
        status_log = f"🚀 LAB PIPELINE TRIGGERED for '{text}'\n"
        final_result = None

        if not text or not dialect:
            return {"error": "Missing input"}, "⚠️ Please provide text and select a dialect."

        if not force_ai:
            status_log += "🗄️ Checking Local Dataset...\n"
            final_result = self.brain.search_local_dataset(text)
            if final_result:
                status_log += "✅ Found in Local Dataset.\n"
            else:
                status_log += "🎭 Checking Persona Context...\n"
                final_result = self.brain.search_personas(text)
        else:
            status_log += "🚀 Force AI Enabled: Bypassing local lookups...\n"

        if not final_result:
            status_log += "🧠 Generating AI interpretation...\n"
            if hasattr(self.brain, 'analyze_dialect_single'):
                final_result = self.brain.analyze_dialect_single(text, dialect)
                status_log += "✅ AI Engine Analysis Complete.\n"
            else:
                return {"error": "Missing AI function"}, status_log + "❌ Error."

        clarification = final_result.get("clarification", "")
        tone = final_result.get("tone", "Neutral")
        context = final_result.get("context", "")
        pragmatics = final_result.get("pragmatics", "")
        
        client_ip = request.headers.get("x-forwarded-for") or request.client.host if request else "Unknown_IP"
        source_tag = f"Lab_Admin_{client_ip}"

        success = self.trust.update_dataset_csv(
            dialect=dialect, utterance=text, clarification=clarification,
            tone=tone, context=context, syntax="", audio_path=None,
            pragmatics=pragmatics, sourceTag=source_tag,
            clar_source=final_result.get("Source", "AI"), userKey=userKey
        )

        if success:
            status_log += "\n💎 SUCCESS: Entry saved to CSV and synced to HF Cloud!"
            
            payload = {
                "original": text, 
                "dialect": dialect, 
                "clarification": clarification, 
                "tone": tone,
                "user": userKey 
            }
            
            threading.Thread(target=self.trust.stamp_on_chain, args=(payload,), daemon=True).start()
            status_log += "\n⛓️ PureChain minting triggered in background."
        else:
            status_log += "\n⚠️ ERROR: Database save failed."

        return final_result, status_log

    # ==========================================
    #                THE RESEARCH UI
    # ==========================================
    def create_ui(self):
        def generate_admin_op_id():
            return "0x" + uuid.uuid4().hex + uuid.uuid4().hex[:8]
            
        custom_css = """
        .gradio-container { max-width: 95% !important; } 
        table { width: 100% !important; table-layout: auto !important; }
        td { white-space: normal !important; word-wrap: break-word !important; }
        """
        import json
        hierarchy_str = self.api_get_language_hierarchy()
        language_hierarchy_dict = json.loads(hierarchy_str)
        language_choices = ["All"] + sorted(list(language_hierarchy_dict.keys()))
        all_dialects = []
        for d_list in language_hierarchy_dict.values():
            all_dialects.extend([d for d in d_list if d != "+ Add New Dialect"])
        dropdown_choices = sorted(list(set(all_dialects)))
        
        available_profiles = self.brain.get_available_profiles() if hasattr(self.brain, 'get_available_profiles') else []
        
        backup_files = []
        if hasattr(self.brain, 'config'):
            if os.path.exists(self.brain.config.DATASET_DIR):
                backup_files.extend([os.path.basename(f) for f in glob.glob(os.path.join(self.brain.config.DATASET_DIR, "**", "*.csv"), recursive=True)])
            if os.path.exists(self.brain.config.PROFILES_DIR):
                backup_files.extend([os.path.basename(f) for f in glob.glob(os.path.join(self.brain.config.PROFILES_DIR, "*.json"))])
        backup_files = sorted(list(set(backup_files))) if backup_files else ["No files found"]
        
        with gr.Blocks(theme=gr.themes.Default(primary_hue=gr.themes.colors.red, secondary_hue=gr.themes.colors.orange)) as ui:
            gr.Markdown("## 🌍 PurePolyglot: Decentralized Multi-Dialect Mediator (Lab View)")
            
            ui_source_tag = gr.Textbox(visible=False, value="Web")
            ui_clar_source = gr.Textbox(visible=False, value="Lab Admin")
            ui_operator_id = gr.Textbox(visible=False, value=generate_admin_op_id) 
            
            api_audio_blob = gr.Audio(visible=False, type="filepath")
            api_confirm = gr.State(False)

            admin_review_key = gr.Textbox(
                label="Reviewer/Admin passcode",
                type="password",
                placeholder="Required for backend dashboard tabs and privileged actions"
            )

            with gr.Tabs():
                with gr.Tab("🎙️ Live Field Analysis"):
                    health_display = gr.HTML(self.get_blockchain_health())
                    
                    with gr.Row():
                        with gr.Column(scale=1):
                            audio_in = gr.Audio(label="Step 1: Speak/Upload", sources=["microphone", "upload"], type="filepath")
                            lang_sel = gr.Dropdown(["en", "yo", "ig", "ko", "ha"], value="en", label="Language Context")
                            btn_run = gr.Button("Analyze Audio 🔄", variant="primary")
                            quota_box = gr.Textbox(label="📊 API Quota", value=self.get_quota_status(), interactive=False)
                            
                            with gr.Row(variant="compact"):
                                background_status_display = gr.Textbox(label="Status", value="Checking...", interactive=False, show_label=False)
                                 
                        with gr.Column(scale=5):
                            log_box = gr.Textbox(label="Linguistic Analysis Log", interactive=False)
                            gr.Markdown("### 🥇 AI Interpretation Baseline")
                            results_table = gr.Dataframe(
                                headers=["Source", "Speaker", "Utterance", "Dialect", "Clarification", "Tone", "Context", "Pragmatic Analysis"],
                                interactive=True,
                                wrap=False,
                                row_count=(1, "dynamic")
                            )
                            with gr.Row():
                                export_btn = gr.Button("📥 Download Analysis CSV", variant="secondary")
                                export_file = gr.File(label="Export Result", visible=False)
                                  
                    gr.Markdown("### ✍️ Active Sociolinguistic Feedback Loop (Edit & Approve)")
                    with gr.Row():
                        with gr.Column(scale=1):
                            orig_text = gr.Textbox(visible=True, label="Utterance (Transcribed)")
                            dialect_sel = gr.Dropdown(choices=dropdown_choices, label="Assigned Dialect", interactive=True, allow_custom_value=True)
                            new_dialect = gr.Textbox(label="Enter New Dialect Name", visible=False, interactive=True)
                        with gr.Column():
                            clar_text = gr.Textbox(label="Final Clarification", interactive=True, lines=2)
                            tone_sel = gr.Dropdown(choices=self.TONES, value="Neutral / Conversational", label="Pragmatic Tone", interactive=True, allow_custom_value=True)
                            ctx_area = gr.TextArea(label="Linguistic Context", interactive=True, lines=1)
                            prag_area = gr.TextArea(label="Pragmatic Analysis ([Force], [Deixis], [Register])", interactive=True, lines=1)

                    with gr.Row():
                        btn_save = gr.Button("💾 Validate & Save", variant="primary")
                        btn_over = gr.Button("⚠️ Confirm Overwrite", variant="stop", visible=False)
                    feedback_msg = gr.Markdown()

                    gr.Markdown("### 📥 PhD Data Export & Training")
                    with gr.Row():
                        export_data_btn = gr.Button("📦 Generate Full Dataset ZIP", variant="secondary")
                        train_btn = gr.Button("🧠 Generate AutoTrain CSV", variant="primary")
                        export_zip_file = gr.File(label="Download")
                        train_status = gr.Textbox(label="Training Data Status", lines=1)

                with gr.Tab("🧪 THE LAB (Force AI)"):
                    gr.Markdown("### 🔬 Test text inputs directly and force AI generation")
                    with gr.Row():
                        with gr.Column():
                            lab_input = gr.Textbox(label="Test Phrase (Text Only)")
                            lab_dialect = gr.Dropdown(choices=dropdown_choices, label="Target Dialect")
                            force_ai_toggle = gr.Checkbox(label="Force Live AI (Skip Local Cache)", value=False)
                            lab_user_key = gr.Textbox(label="Admin User Key", value="Admin_001")
                            lab_btn = gr.Button("RUN ANALYSIS & MINT", variant="primary")
                        
                        with gr.Column():
                            lab_output = gr.JSON(label="Analysis Result")
                            lab_log = gr.Textbox(label="System Logs", lines=10)

                    lab_btn.click(
                        fn=self.lab_analyze_and_mint, 
                        inputs=[lab_input, lab_dialect, force_ai_toggle, lab_user_key, admin_review_key], 
                        outputs=[lab_output, lab_log]
                    )
                
                with gr.Tab("⚙️ Persona Management"):
                    with gr.Row():
                        load_all_btn = gr.Button("📂 Load ALL Profiles", variant="secondary")
                        profile_selector = gr.Dropdown(choices=available_profiles, label="Select Profile", allow_custom_value=True)
                        profile_filename = gr.Textbox(label="Filename")
                        load_profile_btn = gr.Button("📥 Load Selected Profile", variant="primary") # 🟢 NEW
                    profile_editor = gr.Textbox(label="Profile Content (JSON)", lines=20) # 🟢 CHANGED TO TEXTBOX
                    save_profile_btn = gr.Button("💾 Save Profile modifications", variant="primary")
                    profile_status = gr.Textbox(label="System Response", interactive=False)

                    def change_profile(val, review_key): 
                        if not self._is_full_admin_authorized(review_key):
                            return "", ""
                        if not val: return "", ""
                        return json.dumps(self.brain.load_profile_by_name(val), indent=2), val
                    
                    def save_and_refresh_profile(filename, content, review_key):
                        if not self._is_full_admin_authorized(review_key):
                            return self._full_admin_required_message(), gr.update()
                        msg = self.brain.save_specific_profile(filename, content)
                        return msg, gr.update(choices=self.brain.get_available_profiles(), value=filename)

                    def load_all_profiles_with_key(review_key):
                        if not self._is_full_admin_authorized(review_key):
                            return self._full_admin_required_message(), ""
                        self.brain.load_all_profiles_simultaneously()
                        return "Profiles loaded.", self.brain.get_current_profile_text()

                    # 🟢 Changed to click instead of auto-change
                    load_profile_btn.click(change_profile, inputs=[profile_selector, admin_review_key], outputs=[profile_editor, profile_filename])
                    save_profile_btn.click(save_and_refresh_profile, inputs=[profile_filename, profile_editor, admin_review_key], outputs=[profile_status, profile_selector])
                    
                    if hasattr(self.brain, 'load_all_profiles_simultaneously'):
                        load_all_btn.click(load_all_profiles_with_key, inputs=[admin_review_key], outputs=[profile_status, profile_editor])


                with gr.Tab("👮 Pending Audit/Approvals"):
                    pending_header = gr.Markdown("### 👮 Pending (0) - Review Submissions from React Games")
                    gr.Markdown("Use the global reviewer/admin passcode field above before refreshing or changing audit data.")
                    with gr.Row():
                        with gr.Column(scale=3):
                            with gr.Row():
                                filter_language = gr.Dropdown(choices=language_choices, value="All", label="Filter by Language")
                                filter_dialect = gr.Dropdown(choices=["All"] + dropdown_choices, value="All", label="Filter by Dialect")
                            pending_df = gr.Dataframe(headers=[
                                "User", "Data_Origin", "Utterance", "Language", "Dialect",
                                "Clarification", "Clarification_Source", "Tone", "Audio", "Timestamp",
                                "App_Source", "Source_Language", "Source_Dialect", "Source_Input_Mode",
                                "Machine_Transcript_Initial", "User_Transcript_Final",
                                "Transcript_Edit_Distance", "Machine_Translation_Initial",
                                "User_Translation_Final", "Translation_Edit_Distance", "ASR_Model",
                                "AI_Model", "Audio_Sanitation", "Consent_Confirmed", "Queue_ID"
                            ], interactive=False, wrap=False, row_count=(1, "dynamic"))
                        with gr.Column(scale=1):
                            gr.Markdown("#### 🎧 Audio Auditor")
                            btn_refresh = gr.Button("🔄 Refresh List")
                            pending_audio_player = gr.Audio(label="Trim or Preview Audio", type="filepath", interactive=True)
                            audit_log = gr.Textbox(label="Audit Status", interactive=False)
                    
                    # 🟢 NEW: Editable Textboxes for Admin Corrections
                    gr.Markdown("#### ✍️ Edit Selected Entry Before Minting")
                    with gr.Row():
                        pending_timestamp = gr.Textbox(label="Timestamp ID", interactive=False)
                        pending_orig_utt = gr.Textbox(visible=False)
                        edit_utt = gr.Textbox(label="Utterance", interactive=True)
                        edit_lang = gr.Textbox(label="Language", interactive=True)
                        edit_dialect = gr.Textbox(label="Dialect", interactive=True)
                        edit_clar = gr.Textbox(label="Clarification / Meaning", interactive=True)
                        edit_tone = gr.Textbox(label="Tone", interactive=True)

                    with gr.Row():
                        reject_reason_dropdown = gr.Dropdown(["Spam", "Audio Quality", "Dialect Mismatch", "Other"], label="Rejection Reason", value="Audio Quality")
                        reject_reason_text = gr.Textbox(label="Custom Reason (if Other)", visible=False)
                        
                    def toggle_reason(val):
                        return gr.update(visible=(val == "Other"))
                    reject_reason_dropdown.change(toggle_reason, inputs=[reject_reason_dropdown], outputs=[reject_reason_text])

                    with gr.Row():
                        btn_appr_p = gr.Button("✅ Approve & Mint (With Edits)", variant="primary")
                        btn_rejt_p = gr.Button("🗑️ Reject entry", variant="stop")
                        btn_undo_admin = gr.Button("Undo Last Approval/Rejection", variant="secondary")
                        btn_clear_pending = gr.Button("Sweep All Pending", variant="secondary")

                with gr.Tab("⛓️ PureChain History"):
                    gr.Markdown("### 📜 Immutable Transaction Log & Audit Reports")

                    with gr.Row():
                        legacy_checkboxes = gr.CheckboxGroup(
                            choices=["PureIUUY", "PureConvo", "PureVersation", "PureBi (Jun 11)", "PureBi (Jun 12)", "PureBi (Jun 13)", "PureBi"], 
                            label="Fetch Legacy Blockchain Data",
                            value=["PureBi"]
                        )

                    with gr.Row():
                        start_date = gr.DateTime(label="Start Date", type="string")
                        end_date = gr.DateTime(label="End Date", type="string")
                        btn_filter = gr.Button("🔍 Filter & Refresh", variant="primary")
                        gr.HTML("<a href='http://3.34.161.207:3000' target='_blank' style='display:flex; align-items:center; justify-content:center; padding: 8px; background: #4f46e5; color: white; border-radius: 8px; text-decoration: none; font-weight: bold; height: 35px; margin-top: 25px;'>🌐 Open PureChain Explorer</a>")

                    history_df = gr.Dataframe(
                        headers=["Timestamp", "Utterance", "Dialect", "Data_Origin", "Data_Approved", "Block", "TX Hash"], 
                        interactive=False,
                        wrap=False,
                        row_count=(5, "dynamic")
                    )
                    
                    with gr.Row():
                        export_report_btn = gr.Button("📥 Generate CSV Report", variant="secondary")
                        report_file = gr.File(label="Download Audit Report")
                        explorer_link = gr.Markdown("Select a row to generate Explorer Link")

                    def run_filter(s, e, selected_contracts, review_key):
                        if not self._is_full_admin_authorized(review_key):
                            return self._review_empty_history_dataframe()
                        if selected_contracts:
                            try:
                                import recover_chain
                                recover_chain.main(selected_contracts)
                            except Exception as exc:
                                print(f"Recovery failed: {exc}")
                                
                        df = self.trust.get_filtered_history(s, e)
                        display_cols = ["Timestamp", "Utterance", "Dialect", "Data_Origin", "Data_Approved", "Block", "TX Hash"]
                        available = [c for c in display_cols if c in df.columns]
                        
                        # Truncate TX Hash for UI only
                        if "TX Hash" in available:
                            df["TX Hash"] = df["TX Hash"].apply(lambda x: str(x)[:15] + "..." if len(str(x)) > 15 else x)
                            
                        return df[available]

                    def generate_report(s, e, review_key):
                        if not self._is_full_admin_authorized(review_key):
                            return None
                        df = self.trust.get_filtered_history(s, e)
                        report_path = "/app/purechain_audit_report.csv"
                        export_cols = ["Timestamp", "Utterance", "Dialect", "Clarification", "Data_Origin", "Data_Approved", "Block", "TX Hash"]
                        available_cols = [c for c in export_cols if c in df.columns]
                        df[available_cols].to_csv(report_path, index=False, encoding='utf-8-sig')
                        return report_path
                    
                    def make_explorer_link(evt: gr.SelectData, df):
                        try:
                            tx_hash = df.iloc[evt.index[0]]["TX Hash"]
                            return f"🔍 **[View Transaction on Explorer](http://3.34.161.207:3000/tx/{tx_hash})**"
                        except: return "Select a valid row"

                    btn_filter.click(run_filter, [start_date, end_date, legacy_checkboxes, admin_review_key], [history_df])
                    export_report_btn.click(generate_report, inputs=[start_date, end_date, admin_review_key], outputs=[report_file]).then(run_filter, [start_date, end_date, legacy_checkboxes, admin_review_key], [history_df])
                    history_df.select(make_explorer_link, [history_df], [explorer_link])
            
                with gr.Tab("💾 System Backups"):
                    with gr.Row():
                        backup_target = gr.Dropdown(choices=backup_files, label="Select File")
                        backup_desc = gr.Textbox(label="Backup Note", value="Routine check")
                        backup_btn = gr.Button("🚀 Create Immutable Backup", variant="primary")

                        recover_btn = gr.Button("🔄 Recover Data from Blockchain", variant="secondary")
                    backup_log = gr.Textbox(label="Backup Status", interactive=False)
                    
                    gr.Markdown("---")
                    
                    with gr.Row():
                        bytecode_input = gr.Textbox(label="Paste Contract Bytecode", lines=3)
                        deploy_btn = gr.Button("🚀 Force Deploy (Zero Gas)", variant="stop")
                    deployment_output = gr.Textbox(label="Deployment Result", interactive=False)

                    def force_deploy_with_key(bytecode, review_key):
                        if not self._is_full_admin_authorized(review_key):
                            return self._full_admin_required_message()
                        return self.trust.force_deploy_contract(bytecode)

                    deploy_btn.click(
                        fn=force_deploy_with_key,
                        inputs=[bytecode_input, admin_review_key],
                        outputs=[deployment_output]
                    )

                # --- NEW TAB: REJECTED ENTRIES AUDIT ---
                with gr.Tab("🚫 Rejected Entries Audit"):
                    gr.Markdown("### 🪦 The Burn Ward (Rejected & Burned Data)")
                    
                    with gr.Row():
                        btn_refresh_graveyard = gr.Button("🔄 Refresh Graveyard", variant="primary")
                    
                    graveyard_df = gr.Dataframe(
                        headers=["Timestamp", "Utterance", "Dialect", "Rejection_Reason", "Rejector"], 
                        interactive=False,
                        wrap=False,
                        row_count=(5, "dynamic")
                    )
                    
                    def refresh_graveyard(review_key):
                        import os
                        import pandas as pd
                        if not self._is_full_admin_authorized(review_key):
                            return pd.DataFrame(columns=["Timestamp", "Utterance", "Dialect", "Rejection_Reason", "Rejector"])
                        path = os.path.join(self.trust.config.DATASET_DIR, "..", "rejected_graveyard.csv")
                        if os.path.exists(path):
                            df = pd.read_csv(path)
                            cols = ["Timestamp", "Utterance", "Dialect", "Rejection_Reason", "Rejector"]
                            return df[[c for c in cols if c in df.columns]]
                        return pd.DataFrame()
                        
                    btn_refresh_graveyard.click(refresh_graveyard, inputs=[admin_review_key], outputs=[graveyard_df])

                # --- TAB 6: BUG REPORTS & FEEDBACK ---
                with gr.Tab("🐛 Bug Reports & Feedback"):
                    gr.Markdown("### 🛡️ Secure System Feedback Log")
                    
                    with gr.Row():
                        with gr.Column(scale=3):
                            btn_refresh_fb = gr.Button("🔄 Refresh Feedback List", variant="secondary")
                            feedback_df = gr.Dataframe(
                                headers=["Timestamp", "Operator_ID", "Feedback_Text", "Image_Reference"], 
                                interactive=False,
                                wrap=False,
                                row_count=(5, "dynamic")
                            )
                        with gr.Column(scale=1):
                            gr.Markdown("#### 📸 Attached Screenshot")
                            feedback_image = gr.Image(label="Click a row to view screenshot", interactive=False)

                # --- TAB 7: PASSIVE EEQ-S DASHBOARD ---
                with gr.Tab("📊 EEQ-s Dashboard"):
                    gr.Markdown("### Passive EEQ-s Interaction Index")
                    gr.Markdown("Aggregated from frontend passive/manual EEQ-s auto-sync and manual sync events.")
                    with gr.Row():
                        btn_refresh_eeqs = gr.Button("🔄 Refresh EEQ-s Dashboard", variant="secondary")
                        btn_download_eeqs = gr.Button("⬇️ Prepare EEQ-s Audit ZIP", variant="secondary")
                    eeqs_df = gr.Dataframe(
                        headers=[
                            "Game", "AI_Model", "ASR_Model", "Source_Language", "Source_Dialect",
                            "Target_Language", "Target_Dialect", "Interaction_Type", "Event_Count",
                            "Mean_EEQ", "Last_EEQ", "Last_Seen"
                        ],
                        interactive=False,
                        wrap=False,
                        row_count=(8, "dynamic")
                    )
                    eeqs_download_file = gr.File(label="Download passive/manual EEQ-s audit ZIP")

            # ==========================================
            #            EVENT BINDINGS
            # ==========================================

            export_data_btn.click(self.api_get_full_dataset_zip, inputs=[admin_review_key], outputs=[export_zip_file])
            train_btn.click(self.api_generate_training_data, inputs=[admin_review_key], outputs=[train_status])

            def admin_automated_pipeline(audio_path, lang, review_key):
                if not self._is_full_admin_authorized(review_key):
                    return (
                        pd.DataFrame(),
                        self._review_unauthorized_message(),
                        self.get_quota_status(),
                        "",
                        None,
                        "",
                        "Neutral / Conversational",
                        "",
                        ""
                    )
                return self.automated_pipeline(audio_path, lang, None)

            btn_run.click(
                admin_automated_pipeline, 
                [audio_in, lang_sel, admin_review_key], 
                [results_table, log_box, quota_box, orig_text, dialect_sel, clar_text, tone_sel, ctx_area, prag_area]
            )

            audio_in.stop_recording(
                admin_automated_pipeline, 
                [audio_in, lang_sel, admin_review_key], 
                [results_table, log_box, quota_box, orig_text, dialect_sel, clar_text, tone_sel, ctx_area, prag_area]
            )

            audio_in.upload(
                admin_automated_pipeline, 
                [audio_in, lang_sel, admin_review_key], 
                [results_table, log_box, quota_box, orig_text, dialect_sel, clar_text, tone_sel, ctx_area, prag_area]
            )

            def handle_selection(evt: gr.SelectData, df):
                if df is None or not hasattr(df, 'columns') or len(df) == 0: 
                    return "", "", "", "Neutral / Conversational", "", ""
                try:
                    row = df.iloc[evt.index[0]]
                    d = row["Dialect"] if row["Dialect"] in dropdown_choices else None
                    return row["Utterance"], d, row["Clarification"], row["Tone"], row.get("Context", ""), row.get("Pragmatic Analysis", "")
                except: return "", "", "", "Neutral / Conversational", "", ""

            results_table.select(handle_selection, [results_table], [orig_text, dialect_sel, clar_text, tone_sel, ctx_area, prag_area])
            export_btn.click(self.export_analysis_to_csv, [results_table, admin_review_key], [export_file]).then(lambda: gr.update(visible=True), None, [export_file])

            btn_save.click(
                fn=self.admin_check_and_submit_logic, 
                inputs=[
                    orig_text, dialect_sel, new_dialect, clar_text, tone_sel, ctx_area, prag_area,
                    ui_source_tag,       # 8. sourceTag ("Web")
                    ui_clar_source,      # 9. clar_source ("Lab Admin")
                    ui_operator_id,      # 10. userKey (Generated ID)
                    audio_in,            # 11. blob
                    admin_review_key
                ], 
                outputs=[feedback_msg, btn_over]
            )
            
            btn_over.click(
                fn=self.admin_force_overwrite_logic, 
                inputs=[
                    orig_text, dialect_sel, new_dialect, clar_text, tone_sel, ctx_area, prag_area,
                    ui_source_tag,       # 8. sourceTag
                    ui_clar_source,      # 9. clar_source
                    ui_operator_id,      # 10. userKey
                    audio_in,            # 11. blob
                    admin_review_key
                ], 
                outputs=[feedback_msg, btn_over]
            )
            
            
            # Audit / Pending
            def select_pending_row(evt: gr.SelectData, df):
                try: 
                    idx = evt.index[0]
                    row = df.iloc[idx]
                    audio_path = row.get("Audio")
                    timestamp = str(row.get("Timestamp", ""))
                    utt = str(row.get("Utterance", ""))
                    clar = str(row.get("Clarification", ""))
                    tone = str(row.get("Tone", ""))
                    dialect = str(row.get("Dialect", ""))
                    lang = str(row.get("Language", ""))
                    return audio_path, timestamp, utt, utt, lang, dialect, clar, tone
                except: 
                    return None, "", "", "", "", "", "", ""

            def update_dialects(lang):
                if lang == "All":
                    return gr.update(choices=["All"] + dropdown_choices, value="All")
                elif lang in language_hierarchy_dict:
                    new_choices = [d for d in language_hierarchy_dict[lang] if d != "+ Add New Dialect"]
                    return gr.update(choices=["All"] + new_choices, value="All")
                return gr.update(choices=["All"], value="All")
                
            filter_language.change(update_dialects, inputs=[filter_language], outputs=[filter_dialect]).then(
                self.get_pending_dataframe, inputs=[filter_language, filter_dialect, admin_review_key], outputs=[pending_df]
            )

            filter_dialect.change(self.get_pending_dataframe, inputs=[filter_language, filter_dialect, admin_review_key], outputs=[pending_df])
            btn_refresh.click(self.get_pending_dataframe, inputs=[filter_language, filter_dialect, admin_review_key], outputs=[pending_df])
            pending_df.select(select_pending_row, [pending_df], [pending_audio_player, pending_timestamp, pending_orig_utt, edit_utt, edit_lang, edit_dialect, edit_clar, edit_tone])

            btn_appr_p.click(self.admin_approve_pending, inputs=[pending_timestamp, pending_orig_utt, edit_utt, edit_lang, edit_dialect, edit_clar, edit_tone, pending_audio_player, admin_review_key], outputs=[audit_log]).then(self.get_pending_dataframe, inputs=[filter_language, filter_dialect, admin_review_key], outputs=[pending_df])
            btn_rejt_p.click(self.admin_reject_pending, inputs=[pending_timestamp, pending_orig_utt, reject_reason_dropdown, reject_reason_text, admin_review_key], outputs=[audit_log]).then(self.get_pending_dataframe, inputs=[filter_language, filter_dialect, admin_review_key], outputs=[pending_df])
            btn_undo_admin.click(self.admin_undo_last_action, inputs=[admin_review_key], outputs=[audit_log]).then(self.get_pending_dataframe, inputs=[filter_language, filter_dialect, admin_review_key], outputs=[pending_df])
            btn_clear_pending.click(self.admin_clear_all_pending, inputs=[admin_review_key], outputs=[audit_log]).then(self.get_pending_dataframe, inputs=[filter_language, filter_dialect, admin_review_key], outputs=[pending_df])

            def trigger_recovery(review_key):
                if not self._is_full_admin_authorized(review_key):
                    return self._full_admin_required_message()
                try:
                    import recover_chain # Left local specifically to prevent circular dependency at startup
                    result_message = recover_chain.main() 
                    return result_message
                except Exception as e:
                    return f"❌ Recovery Error: {e}"
                    
            recover_btn.click(trigger_recovery, inputs=[admin_review_key], outputs=[backup_log])
            
            # --- Feedback Tab Events ---
            def show_feedback_image(evt: gr.SelectData, df):
                try:
                    img_path = str(df.iloc[evt.index[0]].get("Image_Reference", ""))
                    if img_path == "nan" or not img_path:
                        return None
                        
                    if not os.path.exists(img_path) and os.environ.get("HF_TOKEN"):
                        img_name = os.path.basename(img_path)
                        try:
                            dl_img = hf_hub_download(
                                repo_id="toecm/PureChain_Dataset", 
                                filename=f"feedback_images/{img_name}", 
                                repo_type="dataset", 
                                token=os.environ.get("HF_TOKEN")
                            )
                            os.makedirs(os.path.dirname(img_path), exist_ok=True)
                            shutil.copy(dl_img, img_path)
                        except:
                            pass
                            
                    return img_path if os.path.exists(img_path) else None
                except:
                    return None

            btn_refresh_fb.click(self.get_feedback_dataframe, inputs=[admin_review_key], outputs=[feedback_df])
            feedback_df.select(show_feedback_image, [feedback_df], [feedback_image])
            btn_refresh_eeqs.click(self.get_eeqs_dashboard_dataframe, inputs=[admin_review_key], outputs=[eeqs_df])
            btn_download_eeqs.click(self.get_eeqs_download_file, inputs=[admin_review_key], outputs=[eeqs_download_file])

            # ⬇️ COMPREHENSIVE API BRIDGE FOR REACT FRONTEND ⬇️
            gr.Markdown("---")
            gr.Markdown("### 📡 API Gateway (Headless endpoints for React)")
            
            with gr.Row(visible=False):
                api_sync_out = gr.Textbox()
                api_btn_sync = gr.Button()
                api_btn_sync.click(fn=check_cloud_sync_status, inputs=[], outputs=[api_sync_out], api_name="check_cloud_sync")

                api_btn_admin_undo = gr.Button()
                api_admin_undo_out = gr.Textbox()
                api_admin_undo_key = gr.Textbox()
                api_btn_admin_undo.click(fn=self.admin_undo_last_action, inputs=[api_admin_undo_key], outputs=[api_admin_undo_out], api_name="admin_undo_last_action")
                
                 
                # 🟢 FIX: Added an invisible input box to satisfy the JS client routing
                api_btn_dialects = gr.Button()
                api_dialects_out = gr.Textbox() 
                api_btn_dialects.click(fn=self.api_get_dialects, inputs=[], outputs=[api_dialects_out], api_name="api_get_dialects")
                
                api_btn_hierarchy = gr.Button()
                api_hierarchy_out = gr.Textbox()
                api_btn_hierarchy.click(fn=self.api_get_language_hierarchy, inputs=[], outputs=[api_hierarchy_out], api_name="api_get_language_hierarchy")

                api_admin_key = gr.Textbox()
                api_history_start = gr.Textbox()
                api_history_end = gr.Textbox()
                api_history_contracts = gr.CheckboxGroup(
                    choices=["PureIUUY", "PureConvo", "PureVersation", "PureBi (Jun 11)", "PureBi (Jun 12)", "PureBi (Jun 13)", "PureBi"],
                    value=["PureBi"]
                )
                api_admin_df_out = gr.Dataframe()
                api_btn_history = gr.Button()
                api_btn_history.click(
                    fn=run_filter,
                    inputs=[api_history_start, api_history_end, api_history_contracts, api_admin_key],
                    outputs=[api_admin_df_out],
                    api_name="run_filter"
                )
                api_btn_eeqs_dashboard = gr.Button()
                api_btn_eeqs_dashboard.click(
                    fn=self.get_eeqs_dashboard_dataframe,
                    inputs=[api_admin_key],
                    outputs=[api_admin_df_out],
                    api_name="get_eeqs_dashboard_dataframe"
                )
                api_btn_eeqs_dashboard_alias = gr.Button()
                api_btn_eeqs_dashboard_alias.click(
                    fn=self.get_eeqs_dashboard_dataframe,
                    inputs=[api_admin_key],
                    outputs=[api_admin_df_out],
                    api_name="refresh_eeqs_dashboard"
                )
                
                api_btn_mission = gr.Button()
                api_topic_in = gr.Textbox()
                api_mission_out = gr.Textbox()
                api_btn_mission.click(fn=self.api_generate_mission, inputs=[api_topic_in], outputs=[api_mission_out], api_name="generate_mission")
                
                api_btn_transcribe = gr.Button()
                api_audio_in = gr.File() 
                api_dialect_in = gr.Textbox()
                api_transcribe_out = gr.Textbox()
                api_btn_transcribe.click(fn=self.api_transcribe, inputs=[api_audio_in, api_dialect_in], outputs=[api_transcribe_out], api_name="transcribe_check")

                api_btn_transcribe_v2 = gr.Button()
                api_speech_model_in = gr.Textbox(value="auto")
                api_audio_sanitation_in = gr.Textbox(value="on")
                api_btn_transcribe_v2.click(
                    fn=self.api_transcribe_with_model,
                    inputs=[api_audio_in, api_dialect_in, api_speech_model_in, api_audio_sanitation_in],
                    outputs=[api_transcribe_out],
                    api_name="transcribe_check_v2"
                )

                api_text_in = gr.Textbox()
                api_acoustic_language_in = gr.Textbox()
                api_acoustic_voice_in = gr.Textbox(value="browser-native")
                api_acoustic_models_out = gr.Textbox()
                api_acoustic_json_out = gr.Textbox()
                api_btn_acoustic_models = gr.Button()
                api_btn_acoustic_models.click(
                    fn=self.api_acoustic_models,
                    inputs=[],
                    outputs=[api_acoustic_models_out],
                    api_name="api_acoustic_models"
                )
                api_btn_acoustic_transcribe = gr.Button()
                api_btn_acoustic_transcribe.click(
                    fn=self.api_acoustic_transcribe,
                    inputs=[api_audio_in, api_acoustic_language_in, api_dialect_in, api_speech_model_in, api_audio_sanitation_in],
                    outputs=[api_acoustic_json_out],
                    api_name="api_acoustic_transcribe"
                )
                api_btn_acoustic_tts = gr.Button()
                api_btn_acoustic_tts.click(
                    fn=self.api_acoustic_tts,
                    inputs=[api_text_in, api_acoustic_language_in, api_dialect_in, api_acoustic_voice_in],
                    outputs=[api_acoustic_json_out],
                    api_name="api_acoustic_tts"
                )
                
                api_btn_clarify = gr.Button()
                api_clarify_out = gr.Textbox()
                api_btn_clarify.click(fn=self.api_clarify, inputs=[api_text_in, api_dialect_in], outputs=[api_clarify_out], api_name="generate_clarifications")
                api_ai_model_in = gr.Textbox(value="auto")
                api_btn_clarify_v2 = gr.Button()
                api_btn_clarify_v2.click(
                    fn=self.api_clarify_with_model,
                    inputs=[api_text_in, api_dialect_in, api_ai_model_in],
                    outputs=[api_clarify_out],
                    api_name="generate_clarifications_v2"
                )
                
                api_btn_submit = gr.Button()
                api_custom_d = gr.Textbox()
                api_tone = gr.Textbox()
                api_context = gr.Textbox()
                api_pragmatics = gr.Textbox()
                api_source_tag = gr.Textbox(visible=False, value="Web")
                api_clar_source = gr.Textbox(visible=False, value="AI")
                api_user_key = gr.Textbox(visible=False, value="")
                api_confirm = gr.State(False)
                
                # 🟢 NEW: Peer-to-Peer Translation Endpoint
                api_btn_translate = gr.Button()
                api_translate_text_in = gr.Textbox()
                api_translate_source_in = gr.Textbox()
                api_translate_target_in = gr.Textbox()
                api_translate_out = gr.Textbox()
                api_btn_translate.click(
                    fn=self.api_translate_peer, 
                    inputs=[api_translate_text_in, api_translate_source_in, api_translate_target_in], 
                    outputs=[api_translate_out], 
                    api_name="translate_peer"
                )
                api_btn_translate_v2 = gr.Button()
                api_btn_translate_v2.click(
                    fn=self.api_translate_peer,
                    inputs=[api_translate_text_in, api_translate_source_in, api_translate_target_in, api_ai_model_in],
                    outputs=[api_translate_out],
                    api_name="translate_peer_v2"
                )

                # 🟢 NEW: Dialect Relay Endpoints
                api_btn_join = gr.Button()
                api_q_op = gr.Textbox()
                api_q_dialect = gr.Textbox()
                api_q_target = gr.Textbox()
                api_q_out = gr.Textbox()
                api_btn_join.click(fn=self.api_join_queue, inputs=[api_q_op, api_q_dialect, api_q_target], outputs=[api_q_out], api_name="join_queue")

                api_btn_lobby = gr.Button()
                api_btn_lobby.click(fn=self.api_get_lobby, inputs=[], outputs=[api_q_out], api_name="get_lobby")

                api_btn_check = gr.Button()
                api_c_out = gr.Textbox()
                api_btn_check.click(fn=self.api_check_match, inputs=[api_q_op], outputs=[api_c_out], api_name="check_match")
                
                api_btn_leave = gr.Button()
                api_btn_leave.click(fn=self.api_leave_queue, inputs=[api_q_op], outputs=[api_c_out], api_name="leave_queue")

                api_btn_relay_send = gr.Button()
                api_relay_room = gr.Textbox()
                api_relay_text = gr.Textbox()
                api_relay_target = gr.Textbox() 
                api_relay_meaning = gr.Textbox()
                api_relay_out = gr.Textbox()
                api_btn_relay_send.click(fn=self.api_remote_eval_and_send, inputs=[api_relay_room, api_q_op, api_relay_text, api_q_dialect, api_relay_target, api_relay_meaning], outputs=[api_relay_out], api_name="relay_send")
                api_btn_relay_poll = gr.Button()
                api_poll_idx = gr.Number()
                api_btn_relay_poll.click(fn=self.api_remote_poll, inputs=[api_relay_room, api_poll_idx], outputs=[api_c_out], api_name="relay_poll")
                
                # 7. Secure Feedback Endpoint
                # 8. Oracle XP and Task Endpoints
                api_xp_op = gr.Textbox()
                api_xp_dia = gr.Textbox()
                api_review_key = gr.Textbox()
                api_xp_out = gr.Textbox()
                api_btn_xp = gr.Button()
                api_btn_xp.click(fn=self.api_get_user_xp, inputs=[api_xp_op, api_xp_dia], outputs=[api_xp_out], api_name="get_xp")
                
                api_btn_task = gr.Button()
                api_btn_task.click(fn=self.api_get_oracle_task, inputs=[api_xp_op, api_xp_dia, api_review_key], outputs=[api_xp_out], api_name="get_oracle_task")
                
                api_btn_review = gr.Button()
                api_rev_row = gr.Textbox()
                api_rev_chain = gr.Textbox()
                api_rev_appr = gr.Textbox()
                api_rev_reason = gr.Textbox()
                api_btn_review.click(fn=self.api_submit_oracle_review, inputs=[api_rev_row, api_rev_chain, api_xp_op, api_xp_dia, api_rev_appr, api_rev_reason, api_review_key], outputs=[api_xp_out], api_name="submit_oracle_review")

                api_btn_appeal = gr.Button()
                api_btn_appeal.click(fn=self.api_submit_appeal, inputs=[api_rev_chain, api_xp_op, api_xp_dia, api_review_key], outputs=[api_xp_out], api_name="submit_appeal")

                api_fb_btn = gr.Button()
                api_fb_op = gr.Textbox()
                api_fb_text = gr.Textbox()
                api_fb_img = gr.File()
                api_fb_out = gr.Textbox()
                api_fb_btn.click(
                    fn=self.handle_feedback_submission,
                    inputs=[api_fb_op, api_fb_text, api_fb_img],
                    outputs=[api_fb_out],
                    api_name="submit_feedback"
                )

                api_eeqs_btn = gr.Button()
                api_eeqs_op = gr.Textbox()
                api_eeqs_payload = gr.Textbox()
                api_eeqs_out = gr.Textbox()
                api_eeqs_btn.click(
                    fn=self.handle_eeqs_submission,
                    inputs=[api_eeqs_op, api_eeqs_payload],
                    outputs=[api_eeqs_out],
                    api_name="submit_eeqs"
                )
                
                # 8. Oracle XP and Task Endpoints
                api_btn_get_xp = gr.Button()
                api_xp_op = gr.Textbox()
                api_xp_dialect = gr.Textbox()
                api_review_key_v2 = gr.Textbox()
                api_xp_out = gr.Textbox()
                api_btn_get_xp.click(fn=self.api_get_user_xp, inputs=[api_xp_op, api_xp_dialect], outputs=[api_xp_out], api_name="get_user_xp")

                api_btn_get_oracle = gr.Button()
                api_btn_get_oracle.click(fn=self.api_get_oracle_task, inputs=[api_xp_op, api_xp_dialect, api_review_key_v2], outputs=[api_xp_out], api_name="get_oracle_task")

                api_btn_get_graveyard = gr.Button()
                api_btn_get_graveyard.click(fn=self.api_get_graveyard_task, inputs=[api_xp_op, api_xp_dialect, api_review_key_v2], outputs=[api_xp_out], api_name="get_graveyard_task")

                api_btn_sub_oracle = gr.Button()
                api_sub_row = gr.Textbox()
                api_sub_chain = gr.Textbox()
                api_sub_approve = gr.Textbox()
                api_btn_sub_oracle.click(fn=self.api_submit_oracle_review, inputs=[api_sub_row, api_sub_chain, api_xp_op, api_xp_dialect, api_sub_approve, api_review_key_v2], outputs=[api_xp_out], api_name="submit_oracle_review")
                
                api_btn_appeal = gr.Button()
                api_btn_appeal.click(fn=self.api_submit_appeal, inputs=[api_sub_chain, api_xp_op, api_xp_dialect, api_review_key_v2], outputs=[api_xp_out], api_name="submit_appeal")
                
                api_language_in = gr.Textbox()
                api_btn_submit.click(
                    fn=self.check_and_submit_logic, 
                    inputs=[
                        api_text_in,       # 1. transcribed
                        api_dialect_in,    # 2. dialect
                        api_custom_d,      # 3. customD
                        api_clarify_out,   # 4. clarification
                        api_tone,          # 5. tone
                        api_context,       # 6. context
                        api_pragmatics,    # 7. pragmatics
                        api_source_tag,    # 8. sourceTag
                        api_clar_source,   # 9. clar_source
                        api_user_key,      # 10. userKey
                        api_audio_in,      # 11. blob (audio)
                        api_confirm,       # 12. confirm
                        api_language_in    # 13. language
                    ], 
                    outputs=[feedback_msg, btn_over], 
                    api_name="check_and_submit_logic"
                )
            # ⬆️ END OF API BRIDGE ⬆️

        return ui