File size: 102,787 Bytes
8372659
d4df2a7
8372659
 
15710ed
8372659
def69a7
9a6c98c
ff522ab
d4df2a7
9a6c98c
 
 
35bb43f
9a6c98c
 
 
8372659
9a6c98c
1af10cc
9a6c98c
8372659
d4df2a7
ecc3905
22f81ff
8372659
15710ed
35bb43f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66fc7d8
 
 
ff522ab
 
 
 
 
 
250bf8c
ff522ab
250bf8c
ff522ab
 
 
250bf8c
ff522ab
66fc7d8
 
 
ff522ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66fc7d8
35bb43f
8372659
9a6c98c
bbd9cd6
 
 
250bf8c
9a6c98c
 
250bf8c
bbd9cd6
 
 
 
1af10cc
 
 
250bf8c
9a6c98c
 
250bf8c
9a6c98c
 
 
 
 
 
 
 
 
 
 
250bf8c
 
9a6c98c
 
 
 
 
 
 
 
 
 
1af10cc
9a6c98c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
250bf8c
 
9a6c98c
 
 
 
 
 
 
 
 
250bf8c
9a6c98c
 
 
1af10cc
250bf8c
9a6c98c
 
 
 
 
1af10cc
9a6c98c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1af10cc
9a6c98c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bbd9cd6
 
9a6c98c
 
 
 
 
 
 
 
 
 
 
 
250bf8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bc987d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff522ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
250bf8c
ff522ab
 
 
 
 
66fc7d8
ff522ab
 
 
 
 
250bf8c
ff522ab
 
 
 
 
 
 
 
 
 
 
250bf8c
ff522ab
 
 
250bf8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff522ab
 
 
250bf8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff522ab
250bf8c
ff522ab
250bf8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff522ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
250bf8c
ff522ab
 
 
bc987d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2902ce4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff522ab
8372659
 
ff522ab
2902ce4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff522ab
 
 
 
 
 
 
 
2902ce4
ff522ab
 
 
2902ce4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff522ab
 
2902ce4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
250bf8c
2902ce4
 
 
 
 
 
 
 
ff522ab
2902ce4
bbd9cd6
2902ce4
bbd9cd6
2902ce4
 
 
9a6c98c
2902ce4
9a6c98c
 
2902ce4
9a6c98c
2902ce4
 
 
ff522ab
2902ce4
 
 
 
 
 
 
 
 
 
 
 
9a6c98c
 
 
 
ff522ab
2902ce4
 
9a6c98c
 
 
 
2902ce4
 
9a6c98c
ff522ab
2902ce4
bbd9cd6
ff522ab
2902ce4
ff522ab
2902ce4
 
ff522ab
 
 
2902ce4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a6c98c
 
 
 
bbd9cd6
ff522ab
2902ce4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a6c98c
 
 
 
ff522ab
2902ce4
 
 
 
 
 
ff522ab
 
2902ce4
 
 
 
 
 
 
 
 
ff522ab
 
2902ce4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff522ab
2902ce4
 
 
 
 
 
ff522ab
2902ce4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff522ab
2902ce4
ff522ab
 
 
 
250bf8c
2902ce4
 
 
 
 
 
250bf8c
2902ce4
 
250bf8c
2902ce4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
250bf8c
2902ce4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
250bf8c
 
2902ce4
 
 
 
 
 
250bf8c
 
2902ce4
 
 
 
 
 
 
 
 
 
 
 
250bf8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8372659
2902ce4
 
 
 
 
 
 
ff522ab
 
 
 
 
 
 
 
 
 
 
 
 
250bf8c
ff522ab
 
 
 
2902ce4
 
 
 
 
250bf8c
ff522ab
 
 
 
 
250bf8c
 
 
 
 
ff522ab
 
 
250bf8c
ff522ab
250bf8c
 
 
 
 
 
 
ff522ab
 
 
8372659
2902ce4
 
 
 
 
 
 
ff522ab
 
 
 
 
 
 
 
 
 
 
250bf8c
ff522ab
 
 
 
 
2902ce4
 
 
 
 
ff522ab
 
 
 
 
 
 
 
 
250bf8c
ff522ab
 
 
bc987d8
2902ce4
 
 
 
 
 
 
bc987d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2902ce4
 
 
 
 
 
 
bc987d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2902ce4
 
 
 
 
 
 
250bf8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2902ce4
 
 
 
 
 
 
250bf8c
ff522ab
 
 
 
 
 
 
 
 
 
 
 
 
250bf8c
ff522ab
 
 
 
250bf8c
ff522ab
 
250bf8c
ff522ab
 
 
8372659
2902ce4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8372659
ff522ab
8372659
def69a7
8372659
ff522ab
d4df2a7
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
"""
Gradio web interface for the TutorX MCP Server with SSE support
"""

import os
import json
import asyncio
import gradio as gr
from typing import Optional, Dict,  List, Tuple
import requests
import networkx as nx
import matplotlib
import matplotlib.pyplot as plt
from datetime import datetime

# Set matplotlib to use 'Agg' backend to avoid GUI issues in Gradio
matplotlib.use('Agg')

# Import MCP client components
from mcp.client.sse import sse_client
from mcp.client.session import ClientSession

# Server configuration
# SERVER_URL = "http://localhost:8000/sse"  # Ensure this is the SSE endpoint
SERVER_URL = "https://tutorx-mcp.onrender.com/sse"
# Utility functions

async def ping_mcp_server() -> None:
    """Send a ping request to the MCP server"""
    try:
        async with sse_client(SERVER_URL) as (sse, write):
            async with ClientSession(sse, write) as session:
                await session.initialize()
                print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] Successfully pinged MCP server")
    except Exception as e:
        print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] Error pinging MCP server: {str(e)}")

async def start_periodic_ping(interval_minutes: int = 10) -> None:
    """Start a background task to ping the MCP server periodically"""
    while True:
        await ping_mcp_server()
        await asyncio.sleep(interval_minutes * 60)

# Store the ping task reference
ping_task = None

async def check_plagiarism_async(submission, reference):
    """Check submission for plagiarism against reference sources"""
    async with sse_client(SERVER_URL) as (sse, write):
        async with ClientSession(sse, write) as session:
            await session.initialize()
            response = await session.call_tool(
                "check_submission_originality",
                {
                    "submission": submission,
                    "reference_sources": [reference] if isinstance(reference, str) else reference
                }
            )
            return await extract_response_content(response)

def start_ping_task():
    """Start the ping task when the Gradio app launches"""
    global ping_task
    try:
        if ping_task is None:
            try:
                loop = asyncio.get_event_loop()
            except RuntimeError:
                loop = asyncio.new_event_loop()
                asyncio.set_event_loop(loop)
                
            if loop.is_running():
                ping_task = loop.create_task(start_periodic_ping())
                print("Started periodic ping task")
            else:
                # If loop is not running, we'll start it in a separate thread
                import threading
                def start_loop():
                    asyncio.set_event_loop(loop)
                    loop.run_forever()
                
                thread = threading.Thread(target=start_loop, daemon=True)
                thread.start()
                ping_task = asyncio.run_coroutine_threadsafe(start_periodic_ping(), loop)
                print("Started periodic ping task in new thread")
    except Exception as e:
        print(f"Error starting ping task: {e}")

# Only run this code when the module is executed directly
if __name__ == "__main__" and not hasattr(gr, 'blocks'):
    # This ensures we don't start the task when imported by Gradio
    start_ping_task()



async def load_concept_graph(concept_id: str = None) -> Tuple[Optional[plt.Figure], Dict, List]:
    """
    Load and visualize the concept graph for a given concept ID.
    If no concept_id is provided, returns the first available concept.

    Args:
        concept_id: The ID or name of the concept to load

    Returns:
        tuple: (figure, concept_details, related_concepts) or (None, error_dict, [])
    """
    try:
        async with sse_client(SERVER_URL) as (sse, write):
            async with ClientSession(sse, write) as session:
                await session.initialize()

                # Call the concept graph tool
                result = await session.call_tool(
                    "get_concept_graph_tool",
                    {"concept_id": concept_id} if concept_id else {}
                )
                
                # Extract content if it's a TextContent object
                if hasattr(result, 'content') and isinstance(result.content, list):
                    for item in result.content:
                        if hasattr(item, 'text') and item.text:
                            try:
                                result = json.loads(item.text)
                                break
                            except json.JSONDecodeError as e:
                                return None, {"error": f"Failed to parse JSON from TextContent: {str(e)}"}, []

                # If result is a string, try to parse it as JSON
                if isinstance(result, str):
                    try:
                        result = json.loads(result)
                    except json.JSONDecodeError as e:
                        return None, {"error": f"Failed to parse concept graph data: {str(e)}"}, []
                
                # Handle backend error response
                if isinstance(result, dict) and "error" in result:
                    error_msg = f"Backend error: {result['error']}"
                    return None, {"error": error_msg}, []
                
                concept = None
                
                # Handle different response formats
                if isinstance(result, dict):
                    # Case 1: Direct concept object
                    if "id" in result or "name" in result:
                        concept = result
                    # Case 2: Response with 'concepts' list
                    elif "concepts" in result:
                        if result["concepts"]:
                            concept = result["concepts"][0] if not concept_id else None
                            # Try to find the requested concept by ID or name
                            if concept_id:
                                for c in result["concepts"]:
                                    if (isinstance(c, dict) and
                                        (c.get("id") == concept_id or
                                         str(c.get("name", "")).lower() == concept_id.lower())):
                                        concept = c
                                        break
                                if not concept:
                                    error_msg = f"Concept '{concept_id}' not found in the concept graph"
                                    return None, {"error": error_msg}, []
                        else:
                            error_msg = "No concepts found in the concept graph"
                            return None, {"error": error_msg}, []

                # If we still don't have a valid concept
                if not concept or not isinstance(concept, dict):
                    error_msg = "Could not extract valid concept data from response"
                    return None, {"error": error_msg}, []

                # Ensure required fields exist with defaults
                concept.setdefault('related_concepts', [])
                concept.setdefault('prerequisites', [])
                
                # Create a new directed graph
                G = nx.DiGraph()
                
                # Add the main concept node
                main_node_id = concept["id"]
                G.add_node(main_node_id, 
                          label=concept["name"], 
                          type="main",
                          description=concept["description"])
                
                # Add related concepts and edges
                all_related = []
                
                # Process related concepts
                for rel in concept.get('related_concepts', []):
                    if isinstance(rel, dict):
                        rel_id = rel.get('id', str(hash(str(rel.get('name', '')))))
                        rel_name = rel.get('name', 'Unnamed')
                        rel_desc = rel.get('description', 'Related concept')
                        
                        G.add_node(rel_id, 
                                 label=rel_name, 
                                 type="related",
                                 description=rel_desc)
                        G.add_edge(main_node_id, rel_id, type="related_to")
                        
                        all_related.append(["Related", rel_name, rel_desc])
                
                # Process prerequisites
                for prereq in concept.get('prerequisites', []):
                    if isinstance(prereq, dict):
                        prereq_id = prereq.get('id', str(hash(str(prereq.get('name', '')))))
                        prereq_name = f"[Prerequisite] {prereq.get('name', 'Unnamed')}"
                        prereq_desc = prereq.get('description', 'Prerequisite concept')
                        
                        G.add_node(prereq_id,
                                 label=prereq_name,
                                 type="prerequisite",
                                 description=prereq_desc)
                        G.add_edge(prereq_id, main_node_id, type="prerequisite_for")
                        
                        all_related.append(["Prerequisite", prereq_name, prereq_desc])
                
                # Create the plot
                plt.figure(figsize=(14, 10))
                
                # Calculate node positions using spring layout
                pos = nx.spring_layout(G, k=0.5, iterations=50, seed=42)
                
                # Define node colors and sizes based on type
                node_colors = []
                node_sizes = []
                for node, data in G.nodes(data=True):
                    if data.get('type') == 'main':
                        node_colors.append('#4e79a7')  # Blue for main concept
                        node_sizes.append(1500)
                    elif data.get('type') == 'prerequisite':
                        node_colors.append('#59a14f')  # Green for prerequisites
                        node_sizes.append(1000)
                    else:  # related
                        node_colors.append('#e15759')  # Red for related concepts
                        node_sizes.append(1000)
                
                # Draw nodes
                nx.draw_networkx_nodes(
                    G, pos,
                    node_color=node_colors,
                    node_size=node_sizes,
                    alpha=0.9,
                    edgecolors='white',
                    linewidths=2
                )
                
                # Draw edges with different styles for different relationships
                related_edges = [(u, v) for u, v, d in G.edges(data=True) 
                              if d.get('type') == 'related_to']
                prereq_edges = [(u, v) for u, v, d in G.edges(data=True) 
                             if d.get('type') == 'prerequisite_for']
                
                # Draw related edges
                nx.draw_networkx_edges(
                    G, pos,
                    edgelist=related_edges,
                    width=1.5,
                    alpha=0.7,
                    edge_color="#e15759",
                    style="solid",
                    arrowsize=15,
                    arrowstyle='-|>',
                    connectionstyle='arc3,rad=0.1'
                )
                
                # Draw prerequisite edges
                nx.draw_networkx_edges(
                    G, pos,
                    edgelist=prereq_edges,
                    width=1.5,
                    alpha=0.7,
                    edge_color="#59a14f",
                    style="dashed",
                    arrowsize=15,
                    arrowstyle='-|>',
                    connectionstyle='arc3,rad=0.1'
                )
                
                # Draw node labels with white background for better readability
                node_labels = {node: data["label"] 
                             for node, data in G.nodes(data=True) 
                             if "label" in data}
                
                nx.draw_networkx_labels(
                    G, pos,
                    labels=node_labels,
                    font_size=10,
                    font_weight="bold",
                    font_family="sans-serif",
                    bbox=dict(
                        facecolor="white",
                        edgecolor='none',
                        alpha=0.8,
                        boxstyle='round,pad=0.3',
                        linewidth=0
                    )
                )
                
                # Add a legend
                import matplotlib.patches as mpatches
                legend_elements = [
                    mpatches.Patch(facecolor='#4e79a7', label='Main Concept', alpha=0.9),
                    mpatches.Patch(facecolor='#e15759', label='Related Concept', alpha=0.9),
                    mpatches.Patch(facecolor='#59a14f', label='Prerequisite', alpha=0.9)
                ]
                
                plt.legend(
                    handles=legend_elements, 
                    loc='upper right',
                    bbox_to_anchor=(1.0, 1.0),
                    frameon=True,
                    framealpha=0.9
                )
                
                plt.axis('off')
                plt.tight_layout()
                
                # Create concept details dictionary
                concept_details = {
                    'name': concept['name'],
                    'id': concept['id'],
                    'description': concept['description']
                }
                
                # Return the figure, concept details, and related concepts
                return plt.gcf(), concept_details, all_related
                
    except Exception as e:
        return None, {"error": f"Failed to load concept graph: {str(e)}"}, []

def sync_load_concept_graph(concept_id):
    """Synchronous wrapper for async load_concept_graph, always returns 3 outputs."""
    try:
        result = asyncio.run(load_concept_graph(concept_id))
        if result and len(result) == 3:
            return result
        else:
            return None, {"error": "Unexpected result format"}, []
    except Exception as e:
        return None, {"error": str(e)}, []

# Synchronous wrapper functions for Gradio
def sync_check_plagiarism(submission, reference):
    """Synchronous wrapper for check_plagiarism_async"""
    try:
        return asyncio.run(check_plagiarism_async(submission, reference))
    except Exception as e:
        return {"error": str(e)}

# Interactive Quiz synchronous wrappers
def sync_start_interactive_quiz(quiz_data, student_id):
    """Synchronous wrapper for start_interactive_quiz_async"""
    try:
        return asyncio.run(start_interactive_quiz_async(quiz_data, student_id))
    except Exception as e:
        return {"error": str(e)}

def sync_submit_quiz_answer(session_id, question_id, selected_answer):
    """Synchronous wrapper for submit_quiz_answer_async"""
    try:
        return asyncio.run(submit_quiz_answer_async(session_id, question_id, selected_answer))
    except Exception as e:
        return {"error": str(e)}

def sync_get_quiz_hint(session_id, question_id):
    """Synchronous wrapper for get_quiz_hint_async"""
    try:
        return asyncio.run(get_quiz_hint_async(session_id, question_id))
    except Exception as e:
        return {"error": str(e)}

def sync_get_quiz_session_status(session_id):
    """Synchronous wrapper for get_quiz_session_status_async"""
    try:
        return asyncio.run(get_quiz_session_status_async(session_id))
    except Exception as e:
        return {"error": str(e)}

# Helper functions for interactive quiz interface
def format_question_display(quiz_session_data):
    """Format quiz session data for display"""
    if not quiz_session_data or "error" in quiz_session_data:
        return "โŒ No active quiz session"

    question = quiz_session_data.get("question", {})
    if not question:
        return "โœ… Quiz completed or no current question"

    question_text = question.get("question", "")
    options = question.get("options", [])
    question_num = quiz_session_data.get("current_question_number", 1)
    total = quiz_session_data.get("total_questions", 1)

    display_text = f"""
### Question {question_num} of {total}

**{question_text}**

**Options:**
"""
    for option in options:
        display_text += f"\n- {option}"

    return display_text

def update_answer_options(quiz_session_data):
    """Update answer options based on current question"""
    if not quiz_session_data or "error" in quiz_session_data:
        return gr.Radio(choices=["No options available"], value=None)

    question = quiz_session_data.get("question", {})
    options = question.get("options", ["A) Option A", "B) Option B", "C) Option C", "D) Option D"])

    return gr.Radio(choices=options, value=None, label="Select Your Answer")

def extract_question_id(quiz_session_data):
    """Extract question ID from quiz session data"""
    if not quiz_session_data or "error" in quiz_session_data:
        return ""

    question = quiz_session_data.get("question", {})
    return question.get("question_id", "")

def sync_generate_quiz(concept, difficulty):
    """Synchronous wrapper for on_generate_quiz"""
    try:
        return asyncio.run(on_generate_quiz(concept, difficulty))
    except Exception as e:
        return {"error": str(e)}

def sync_generate_lesson(topic, grade, duration):
    """Synchronous wrapper for generate_lesson_async"""
    try:
        return asyncio.run(generate_lesson_async(topic, grade, duration))
    except Exception as e:
        return {"error": str(e)}

def sync_generate_learning_path(student_id, concept_ids, student_level):
    """Synchronous wrapper for on_generate_learning_path"""
    try:
        return asyncio.run(on_generate_learning_path(student_id, concept_ids, student_level))
    except Exception as e:
        return {"error": str(e)}

def sync_text_interaction(text, student_id):
    """Synchronous wrapper for text_interaction_async"""
    try:
        return asyncio.run(text_interaction_async(text, student_id))
    except Exception as e:
        return {"error": str(e)}

def sync_document_ocr(file):
    """Synchronous wrapper for document_ocr_async"""
    try:
        return asyncio.run(document_ocr_async(file))
    except Exception as e:
        return {"error": str(e)}

# Adaptive learning synchronous wrappers
def sync_start_adaptive_session(student_id, concept_id, difficulty):
    """Synchronous wrapper for start_adaptive_session_async"""
    try:
        return asyncio.run(start_adaptive_session_async(student_id, concept_id, difficulty))
    except Exception as e:
        return {"error": str(e)}

def sync_record_learning_event(student_id, concept_id, event_type, session_id, correct, time_taken):
    """Synchronous wrapper for record_learning_event_async"""
    try:
        return asyncio.run(record_learning_event_async(student_id, concept_id, event_type, session_id, correct, time_taken))
    except Exception as e:
        return {"error": str(e)}

def sync_get_adaptive_recommendations(student_id, concept_id, session_id=None):
    """Synchronous wrapper for get_adaptive_recommendations_async"""
    try:
        return asyncio.run(get_adaptive_recommendations_async(student_id, concept_id, session_id))
    except Exception as e:
        return {"error": str(e)}

def sync_get_adaptive_learning_path(student_id, concept_ids, strategy, max_concepts):
    """Synchronous wrapper for get_adaptive_learning_path_async"""
    try:
        return asyncio.run(get_adaptive_learning_path_async(student_id, concept_ids, strategy, max_concepts))
    except Exception as e:
        return {"error": str(e)}

def sync_get_progress_summary(student_id, days=7):
    """Synchronous wrapper for get_progress_summary_async"""
    try:
        return asyncio.run(get_progress_summary_async(student_id, days))
    except Exception as e:
        return {"error": str(e)}

# AI Tutoring synchronous wrappers
def sync_start_tutoring_session(student_id, subject, learning_objectives):
    """Synchronous wrapper for start_tutoring_session_async"""
    try:
        return asyncio.run(start_tutoring_session_async(student_id, subject, learning_objectives))
    except Exception as e:
        return {"error": str(e)}

def sync_ai_tutor_chat(session_id, student_query, request_type):
    """Synchronous wrapper for ai_tutor_chat_async"""
    try:
        return asyncio.run(ai_tutor_chat_async(session_id, student_query, request_type))
    except Exception as e:
        return {"error": str(e)}

def sync_get_step_by_step_guidance(session_id, concept, current_step):
    """Synchronous wrapper for get_step_by_step_guidance_async"""
    try:
        return asyncio.run(get_step_by_step_guidance_async(session_id, concept, current_step))
    except Exception as e:
        return {"error": str(e)}

def sync_get_alternative_explanations(session_id, concept, explanation_types):
    """Synchronous wrapper for get_alternative_explanations_async"""
    try:
        return asyncio.run(get_alternative_explanations_async(session_id, concept, explanation_types))
    except Exception as e:
        return {"error": str(e)}

def sync_end_tutoring_session(session_id, session_summary):
    """Synchronous wrapper for end_tutoring_session_async"""
    try:
        return asyncio.run(end_tutoring_session_async(session_id, session_summary))
    except Exception as e:
        return {"error": str(e)}

# Content Generation synchronous wrappers
def sync_generate_interactive_exercise(concept, exercise_type, difficulty_level, student_level):
    """Synchronous wrapper for generate_interactive_exercise_async"""
    try:
        return asyncio.run(generate_interactive_exercise_async(concept, exercise_type, difficulty_level, student_level))
    except Exception as e:
        return {"error": str(e)}

def sync_generate_scenario_based_learning(concept, scenario_type, complexity_level):
    """Synchronous wrapper for generate_scenario_based_learning_async"""
    try:
        return asyncio.run(generate_scenario_based_learning_async(concept, scenario_type, complexity_level))
    except Exception as e:
        return {"error": str(e)}

def sync_generate_gamified_content(concept, game_type, target_age_group):
    """Synchronous wrapper for generate_gamified_content_async"""
    try:
        return asyncio.run(generate_gamified_content_async(concept, game_type, target_age_group))
    except Exception as e:
        return {"error": str(e)}

# Define async functions outside the interface
async def on_generate_quiz(concept, difficulty):
    try:
        if not concept or not str(concept).strip():
            return {"error": "Please enter a concept"}
        try:
            difficulty = int(float(difficulty))
            difficulty = max(1, min(5, difficulty))
        except (ValueError, TypeError):
            difficulty = 3
        if difficulty <= 2:
            difficulty_str = "easy"
        elif difficulty == 3:
            difficulty_str = "medium"
        else:
            difficulty_str = "hard"
        async with sse_client(SERVER_URL) as (sse, write):
            async with ClientSession(sse, write) as session:
                await session.initialize()
                response = await session.call_tool("generate_quiz_tool", {"concept": concept.strip(), "difficulty": difficulty_str})
                return await extract_response_content(response)
    except Exception as e:
        import traceback
        return {
            "error": f"Error generating quiz: {str(e)}\n\n{traceback.format_exc()}"
        }

async def generate_lesson_async(topic, grade, duration):
    async with sse_client(SERVER_URL) as (sse, write):
        async with ClientSession(sse, write) as session:
            await session.initialize()
            response = await session.call_tool("generate_lesson_tool", {"topic": topic, "grade_level": grade, "duration_minutes": duration})
            return await extract_response_content(response)

async def on_generate_learning_path(student_id, concept_ids, student_level):
    try:
        async with sse_client(SERVER_URL) as (sse, write):
            async with ClientSession(sse, write) as session:
                await session.initialize()
                result = await session.call_tool("get_learning_path", {
                    "student_id": student_id,
                    "concept_ids": [c.strip() for c in concept_ids.split(",") if c.strip()],
                    "student_level": student_level
                })
                return await extract_response_content(result)
    except Exception as e:
        return {"error": str(e)}

# New adaptive learning functions
async def start_adaptive_session_async(student_id, concept_id, difficulty):
    try:
        async with sse_client(SERVER_URL) as (sse, write):
            async with ClientSession(sse, write) as session:
                await session.initialize()
                result = await session.call_tool("start_adaptive_session", {
                    "student_id": student_id,
                    "concept_id": concept_id,
                    "initial_difficulty": float(difficulty)
                })
                return await extract_response_content(result)
    except Exception as e:
        return {"error": str(e)}

async def record_learning_event_async(student_id, concept_id, event_type, session_id, correct, time_taken):
    try:
        async with sse_client(SERVER_URL) as (sse, write):
            async with ClientSession(sse, write) as session:
                await session.initialize()
                result = await session.call_tool("record_learning_event", {
                    "student_id": student_id,
                    "concept_id": concept_id,
                    "event_type": event_type,
                    "session_id": session_id,
                    "event_data": {"correct": correct, "time_taken": time_taken}
                })
                return await extract_response_content(result)
    except Exception as e:
        return {"error": str(e)}

async def get_adaptive_recommendations_async(student_id, concept_id, session_id=None):
    try:
        async with sse_client(SERVER_URL) as (sse, write):
            async with ClientSession(sse, write) as session:
                await session.initialize()
                params = {
                    "student_id": student_id,
                    "concept_id": concept_id
                }
                if session_id:
                    params["session_id"] = session_id
                result = await session.call_tool("get_adaptive_recommendations", params)
                return await extract_response_content(result)
    except Exception as e:
        return {"error": str(e)}


async def get_adaptive_learning_path_async(student_id, concept_ids, strategy, max_concepts):
    try:
        # Parse concept_ids if it's a string
        if isinstance(concept_ids, str):
            concept_ids = [c.strip() for c in concept_ids.split(',') if c.strip()]

        async with sse_client(SERVER_URL) as (sse, write):
            async with ClientSession(sse, write) as session:
                await session.initialize()
                result = await session.call_tool("get_adaptive_learning_path", {
                    "student_id": student_id,
                    "target_concepts": concept_ids,
                    "strategy": strategy,
                    "max_concepts": int(max_concepts)
                })
                return await extract_response_content(result)
    except Exception as e:
        return {"error": str(e)}

async def get_progress_summary_async(student_id, days=7):
    try:
        async with sse_client(SERVER_URL) as (sse, write):
            async with ClientSession(sse, write) as session:
                await session.initialize()
                result = await session.call_tool("get_student_progress_summary", {
                    "student_id": student_id,
                    "days": int(days)
                })
                return await extract_response_content(result)
    except Exception as e:
        return {"error": str(e)}

# Interactive Quiz async functions
async def start_interactive_quiz_async(quiz_data, student_id):
    async with sse_client(SERVER_URL) as (sse, write):
        async with ClientSession(sse, write) as session:
            await session.initialize()
            response = await session.call_tool("start_interactive_quiz_tool", {"quiz_data": quiz_data, "student_id": student_id})
            return await extract_response_content(response)

async def submit_quiz_answer_async(session_id, question_id, selected_answer):
    async with sse_client(SERVER_URL) as (sse, write):
        async with ClientSession(sse, write) as session:
            await session.initialize()
            response = await session.call_tool("submit_quiz_answer_tool", {"session_id": session_id, "question_id": question_id, "selected_answer": selected_answer})
            return await extract_response_content(response)

async def get_quiz_hint_async(session_id, question_id):
    async with sse_client(SERVER_URL) as (sse, write):
        async with ClientSession(sse, write) as session:
            await session.initialize()
            response = await session.call_tool("get_quiz_hint_tool", {"session_id": session_id, "question_id": question_id})
            return await extract_response_content(response)

async def get_quiz_session_status_async(session_id):
    async with sse_client(SERVER_URL) as (sse, write):
        async with ClientSession(sse, write) as session:
            await session.initialize()
            response = await session.call_tool("get_quiz_session_status_tool", {"session_id": session_id})
            return await extract_response_content(response)

async def extract_response_content(response):
    """Helper function to extract content from MCP response"""
    # Handle direct dictionary responses (new format)
    if isinstance(response, dict):
        return response

    # Handle MCP response with content structure (CallToolResult format)
    if hasattr(response, 'content') and isinstance(response.content, list):
        for item in response.content:
            # Handle TextContent objects
            if hasattr(item, 'text') and item.text:
                try:
                    return json.loads(item.text)
                except Exception as e:
                    return {"error": f"Failed to parse response: {str(e)}", "raw_text": item.text}
            # Handle other content types
            elif hasattr(item, 'type') and item.type == 'text':
                try:
                    return json.loads(str(item))
                except Exception:
                    return {"error": "Failed to parse text content", "raw_text": str(item)}

    # Handle string responses
    if isinstance(response, str):
        try:
            return json.loads(response)
        except Exception:
            return {"error": "Failed to parse string response", "raw_text": response}

    # Handle any other response type - try to extract useful information
    if hasattr(response, '__dict__'):
        return {"error": "Unexpected response format", "type": type(response).__name__, "raw_text": str(response)}

    return {"error": "Unknown response format", "type": type(response).__name__, "raw_text": str(response)}

async def text_interaction_async(text, student_id):
    async with sse_client(SERVER_URL) as (sse, write):
        async with ClientSession(sse, write) as session:
            await session.initialize()
            response = await session.call_tool("text_interaction", {"query": text, "student_id": student_id})
            return await extract_response_content(response)

async def upload_file_to_storage(file_path):
    """Helper function to upload file to storage API"""
    try:
        url = "https://storage-bucket-api.vercel.app/upload"
        with open(file_path, 'rb') as f:
            files = {'file': (os.path.basename(file_path), f)}
            response = requests.post(url, files=files)
            response.raise_for_status()
            return response.json()
    except Exception as e:
        return {"error": f"Error uploading file to storage: {str(e)}", "success": False}

async def document_ocr_async(file):
    if not file:
        return {"error": "No file provided", "success": False}
    try:
        if isinstance(file, dict):
            file_path = file.get("path", "")
        else:
            file_path = file
        if not file_path or not os.path.exists(file_path):
            return {"error": "File not found", "success": False}
        upload_result = await upload_file_to_storage(file_path)
        if not upload_result.get("success"):
            return upload_result
        storage_url = upload_result.get("storage_url")
        if not storage_url:
            return {"error": "No storage URL returned from upload", "success": False}
        async with sse_client(SERVER_URL) as (sse, write):
            async with ClientSession(sse, write) as session:
                await session.initialize()
                response = await session.call_tool("mistral_document_ocr", {"document_url": storage_url})
                return await extract_response_content(response)
    except Exception as e:
        return {"error": f"Error processing document: {str(e)}", "success": False}

# AI Tutoring async functions
async def start_tutoring_session_async(student_id, subject, learning_objectives):
    try:
        async with sse_client(SERVER_URL) as (sse, write):
            async with ClientSession(sse, write) as session:
                await session.initialize()
                response = await session.call_tool("start_tutoring_session", {
                    "student_id": student_id,
                    "subject": subject,
                    "learning_objectives": learning_objectives
                })
                return await extract_response_content(response)
    except Exception as e:
        return {"error": str(e)}

async def ai_tutor_chat_async(session_id, student_query, request_type):
    try:
        async with sse_client(SERVER_URL) as (sse, write):
            async with ClientSession(sse, write) as session:
                await session.initialize()
                response = await session.call_tool("ai_tutor_chat", {
                    "session_id": session_id,
                    "student_query": student_query,
                    "request_type": request_type
                })
                return await extract_response_content(response)
    except Exception as e:
        return {"error": str(e)}

async def get_step_by_step_guidance_async(session_id, concept, current_step):
    try:
        async with sse_client(SERVER_URL) as (sse, write):
            async with ClientSession(sse, write) as session:
                await session.initialize()
                response = await session.call_tool("get_step_by_step_guidance", {
                    "session_id": session_id,
                    "concept": concept,
                    "current_step": current_step
                })
                return await extract_response_content(response)
    except Exception as e:
        return {"error": str(e)}

async def get_alternative_explanations_async(session_id, concept, explanation_types):
    try:
        async with sse_client(SERVER_URL) as (sse, write):
            async with ClientSession(sse, write) as session:
                await session.initialize()
                response = await session.call_tool("get_alternative_explanations", {
                    "session_id": session_id,
                    "concept": concept,
                    "explanation_types": explanation_types
                })
                return await extract_response_content(response)
    except Exception as e:
        return {"error": str(e)}

async def end_tutoring_session_async(session_id, session_summary):
    try:
        async with sse_client(SERVER_URL) as (sse, write):
            async with ClientSession(sse, write) as session:
                await session.initialize()
                response = await session.call_tool("end_tutoring_session", {
                    "session_id": session_id,
                    "session_summary": session_summary
                })
                return await extract_response_content(response)
    except Exception as e:
        return {"error": str(e)}

# Content Generation async functions
async def generate_interactive_exercise_async(concept, exercise_type, difficulty_level, student_level):
    try:
        async with sse_client(SERVER_URL) as (sse, write):
            async with ClientSession(sse, write) as session:
                await session.initialize()
                response = await session.call_tool("generate_interactive_exercise", {
                    "concept": concept,
                    "exercise_type": exercise_type,
                    "difficulty_level": difficulty_level,
                    "student_level": student_level
                })
                return await extract_response_content(response)
    except Exception as e:
        return {"error": str(e)}

async def generate_scenario_based_learning_async(concept, scenario_type, complexity_level):
    try:
        async with sse_client(SERVER_URL) as (sse, write):
            async with ClientSession(sse, write) as session:
                await session.initialize()
                response = await session.call_tool("generate_scenario_based_learning", {
                    "concept": concept,
                    "scenario_type": scenario_type,
                    "complexity_level": complexity_level
                })
                return await extract_response_content(response)
    except Exception as e:
        return {"error": str(e)}

async def generate_gamified_content_async(concept, game_type, target_age_group):
    try:
        async with sse_client(SERVER_URL) as (sse, write):
            async with ClientSession(sse, write) as session:
                await session.initialize()
                response = await session.call_tool("generate_gamified_content", {
                    "concept": concept,
                    "game_type": game_type,
                    "target_age_group": target_age_group
                })
                return await extract_response_content(response)
    except Exception as e:
        return {"error": str(e)}

# Enhanced UI/UX helper functions with Gradio Soft theme colors
def get_info_card_html(title, description, icon="โ„น๏ธ"):
    """Get HTML for a consistent info card component matching Gradio Soft theme"""
    return f"""
    <div style="background: var(--background-fill-secondary, #f7f7f7);
                border-left: 4px solid var(--color-accent, #ff6b6b);
                padding: 1rem;
                margin: 0.5rem 0;
                border-radius: var(--radius-lg, 8px);
                border: 1px solid var(--border-color-primary, #e5e5e5);">
        <h4 style="margin: 0 0 0.5rem 0; color: var(--body-text-color, #374151); font-weight: 600;">
            {icon} {title}
        </h4>
        <p style="margin: 0; color: var(--body-text-color-subdued, #6b7280); font-size: 0.9rem; line-height: 1.5;">
            {description}
        </p>
    </div>
    """

def get_status_display_html(message, status_type="info"):
    """Get HTML for a status display with Gradio Soft theme compatible styling"""
    # Using softer, more muted colors that match Gradio Soft theme
    colors = {
        "success": "var(--color-green-500, #10b981)",
        "error": "var(--color-red-500, #ef4444)",
        "warning": "var(--color-yellow-500, #f59e0b)",
        "info": "var(--color-blue-500, #3b82f6)"
    }
    icons = {
        "success": "โœ…",
        "error": "โŒ",
        "warning": "โš ๏ธ",
        "info": "โ„น๏ธ"
    }

    color = colors.get(status_type, colors["info"])
    icon = icons.get(status_type, icons["info"])

    return f"""
    <div style="background: var(--background-fill-secondary, #f7f7f7);
                border: 1px solid {color};
                color: var(--body-text-color, #374151);
                padding: 0.75rem;
                border-radius: var(--radius-md, 6px);
                margin: 0.5rem 0;
                border-left: 4px solid {color};">
        <span style="color: {color}; font-weight: 600;">{icon}</span> {message}
    </div>
    """

def create_feature_section(title, description, icon="๐Ÿ”ง"):
    """Create a consistent feature section header matching Gradio Soft theme"""
    gr.Markdown(f"""
    <div style="background: var(--color-accent-soft, #ff6b6b20);
                border: 1px solid var(--color-accent, #ff6b6b);
                color: var(--body-text-color, #374151);
                padding: 1.5rem;
                margin: 1rem 0 0.5rem 0;
                border-radius: var(--radius-lg, 8px);
                box-shadow: var(--shadow-drop, 0 1px 3px rgba(0,0,0,0.1));">
        <h2 style="margin: 0; font-size: 1.5rem; color: var(--body-text-color, #374151); font-weight: 700;">
            {icon} {title}
        </h2>
        <p style="margin: 0.5rem 0 0 0; color: var(--body-text-color-subdued, #6b7280); font-size: 0.95rem; line-height: 1.5;">
            {description}
        </p>
    </div>
    """)

# Create Gradio interface with enhanced UI/UX
def create_gradio_interface():
    # Set a default student ID for the demo
    student_id = "student_12345"

    # Custom CSS for enhanced styling - Gradio Soft theme compatible
    custom_css = """
    .gradio-container {
        max-width: 1400px !important;
        margin: 0 auto !important;
    }

    /* Tab navigation with Gradio Soft theme colors */
    .tab-nav {
        background: var(--color-accent-soft, #ff6b6b20) !important;
        border: 1px solid var(--color-accent, #ff6b6b) !important;
        border-radius: var(--radius-lg, 8px) var(--radius-lg, 8px) 0 0 !important;
    }

    .tab-nav button {
        color: var(--body-text-color, #374151) !important;
        font-weight: 500 !important;
        padding: 12px 20px !important;
        margin: 0 2px !important;
        border-radius: var(--radius-md, 6px) var(--radius-md, 6px) 0 0 !important;
        transition: all 0.3s ease !important;
        background: transparent !important;
    }

    .tab-nav button:hover {
        background: var(--color-accent-soft, #ff6b6b20) !important;
        transform: translateY(-1px) !important;
    }

    .tab-nav button.selected {
        background: var(--background-fill-primary, #ffffff) !important;
        color: var(--body-text-color, #374151) !important;
        box-shadow: var(--shadow-drop, 0 1px 3px rgba(0,0,0,0.1)) !important;
        border-bottom: 2px solid var(--color-accent, #ff6b6b) !important;
    }

    /* Accordion styling */
    .accordion {
        border: 1px solid var(--border-color-primary, #e5e5e5) !important;
        border-radius: var(--radius-lg, 8px) !important;
        margin: 0.5rem 0 !important;
        overflow: hidden !important;
        background: var(--background-fill-primary, #ffffff) !important;
    }

    .accordion summary {
        background: var(--background-fill-secondary, #f7f7f7) !important;
        padding: 1rem !important;
        font-weight: 600 !important;
        cursor: pointer !important;
        border-bottom: 1px solid var(--border-color-primary, #e5e5e5) !important;
        color: var(--body-text-color, #374151) !important;
    }

    .accordion[open] summary {
        border-bottom: 1px solid var(--border-color-primary, #e5e5e5) !important;
    }

    /* Button styling with Gradio theme */
    .button-primary {
        background: var(--color-accent, #ff6b6b) !important;
        border: none !important;
        color: white !important;
        font-weight: 500 !important;
        padding: 10px 20px !important;
        border-radius: var(--radius-md, 6px) !important;
        transition: all 0.3s ease !important;
        box-shadow: var(--shadow-drop, 0 1px 3px rgba(0,0,0,0.1)) !important;
    }

    .button-primary:hover {
        background: var(--color-accent-hover, #ff5252) !important;
        transform: translateY(-1px) !important;
        box-shadow: var(--shadow-drop-lg, 0 4px 6px rgba(0,0,0,0.1)) !important;
    }

    /* Loading spinner */
    .loading-spinner {
        display: inline-block;
        width: 20px;
        height: 20px;
        border: 3px solid var(--border-color-primary, #e5e5e5);
        border-top: 3px solid var(--color-accent, #ff6b6b);
        border-radius: 50%;
        animation: spin 1s linear infinite;
    }

    @keyframes spin {
        0% { transform: rotate(0deg); }
        100% { transform: rotate(360deg); }
    }

    /* Status cards */
    .status-card {
        background: var(--background-fill-primary, #ffffff);
        border: 1px solid var(--border-color-primary, #e5e5e5);
        border-radius: var(--radius-lg, 8px);
        padding: 1rem;
        margin: 0.5rem 0;
        box-shadow: var(--shadow-drop, 0 1px 3px rgba(0,0,0,0.1));
    }

    /* Feature highlights */
    .feature-highlight {
        background: var(--color-accent, #ff6b6b);
        color: white;
        padding: 0.5rem 1rem;
        border-radius: var(--radius-full, 20px);
        font-size: 0.85rem;
        font-weight: 500;
        display: inline-block;
        margin: 0.25rem;
        box-shadow: var(--shadow-drop, 0 1px 3px rgba(0,0,0,0.1));
    }

    /* Custom scrollbar for better theme integration */
    ::-webkit-scrollbar {
        width: 8px;
    }

    ::-webkit-scrollbar-track {
        background: var(--background-fill-secondary, #f7f7f7);
    }

    ::-webkit-scrollbar-thumb {
        background: var(--color-accent-soft, #ff6b6b40);
        border-radius: var(--radius-md, 6px);
    }

    ::-webkit-scrollbar-thumb:hover {
        background: var(--color-accent, #ff6b6b);
    }
    """

    with gr.Blocks(
        title="TutorX Educational AI",
        theme=gr.themes.Soft(),
        css=custom_css
    ) as demo:
        # Start the ping task when the app loads
        demo.load(
            fn=start_ping_task,
            inputs=None,
            outputs=None,
            queue=False
        )

        # Enhanced Header Section with Welcome and Quick Start - Gradio Soft theme
        with gr.Row():
            with gr.Column():
                gr.Markdown("""
                <div style="background: var(--background-fill-primary, #ffffff);
                           border: 2px solid var(--color-accent, #ff6b6b);
                           color: var(--body-text-color, #374151);
                           padding: 2rem;
                           border-radius: var(--radius-xl, 12px);
                           text-align: center;
                           margin-bottom: 1rem;
                           box-shadow: var(--shadow-drop-lg, 0 4px 6px rgba(0,0,0,0.1));">
                    <h1 style="margin: 0 0 1rem 0; font-size: 2.5rem; font-weight: 700; color: var(--body-text-color, #374151);">
                        ๐Ÿง  TutorX Educational AI Platform
                    </h1>
                    <p style="margin: 0 0 1rem 0; font-size: 1.2rem; color: var(--body-text-color-subdued, #6b7280); line-height: 1.5;">
                        An adaptive, multi-modal, and collaborative AI tutoring platform with real-time personalization
                    </p>
                    <div style="display: flex; justify-content: center; gap: 1rem; flex-wrap: wrap; margin-top: 1.5rem;">
                        <span class="feature-highlight">๐ŸŽฏ Adaptive Learning</span>
                        <span class="feature-highlight">๐Ÿค– AI Tutoring</span>
                        <span class="feature-highlight">๐Ÿ“Š Real-time Analytics</span>
                        <span class="feature-highlight">๐ŸŽฎ Interactive Content</span>
                    </div>
                </div>
                """)

        # Quick Start Guide - Gradio Soft theme compatible
        with gr.Accordion("๐Ÿš€ Quick Start Guide - New Users Start Here!", open=True):
            gr.Markdown("""
            <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 1rem; margin: 1rem 0;">
                <div style="background: var(--background-fill-secondary, #f7f7f7);
                           padding: 1.5rem;
                           border-radius: var(--radius-lg, 8px);
                           border-left: 4px solid var(--color-green-500, #10b981);
                           border: 1px solid var(--border-color-primary, #e5e5e5);">
                    <h4 style="color: var(--color-green-500, #10b981); margin: 0 0 0.5rem 0; font-weight: 600;">๐ŸŽฏ Step 1: Explore Concepts</h4>
                    <p style="margin: 0; color: var(--body-text-color-subdued, #6b7280); line-height: 1.5;">Start with the <strong>Core Features</strong> tab to visualize concept relationships and generate your first quiz.</p>
                </div>
                <div style="background: var(--background-fill-secondary, #f7f7f7);
                           padding: 1.5rem;
                           border-radius: var(--radius-lg, 8px);
                           border-left: 4px solid var(--color-blue-500, #3b82f6);
                           border: 1px solid var(--border-color-primary, #e5e5e5);">
                    <h4 style="color: var(--color-blue-500, #3b82f6); margin: 0 0 0.5rem 0; font-weight: 600;">๐Ÿค– Step 2: Try AI Tutoring</h4>
                    <p style="margin: 0; color: var(--body-text-color-subdued, #6b7280); line-height: 1.5;">Visit the <strong>AI Tutoring</strong> tab for personalized, step-by-step learning assistance.</p>
                </div>
                <div style="background: var(--background-fill-secondary, #f7f7f7);
                           padding: 1.5rem;
                           border-radius: var(--radius-lg, 8px);
                           border-left: 4px solid var(--color-purple-500, #8b5cf6);
                           border: 1px solid var(--border-color-primary, #e5e5e5);">
                    <h4 style="color: var(--color-purple-500, #8b5cf6); margin: 0 0 0.5rem 0; font-weight: 600;">๐Ÿง  Step 3: Adaptive Learning</h4>
                    <p style="margin: 0; color: var(--body-text-color-subdued, #6b7280); line-height: 1.5;">Experience the <strong>Adaptive Learning</strong> system that adjusts to your performance in real-time.</p>
                </div>
            </div>
            """)
# Main Tabs with enhanced navigation
        with gr.Tabs():
            # Tab 1: Core Features - Enhanced with better organization
            with gr.Tab("๐ŸŽฏ Core Features", elem_id="core_features_tab"):
                # Feature section header
                create_feature_section(
                    "Concept Graph Visualization",
                    "Explore relationships between educational concepts through interactive graph visualization",
                    "๐Ÿ”"
                )

                # Enhanced concept graph interface with better UX
                with gr.Row():
                    # Left panel - Controls and Information
                    with gr.Column(scale=3):
                        # Input section with enhanced styling
                        with gr.Group():
                            gr.Markdown("### ๐ŸŽฏ Concept Explorer")
                            concept_input = gr.Textbox(
                                label="Enter Concept to Explore",
                                placeholder="e.g., machine_learning, calculus, quantum_physics",
                                value="machine_learning",
                                info="Enter any educational concept to visualize its relationships"
                            )
                            with gr.Row():
                                load_btn = gr.Button("๐Ÿ” Load Graph", variant="primary", scale=2)
                                clear_btn = gr.Button("๐Ÿ—‘๏ธ Clear", variant="secondary", scale=1)

                        # Quick examples for easy access
                        with gr.Group():
                            gr.Markdown("### ๐Ÿ’ก Quick Examples")
                            with gr.Row():
                                example_btns = []
                                examples = ["machine_learning", "calculus", "quantum_physics", "biology"]
                                for example in examples:
                                    btn = gr.Button(example.replace("_", " ").title(), size="sm")
                                    example_btns.append(btn)

                        # Concept details with enhanced presentation
                        with gr.Accordion("๐Ÿ“‹ Concept Details", open=True):
                            concept_details = gr.JSON(
                                label=None,
                                show_label=False
                            )

                        # Related concepts with better formatting
                        with gr.Accordion("๐Ÿ”— Related Concepts & Prerequisites", open=True):
                            related_concepts = gr.Dataframe(
                                headers=["Type", "Name", "Description"],
                                datatype=["str", "str", "str"],
                                interactive=False,
                                wrap=True
                                # height=200
                            )

                    # Right panel - Graph visualization
                    with gr.Column(scale=7):
                        with gr.Group():
                            gr.Markdown("### ๐ŸŒ Interactive Concept Graph")
                            graph_plot = gr.Plot(
                                label=None,
                                show_label=False,
                                container=True
                            )

                            # Graph legend and instructions
                            gr.Markdown("""
                            <div style="background: #f8f9fa; padding: 1rem; border-radius: 6px; margin-top: 0.5rem;">
                                <strong>๐Ÿ“– Graph Legend:</strong><br>
                                ๐Ÿ”ต <span style="color: #4e79a7;">Main Concept</span> |
                                ๐Ÿ”ด <span style="color: #e15759;">Related Concepts</span> |
                                ๐ŸŸข <span style="color: #59a14f;">Prerequisites</span><br>
                                <em>Tip: The graph shows how concepts connect and build upon each other</em>
                            </div>
                            """)

                # Enhanced event handlers with better UX
                def clear_concept_input():
                    return "", None, {"message": "Enter a concept to explore"}, []

                def load_example_concept(example):
                    return example

                # Main load button
                load_btn.click(
                    fn=sync_load_concept_graph,
                    inputs=[concept_input],
                    outputs=[graph_plot, concept_details, related_concepts]
                )

                # Clear button
                clear_btn.click(
                    fn=clear_concept_input,
                    inputs=[],
                    outputs=[concept_input, graph_plot, concept_details, related_concepts]
                )

                # Example buttons
                for i, (btn, example) in enumerate(zip(example_btns, examples)):
                    btn.click(
                        fn=lambda ex=example: load_example_concept(ex),
                        inputs=[],
                        outputs=[concept_input]
                    ).then(
                        fn=sync_load_concept_graph,
                        inputs=[concept_input],
                        outputs=[graph_plot, concept_details, related_concepts]
                    )

                # Load initial graph on startup
                demo.load(
                    fn=lambda: sync_load_concept_graph("machine_learning"),
                    outputs=[graph_plot, concept_details, related_concepts]
                )

                # Enhanced Assessment Generation Section
                create_feature_section(
                    "Assessment Generation",
                    "Create customized quizzes and assessments with immediate feedback and detailed explanations",
                    "๐Ÿ“"
                )

                with gr.Row():
                    # Left panel - Quiz configuration
                    with gr.Column(scale=2):
                        with gr.Group():
                            gr.Markdown("### โš™๏ธ Quiz Configuration")
                            quiz_concept_input = gr.Textbox(
                                label="๐Ÿ“š Concept or Topic",
                                placeholder="e.g., Linear Equations, Photosynthesis, World War II",
                                lines=2,
                                info="Enter the subject matter for your quiz"
                            )

                            with gr.Row():
                                diff_input = gr.Slider(
                                    minimum=1,
                                    maximum=5,
                                    value=3,
                                    step=1,
                                    label="๐ŸŽฏ Difficulty Level",
                                    info="1=Very Easy, 3=Medium, 5=Very Hard"
                                )

                            with gr.Row():
                                gen_quiz_btn = gr.Button("๐ŸŽฒ Generate Quiz", variant="primary", scale=2)
                                preview_btn = gr.Button("๐Ÿ‘๏ธ Preview", variant="secondary", scale=1)

                    # Right panel - Generated quiz display
                    with gr.Column(scale=3):
                        with gr.Group():
                            gr.Markdown("### ๐Ÿ“‹ Generated Quiz")
                            quiz_output = gr.JSON(
                                label=None,
                                show_label=False,
                                container=True
                            )

                            # Quiz statistics - Gradio Soft theme compatible
                            quiz_stats = gr.Markdown("""
                            <div style="background: var(--background-fill-secondary, #f7f7f7);
                                       padding: 1rem;
                                       border-radius: var(--radius-md, 6px);
                                       margin-top: 0.5rem;
                                       border: 1px solid var(--border-color-primary, #e5e5e5);">
                                <strong style="color: var(--body-text-color, #374151);">๐Ÿ“Š Quiz will appear here after generation</strong><br>
                                <em style="color: var(--body-text-color-subdued, #6b7280);">Click "Generate Quiz" to create your assessment</em>
                            </div>
                            """)

                # Enhanced quiz generation with better UX
                def generate_quiz_with_feedback(concept, difficulty):
                    """Generate quiz with user-friendly feedback"""
                    if not concept or not concept.strip():
                        return {
                            "error": "Please enter a concept or topic for the quiz",
                            "status": "error"
                        }

                    # Show loading state
                    result = sync_generate_quiz(concept, difficulty)

                    # Add user-friendly formatting
                    if isinstance(result, dict) and "error" not in result:
                        # Add metadata for better display
                        result["_ui_metadata"] = {
                            "concept": concept,
                            "difficulty": difficulty,
                            "generated_at": "Just now",
                            "status": "success"
                        }

                    return result

                # Connect enhanced quiz generation
                gen_quiz_btn.click(
                    fn=generate_quiz_with_feedback,
                    inputs=[quiz_concept_input, diff_input],
                    outputs=[quiz_output],
                    api_name="generate_quiz"
                )

                # Enhanced Interactive Quiz Section
                create_feature_section(
                    "Interactive Quiz Taking",
                    "Take quizzes with immediate feedback, hints, and detailed explanations for enhanced learning",
                    "๐ŸŽฎ"
                )

                # Quiz workflow with step-by-step guidance
                with gr.Accordion("๐Ÿš€ Step 1: Start Interactive Quiz Session", open=True):
                    with gr.Row():
                        with gr.Column(scale=2):
                            with gr.Group():
                                gr.Markdown("### ๐Ÿ‘ค Student Information")
                                quiz_student_id = gr.Textbox(
                                    label="Student ID",
                                    value=student_id,
                                    info="Your unique identifier for tracking progress"
                                )
                                start_quiz_btn = gr.Button("๐ŸŽฏ Start Interactive Quiz", variant="primary")

                                gr.Markdown(get_info_card_html(
                                    "๐Ÿ“‹ Prerequisites",
                                    "Make sure you have generated a quiz above before starting an interactive session"
                                ))

                        with gr.Column(scale=3):
                            with gr.Group():
                                gr.Markdown("### ๐Ÿ“Š Session Status")
                                quiz_session_output = gr.JSON(
                                    label=None,
                                    show_label=False
                                )

                # Enhanced Quiz Taking Interface
                with gr.Accordion("๐Ÿ“ Step 2: Answer Questions", open=True):
                    with gr.Row():
                        # Left panel - Question and controls
                        with gr.Column(scale=2):
                            with gr.Group():
                                gr.Markdown("### ๐ŸŽฏ Current Question")
                                session_id_input = gr.Textbox(
                                    label="Session ID",
                                    placeholder="Enter session ID from above",
                                    info="Copy the session ID from the status above"
                                )
                                question_id_input = gr.Textbox(
                                    label="Question ID",
                                    placeholder="e.g., q1",
                                    info="Current question identifier"
                                )

                                # Enhanced answer options
                                answer_choice = gr.Radio(
                                    choices=["A) Option A", "B) Option B", "C) Option C", "D) Option D"],
                                    label="๐Ÿ“‹ Select Your Answer",
                                    value=None,
                                    info="Choose the best answer from the options below"
                                )

                                # Action buttons with better organization
                                with gr.Row():
                                    submit_answer_btn = gr.Button("โœ… Submit Answer", variant="primary", scale=2)
                                    get_hint_btn = gr.Button("๐Ÿ’ก Get Hint", variant="secondary", scale=1)

                                with gr.Row():
                                    check_status_btn = gr.Button("๐Ÿ“Š Check Progress", variant="secondary")

                        # Right panel - Feedback and results
                        with gr.Column(scale=3):
                            with gr.Group():
                                gr.Markdown("### ๐Ÿ“‹ Question & Feedback")
                                current_question_display = gr.Markdown("*Start a quiz session to see questions here*")

                            with gr.Accordion("๐Ÿ’ฌ Answer Feedback", open=True):
                                answer_feedback = gr.JSON(
                                    label=None,
                                    show_label=False
                                )

                            with gr.Accordion("๐Ÿ’ก Hints & Help", open=False):
                                hint_output = gr.JSON(
                                    label=None,
                                    show_label=False
                                )

                # Enhanced Quiz Progress and Results
                with gr.Accordion("๐Ÿ“Š Step 3: Track Progress & Results", open=True):
                    with gr.Row():
                        with gr.Column():
                            with gr.Group():
                                gr.Markdown("### ๐Ÿ“ˆ Progress Overview")
                                quiz_stats_display = gr.JSON(
                                    label=None,
                                    show_label=False
                                )

                        with gr.Column():
                            with gr.Group():
                                gr.Markdown("### ๐Ÿ† Performance Summary")
                                performance_summary = gr.Markdown("""
                                <div style="background: var(--background-fill-secondary, #f7f7f7);
                                           padding: 1rem;
                                           border-radius: var(--radius-md, 6px);
                                           text-align: center;
                                           border: 1px solid var(--border-color-primary, #e5e5e5);">
                                    <strong style="color: var(--body-text-color, #374151);">๐Ÿ“Š Complete a quiz to see your performance metrics</strong><br>
                                    <em style="color: var(--body-text-color-subdued, #6b7280);">Accuracy โ€ข Speed โ€ข Learning Progress</em>
                                </div>
                                """)

                # Connect interactive quiz buttons with enhanced functionality
                def start_quiz_with_display(student_id, quiz_data):
                    """Start quiz and update displays"""
                    if not quiz_data or "error" in quiz_data:
                        return {"error": "Please generate a quiz first"}, "*Please generate a quiz first*", gr.Radio(choices=["No options available"], value=None), ""

                    session_result = sync_start_interactive_quiz(quiz_data, student_id)
                    question_display = format_question_display(session_result)
                    answer_options = update_answer_options(session_result)
                    question_id = extract_question_id(session_result)

                    return session_result, question_display, answer_options, question_id

                def submit_answer_with_feedback(session_id, question_id, selected_answer):
                    """Submit answer and update displays"""
                    feedback = sync_submit_quiz_answer(session_id, question_id, selected_answer)

                    # Update question display if there's a next question
                    if "next_question" in feedback:
                        next_q_data = {"question": feedback["next_question"]}
                        question_display = format_question_display(next_q_data)
                        answer_options = update_answer_options(next_q_data)
                        next_question_id = feedback["next_question"].get("question_id", "")
                    else:
                        question_display = "โœ… Quiz completed! Check your final results below."
                        answer_options = gr.Radio(choices=["Quiz completed"], value=None)
                        next_question_id = ""

                    return feedback, question_display, answer_options, next_question_id

                start_quiz_btn.click(
                    fn=start_quiz_with_display,
                    inputs=[quiz_student_id, quiz_output],
                    outputs=[quiz_session_output, current_question_display, answer_choice, question_id_input]
                )

                submit_answer_btn.click(
                    fn=submit_answer_with_feedback,
                    inputs=[session_id_input, question_id_input, answer_choice],
                    outputs=[answer_feedback, current_question_display, answer_choice, question_id_input]
                )

                get_hint_btn.click(
                    fn=sync_get_quiz_hint,
                    inputs=[session_id_input, question_id_input],
                    outputs=[hint_output]
                )

                check_status_btn.click(
                    fn=sync_get_quiz_session_status,
                    inputs=[session_id_input],
                    outputs=[quiz_stats_display]
                )

                # Instructions and Examples
                with gr.Accordion("๐Ÿ“– How to Use Interactive Quizzes", open=False):
                    gr.Markdown("""
                    ### ๐Ÿš€ Quick Start Guide

                    **Step 1: Generate a Quiz**
                    1. Enter a concept (e.g., "Linear Equations", "Photosynthesis")
                    2. Set difficulty level (1-5)
                    3. Click "Generate Quiz"

                    **Step 2: Start Interactive Session**
                    1. Enter your Student ID
                    2. Click "Start Interactive Quiz"
                    3. Copy the Session ID for tracking

                    **Step 3: Answer Questions**
                    1. Read the question displayed
                    2. Select your answer from the options
                    3. Click "Submit Answer" for immediate feedback
                    4. Use "Get Hint" if you need help

                    **Step 4: Track Progress**
                    - Use "Check Status" to see your overall progress
                    - View explanations for each answer
                    - See your final score when completed

                    ### ๐ŸŽฏ Features
                    - **Immediate Feedback**: Get instant results for each answer
                    - **Detailed Explanations**: Understand why answers are correct/incorrect
                    - **Helpful Hints**: Get guidance when you're stuck
                    - **Progress Tracking**: Monitor your performance throughout
                    - **Adaptive Content**: Questions tailored to your difficulty level

                    ### ๐Ÿ’ก Tips
                    - Read questions carefully before selecting answers
                    - Use hints strategically to learn concepts
                    - Review explanations to reinforce learning
                    - Track your progress to identify improvement areas
                    """)

                gr.Markdown("---")
            
            # Tab 2: Advanced Features - Enhanced
            with gr.Tab("๐Ÿ“š Advanced Features", elem_id="advanced_features_tab"):
                create_feature_section(
                    "Lesson Generation",
                    "Create comprehensive lesson plans with structured content and learning objectives",
                    "๐Ÿ“–"
                )

                with gr.Row():
                    with gr.Column():
                        topic_input = gr.Textbox(label="Lesson Topic", value="Solving Quadratic Equations")
                        grade_input = gr.Slider(minimum=1, maximum=12, value=9, step=1, label="Grade Level")
                        duration_input = gr.Slider(minimum=15, maximum=90, value=45, step=5, label="Duration (minutes)")
                        gen_lesson_btn = gr.Button("Generate Lesson Plan")

                    with gr.Column():
                        lesson_output = gr.JSON(label="Lesson Plan")

                # Connect lesson generation button
                gen_lesson_btn.click(
                    fn=sync_generate_lesson,
                    inputs=[topic_input, grade_input, duration_input],
                    outputs=[lesson_output]
                )

                create_feature_section(
                    "Learning Path Generation",
                    "Enhanced with adaptive learning capabilities for personalized educational journeys",
                    "๐Ÿ›ค๏ธ"
                )

                with gr.Row():
                    with gr.Column():
                        lp_student_id = gr.Textbox(label="Student ID", value=student_id)
                        lp_concept_ids = gr.Textbox(label="Concept IDs (comma-separated)", placeholder="e.g., python,functions,oop")
                        lp_student_level = gr.Dropdown(choices=["beginner", "intermediate", "advanced"], value="beginner", label="Student Level")

                        with gr.Row():
                            lp_btn = gr.Button("Generate Basic Path")
                            adaptive_lp_btn = gr.Button("Generate Adaptive Path", variant="primary")

                    with gr.Column():
                        lp_output = gr.JSON(label="Learning Path")

                # Connect learning path generation buttons
                lp_btn.click(
                    fn=sync_generate_learning_path,
                    inputs=[lp_student_id, lp_concept_ids, lp_student_level],
                    outputs=[lp_output]
                )

                adaptive_lp_btn.click(
                    fn=lambda sid, cids, _: sync_get_adaptive_learning_path(sid, cids, "adaptive", 10),
                    inputs=[lp_student_id, lp_concept_ids, lp_student_level],
                    outputs=[lp_output]
                )
        
            # Tab 3: Interactive Tools - Enhanced
            with gr.Tab("๐Ÿ› ๏ธ Interactive Tools", elem_id="interactive_tools_tab"):
                create_feature_section(
                    "Text Interaction & Document Processing",
                    "Ask questions, get explanations, and process documents with AI-powered analysis",
                    "๐Ÿ’ฌ"
                )

                with gr.Row():
                    with gr.Column():
                        text_input = gr.Textbox(label="Ask a Question", value="How do I solve a quadratic equation?")
                        text_btn = gr.Button("Submit")

                    with gr.Column():
                        text_output = gr.JSON(label="Response")

                # Connect text interaction button
                text_btn.click(
                    fn=lambda text: sync_text_interaction(text, student_id),
                    inputs=[text_input],
                    outputs=[text_output]
                )

                # Document OCR (PDF, images, etc.)
                create_feature_section(
                    "Document OCR & LLM Analysis",
                    "Upload and analyze documents with advanced OCR and AI-powered content extraction",
                    "๐Ÿ“„"
                )
                with gr.Row():
                    with gr.Column():
                        doc_input = gr.File(label="Upload PDF or Document", file_types=[".pdf", ".jpg", ".jpeg", ".png"])
                        doc_ocr_btn = gr.Button("Extract Text & Analyze")
                    with gr.Column():
                        doc_output = gr.JSON(label="Document OCR & LLM Analysis")

                # Connect document OCR button
                doc_ocr_btn.click(
                    fn=sync_document_ocr,
                    inputs=[doc_input],
                    outputs=[doc_output]
                )

            # Tab 4: AI Tutoring - Enhanced
            with gr.Tab("๐Ÿค– AI Tutoring", elem_id="ai_tutoring_tab"):
                create_feature_section(
                    "Contextualized AI Tutoring",
                    "Experience personalized AI tutoring with step-by-step guidance and alternative explanations",
                    "๐Ÿค–"
                )

                with gr.Accordion("โ„น๏ธ How AI Tutoring Works", open=False):
                    gr.Markdown("""
                    ### ๐ŸŽฏ Contextualized Learning
                    - **Session Memory**: AI remembers your conversation and adapts responses
                    - **Step-by-Step Guidance**: Break down complex concepts into manageable steps
                    - **Alternative Explanations**: Multiple ways to understand the same concept
                    - **Personalized Feedback**: Responses tailored to your understanding level

                    ### ๐Ÿš€ Getting Started
                    1. Start a tutoring session with your preferred subject
                    2. Ask questions or request explanations
                    3. Get step-by-step guidance for complex topics
                    4. Request alternative explanations if needed
                    5. End session to get a comprehensive summary
                    """)

                # Tutoring Session Management
                with gr.Accordion("๐Ÿ“š Start Tutoring Session", open=True):
                    with gr.Row():
                        with gr.Column():
                            tutor_student_id = gr.Textbox(label="Student ID", value=student_id)
                            tutor_subject = gr.Textbox(label="Subject", value="Mathematics", placeholder="e.g., Mathematics, Physics, Chemistry")
                            tutor_objectives = gr.Textbox(
                                label="Learning Objectives (optional)",
                                placeholder="e.g., Understand quadratic equations, Learn calculus basics",
                                lines=2
                            )
                            start_tutor_btn = gr.Button("Start Tutoring Session", variant="primary")

                        with gr.Column():
                            tutor_session_output = gr.JSON(label="Session Information")

                # AI Chat Interface
                with gr.Accordion("๐Ÿ’ฌ Chat with AI Tutor", open=True):
                    with gr.Row():
                        with gr.Column():
                            chat_session_id = gr.Textbox(label="Session ID", placeholder="Enter session ID from above")
                            chat_query = gr.Textbox(
                                label="Ask Your Question",
                                placeholder="e.g., How do I solve quadratic equations?",
                                lines=3
                            )
                            chat_request_type = gr.Dropdown(
                                choices=["explanation", "step_by_step", "alternative", "practice", "clarification"],
                                value="explanation",
                                label="Request Type"
                            )
                            chat_btn = gr.Button("Ask AI Tutor", variant="primary")

                        with gr.Column():
                            chat_response = gr.JSON(label="AI Tutor Response")

                # Step-by-Step Guidance
                with gr.Accordion("๐Ÿ“‹ Step-by-Step Guidance", open=True):
                    with gr.Row():
                        with gr.Column():
                            step_session_id = gr.Textbox(label="Session ID")
                            step_concept = gr.Textbox(label="Concept", placeholder="e.g., Solving quadratic equations")
                            step_current = gr.Number(label="Current Step", value=1, minimum=1)
                            get_steps_btn = gr.Button("Get Step-by-Step Guidance")

                        with gr.Column():
                            steps_output = gr.JSON(label="Step-by-Step Guidance")

                # Alternative Explanations
                with gr.Accordion("๐Ÿ”„ Alternative Explanations", open=True):
                    with gr.Row():
                        with gr.Column():
                            alt_session_id = gr.Textbox(label="Session ID")
                            alt_concept = gr.Textbox(label="Concept", placeholder="e.g., Photosynthesis")
                            alt_types = gr.CheckboxGroup(
                                choices=["visual", "analogy", "real_world", "simplified", "technical"],
                                value=["visual", "analogy", "real_world"],
                                label="Explanation Types"
                            )
                            get_alt_btn = gr.Button("Get Alternative Explanations")

                        with gr.Column():
                            alt_output = gr.JSON(label="Alternative Explanations")

                # Session Management
                with gr.Accordion("๐Ÿ”š End Session & Summary", open=True):
                    with gr.Row():
                        with gr.Column():
                            end_session_id = gr.Textbox(label="Session ID")
                            session_summary = gr.Textbox(
                                label="Session Summary (optional)",
                                placeholder="What did you learn? Any feedback?",
                                lines=3
                            )
                            end_session_btn = gr.Button("End Session & Get Summary", variant="secondary")

                        with gr.Column():
                            session_end_output = gr.JSON(label="Session Summary")

                # Connect all AI tutoring buttons
                start_tutor_btn.click(
                    fn=lambda sid, subj, obj: sync_start_tutoring_session(sid, subj, obj.split(',') if obj else []),
                    inputs=[tutor_student_id, tutor_subject, tutor_objectives],
                    outputs=[tutor_session_output]
                )

                chat_btn.click(
                    fn=sync_ai_tutor_chat,
                    inputs=[chat_session_id, chat_query, chat_request_type],
                    outputs=[chat_response]
                )

                get_steps_btn.click(
                    fn=sync_get_step_by_step_guidance,
                    inputs=[step_session_id, step_concept, step_current],
                    outputs=[steps_output]
                )

                get_alt_btn.click(
                    fn=sync_get_alternative_explanations,
                    inputs=[alt_session_id, alt_concept, alt_types],
                    outputs=[alt_output]
                )

                end_session_btn.click(
                    fn=sync_end_tutoring_session,
                    inputs=[end_session_id, session_summary],
                    outputs=[session_end_output]
                )

            # Tab 5: Content Generation - Enhanced
            with gr.Tab("๐ŸŽจ Content Generation", elem_id="content_generation_tab"):
                create_feature_section(
                    "Advanced Content Generation",
                    "Generate interactive exercises, scenarios, and gamified content automatically with AI assistance",
                    "๐ŸŽจ"
                )

                # Interactive Exercise Generation
                with gr.Accordion("๐ŸŽฏ Interactive Exercise Generation", open=True):
                    with gr.Row():
                        with gr.Column():
                            ex_concept = gr.Textbox(label="Concept", placeholder="e.g., Photosynthesis, Linear Algebra")
                            ex_type = gr.Dropdown(
                                choices=["problem_solving", "simulation", "case_study", "lab", "project"],
                                value="problem_solving",
                                label="Exercise Type"
                            )
                            ex_difficulty = gr.Slider(minimum=0.1, maximum=1.0, value=0.5, step=0.1, label="Difficulty Level")
                            ex_level = gr.Dropdown(
                                choices=["beginner", "intermediate", "advanced"],
                                value="intermediate",
                                label="Student Level"
                            )
                            gen_exercise_btn = gr.Button("Generate Interactive Exercise", variant="primary")

                        with gr.Column():
                            exercise_output = gr.JSON(label="Generated Exercise")

                # Scenario-Based Learning
                with gr.Accordion("๐ŸŽญ Scenario-Based Learning", open=True):
                    with gr.Row():
                        with gr.Column():
                            scenario_concept = gr.Textbox(label="Concept", placeholder="e.g., Climate Change, Economics")
                            scenario_type = gr.Dropdown(
                                choices=["real_world", "historical", "futuristic", "problem_solving"],
                                value="real_world",
                                label="Scenario Type"
                            )
                            scenario_complexity = gr.Dropdown(
                                choices=["simple", "moderate", "complex"],
                                value="moderate",
                                label="Complexity Level"
                            )
                            gen_scenario_btn = gr.Button("Generate Scenario", variant="primary")

                        with gr.Column():
                            scenario_output = gr.JSON(label="Generated Scenario")

                # Gamified Content
                with gr.Accordion("๐ŸŽฎ Gamified Content Generation", open=True):
                    with gr.Row():
                        with gr.Column():
                            game_concept = gr.Textbox(label="Concept", placeholder="e.g., Fractions, Chemical Reactions")
                            game_type = gr.Dropdown(
                                choices=["quest", "puzzle", "simulation", "competition", "story"],
                                value="quest",
                                label="Game Type"
                            )
                            game_age = gr.Dropdown(
                                choices=["child", "teen", "adult"],
                                value="teen",
                                label="Target Age Group"
                            )
                            gen_game_btn = gr.Button("Generate Gamified Content", variant="primary")

                        with gr.Column():
                            game_output = gr.JSON(label="Generated Game Content")

                # Connect content generation buttons
                gen_exercise_btn.click(
                    fn=sync_generate_interactive_exercise,
                    inputs=[ex_concept, ex_type, ex_difficulty, ex_level],
                    outputs=[exercise_output]
                )

                gen_scenario_btn.click(
                    fn=sync_generate_scenario_based_learning,
                    inputs=[scenario_concept, scenario_type, scenario_complexity],
                    outputs=[scenario_output]
                )

                gen_game_btn.click(
                    fn=sync_generate_gamified_content,
                    inputs=[game_concept, game_type, game_age],
                    outputs=[game_output]
                )

            # Tab 6: Adaptive Learning - Enhanced
            with gr.Tab("๐Ÿง  Adaptive Learning", elem_id="adaptive_learning_tab"):
                create_feature_section(
                    "Adaptive Learning System",
                    "Experience personalized learning with real-time adaptation based on your performance and learning patterns",
                    "๐Ÿง "
                )

                with gr.Accordion("โ„น๏ธ How It Works", open=False):
                    gr.Markdown("""
                    ### ๐ŸŽฏ Real-Time Adaptation
                    - **Performance Tracking**: Monitor accuracy, time spent, and engagement
                    - **Difficulty Adjustment**: Automatically adjust content difficulty based on performance
                    - **Learning Path Optimization**: Personalize learning sequences based on your progress
                    - **Mastery Detection**: Multi-indicator assessment of concept understanding

                    ### ๐Ÿ“Š Analytics & Insights
                    - **Learning Patterns**: Detect your learning style and preferences
                    - **Progress Monitoring**: Track milestones and achievements
                    - **Predictive Recommendations**: Suggest next best concepts to learn

                    ### ๐Ÿš€ Getting Started
                    1. Start an adaptive session with a concept you want to learn
                    2. Record your learning events (answers, time taken, etc.)
                    3. Get real-time recommendations for difficulty adjustments
                    4. View your progress and mastery assessments
                    """)

                # Adaptive Learning Session Management
                with gr.Accordion("๐Ÿ“š Learning Session Management", open=True):
                    with gr.Row():
                        with gr.Column():
                            session_student_id = gr.Textbox(label="Student ID", value=student_id)
                            session_concept_id = gr.Textbox(label="Concept ID", value="algebra_linear_equations")
                            session_difficulty = gr.Slider(minimum=0.1, maximum=1.0, value=0.5, step=0.1, label="Initial Difficulty")
                            start_session_btn = gr.Button("Start Adaptive Session", variant="primary")

                        with gr.Column():
                            session_output = gr.JSON(label="Session Status")

                    # Record Learning Events
                    with gr.Row():
                        with gr.Column():
                            event_session_id = gr.Textbox(label="Session ID", placeholder="Enter session ID from above")
                            event_type = gr.Dropdown(
                                choices=["answer_submitted", "hint_used", "session_pause", "session_resume"],
                                value="answer_submitted",
                                label="Event Type"
                            )
                            event_correct = gr.Checkbox(label="Answer Correct", value=True)
                            event_time = gr.Number(label="Time Taken (seconds)", value=30)
                            record_event_btn = gr.Button("Record Event")

                        with gr.Column():
                            event_output = gr.JSON(label="Event Response")

                # Learning Path Optimization
                with gr.Accordion("๐Ÿ›ค๏ธ Learning Path Optimization", open=True):
                    with gr.Row():
                        with gr.Column():
                            opt_student_id = gr.Textbox(label="Student ID", value=student_id)
                            opt_concepts = gr.Textbox(
                                label="Target Concepts (comma-separated)",
                                value="algebra_basics,linear_equations,quadratic_equations"
                            )
                            opt_strategy = gr.Dropdown(
                                choices=["mastery_focused", "breadth_first", "depth_first", "adaptive", "remediation"],
                                value="adaptive",
                                label="Optimization Strategy"
                            )
                            opt_max_concepts = gr.Slider(minimum=3, maximum=15, value=8, step=1, label="Max Concepts")
                            optimize_path_btn = gr.Button("Optimize Learning Path", variant="primary")

                        with gr.Column():
                            optimization_output = gr.JSON(label="Optimized Learning Path")

                # Mastery Assessment
                with gr.Accordion("๐ŸŽ“ Mastery Assessment", open=True):
                    with gr.Row():
                        with gr.Column():
                            mastery_student_id = gr.Textbox(label="Student ID", value=student_id)
                            mastery_concept_id = gr.Textbox(label="Concept ID", value="algebra_linear_equations")
                            assess_mastery_btn = gr.Button("Assess Mastery", variant="primary")

                        with gr.Column():
                            mastery_output = gr.JSON(label="Mastery Assessment")

                # Learning Analytics
                with gr.Accordion("๐Ÿ“Š Learning Analytics & Progress", open=True):
                    with gr.Row():
                        with gr.Column():
                            analytics_student_id = gr.Textbox(label="Student ID", value=student_id)
                            analytics_days = gr.Slider(minimum=7, maximum=90, value=30, step=7, label="Analysis Period (days)")
                            get_analytics_btn = gr.Button("Get Learning Analytics")
                            get_progress_btn = gr.Button("Get Progress Summary")

                        with gr.Column():
                            analytics_output = gr.JSON(label="Learning Analytics")
                            progress_output = gr.JSON(label="Progress Summary")

                # Connect all the buttons
                start_session_btn.click(
                    fn=sync_start_adaptive_session,
                    inputs=[session_student_id, session_concept_id, session_difficulty],
                    outputs=[session_output]
                )

                record_event_btn.click(
                    fn=sync_record_learning_event,
                    inputs=[session_student_id, session_concept_id, event_type, event_session_id, event_correct, event_time],
                    outputs=[event_output]
                )

                optimize_path_btn.click(
                    fn=sync_get_adaptive_learning_path,
                    inputs=[opt_student_id, opt_concepts, opt_strategy, opt_max_concepts],
                    outputs=[optimization_output]
                )

                assess_mastery_btn.click(
                    fn=lambda sid, cid: sync_get_adaptive_recommendations(sid, cid),
                    inputs=[mastery_student_id, mastery_concept_id],
                    outputs=[mastery_output]
                )

                get_analytics_btn.click(
                    fn=lambda sid, days: sync_get_progress_summary(sid, days),
                    inputs=[analytics_student_id, analytics_days],
                    outputs=[analytics_output]
                )

                get_progress_btn.click(
                    fn=lambda sid: sync_get_progress_summary(sid, 7),
                    inputs=[analytics_student_id],
                    outputs=[progress_output]
                )

                # Examples and Tips
                with gr.Accordion("๐Ÿ’ก Examples & Tips", open=False):
                    gr.Markdown("""
                    ### ๐Ÿ“ Example Workflow

                    **1. Start a Session:**
                    - Student ID: `student_001`
                    - Concept: `algebra_linear_equations`
                    - Difficulty: `0.5` (medium)

                    **2. Record Events:**
                    - Answer submitted: correct=True, time=30s
                    - Hint used: correct=False, time=45s

                    **3. Get Recommendations:**
                    - System suggests difficulty adjustments
                    - Provides next concept suggestions

                    **4. Optimize Learning Path:**
                    - Target concepts: `algebra_basics,linear_equations,quadratic_equations`
                    - Strategy: `adaptive` (recommended)

                    ### ๐ŸŽฏ Optimization Strategies
                    - **Mastery Focused**: Deep understanding before moving on
                    - **Breadth First**: Cover many concepts quickly
                    - **Depth First**: Thorough exploration of fewer concepts
                    - **Adaptive**: System chooses best strategy for you
                    - **Remediation**: Focus on filling knowledge gaps

                    ### ๐Ÿ“Š Understanding Analytics
                    - **Learning Patterns**: Identifies your learning style
                    - **Performance Trends**: Shows improvement over time
                    - **Mastery Levels**: Tracks concept understanding
                    - **Engagement Metrics**: Measures learning engagement
                    """)

            # Tab 7: Data Analytics - Enhanced
            with gr.Tab("๐Ÿ“Š Analytics", elem_id="data_analytics_tab"):
                create_feature_section(
                    "Plagiarism Detection & Analytics",
                    "Advanced plagiarism detection with detailed similarity analysis and originality reporting",
                    "๐Ÿ“Š"
                )

                with gr.Row():
                    with gr.Column():
                        submission_input = gr.Textbox(
                            label="Student Submission",
                            lines=5,
                            value="The quadratic formula states that if axยฒ + bx + c = 0, then x = (-b ยฑ โˆš(bยฒ - 4ac)) / 2a."
                        )
                        reference_input = gr.Textbox(
                            label="Reference Source",
                            lines=5,
                            value="According to the quadratic formula, for any equation in the form axยฒ + bx + c = 0, the solutions are x = (-b ยฑ โˆš(bยฒ - 4ac)) / 2a."
                        )
                        plagiarism_btn = gr.Button("Check Originality")

                    with gr.Column():
                        with gr.Group():
                            gr.Markdown("### ๐Ÿ” Originality Report")
                            plagiarism_output = gr.JSON(label="", show_label=False, container=False)

                # Connect the button to the plagiarism check function
                plagiarism_btn.click(
                    fn=sync_check_plagiarism,
                    inputs=[submission_input, reference_input],
                    outputs=[plagiarism_output]
                )
            
            # Enhanced Footer Section - Gradio Soft theme compatible
            gr.Markdown("""
            <div style="background: var(--background-fill-secondary, #f7f7f7);
                       padding: 2rem;
                       margin-top: 2rem;
                       border-radius: var(--radius-xl, 12px);
                       border-top: 3px solid var(--color-accent, #ff6b6b);
                       border: 1px solid var(--border-color-primary, #e5e5e5);">
                <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 2rem;">
                    <div>
                        <h3 style="color: var(--body-text-color, #374151); margin: 0 0 1rem 0; font-weight: 600;">๐Ÿง  About TutorX</h3>
                        <p style="color: var(--body-text-color-subdued, #6b7280); margin: 0; line-height: 1.6;">
                            TutorX is an AI-powered educational platform that provides adaptive learning,
                            interactive assessments, and personalized tutoring to enhance the learning experience.
                        </p>
                    </div>
                    <div>
                        <h3 style="color: var(--body-text-color, #374151); margin: 0 0 1rem 0; font-weight: 600;">๐Ÿ”— Quick Links</h3>
                        <div style="color: var(--body-text-color-subdued, #6b7280);">
                            <p style="margin: 0.5rem 0;">๐Ÿ“– <a href="https://github.com/Meetpatel006/TutorX/blob/main/README.md" target="_blank" style="color: var(--color-accent, #ff6b6b); text-decoration: none;">Documentation</a></p>
                            <p style="margin: 0.5rem 0;">๐Ÿ’ป <a href="https://github.com/Meetpatel006/TutorX" target="_blank" style="color: var(--color-accent, #ff6b6b); text-decoration: none;">GitHub Repository</a></p>
                            <p style="margin: 0.5rem 0;">๐Ÿ› <a href="https://github.com/Meetpatel006/TutorX/issues" target="_blank" style="color: var(--color-accent, #ff6b6b); text-decoration: none;">Report an Issue</a></p>
                        </div>
                    </div>
                    <div>
                        <h3 style="color: var(--body-text-color, #374151); margin: 0 0 1rem 0; font-weight: 600;">โœจ Key Features</h3>
                        <div style="color: var(--body-text-color-subdued, #6b7280); font-size: 0.9rem;">
                            <p style="margin: 0.25rem 0;">๐ŸŽฏ Adaptive Learning Paths</p>
                            <p style="margin: 0.25rem 0;">๐Ÿค– AI-Powered Tutoring</p>
                            <p style="margin: 0.25rem 0;">๐Ÿ“Š Real-time Analytics</p>
                            <p style="margin: 0.25rem 0;">๐ŸŽฎ Interactive Assessments</p>
                        </div>
                    </div>
                </div>
                <div style="text-align: center; margin-top: 2rem; padding-top: 1rem; border-top: 1px solid var(--border-color-primary, #e5e5e5);">
                    <p style="color: var(--body-text-color-subdued, #6b7280); margin: 0; font-size: 0.9rem;">
                        ยฉ 2025 TutorX Educational AI Platform - Empowering Learning Through Technology
                    </p>
                </div>
            </div>
            """)
        
        return demo

# Launch the interface
if __name__ == "__main__":
    demo = create_gradio_interface()
    demo.queue().launch(server_name="0.0.0.0", server_port=7860)