File size: 164,008 Bytes
5ccefae
 
 
 
 
 
 
e90ad51
5ccefae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19c413d
 
 
5ccefae
 
 
 
 
 
19c413d
 
 
 
 
 
 
 
 
 
5ccefae
 
 
 
 
 
 
 
b8b9061
5ccefae
 
 
b8b9061
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ccefae
b8b9061
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ccefae
b8b9061
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ccefae
b8b9061
 
5ccefae
b8b9061
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ccefae
b8b9061
 
5ccefae
b8b9061
 
 
 
 
 
 
 
 
5ccefae
b8b9061
 
5ccefae
b8b9061
 
 
 
 
 
 
5ccefae
b8b9061
 
 
5ccefae
b8b9061
 
 
 
 
 
5ccefae
b8b9061
5ccefae
b8b9061
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ccefae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8b9061
 
 
5ccefae
b8b9061
 
5ccefae
b8b9061
 
5ccefae
b8b9061
 
 
 
 
5ccefae
b8b9061
 
5ccefae
b8b9061
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ccefae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2260ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ccefae
b2260ca
 
 
 
a533f17
 
b2260ca
 
8d079d1
5ccefae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3343b22
5ccefae
 
8ba8abb
 
3343b22
5ccefae
8ba8abb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ccefae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8b9061
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0830ce1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40aab9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0830ce1
40aab9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0830ce1
40aab9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0830ce1
40aab9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0830ce1
40aab9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0830ce1
 
 
40aab9f
 
0830ce1
 
 
 
40aab9f
 
 
0830ce1
 
40aab9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0830ce1
660b778
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ccefae
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
"""
Insight Engine - FastAPI Backend
Handles API proxying, dataset operations, caching, and static file serving.
"""
from fastapi import FastAPI, HTTPException, Query, Body
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse, StreamingResponse, HTMLResponse
import requests
import aiohttp
import asyncio
import json
import os
import time
import hashlib
from typing import List, Dict, Optional, Any
from dataclasses import dataclass, asdict
from datetime import datetime, timedelta
import pandas as pd
import numpy as np

app = FastAPI(title="Insight Engine API", version="5.0")

# CORS - allow all origins for now (HF Spaces handles auth)
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_methods=["*"],
    allow_headers=["*"],
)

# In-memory cache with TTL
_cache: Dict[str, tuple] = {}  # key -> (value, expires_at)
CACHE_TTL = 3600  # 1 hour

def _cache_get(key: str) -> Any:
    if key in _cache:
        value, expires = _cache[key]
        if time.time() < expires:
            return value
        del _cache[key]
    return None

def _cache_set(key: str, value: Any, ttl: int = CACHE_TTL):
    _cache[key] = (value, time.time() + ttl)

# --- DATASET STORAGE ---
# Users can upload datasets via POST /api/dataset/upload
# Each dataset gets a unique ID and is stored in memory
_loaded_datasets: Dict[str, Dict[str, Any]] = {}

def _make_id() -> str:
    return hashlib.sha256(str(time.time()).encode()).hexdigest()[:12]

# ============================================================================
# API ROUTES (must be defined BEFORE the catch-all root route)
# ============================================================================

# Root health endpoint
@app.get("/api/health")
def health():
    return {"status": "ok", "datasets_loaded": len(_loaded_datasets), "cache_entries": len(_cache)}

# Static files β€” served at /static/ for JS/CSS
app.mount("/static", StaticFiles(directory="static"), name="static")

# SPA catch-all: serve index.html for all non-API paths
# HF Spaces proxy may send POST to / for the initial page load
from fastapi import Request
@app.api_route("/", methods=["GET", "POST"], response_class=HTMLResponse)
def serve_index(request: Request):
    with open("static/index.html", "r") as f:
        return f.read()

# ============================================================================
# WORLD BANK API
# ============================================================================

WORLD_BANK_BASE = "https://api.worldbank.org/v2"

WORLDBANK_INDICATORS = {
    # --- ECONOMY ---
    "gdp_current": "NY.GDP.MKTP.CD",
    "gdp_per_capita": "NY.GDP.PCAP.CD",
    "gdp_growth": "NY.GDP.MKTP.KD.ZG",
    "gdp_ppp": "NY.GDP.MKTP.PP.CD",
    "gdp_ppp_per_capita": "NY.GDP.PCAP.PP.CD",
    "inflation": "FP.CPI.TOTL.ZG",
    "exports": "NE.EXP.GNFS.CD",
    "imports": "NE.IMP.GNFS.CD",
    "trade_balance": "NE.TRD.GNFS.ZS",
    "fdi_inflows": "BX.KLT.DINV.WD.GD.ZS",
    "fdi_net_inflows": "BX.KLT.DINV.CD.WD",
    "industry_value_added": "NV.IND.TOTL.ZS",
    "services_value_added": "NV.SRV.TOTL.ZS",
    "agriculture_value_added": "NV.AGR.TOTL.ZS",
    "manufacturing_value_added": "NV.IND.MANF.ZS",
    "gov_debt": "GC.DOD.TOTL.GD.ZS",
    "gov_expense": "GC.XPN.TOTL.GD.ZS",
    "gov_revenue": "GC.REV.XGRT.GD.ZS",
    "tax_revenue": "GC.TAX.TOTL.GD.ZS",
    "central_gov_debt": "GC.DOD.TOTL.CN",
    "broad_money_growth": "FM.LBC.GDP.ZS",
    "consumer_price_index": "FP.CPI.TOTL",
    "exchange_rate": "PA.NUS.PPP",
    "business_extent": "IC.BUS.EASE.XQ",
    "private_sector_credit": "GFDD.DI.14",
    "stock_market_capitalization": "CM.MKT.LCAP.GD.ZS",
    "total_reserves": "FI.RES.TOTL.CD",
    "external_debt_total": "DT.DOD.DECT.CD",
    "debt_service": "DT.TDS.DECT.CD",
    "gni_per_capita": "NY.GNP.PCAP.CD",
    "gni_current": "NY.GNP.MKTP.CD",
    "patent_applications": "IP.PAT.RESD",
    "research_spending": "GB.XPD.RSDV.GD.ZS",
    "high_tech_exports": "TX.VAL.TECH.CD",
    "transport_infrastructure_quality": "IQ.WEF.INFR.XQ",
    "logistics_performance": "LP.LPI.OVRL.XQ",
    "port_infrastructure": "IQ.WEF.PORT.XQ",
    # --- SOCIAL / DEMOGRAPHICS ---
    "population": "SP.POP.TOTL",
    "population_density": "EN.POP.DNST",
    "population_growth": "SP.POP.GROW",
    "urban_population": "SP.URB.TOTL.IN.ZS",
    "rural_population": "SP.RUR.TOTL.ZS",
    "population_0_14": "SP.POP.0014.TO.ZS",
    "population_15_64": "SP.POP.1564.TO.ZS",
    "population_65_plus": "SP.POP.65UP.TO.ZS",
    "dependency_ratio": "SP.POP.DPND",
    "fertility_rate": "SP.DYN.TFRT.IN",
    "birth_rate": "SP.DYN.CBRT.IN",
    "death_rate": "SP.DYN.CDRT.IN",
    "migration_net": "SM.POP.NETM",
    "refugee_population": "SM.POP.REFG",
    "internally_displaced": "VC.IDP.TOTL.OL",
    "gini": "SI.POV.GINI",
    "poverty_headcount": "SI.POV.DDAY",
    "poverty_gap": "SI.POV.NAHC",
    "income_share_lowest_20": "SI.DST.FRST.20",
    "income_share_highest_10": "SI.DST.10TH.10",
    "income_share_highest_20": "SI.DST.05TH.20",
    "multidimensional_poverty": "SI.POV.MDIM",
    "vulnerable_employment": "SL.EMP.VULN.ZS",
    "self_employed": "SL.EMP.SELF.ZS",
    "unemployment": "SL.UEM.TOTL.ZS",
    "unemployment_youth": "SL.UEM.TOTL.ZS",
    "unemployment_female": "SL.UEM.TOTL.FE.ZS",
    "labor_force_participation": "SL.TLF.CACT.ZS",
    "labor_force_female": "SL.TLF.CACT.FE.ZS",
    "wage_and_salaried": "SL.EMP.WORK.ZS",
    "child_labor": "SL.CHLD.WORK.ZS",
    "time_spent_unpaid_work_female": "SG.TIM.UWRK.FE",
    # --- HEALTH ---
    "life_expectancy": "SP.DYN.LE00.IN",
    "life_expectancy_female": "SP.DYN.LE00.FE.IN",
    "life_expectancy_male": "SP.DYN.LE00.MA.IN",
    "health_exp_per_capita": "SH.XPD.CHEX.PC.CD",
    "health_exp_gdp": "SH.XPD.CHEX.GD.ZS",
    "health_exp_gov": "SH.XPD.GHED.GD.ZS",
    "health_exp_private": "SH.XPD.PCAP.GD.ZS",
    "physicians_per_1000": "SH.MED.PHYS.ZS",
    "nurses_per_1000": "SH.MED.NURS.ZS",
    "hospital_beds_per_1000": "SH.MED.BEDS.ZS",
    "immunization_dpt": "SH.IMM.IBCG",
    "immunization_measles": "SH.IMM.MEAS",
    "infant_mortality": "SP.DYN.IMRT.IN",
    "under5_mortality": "SH.DYN.MORT",
    "maternal_mortality": "SH.STA.MMRT",
    "neonatal_mortality": "SH.DYN.NMRT",
    "adolescent_fertility": "SP.ADO.TFRT",
    "contraceptive_prevalence": "SP.DYN.CONM.ZS",
    "births_attended": "SH.STA.BRTC.ZS",
    "sanitation": "SH.STA.SMSS.ZS",
    "drinking_water": "SH.H2O.SMDW.ZS",
    "handwashing": "SH.STA.HYGN.ZS",
    "stunting": "SH.STA.STNT.ME.ZS",
    "wasting": "SH.STA.WAST.ZS",
    "overweight": "SH.STA.OWGH.ZS",
    "undernourishment": "SN.ITK.DEFC.ZS",
    "diabetes_prevalence": "SH.STA.DIAB.ZS",
    "tuberculosis_incidence": "SH.TBS.INCD",
    "malaria_incidence": "SH.MLR.INCD.P3",
    "hiv_prevalence": "SH.DYN.AIDS.ZS",
    "alcohol_consumption": "SH.ALC.PCAP.LI",
    "smoking_prevalence": "SH.PRV.SMOK",
    "suicide_mortality": "SH.STA.SUIC.P5",
    "road_traffic_mortality": "SH.STA.TRAF.P5",
    "air_pollution_deaths": "SH.STA.AIRP.P5",
    "ambient_pm25": "EN.ATM.PM25.MC.M3",
    "household_air_pollution": "EN.ATM.HOUS.ZS",
    "daly": "SH.DTH.COMM.ZS",
    "cause_of_death_communicable": "SH.DTH.COMM.ZS",
    "cause_of_death_noncommunicable": "SH.DTH.NCOM.ZS",
    "cause_of_death_injury": "SH.DTH.INJR.ZS",
    "raised_blood_pressure": "SH.STA.HYPB.ZS",
    "raised_blood_glucose": "SH.STA.DIAB.ZS",
    "overweight_adults": "SH.STA.OWAD.ZS",
    # --- EDUCATION ---
    "primary_enrollment": "SE.PRM.ENRR",
    "primary_completion": "SE.PRM.CMPT.ZS",
    "secondary_enrollment": "SE.SEC.ENRR",
    "tertiary_enrollment": "SE.TER.ENRR",
    "literacy_rate": "SE.ADT.LITR.ZS",
    "literacy_rate_youth": "SE.ADT.1524.LT.ZS",
    "literacy_rate_female": "SE.ADT.LITR.FE.ZS",
    "mean_years_schooling": "BAR.SCHL.15UP",
    "expected_years_schooling": "SE.SCH.LIFE",
    "pupil_teacher_ratio_primary": "SE.PRM.ENRL.TC.ZS",
    "school_enrollment_preprimary": "SE.PRE.ENRR",
    "out_of_school_primary": "SE.PRM.UNER",
    "out_of_school_secondary": "SE.SEC.UNER.LO",
    "gov_education_exp": "SE.XPD.TOTL.GD.ZS",
    "scholarship_travel": "DT.ODA.ATCD.KSCH",
    "trained_teachers": "SE.PRM.TCAQ.ZS",
    "children_out_of_school": "SE.PRM.UNER.ZS",
    "youth_literacy": "SE.ADT.1524.LT.ZS",
    "gov_education_exp_primary": "SE.XPD.PRIM.PC.ZS",
    # --- INFRASTRUCTURE / TECHNOLOGY ---
    "electricity_access": "EG.ELC.ACCS.ZS",
    "electricity_access_rural": "EG.ELC.ACCS.RU.ZS",
    "electricity_access_urban": "EG.ELC.ACCS.UR.ZS",
    "internet_users": "IT.NET.USER.ZS",
    "fixed_broadband": "IT.NET.BBND.P2",
    "mobile_subscriptions": "IT.CEL.SETS.P2",
    "secure_internet_servers": "IT.NET.SECR.P6",
    "individuals_using_internet": "IT.NET.USER.ZS",
    "researchers_per_million": "SP.POP.SCIE.RD.P6",
    "technicians_in_rd": "SP.POP.TECH.RD.P6",
    "rd_expenditure": "GB.XPD.RSDV.GD.ZS",
    "scientific_journal_articles": "IP.JRN.ARTC.SC",
    # --- ENVIRONMENT ---
    "co2_per_capita": "EN.ATM.CO2E.PC",
    "co2_total": "EN.ATM.CO2E.KT",
    "co2_intensity": "EN.ATM.CO2E.PP.GD",
    "renewable_energy": "EG.FEC.RNEW.ZS",
    "fossil_fuel_energy": "EG.USE.COMM.FO.ZS",
    "nuclear_energy": "EG.EGY.NUCL.ZS",
    "electricity_from_renewables": "EG.ELC.RNEW.ZS",
    "electricity_from_fossil": "EG.ELC.FOSL.ZS",
    "electricity_from_nuclear": "EG.ELC.NUCL.ZS",
    "energy_use_per_capita": "EG.USE.PCAP.KG.OE",
    "energy_intensity": "EG.EGY.PRIM.PP.KD",
    "forest_cover": "AG.LND.FRST.ZS",
    "forest_area": "AG.LND.FRST.K2",
    "terrestrial_protected_areas": "ER.PTD.TOTL.ZS",
    "marine_protected_areas": "ER.MRN.PTMN.ZS",
    "freshwater_withdrawal": "ER.H2O.FWTL.ZS",
    "renewable_freshwater_per_capita": "ER.H2O.FWTL.ZS",
    "water_productivity": "ER.H2O.FWTL.KD.M3",
    "agricultural_land": "AG.LND.AGRI.ZS",
    "arable_land": "AG.LND.ARBL.ZS",
    "cereal_yield": "AG.YLD.CREL.KG",
    "fertilizer_consumption": "AG.CON.FERT.ZS",
    "methane_emissions": "EN.ATM.METH.AG.KT.CE",
    "nitrous_oxide_emissions": "EN.ATM.NOXE.AG.KT.CE",
    "nox_emissions": "EN.ATM.NOXE.AG.KT.CE",
    "ghg_emissions_total": "EN.ATM.GHGT.KT.CE",
    "pm25_exposure": "EN.ATM.PM25.MC.M3",
    "co2_emissions_transport": "EN.CO2.TRAN.ZS",
    "co2_emissions_manufacturing": "EN.CO2.MANF.ZS",
    "co2_emissions_electricity": "EN.CO2.ETOT.ZS",
    "co2_emissions_residential": "EN.CO2.BLDG.ZS",
    "biodiversity": "EN.BIR.THRD.NO",
    "threatened_species": "EN.BIR.THRD.NO",
    "threatened_mammals": "EN.MAM.THRD.NO",
    "threatened_fish": "EN.FSH.THRD.NO",
    "threatened_plants": "EN.HPT.THRD.NO",
    "natural_resource_rents": "NY.GDP.TOTL.RT.ZS",
    "mineral_rents": "NY.GDP.MINL.RT.ZS",
    "oil_rents": "NY.GDP.PETR.RT.ZS",
    "gas_rents": "NY.GDP.NGAS.RT.ZS",
    "coal_rents": "NY.GDP.COAL.RT.ZS",
    "forest_rents": "NY.GDP.FRST.RT.ZS",
    "total_greenhouse_gas": "EN.ATM.GHGT.KT.CE",
    "adjustedsavings_co2": "NY.ADJ.DCO2.GN.ZS",
    "adjustedsavings_particulate": "NY.ADJ.DPEM.GN.ZS",
    "waste_collection": "EN.CLC.WSTC.CD",
    "material_footprint": "EN.MWF.MTRC.MT.CD",
    "consumption_footprint": "EN.CLC.MDPT.ZS",
    "ocean_acidification": "EN.ATM.CO2E.PC",
    "adaptation_fund": "EN.CLC.ADPT.XD",
    # --- GOVERNANCE / INSTITUTIONS ---
    "voice_accountability": "RV.ACCES.RANK.XD",
    "political_stability": "RV.POLI.RANK.XD",
    "government_effectiveness": "RV.GEFF.RANK.XD",
    "regulatory_quality": "RV.REGR.RANK.XD",
    "rule_of_law": "RV.RULE.RANK.XD",
    "control_of_corruption": "RV.CORR.RANK.XD",
    "battle_deaths": "VC.BTL.DETH",
    "intentional_homicides": "VC.IHR.PSRC.P5",
    "detention_rate": "IC.PRP.DETN.PS",
    "prison_population": "IC.PRP.TOTL.P2",
    "military_expenditure": "MS.MIL.XPND.GD.ZS",
    "armed_forces_personnel": "MS.MIL.TOTL.P1",
    "arms_exports": "MS.MIL.XPRT.KD",
    "arms_imports": "MS.MIL.MPRT.KD",
    "conflict_affected": "VC.IDP.TOTL.OL",
    "terrorism_incidents": "VC.IHR.PSRC.P5",
    "procedures_to_start_business": "IC.REG.DURS",
    "time_to_start_business": "IC.REG.COST.PC.FE.ZS",
    "cost_to_start_business": "IC.REG.COST.PC.FE.ZS",
    "tax_payments": "IC.TAX.PAYM",
    "time_to_pay_taxes": "IC.TAX.DURS",
    "total_tax_rate": "IC.TAX.TOTL.CP.ZS",
    "property_registering_procedures": "IC.PRP.PRO",
    "time_to_register_property": "IC.PRP.DURS",
    "building_permit_procedures": "IC.CON.PROC",
    "time_to_get_electricity": "IC.ELC.TIME",
    "getting_credit_rank": "IC.CRD.PUBL.ZS",
    "protecting_minority_investors": "IC.INV.PROT",
    "resolving_insolvency": "IC.ISV.RECD",
    "contract_enforcement": "IC.LGL.DURS",
    "women_business_owners": "IC.WOB.OWN.ZS",
    "female_top_managers": "IC.WOB.MGT.ZS",
    "women_parliament_seats": "SG.GEN.PARL.ZS",
    "women_minister_positions": "SG.GEN.MNST.ZS",
    "gender_inequality_index": "SE.GPI.IALL",
    "gender_wage_gap": "SL.EMP.GAP.WAGE.ZS",
    "legal_framework_gender": "SG.LAW.FRWK.WE",
    "assets_ownership_gender": "SG.OWN.LAND.FE.ZS",
    # --- AID / DEVELOPMENT ---
    "net_oda_received": "DT.ODA.ODAT.CD",
    "oda_gni": "DT.ODA.ODAT.GN.ZS",
    "oda_per_capita": "DT.ODA.ODAT.PC.ZD",
    "oda_given": "DAC.TOTL.ALLEF",
    "remittances_received": "BX.TRF.PWKR.CD.DT",
    "remittances_gdp": "BX.TRF.PWKR.DT.GD.ZS",
    "net_migration": "SM.POP.NETM",
    "net_bilateral_aid": "DC.DAC.AIDP.CD",
    "oda_education": "SE.XPD.TOTL.GD.ZS",
    "oda_health": "SH.XPD.CHEX.GD.ZS",
    "oda_infrastructure": "DT.ODA.ALLD.CD",
    "debt_relief": "DT.DOD.DLXF.CD",
    # --- GENDER ---
    "gender_gap_economic": "IQ.CPA.GNDR.XQ",
    "gender_gap_education": "IQ.CPA.GNDR.XQ",
    "gender_gap_health": "IQ.CPA.GNDR.XQ",
    "gender_gap_political": "IQ.CPA.GNDR.XQ",
    "firms_with_female_top_manager": "IC.FRM.FEMM.ZS",
    "firms_with_female_ownership": "IC.FRM.FEMO.ZS",
    "female_labor_force": "SL.TLF.TOTL.FE.ZS",
    "female_wage_salary": "SL.EMP.WORK.FE.ZS",
    "female_vulnerable_employment": "SL.EMP.VULN.FE.ZS",
    "youth_not_in_edu_employment": "SL.UEM.NEET.ZS",
    "youth_not_in_edu_employment_female": "SL.UEM.NEET.FE.ZS",
    # --- DIGITAL ---
    "digital_government_index": "EG.DIG.GOVT",
    "online_services_index": "EG.OVI.SCOR.XQ",
    "e_participation_index": "EG.OVI.ACCS.XQ",
    "telecommunication_infrastructure": "IT.NET.SECR.P6",
    "ict_goods_exports": "TX.VAL.ICTG.ZS.UN",
    "ict_goods_imports": "TM.VAL.ICTG.ZS.UN",
    "ict_services_exports": "TX.VAL.ICTS.ZS.UN",
    "ict_services_imports": "TM.VAL.ICTS.ZS.UN",
    "computer_communication_services": "BX.GSR.CMCP.ZS",
    # --- TOURISM ---
    "international_tourist_arrivals": "ST.INT.ARVL",
    "international_tourism_receipts": "ST.INT.RCPT.CD",
    "international_tourism_expenditures": "ST.INT.XPND.CD",
    "tourism_receipts_percent_exports": "ST.INT.RCPT.XP.ZS",
    "tourism_receipts_percent_gdp": "ST.INT.RCPT.GD.ZS",
}

COUNTRY_NAME_MAP = {
    "United States of America": "United States",
    "Russian Federation": "Russia",
    "Korea, Rep.": "South Korea",
    "Korea, Dem. People's Rep.": "North Korea",
    "Iran, Islamic Rep.": "Iran",
    "Egypt, Arab Rep.": "Egypt",
    "Yemen, Rep.": "Yemen",
    "Venezuela, RB": "Venezuela",
    "Syrian Arab Republic": "Syria",
    "Lao PDR": "Laos",
    "Kyrgyz Republic": "Kyrgyzstan",
    "Czechia": "Czech Republic",
    "Slovak Republic": "Slovakia",
    "Bahamas, The": "Bahamas",
    "Gambia, The": "Gambia",
    "Congo, Dem. Rep.": "Democratic Republic of Congo",
    "Congo, Rep.": "Congo",
    "Brunei Darussalam": "Brunei",
    "Cabo Verde": "Cape Verde",
    "Timor-Leste": "Timor Leste",
    "Guinea-Bissau": "Guinea Bissau",
    "Micronesia, Fed. Sts.": "Micronesia",
    "North Macedonia": "Macedonia",
    "Eswatini": "Swaziland",
    "TΓΌrkiye": "Turkey",
    "Cote d'Ivoire": "Ivory Coast",
    "Hong Kong SAR, China": "Hong Kong",
    "Macao SAR, China": "Macao",
    "West Bank and Gaza": "Palestine",
    "British Virgin Islands": "Virgin Islands",
    "Tanzania": "Tanzania",
}

def normalize_country(name: str) -> Optional[str]:
    if not name:
        return None
    name = name.strip()
    return COUNTRY_NAME_MAP.get(name, name)

@app.get("/api/worldbank/indicators")
def list_worldbank_indicators():
    """List all available World Bank indicators with metadata."""
    result = []
    for key, wb_code in WORLDBANK_INDICATORS.items():
        result.append({
            "id": key,
            "wb_code": wb_code,
            "name": key.replace("_", " ").title(),
            "source": "World Bank",
            "category": _guess_category(key),
        })
    return {"indicators": result, "count": len(result)}

def _guess_category(key: str) -> str:
    key_lc = key.lower()
    # Economy
    if any(x in key_lc for x in ["gdp", "gni", "inflation", "export", "import", "trade", "fdi", "invest", "industry", "agriculture_value", "manufacturing", "service", "debt", "expense", "revenue", "tax", "money", "price", "exchange", "business", "credit", "stock", "reserve", "rent", "income_share", "wage", "salary", "labor_force", "employment", "unemployment", "self_employed", "vulnerable_employment", "poverty", "gini"]):
        return "Economy"
    # Health
    if any(x in key_lc for x in ["life_expectancy", "health", "mortality", "sanitation", "drinking_water", "handwashing", "stunting", "wasting", "overweight", "undernourishment", "diabetes", "tuberculosis", "malaria", "hiv", "alcohol", "smoking", "suicide", "traffic", "pollution", "pm25", "physician", "nurse", "hospital", "immunization", "births_attended", "fertility", "adolescent", "contraceptive", "maternal", "neonatal", "daly", "blood_pressure", "blood_glucose"]):
        return "Health"
    # Education
    if any(x in key_lc for x in ["enrollment", "education", "literacy", "school", "pupil", "teacher", "trained_teacher", "scholarship", "mean_years", "expected_years"]):
        return "Education"
    # Infrastructure / Digital / Technology
    if any(x in key_lc for x in ["electricity", "internet", "broadband", "mobile", "server", "researcher", "technician", "rd_expenditure", "scientific_journal", "ict", "telecommunication", "digital", "cybersecurity", "innovation", "patent", "high_tech"]):
        return "Digital"
    # Environment
    if any(x in key_lc for x in ["co2", "carbon", "renewable", "fossil", "nuclear", "energy", "electricity_from", "forest", "freshwater", "water", "methane", "nitrous", "nox", "ghg", "emission", "pollution", "pm25_exposure", "biodiversity", "threatened", "species", "natural_resource", "ocean", "material_footprint", "consumption_footprint", "agricultural_land", "arable", "cereal", "fertilizer", "climate"]):
        return "Environment"
    # Social / Demographics
    if any(x in key_lc for x in ["population", "urban", "rural", "dependency", "migration", "refugee", "displaced", "birth_rate", "death_rate", "gender", "women", "female", "youth", "child_labor", "time_spent"]):
        return "Social"
    # Governance / Institutions
    if any(x in key_lc for x in ["voice", "accountability", "political", "government_effectiveness", "regulatory", "rule_of_law", "corruption", "corrupt", "cpi", "press_freedom", "battle", "homicide", "detention", "prison", "military", "armed_force", "arms", "conflict", "terrorism", "procedures", "time_to_start", "cost_to_start", "tax_payment", "property", "building", "electricity_time", "credit_rank", "minority", "insolvency", "contract", "women_business", "female_top_manager", "legal_framework"]):
        return "Governance"
    # Tourism
    if any(x in key_lc for x in ["tourism", "tourist"]):
        return "Tourism"
    # Aid / Development
    if any(x in key_lc for x in ["oda", "aid", "remittance", "debt_relief"]):
        return "Aid"
    # Food
    if any(x in key_lc for x in ["food"]):
        return "Food"
    # Labor
    if any(x in key_lc for x in ["workers_rights", "collective_bargaining", "trade_union", "freedom_of_association", "right_to_strike"]):
        return "Labor"
    return "General"

@app.get("/api/worldbank/fetch")
def fetch_worldbank(
    indicators: str = Query(..., description="Comma-separated indicator IDs (e.g., gdp_per_capita,life_expectancy)"),
    countries: str = Query("all", description="Comma-separated country codes or 'all'"),
    date_range: str = Query("2020:2023", description="Date range like 2020:2023"),
    latest_only: bool = Query(True, description="Only return latest year per country"),
):
    """Fetch World Bank data for specified indicators. Returns normalized dataset."""
    cache_key = f"wb:{indicators}:{countries}:{date_range}:{latest_only}"
    cached = _cache_get(cache_key)
    if cached:
        return cached

    ind_list = [i.strip() for i in indicators.split(",")]
    records = {}

    for ind_id in ind_list:
        wb_code = WORLDBANK_INDICATORS.get(ind_id)
        if not wb_code:
            continue
        # Fetch all pages for this indicator
        page = 1
        all_wb_data = []
        while True:
            paged_url = f"{WORLD_BANK_BASE}/country/{countries}/indicator/{wb_code}"
            try:
                r = requests.get(paged_url, params={
                    "format": "json", "per_page": 300, "date": date_range, "page": page
                }, timeout=30)
                paged_data = r.json()
                if len(paged_data) < 2 or not paged_data[1]:
                    break
                all_wb_data.extend(paged_data[1])
                meta = paged_data[0]
                if meta.get("page", 1) >= meta.get("pages", 1):
                    break
                page += 1
            except Exception as e:
                print(f"WB page fetch error for {ind_id} page {page}: {e}")
                break

        # Known World Bank aggregate/region codes (not real countries)
        # Observed from actual API: 1A,1W,4E,7E,8S,B8,F1,OE,EU, ZH,ZI,ZG,ZF,Z4,Z7,ZJ,ZQ,ZT, S1-S4, T2-T7, V1-V4, XC-XT,XY, and long 3-char region codes
        WB_AGGREGATE_CODES = {
            "1A","1W","4E","7E","8S","B8","F1","OE","EU",
            "ZH","ZI","ZG","ZF","Z4","Z7","ZJ","ZQ","ZT",
            "S1","S2","S3","S4","T2","T3","T4","T5","T6","T7",
            "V1","V2","V3","V4",
            "XC","XD","XE","XF","XG","XH","XI","XJ","XL","XM","XN","XO","XP","XQ","XT","XU","XY",
            "AFE","AFW","ARB","CSS","CEB","EAP","EAR","EAS","ECA","ECS",
            "EUU","FCS","HPC","IBD","IBT","IDA","IDB","IDX","INX",
            "LAC","LCN","LDC","LIC","LMC","LMY","LTE","MEA","MIC","MNA",
            "NAC","OED","OSS","PSS","PRE","PST","SAS","SSA","SSF","SST",
            "TEA","TEC","TLA","TMN","TSA","TSS","WLD"
        }
        # Common aggregate name patterns
        AGGREGATE_NAME_PATTERNS = ["(excluding", "income levels)", "all income", "small states", "demographic dividend", "World", "Arab World", "European Union"]
        for item in all_wb_data:
            country_info = item.get("country", {})
            country_id = country_info.get("id", "")
            country_raw = country_info.get("value", "")
            country = normalize_country(country_raw)
            value = item.get("value")
            year = item.get("date")
            if not country or value is None:
                continue
            # Skip known aggregate codes AND all single/double-letter codes (they're all aggregates)
            if country_id in WB_AGGREGATE_CODES:
                continue
            # Also skip if name contains aggregate patterns
            if any(p in country_raw for p in AGGREGATE_NAME_PATTERNS):
                continue
            if country not in records:
                records[country] = {"country": country}
            # Keep the latest year for each indicator
            current = records[country].get(ind_id)
            current_year = records[country].get("_year_" + ind_id, "0")
            if current is None or (year and year > current_year):
                records[country][ind_id] = round(float(value), 3)
                records[country]["_year_" + ind_id] = year
        asyncio.sleep(0.15)  # Rate limit

    # Clean up internal year fields
    result_records = []
    for rec in records.values():
        clean = {k: v for k, v in rec.items() if not k.startswith("_")}
        if len(clean) > 1:  # At least country + one indicator
            result_records.append(clean)

    result = {
        "source": "World Bank",
        "record_count": len(result_records),
        "indicators_fetched": len(ind_list),
        "data": result_records,
    }
    _cache_set(cache_key, result)
    return result

# ============================================================================
# REST COUNTRIES API
# ============================================================================

@app.get("/api/countries/list")
def fetch_countries_list():
    """Fetch basic country data (population, area, region, etc.)"""
    cache_key = "restcountries"
    cached = _cache_get(cache_key)
    if cached:
        return cached

    try:
        url = "https://restcountries.com/v3.1/all?fields=name,population,area,region,subregion,flags,capital,cca3,landlocked,independent,unMember"
        r = requests.get(url, timeout=30)
        data = r.json()
        records = []
        for item in data:
            country = normalize_country(item.get("name", {}).get("common"))
            if not country:
                continue
            rec = {
                "country": country,
                "population": item.get("population"),
                "area_km2": item.get("area"),
                "region": item.get("region"),
                "subregion": item.get("subregion"),
                "landlocked": 1 if item.get("landlocked") else 0,
                "independent": 1 if item.get("independent") else 0,
                "un_member": 1 if item.get("unMember") else 0,
                "num_borders": len(item.get("borders", [])),
                "cca3": item.get("cca3"),
                "flag_url": item.get("flags", {}).get("png", ""),
            }
            records.append(rec)
        result = {"source": "REST Countries", "record_count": len(records), "data": records}
        _cache_set(cache_key, result)
        return result
    except Exception as e:
        raise HTTPException(status_code=502, detail=str(e))

# ============================================================================
# OPEN-METEO CLIMATE API
# ============================================================================

CAPITALS = {
    "United States": (38.9072, -77.0369),
    "China": (39.9042, 116.4074),
    "India": (28.6139, 77.2090),
    "Brazil": (-15.7975, -47.8919),
    "Russia": (55.7558, 37.6173),
    "Japan": (35.6762, 139.6503),
    "Germany": (52.5200, 13.4050),
    "United Kingdom": (51.5074, -0.1278),
    "France": (48.8566, 2.3522),
    "Italy": (41.9028, 12.4964),
    "Canada": (45.4215, -75.6972),
    "Australia": (-35.2809, 149.1300),
    "South Korea": (37.5665, 126.9780),
    "Spain": (40.4168, -3.7038),
    "Mexico": (19.4326, -99.1332),
    "Indonesia": (-6.2088, 106.8456),
    "Turkey": (39.9334, 32.8597),
    "Saudi Arabia": (24.7136, 46.6753),
    "South Africa": (-25.7479, 28.2293),
    "Egypt": (30.0444, 31.2357),
    "Argentina": (-34.6037, -58.3816),
    "Nigeria": (9.0820, 7.3983),
    "Pakistan": (33.6844, 73.0479),
    "Bangladesh": (23.8103, 90.4125),
    "Vietnam": (21.0278, 105.8342),
    "Philippines": (14.5995, 120.9842),
    "Ethiopia": (9.0084, 38.7575),
    "Iran": (35.6892, 51.3890),
    "Thailand": (13.7563, 100.5018),
    "Poland": (52.2297, 21.0122),
    "Ukraine": (50.4501, 30.5234),
    "Colombia": (4.7110, -74.0721),
    "Malaysia": (3.1390, 101.6869),
    "Morocco": (34.0209, -6.8416),
    "Peru": (-12.0464, -77.0428),
    "Czech Republic": (50.0755, 14.4378),
    "Netherlands": (52.3676, 4.9041),
    "Belgium": (50.8503, 4.3517),
    "Sweden": (59.3293, 18.0686),
    "Chile": (-33.4489, -70.6693),
    "Portugal": (38.7223, -9.1393),
    "Austria": (48.2082, 16.3738),
    "Switzerland": (46.9480, 7.4474),
    "Israel": (31.7683, 35.2137),
    "Singapore": (1.3521, 103.8198),
    "Denmark": (55.6761, 12.5683),
    "Finland": (60.1699, 24.9384),
    "Norway": (59.9139, 10.7522),
    "Ireland": (53.3498, -6.2603),
    "New Zealand": (-41.2865, 174.7762),
    "Chad": (12.1348, 15.0557),
    "Mali": (12.6392, -8.0029),
    "Niger": (13.5116, 2.1254),
    "Burkina Faso": (12.3714, -1.5197),
    "Madagascar": (-18.8792, 47.5079),
    "Malawi": (-13.9626, 33.7741),
    "Rwanda": (-1.9441, 30.0619),
    "Haiti": (18.5944, -72.3074),
    "Nepal": (27.7172, 85.3240),
    "Myanmar": (19.7633, 96.0785),
    "Cambodia": (11.5564, 104.9282),
    "Laos": (17.9757, 102.6331),
    "Mongolia": (47.8864, 106.9057),
    "Bolivia": (-17.7833, -63.1821),
    "Ecuador": (-0.1807, -78.4678),
    "Guatemala": (14.6349, -90.5069),
    "Honduras": (14.0723, -87.2021),
    "Nicaragua": (12.1364, -86.2514),
    "Paraguay": (-25.2637, -57.5759),
    "Senegal": (14.7167, -17.4677),
    "Ghana": (5.6037, -0.1870),
    "Kenya": (-1.2921, 36.8219),
    "Uganda": (0.3476, 32.5825),
    "Tanzania": (-6.1630, 35.7516),
    "Zambia": (-15.3875, 28.3228),
    "Zimbabwe": (-17.8252, 31.0335),
    "Algeria": (36.7538, 3.0588),
    "Angola": (-8.8390, 13.2894),
    "Cameroon": (3.8480, 11.5021),
    "Congo": (-4.4419, 15.2663),
    "Ivory Coast": (6.8276, -5.2893),
    "Gabon": (0.4162, 9.4673),
    "Guinea": (9.6412, -13.5784),
    "Liberia": (6.2907, -10.7605),
    "Libya": (32.8872, 13.1913),
    "Mozambique": (-25.9692, 32.5732),
    "Sierra Leone": (8.4657, -13.2317),
    "Somalia": (2.0469, 45.3182),
    "Sudan": (15.5007, 32.5599),
    "Togo": (6.1256, 1.2254),
    "Democratic Republic of Congo": (-4.3250, 15.3222),
    "Central African Republic": (4.3947, 18.5582),
    "Equatorial Guinea": (1.6508, 10.2679),
    "Eritrea": (15.3229, 38.9251),
    "Lesotho": (-29.6100, 28.2336),
    "Botswana": (-24.6282, 25.9231),
    "Namibia": (-22.5609, 17.0658),
    "Swaziland": (-26.3054, 31.1367),
    "Mauritania": (18.0735, -15.9582),
    "Western Sahara": (27.1536, -13.2033),
    "Djibouti": (11.5721, 43.1456),
    "Comoros": (-11.6455, 43.3333),
    "Cape Verde": (14.9330, -23.5133),
    "Sao Tome and Principe": (0.1864, 6.6131),
    "Seychelles": (-4.6796, 55.4920),
    "Mauritius": (-20.1619, 57.4989),
    "Maldives": (4.1755, 73.5093),
    "Bhutan": (27.4728, 89.6390),
    "Brunei": (4.5353, 114.7277),
    "Timor Leste": (-8.5569, 125.5603),
    "Papua New Guinea": (-9.4438, 147.1803),
    "Solomon Islands": (-9.4456, 159.9729),
    "Vanuatu": (-17.7333, 168.3273),
    "Fiji": (-18.1248, 178.4501),
    "Samoa": (-13.7590, -172.1046),
    "Tonga": (-21.1394, -175.2018),
    "Kiribati": (1.8709, -157.3630),
    "Tuvalu": (-8.6319, 179.1583),
    "Nauru": (-0.5477, 166.9209),
    "Palau": (7.5000, 134.6243),
    "Marshall Islands": (7.0897, 171.3803),
    "Micronesia": (6.9240, 158.1618),
    "Guyana": (6.8013, -58.1551),
    "Suriname": (5.8520, -55.2038),
    "Belize": (17.1899, -88.4976),
    "Barbados": (13.1939, -59.5432),
    "Trinidad and Tobago": (10.6918, -61.2225),
    "Jamaica": (18.0179, -76.8099),
    "Bahamas": (25.0343, -77.3963),
    "Cuba": (23.1136, -82.3666),
    "Dominican Republic": (18.4861, -69.9312),
    "Puerto Rico": (18.2208, -66.5901),
    "Grenada": (12.1165, -61.6790),
    "Saint Vincent and the Grenadines": (13.1600, -61.2248),
    "Saint Lucia": (13.9094, -60.9789),
    "Dominica": (15.4150, -61.3710),
    "Antigua and Barbuda": (17.1274, -61.8468),
    "Saint Kitts and Nevis": (17.3578, -62.7820),
    "Armenia": (40.1792, 44.4991),
    "Azerbaijan": (40.4093, 49.8671),
    "Georgia": (41.7151, 44.8271),
    "Kazakhstan": (51.1605, 71.4704),
    "Kyrgyzstan": (42.8746, 74.5698),
    "Tajikistan": (38.5598, 68.7870),
    "Turkmenistan": (37.9601, 58.3261),
    "Uzbekistan": (41.2995, 69.2401),
    "Afghanistan": (34.5553, 69.2075),
    "Iraq": (33.3152, 44.3661),
    "Jordan": (31.9454, 35.9284),
    "Lebanon": (33.8938, 35.5018),
    "Palestine": (31.8980, 35.2042),
    "Syria": (33.5138, 36.2765),
    "Yemen": (15.3694, 44.1910),
    "Oman": (23.5880, 58.3829),
    "Qatar": (25.2854, 51.5310),
    "Kuwait": (29.3759, 47.9774),
    "Bahrain": (26.0667, 50.5577),
    "United Arab Emirates": (24.4539, 54.3773),
    "Sri Lanka": (6.9271, 79.8612),
    "Bangladesh": (23.8103, 90.4125),
    "Pakistan": (33.6844, 73.0479),
    "Nepal": (27.7172, 85.3240),
    "India": (28.6139, 77.2090),
    "Bhutan": (27.4728, 89.6390),
    "Maldives": (4.1755, 73.5093),
    "Thailand": (13.7563, 100.5018),
    "Vietnam": (21.0278, 105.8342),
    "Laos": (17.9757, 102.6331),
    "Cambodia": (11.5564, 104.9282),
    "Myanmar": (19.7633, 96.0785),
    "Malaysia": (3.1390, 101.6869),
    "Singapore": (1.3521, 103.8198),
    "Indonesia": (-6.2088, 106.8456),
    "Philippines": (14.5995, 120.9842),
    "Taiwan": (25.0330, 121.5654),
    "North Korea": (39.0392, 125.7625),
    "South Korea": (37.5665, 126.9780),
    "Japan": (35.6762, 139.6503),
    "Mongolia": (47.8864, 106.9057),
    "China": (39.9042, 116.4074),
    "Hong Kong": (22.3193, 114.1694),
    "Macao": (22.1987, 113.5439),
    "Australia": (-35.2809, 149.1300),
    "New Zealand": (-41.2865, 174.7762),
    "Papua New Guinea": (-9.4438, 147.1803),
    "Fiji": (-18.1248, 178.4501),
    "Solomon Islands": (-9.4456, 159.9729),
    "Vanuatu": (-17.7333, 168.3273),
    "Samoa": (-13.7590, -172.1046),
    "Tonga": (-21.1394, -175.2018),
    "Kiribati": (1.8709, -157.3630),
    "Tuvalu": (-8.6319, 179.1583),
    "Nauru": (-0.5477, 166.9209),
    "Palau": (7.5000, 134.6243),
    "Marshall Islands": (7.0897, 171.3803),
    "Micronesia": (6.9240, 158.1618),
    "United States": (38.9072, -77.0369),
    "Canada": (45.4215, -75.6972),
    "Mexico": (19.4326, -99.1332),
    "Guatemala": (14.6349, -90.5069),
    "Belize": (17.1899, -88.4976),
    "El Salvador": (13.6929, -89.2182),
    "Honduras": (14.0723, -87.2021),
    "Nicaragua": (12.1364, -86.2514),
    "Costa Rica": (9.7489, -83.7534),
    "Panama": (8.9824, -79.5199),
    "Colombia": (4.7110, -74.0721),
    "Venezuela": (10.4806, -66.9036),
    "Ecuador": (-0.1807, -78.4678),
    "Peru": (-12.0464, -77.0428),
    "Bolivia": (-17.7833, -63.1821),
    "Brazil": (-15.7975, -47.8919),
    "Chile": (-33.4489, -70.6693),
    "Argentina": (-34.6037, -58.3816),
    "Uruguay": (-34.9011, -56.1645),
    "Paraguay": (-25.2637, -57.5759),
    "Guyana": (6.8013, -58.1551),
    "Suriname": (5.8520, -55.2038),
    "French Guiana": (4.9333, -52.3306),
    "Germany": (52.5200, 13.4050),
    "United Kingdom": (51.5074, -0.1278),
    "France": (48.8566, 2.3522),
    "Italy": (41.9028, 12.4964),
    "Spain": (40.4168, -3.7038),
    "Portugal": (38.7223, -9.1393),
    "Netherlands": (52.3676, 4.9041),
    "Belgium": (50.8503, 4.3517),
    "Luxembourg": (49.6116, 6.1319),
    "Switzerland": (46.9480, 7.4474),
    "Austria": (48.2082, 16.3738),
    "Liechtenstein": (47.1410, 9.5209),
    "Denmark": (55.6761, 12.5683),
    "Sweden": (59.3293, 18.0686),
    "Norway": (59.9139, 10.7522),
    "Finland": (60.1699, 24.9384),
    "Iceland": (64.1466, -21.9426),
    "Ireland": (53.3498, -6.2603),
    "Poland": (52.2297, 21.0122),
    "Czech Republic": (50.0755, 14.4378),
    "Slovakia": (48.1486, 17.1077),
    "Hungary": (47.4979, 19.0402),
    "Slovenia": (46.0569, 14.5058),
    "Croatia": (45.8150, 15.9819),
    "Bosnia and Herzegovina": (43.8563, 18.4131),
    "Serbia": (44.7866, 20.4489),
    "Montenegro": (42.4304, 19.2594),
    "North Macedonia": (41.9981, 21.4254),
    "Albania": (41.3275, 19.8187),
    "Greece": (37.9838, 23.7275),
    "Bulgaria": (42.6977, 23.3219),
    "Romania": (44.4268, 26.1025),
    "Moldova": (47.0105, 28.8638),
    "Ukraine": (50.4501, 30.5234),
    "Belarus": (53.9045, 27.5615),
    "Lithuania": (54.6872, 25.2797),
    "Latvia": (56.9496, 24.1052),
    "Estonia": (59.4370, 24.7536),
    "Russia": (55.7558, 37.6173),
    "Turkey": (39.9334, 32.8597),
    "Cyprus": (35.1856, 33.3823),
    "Malta": (35.8989, 14.5146),
    "Andorra": (42.5063, 1.5218),
    "Monaco": (43.7384, 7.4246),
    "San Marino": (43.9424, 12.4578),
    "Vatican City": (41.9029, 12.4534),
    "Kosovo": (42.6026, 20.9030),
    "South Africa": (-25.7479, 28.2293),
    "Egypt": (30.0444, 31.2357),
    "Morocco": (34.0209, -6.8416),
    "Algeria": (36.7538, 3.0588),
    "Tunisia": (36.8065, 10.1815),
    "Libya": (32.8872, 13.1913),
    "Sudan": (15.5007, 32.5599),
    "Mauritania": (18.0735, -15.9582),
    "Mali": (12.6392, -8.0029),
    "Niger": (13.5116, 2.1254),
    "Chad": (12.1348, 15.0557),
    "Burkina Faso": (12.3714, -1.5197),
    "Senegal": (14.7167, -17.4677),
    "Gambia": (13.4432, -15.3101),
    "Guinea Bissau": (11.8632, -15.5843),
    "Guinea": (9.6412, -13.5784),
    "Sierra Leone": (8.4657, -13.2317),
    "Liberia": (6.2907, -10.7605),
    "Ivory Coast": (6.8276, -5.2893),
    "Ghana": (5.6037, -0.1870),
    "Togo": (6.1256, 1.2254),
    "Benin": (6.4969, 2.6283),
    "Nigeria": (9.0820, 7.3983),
    "Cameroon": (3.8480, 11.5021),
    "Central African Republic": (4.3947, 18.5582),
    "Equatorial Guinea": (1.6508, 10.2679),
    "Gabon": (0.4162, 9.4673),
    "Democratic Republic of Congo": (-4.3250, 15.3222),
    "Republic of Congo": (-4.2634, 15.2429),
    "Angola": (-8.8390, 13.2894),
    "Zambia": (-15.3875, 28.3228),
    "Zimbabwe": (-17.8252, 31.0335),
    "Malawi": (-13.9626, 33.7741),
    "Mozambique": (-25.9692, 32.5732),
    "Botswana": (-24.6282, 25.9231),
    "Namibia": (-22.5609, 17.0658),
    "South Africa": (-25.7479, 28.2293),
    "Lesotho": (-29.6100, 28.2336),
    "Eswatini": (-26.3054, 31.1367),
    "Madagascar": (-18.8792, 47.5079),
    "Comoros": (-11.6455, 43.3333),
    "Seychelles": (-4.6796, 55.4920),
    "Mauritius": (-20.1619, 57.4989),
    "Djibouti": (11.5721, 43.1456),
    "Eritrea": (15.3229, 38.9251),
    "Ethiopia": (9.0084, 38.7575),
    "Somalia": (2.0469, 45.3182),
    "Kenya": (-1.2921, 36.8219),
    "Uganda": (0.3476, 32.5825),
    "Tanzania": (-6.1630, 35.7516),
    "Rwanda": (-1.9441, 30.0619),
    "Burundi": (-3.3614, 29.3599),
    "South Sudan": (4.8594, 31.5713),
    "Saudi Arabia": (24.7136, 46.6753),
    "Iran": (35.6892, 51.3890),
    "Iraq": (33.3152, 44.3661),
    "Jordan": (31.9454, 35.9284),
    "Lebanon": (33.8938, 35.5018),
    "Syria": (33.5138, 36.2765),
    "Yemen": (15.3694, 44.1910),
    "Oman": (23.5880, 58.3829),
    "Qatar": (25.2854, 51.5310),
    "Kuwait": (29.3759, 47.9774),
    "Bahrain": (26.0667, 50.5577),
    "United Arab Emirates": (24.4539, 54.3773),
    "Israel": (31.7683, 35.2137),
    "Palestine": (31.8980, 35.2042),
    "Azerbaijan": (40.4093, 49.8671),
    "Armenia": (40.1792, 44.4991),
    "Georgia": (41.7151, 44.8271),
    "Kazakhstan": (51.1605, 71.4704),
    "Kyrgyzstan": (42.8746, 74.5698),
    "Tajikistan": (38.5598, 68.7870),
    "Turkmenistan": (37.9601, 58.3261),
    "Uzbekistan": (41.2995, 69.2401),
    "Afghanistan": (34.5553, 69.2075),
    "Pakistan": (33.6844, 73.0479),
    "India": (28.6139, 77.2090),
    "Nepal": (27.7172, 85.3240),
    "Bhutan": (27.4728, 89.6390),
    "Bangladesh": (23.8103, 90.4125),
    "Sri Lanka": (6.9271, 79.8612),
    "Maldives": (4.1755, 73.5093),
    "Myanmar": (19.7633, 96.0785),
    "Thailand": (13.7563, 100.5018),
    "Laos": (17.9757, 102.6331),
    "Cambodia": (11.5564, 104.9282),
    "Vietnam": (21.0278, 105.8342),
    "Malaysia": (3.1390, 101.6869),
    "Singapore": (1.3521, 103.8198),
    "Indonesia": (-6.2088, 106.8456),
    "Philippines": (14.5995, 120.9842),
    "Brunei": (4.5353, 114.7277),
    "Timor Leste": (-8.5569, 125.5603),
    "China": (39.9042, 116.4074),
    "Japan": (35.6762, 139.6503),
    "South Korea": (37.5665, 126.9780),
    "North Korea": (39.0392, 125.7625),
    "Mongolia": (47.8864, 106.9057),
    "Taiwan": (25.0330, 121.5654),
    "Hong Kong": (22.3193, 114.1694),
    "Macao": (22.1987, 113.5439),
    "Australia": (-35.2809, 149.1300),
    "New Zealand": (-41.2865, 174.7762),
    "Papua New Guinea": (-9.4438, 147.1803),
    "Fiji": (-18.1248, 178.4501),
    "Solomon Islands": (-9.4456, 159.9729),
    "Vanuatu": (-17.7333, 168.3273),
    "Samoa": (-13.7590, -172.1046),
    "Tonga": (-21.1394, -175.2018),
    "Kiribati": (1.8709, -157.3630),
    "Tuvalu": (-8.6319, 179.1583),
    "Nauru": (-0.5477, 166.9209),
    "Palau": (7.5000, 134.6243),
    "Marshall Islands": (7.0897, 171.3803),
    "Micronesia": (6.9240, 158.1618),
    "United States": (38.9072, -77.0369),
    "Canada": (45.4215, -75.6972),
    "Mexico": (19.4326, -99.1332),
    "Guatemala": (14.6349, -90.5069),
    "Belize": (17.1899, -88.4976),
    "El Salvador": (13.6929, -89.2182),
    "Honduras": (14.0723, -87.2021),
    "Nicaragua": (12.1364, -86.2514),
    "Costa Rica": (9.7489, -83.7534),
    "Panama": (8.9824, -79.5199),
    "Cuba": (23.1136, -82.3666),
    "Jamaica": (18.0179, -76.8099),
    "Haiti": (18.5944, -72.3074),
    "Dominican Republic": (18.4861, -69.9312),
    "Puerto Rico": (18.2208, -66.5901),
    "Bahamas": (25.0343, -77.3963),
    "Barbados": (13.1939, -59.5432),
    "Trinidad and Tobago": (10.6918, -61.2225),
    "Grenada": (12.1165, -61.6790),
    "Saint Vincent and the Grenadines": (13.1600, -61.2248),
    "Saint Lucia": (13.9094, -60.9789),
    "Dominica": (15.4150, -61.3710),
    "Antigua and Barbuda": (17.1274, -61.8468),
    "Saint Kitts and Nevis": (17.3578, -62.7820),
    "Virgin Islands": (18.3358, -64.8963),
    "Bermuda": (32.2949, -64.7820),
    "Greenland": (64.1814, -51.6941),
    "Colombia": (4.7110, -74.0721),
    "Venezuela": (10.4806, -66.9036),
    "Ecuador": (-0.1807, -78.4678),
    "Peru": (-12.0464, -77.0428),
    "Bolivia": (-17.7833, -63.1821),
    "Chile": (-33.4489, -70.6693),
    "Argentina": (-34.6037, -58.3816),
    "Uruguay": (-34.9011, -56.1645),
    "Paraguay": (-25.2637, -57.5759),
    "Guyana": (6.8013, -58.1551),
    "Suriname": (5.8520, -55.2038),
    "French Guiana": (4.9333, -52.3306),
    "Falkland Islands": (-51.7963, -59.5236),
}

@app.get("/api/climate/fetch")
def fetch_climate(countries: str = Query("all", description="Comma-separated country names or 'all'")):
    """Fetch climate data (temperature, precipitation) for capital cities."""
    cache_key = f"climate:{countries}"
    cached = _cache_get(cache_key)
    if cached:
        return cached

    target_countries = list(CAPITALS.keys()) if countries == "all" else [c.strip() for c in countries.split(",")]
    records = []

    for country in target_countries:
        if country not in CAPITALS:
            continue
        lat, lon = CAPITALS[country]
        url = "https://archive-api.open-meteo.com/v1/archive"
        params = {
            "latitude": lat, "longitude": lon,
            "start_date": "2020-01-01", "end_date": "2023-12-31",
            "daily": "temperature_2m_mean,precipitation_sum",
            "timezone": "auto"
        }
        try:
            r = requests.get(url, params=params, timeout=15)
            data = r.json()
            daily = data.get("daily", {})
            temps = daily.get("temperature_2m_mean", [])
            precip = daily.get("precipitation_sum", [])
            if temps and precip:
                valid_temps = [t for t in temps if t is not None]
                valid_precip = [p for p in precip if p is not None]
                records.append({
                    "country": country,
                    "avg_temp_c": round(sum(valid_temps) / len(valid_temps), 2) if valid_temps else None,
                    "total_precip_mm": round(sum(valid_precip), 2) if valid_precip else None,
                    "max_temp_c": round(max(valid_temps), 2) if valid_temps else None,
                    "min_temp_c": round(min(valid_temps), 2) if valid_temps else None,
                })
        except Exception as e:
            print(f"Climate fetch error for {country}: {e}")
        time.sleep(0.1)

    result = {"source": "Open-Meteo", "record_count": len(records), "data": records}
    _cache_set(cache_key, result)
    return result

# ============================================================================
# DATASET MANAGEMENT
# ============================================================================

@app.post("/api/dataset/upload")
def upload_dataset(data: dict = Body(...)):
    """Upload a custom dataset. Returns a dataset ID."""
    ds_id = _make_id()
    records = data.get("records", [])
    indicators = data.get("indicators", [])
    _loaded_datasets[ds_id] = {
        "id": ds_id,
        "name": data.get("name", f"Custom Dataset {ds_id[:6]}"),
        "uploaded_at": datetime.utcnow().isoformat(),
        "record_count": len(records),
        "indicators": indicators,
        "data": records,
    }
    return {"dataset_id": ds_id, "record_count": len(records), "indicators_count": len(indicators)}

@app.get("/api/dataset/list")
def list_datasets():
    """List all loaded datasets (built-in + user uploads)."""
    result = []
    for ds_id, ds in _loaded_datasets.items():
        result.append({
            "id": ds_id,
            "name": ds["name"],
            "record_count": ds["record_count"],
            "indicators_count": len(ds["indicators"]),
            "uploaded_at": ds.get("uploaded_at"),
        })
    return {"datasets": result}

@app.get("/api/dataset/{ds_id}")
def get_dataset(ds_id: str):
    """Get full dataset by ID."""
    if ds_id not in _loaded_datasets:
        raise HTTPException(status_code=404, detail="Dataset not found")
    return _loaded_datasets[ds_id]

@app.delete("/api/dataset/{ds_id}")
def delete_dataset(ds_id: str):
    """Delete a dataset."""
    if ds_id not in _loaded_datasets:
        raise HTTPException(status_code=404, detail="Dataset not found")
    del _loaded_datasets[ds_id]
    return {"deleted": ds_id}

# ============================================================================
# MERGED / COMBINED DATASETS
# ============================================================================

@app.post("/api/dataset/merge")
def merge_datasets(request: dict = Body(...)):
    """Merge multiple datasets on the 'country' key. Returns a new combined dataset."""
    ds_ids = request.get("dataset_ids", [])
    if len(ds_ids) < 2:
        raise HTTPException(status_code=400, detail="Need at least 2 datasets to merge")

    # Fetch all datasets
    all_data = []
    all_indicators = []
    for ds_id in ds_ids:
        if ds_id.startswith("builtin:"):
            # Built-in datasets fetched via other endpoints
            continue
        if ds_id not in _loaded_datasets:
            raise HTTPException(status_code=404, detail=f"Dataset {ds_id} not found")
        ds = _loaded_datasets[ds_id]
        all_data.append(ds["data"])
        all_indicators.extend(ds["indicators"])

    # Merge on country
    merged = {}
    for data in all_data:
        for rec in data:
            country = rec.get("country")
            if not country:
                continue
            if country not in merged:
                merged[country] = {"country": country}
            merged[country].update(rec)

    result_records = list(merged.values())
    ds_id = _make_id()
    _loaded_datasets[ds_id] = {
        "id": ds_id,
        "name": request.get("name", f"Merged Dataset {ds_id[:6]}"),
        "uploaded_at": datetime.utcnow().isoformat(),
        "record_count": len(result_records),
        "indicators": all_indicators,
        "data": result_records,
        "merged_from": ds_ids,
    }
    return {"dataset_id": ds_id, "record_count": len(result_records), "indicators_count": len(all_indicators)}

# ============================================================================
# CORRELATION / ANALYSIS ENDPOINTS (Server-side computation)
# ============================================================================

@app.post("/api/analyze/correlations")
def analyze_correlations(request: dict = Body(...)):
    """Compute Pearson correlations between all variable pairs in a dataset."""
    ds_id = request.get("dataset_id")
    if not ds_id or ds_id not in _loaded_datasets:
        raise HTTPException(status_code=400, detail="Valid dataset_id required")

    ds = _loaded_datasets[ds_id]
    records = ds["data"]
    indicators = ds["indicators"]

    if not records:
        return {"correlations": [], "count": 0}

    df = pd.DataFrame(records)
    numeric_cols = []
    for ind in indicators:
        key = ind.get("key")
        if key and key in df.columns and pd.api.types.is_numeric_dtype(df[key]):
            numeric_cols.append((key, ind))

    # Early return if insufficient columns for correlations
    if len(numeric_cols) < 2:
        return {"correlations": [], "count": 0}

    # Use pandas corr() method for better performance on all pairs
    numeric_keys = [k for k, _ in numeric_cols]
    corr_matrix = df[numeric_keys].corr(method='pearson')
    
    correlations = []
    # Create a mask to get upper triangle without diagonal
    mask = np.triu(np.ones_like(corr_matrix, dtype=bool), k=1)
    # Get indices where correlations are not null
    valid_mask = ~pd.isnull(corr_matrix).values & mask
    rows, cols = np.where(valid_mask)
    
    for i, j in zip(rows, cols):
        k1, ind1 = numeric_cols[i]
        k2, ind2 = numeric_cols[j]
        corr_val = corr_matrix.iloc[i, j]
        if pd.isna(corr_val):
            continue
        # Count non-null pairs
        valid_pairs = df[[k1, k2]].dropna()
        correlations.append({
            "var1": {"key": k1, "name": ind1["name"], "category": ind1.get("category", "General")},
            "var2": {"key": k2, "name": ind2["name"], "category": ind2.get("category", "General")},
            "r": round(float(corr_val), 4),
            "abs_r": round(abs(float(corr_val)), 4),
            "n": len(valid_pairs),
        })
    
    correlations.sort(key=lambda x: x["abs_r"], reverse=True)
    return {"correlations": correlations, "count": len(correlations)}

@app.post("/api/analyze/outliers")
def analyze_outliers(request: dict = Body(...)):
    """Find statistical outliers (z-score > threshold) in a dataset."""
    ds_id = request.get("dataset_id")
    threshold = request.get("threshold", 2.0)
    if not ds_id or ds_id not in _loaded_datasets:
        raise HTTPException(status_code=400, detail="Valid dataset_id required")

    ds = _loaded_datasets[ds_id]
    df = pd.DataFrame(ds["data"])
    indicators = ds["indicators"]

    outliers = []
    for ind in indicators:
        key = ind.get("key")
        if not key or key not in df.columns:
            continue
        col = pd.to_numeric(df[key], errors="coerce").dropna()
        if len(col) < 5:
            continue
        mean, std = col.mean(), col.std()
        if std == 0:
            continue
        z_scores = (col - mean) / std
        for country, z in z_scores.items():
            if abs(z) > threshold:
                outliers.append({
                    "country": df.iloc[country]["country"] if "country" in df.columns else str(country),
                    "variable": key,
                    "variable_name": ind["name"],
                    "value": round(float(col.iloc[country]), 3),
                    "z_score": round(float(z), 2),
                    "direction": "high" if z > 0 else "low",
                })

    # Group by country
    grouped = {}
    for o in outliers:
        c = o["country"]
        if c not in grouped:
            grouped[c] = []
        grouped[c].append(o)

    result = []
    for country, devs in grouped.items():
        devs.sort(key=lambda x: abs(x["z_score"]), reverse=True)
        result.append({
            "country": country,
            "deviation_count": len(devs),
            "top_deviation": devs[0],
            "deviations": devs,
        })

    result.sort(key=lambda x: abs(x["top_deviation"]["z_score"]), reverse=True)
    return {"outliers": result, "country_count": len(result), "total_deviations": len(outliers)}

# ============================================================================
# BUILT-IN REFERENCE DATASETS
# ============================================================================

_BUILTIN_DATASETS = {
    "happiness_2023": {
        "name": "World Happiness Report 2023",
        "source": "UN Sustainable Development Solutions Network",
        "category": "Wellbeing",
        "description": "Happiness scores, social support, freedom, generosity, and perceptions of corruption.",
        "last_updated": "2023",
        "records": [
            {"country": "Finland", "happiness_score": 7.804, "gdp_per_capita": 1.888, "social_support": 1.585, "healthy_life_expectancy": 0.519, "freedom": 0.637, "generosity": 0.142, "perceptions_of_corruption": 0.515, "dystopia_residual": 2.518},
            {"country": "Denmark", "happiness_score": 7.586, "gdp_per_capita": 1.949, "social_support": 1.548, "healthy_life_expectancy": 0.537, "freedom": 0.609, "generosity": 0.222, "perceptions_of_corruption": 0.485, "dystopia_residual": 2.236},
            {"country": "Iceland", "happiness_score": 7.530, "gdp_per_capita": 1.882, "social_support": 1.639, "healthy_life_expectancy": 0.536, "freedom": 0.610, "generosity": 0.249, "perceptions_of_corruption": 0.168, "dystopia_residual": 2.446},
            {"country": "Israel", "happiness_score": 7.473, "gdp_per_capita": 1.796, "social_support": 1.455, "healthy_life_expectancy": 0.532, "freedom": 0.465, "generosity": 0.195, "perceptions_of_corruption": 0.306, "dystopia_residual": 2.724},
            {"country": "Netherlands", "happiness_score": 7.403, "gdp_per_capita": 1.913, "social_support": 1.398, "healthy_life_expectancy": 0.529, "freedom": 0.535, "generosity": 0.262, "perceptions_of_corruption": 0.290, "dystopia_residual": 2.476},
            {"country": "Sweden", "happiness_score": 7.395, "gdp_per_capita": 1.882, "social_support": 1.461, "healthy_life_expectancy": 0.532, "freedom": 0.587, "generosity": 0.233, "perceptions_of_corruption": 0.434, "dystopia_residual": 2.266},
            {"country": "Norway", "happiness_score": 7.315, "gdp_per_capita": 1.936, "social_support": 1.530, "healthy_life_expectancy": 0.538, "freedom": 0.584, "generosity": 0.228, "perceptions_of_corruption": 0.383, "dystopia_residual": 2.116},
            {"country": "Switzerland", "happiness_score": 7.240, "gdp_per_capita": 1.913, "social_support": 1.449, "healthy_life_expectancy": 0.561, "freedom": 0.572, "generosity": 0.218, "perceptions_of_corruption": 0.332, "dystopia_residual": 2.195},
            {"country": "Luxembourg", "happiness_score": 7.228, "gdp_per_capita": 2.045, "social_support": 1.387, "healthy_life_expectancy": 0.528, "freedom": 0.561, "generosity": 0.168, "perceptions_of_corruption": 0.386, "dystopia_residual": 2.153},
            {"country": "New Zealand", "happiness_score": 7.123, "gdp_per_capita": 1.762, "social_support": 1.566, "healthy_life_expectancy": 0.531, "freedom": 0.606, "generosity": 0.261, "perceptions_of_corruption": 0.362, "dystopia_residual": 2.035},
            {"country": "Austria", "happiness_score": 7.097, "gdp_per_capita": 1.855, "social_support": 1.454, "healthy_life_expectancy": 0.529, "freedom": 0.496, "generosity": 0.182, "perceptions_of_corruption": 0.332, "dystopia_residual": 2.249},
            {"country": "Australia", "happiness_score": 7.100, "gdp_per_capita": 1.806, "social_support": 1.511, "healthy_life_expectancy": 0.557, "freedom": 0.542, "generosity": 0.270, "perceptions_of_corruption": 0.308, "dystopia_residual": 2.106},
            {"country": "Ireland", "happiness_score": 7.021, "gdp_per_capita": 1.832, "social_support": 1.529, "healthy_life_expectancy": 0.517, "freedom": 0.556, "generosity": 0.233, "perceptions_of_corruption": 0.354, "dystopia_residual": 2.000},
            {"country": "Germany", "happiness_score": 7.018, "gdp_per_capita": 1.873, "social_support": 1.428, "healthy_life_expectancy": 0.537, "freedom": 0.496, "generosity": 0.219, "perceptions_of_corruption": 0.466, "dystopia_residual": 2.000},
            {"country": "Canada", "happiness_score": 6.900, "gdp_per_capita": 1.840, "social_support": 1.487, "healthy_life_expectancy": 0.548, "freedom": 0.528, "generosity": 0.240, "perceptions_of_corruption": 0.257, "dystopia_residual": 2.000},
            {"country": "United States", "happiness_score": 6.894, "gdp_per_capita": 1.910, "social_support": 1.401, "healthy_life_expectancy": 0.520, "freedom": 0.447, "generosity": 0.220, "perceptions_of_corruption": 0.396, "dystopia_residual": 2.000},
            {"country": "United Kingdom", "happiness_score": 6.796, "gdp_per_capita": 1.810, "social_support": 1.481, "healthy_life_expectancy": 0.524, "freedom": 0.474, "generosity": 0.304, "perceptions_of_corruption": 0.203, "dystopia_residual": 2.000},
            {"country": "Czech Republic", "happiness_score": 6.845, "gdp_per_capita": 1.697, "social_support": 1.508, "healthy_life_expectancy": 0.502, "freedom": 0.502, "generosity": 0.107, "perceptions_of_corruption": 0.029, "dystopia_residual": 2.500},
            {"country": "Belgium", "happiness_score": 6.805, "gdp_per_capita": 1.838, "social_support": 1.437, "healthy_life_expectancy": 0.517, "freedom": 0.444, "generosity": 0.128, "perceptions_of_corruption": 0.441, "dystopia_residual": 2.000},
            {"country": "France", "happiness_score": 6.661, "gdp_per_capita": 1.816, "social_support": 1.393, "healthy_life_expectancy": 0.553, "freedom": 0.456, "generosity": 0.103, "perceptions_of_corruption": 0.340, "dystopia_residual": 2.000},
            {"country": "Bahrain", "happiness_score": 6.647, "gdp_per_capita": 1.684, "social_support": 1.400, "healthy_life_expectancy": 0.540, "freedom": 0.498, "generosity": 0.155, "perceptions_of_corruption": 0.270, "dystopia_residual": 2.100},
            {"country": "Slovenia", "happiness_score": 6.630, "gdp_per_capita": 1.713, "social_support": 1.487, "healthy_life_expectancy": 0.514, "freedom": 0.548, "generosity": 0.121, "perceptions_of_corruption": 0.247, "dystopia_residual": 2.000},
            {"country": "Saudi Arabia", "happiness_score": 6.587, "gdp_per_capita": 1.778, "social_support": 1.310, "healthy_life_expectancy": 0.477, "freedom": 0.449, "generosity": 0.114, "perceptions_of_corruption": 0.459, "dystopia_residual": 2.000},
            {"country": "Taiwan", "happiness_score": 6.535, "gdp_per_capita": 1.695, "social_support": 1.419, "healthy_life_expectancy": 0.537, "freedom": 0.385, "generosity": 0.133, "perceptions_of_corruption": 0.166, "dystopia_residual": 2.200},
            {"country": "Singapore", "happiness_score": 6.587, "gdp_per_capita": 1.915, "social_support": 1.321, "healthy_life_expectancy": 0.664, "freedom": 0.525, "generosity": 0.180, "perceptions_of_corruption": 0.462, "dystopia_residual": 1.520},
            {"country": "Spain", "happiness_score": 6.401, "gdp_per_capita": 1.722, "social_support": 1.398, "healthy_life_expectancy": 0.562, "freedom": 0.389, "generosity": 0.140, "perceptions_of_corruption": 0.190, "dystopia_residual": 2.000},
            {"country": "Italy", "happiness_score": 6.324, "gdp_per_capita": 1.748, "social_support": 1.357, "healthy_life_expectancy": 0.541, "freedom": 0.327, "generosity": 0.141, "perceptions_of_corruption": 0.210, "dystopia_residual": 2.010},
            {"country": "South Korea", "happiness_score": 6.125, "gdp_per_capita": 1.723, "social_support": 1.247, "healthy_life_expectancy": 0.575, "freedom": 0.261, "generosity": 0.126, "perceptions_of_corruption": 0.193, "dystopia_residual": 2.000},
            {"country": "Japan", "happiness_score": 6.129, "gdp_per_capita": 1.730, "social_support": 1.332, "healthy_life_expectancy": 0.595, "freedom": 0.441, "generosity": 0.032, "perceptions_of_corruption": 0.236, "dystopia_residual": 1.763},
            {"country": "China", "happiness_score": 5.818, "gdp_per_capita": 1.336, "social_support": 1.230, "healthy_life_expectancy": 0.524, "freedom": 0.448, "generosity": 0.063, "perceptions_of_corruption": 0.217, "dystopia_residual": 2.000},
            {"country": "Thailand", "happiness_score": 6.101, "gdp_per_capita": 1.305, "social_support": 1.351, "healthy_life_expectancy": 0.504, "freedom": 0.529, "generosity": 0.374, "perceptions_of_corruption": 0.037, "dystopia_residual": 2.001},
            {"country": "Malaysia", "happiness_score": 6.012, "gdp_per_capita": 1.411, "social_support": 1.273, "healthy_life_expectancy": 0.454, "freedom": 0.489, "generosity": 0.206, "perceptions_of_corruption": 0.180, "dystopia_residual": 2.000},
            {"country": "Brazil", "happiness_score": 6.125, "gdp_per_capita": 1.250, "social_support": 1.345, "healthy_life_expectancy": 0.422, "freedom": 0.357, "generosity": 0.125, "perceptions_of_corruption": 0.626, "dystopia_residual": 2.000},
            {"country": "Mexico", "happiness_score": 6.330, "gdp_per_capita": 1.383, "social_support": 1.247, "healthy_life_expectancy": 0.418, "freedom": 0.399, "generosity": 0.089, "perceptions_of_corruption": 0.794, "dystopia_residual": 2.000},
            {"country": "Russia", "happiness_score": 5.661, "gdp_per_capita": 1.535, "social_support": 1.468, "healthy_life_expectancy": 0.375, "freedom": 0.232, "generosity": 0.066, "perceptions_of_corruption": 0.986, "dystopia_residual": 1.999},
            {"country": "India", "happiness_score": 4.036, "gdp_per_capita": 1.088, "social_support": 0.769, "healthy_life_expectancy": 0.346, "freedom": 0.462, "generosity": 0.219, "perceptions_of_corruption": 0.152, "dystopia_residual": 2.000},
            {"country": "South Africa", "happiness_score": 4.729, "gdp_per_capita": 1.237, "social_support": 1.286, "healthy_life_expectancy": 0.276, "freedom": 0.384, "generosity": 0.089, "perceptions_of_corruption": 0.858, "dystopia_residual": 1.600},
            {"country": "Nigeria", "happiness_score": 4.981, "gdp_per_capita": 0.860, "social_support": 0.842, "healthy_life_expectancy": 0.171, "freedom": 0.329, "generosity": 0.174, "perceptions_of_corruption": 0.111, "dystopia_residual": 2.493},
            {"country": "Pakistan", "happiness_score": 4.555, "gdp_per_capita": 0.837, "social_support": 0.595, "healthy_life_expectancy": 0.270, "freedom": 0.313, "generosity": 0.255, "perceptions_of_corruption": 0.098, "dystopia_residual": 2.187},
            {"country": "Afghanistan", "happiness_score": 1.859, "gdp_per_capita": 0.473, "social_support": 0.500, "healthy_life_expectancy": 0.126, "freedom": 0.131, "generosity": 0.121, "perceptions_of_corruption": 0.062, "dystopia_residual": 0.446},
        ],
        "indicators": [
            {"key": "happiness_score", "name": "Happiness Score", "unit": "0-10", "category": "Wellbeing", "higherIsBetter": True},
            {"key": "gdp_per_capita", "name": "GDP per Capita (Log)", "unit": "", "category": "Economy", "higherIsBetter": True},
            {"key": "social_support", "name": "Social Support", "unit": "", "category": "Social", "higherIsBetter": True},
            {"key": "healthy_life_expectancy", "name": "Healthy Life Expectancy", "unit": "", "category": "Health", "higherIsBetter": True},
            {"key": "freedom", "name": "Freedom to Make Life Choices", "unit": "", "category": "Governance", "higherIsBetter": True},
            {"key": "generosity", "name": "Generosity", "unit": "", "category": "Social", "higherIsBetter": True},
            {"key": "perceptions_of_corruption", "name": "Perceptions of Corruption", "unit": "", "category": "Governance", "higherIsBetter": False},
            {"key": "dystopia_residual", "name": "Dystopia + Residual", "unit": "", "category": "Wellbeing", "higherIsBetter": True},
        ]
    },
    "hdi_2022": {
        "name": "Human Development Index 2021/2022",
        "source": "UNDP",
        "category": "Development",
        "description": "HDI, life expectancy, expected/mean years of schooling, and GNI per capita.",
        "last_updated": "2022",
        "records": [
            {"country": "Switzerland", "hdi": 0.967, "life_expectancy": 84.0, "expected_schooling": 16.5, "mean_schooling": 13.9, "gni_per_capita": 66933},
            {"country": "Norway", "hdi": 0.966, "life_expectancy": 83.2, "expected_schooling": 18.2, "mean_schooling": 12.9, "gni_per_capita": 64660},
            {"country": "Iceland", "hdi": 0.959, "life_expectancy": 82.7, "expected_schooling": 19.2, "mean_schooling": 12.9, "gni_per_capita": 55782},
            {"country": "Hong Kong", "hdi": 0.952, "life_expectancy": 85.5, "expected_schooling": 17.3, "mean_schooling": 12.2, "gni_per_capita": 62507},
            {"country": "Australia", "hdi": 0.951, "life_expectancy": 83.2, "expected_schooling": 21.1, "mean_schooling": 12.7, "gni_per_capita": 49238},
            {"country": "Denmark", "hdi": 0.952, "life_expectancy": 81.4, "expected_schooling": 18.7, "mean_schooling": 12.6, "gni_per_capita": 60365},
            {"country": "Sweden", "hdi": 0.947, "life_expectancy": 83.0, "expected_schooling": 19.4, "mean_schooling": 12.6, "gni_per_capita": 54489},
            {"country": "Ireland", "hdi": 0.950, "life_expectancy": 82.1, "expected_schooling": 18.9, "mean_schooling": 11.6, "gni_per_capita": 68658},
            {"country": "Germany", "hdi": 0.950, "life_expectancy": 80.9, "expected_schooling": 17.0, "mean_schooling": 14.2, "gni_per_capita": 54534},
            {"country": "Netherlands", "hdi": 0.946, "life_expectancy": 81.7, "expected_schooling": 18.6, "mean_schooling": 12.4, "gni_per_capita": 55797},
            {"country": "Finland", "hdi": 0.942, "life_expectancy": 81.8, "expected_schooling": 19.4, "mean_schooling": 12.9, "gni_per_capita": 49678},
            {"country": "Singapore", "hdi": 0.939, "life_expectancy": 83.6, "expected_schooling": 16.4, "mean_schooling": 11.6, "gni_per_capita": 90855},
            {"country": "Belgium", "hdi": 0.942, "life_expectancy": 81.3, "expected_schooling": 15.9, "mean_schooling": 11.9, "gni_per_capita": 52293},
            {"country": "New Zealand", "hdi": 0.937, "life_expectancy": 82.2, "expected_schooling": 20.3, "mean_schooling": 12.9, "gni_per_capita": 42084},
            {"country": "Canada", "hdi": 0.936, "life_expectancy": 82.7, "expected_schooling": 16.4, "mean_schooling": 13.8, "gni_per_capita": 46808},
            {"country": "United Kingdom", "hdi": 0.940, "life_expectancy": 80.7, "expected_schooling": 17.4, "mean_schooling": 13.4, "gni_per_capita": 45225},
            {"country": "Japan", "hdi": 0.920, "life_expectancy": 84.8, "expected_schooling": 15.2, "mean_schooling": 13.0, "gni_per_capita": 42174},
            {"country": "United States", "hdi": 0.921, "life_expectancy": 77.2, "expected_schooling": 16.3, "mean_schooling": 13.4, "gni_per_capita": 64765},
            {"country": "South Korea", "hdi": 0.925, "life_expectancy": 83.7, "expected_schooling": 16.5, "mean_schooling": 12.2, "gni_per_capita": 44947},
            {"country": "Israel", "hdi": 0.919, "life_expectancy": 82.3, "expected_schooling": 16.2, "mean_schooling": 12.8, "gni_per_capita": 42257},
            {"country": "Slovenia", "hdi": 0.926, "life_expectancy": 80.8, "expected_schooling": 17.4, "mean_schooling": 12.8, "gni_per_capita": 38430},
            {"country": "Austria", "hdi": 0.926, "life_expectancy": 81.6, "expected_schooling": 16.0, "mean_schooling": 12.3, "gni_per_capita": 54814},
            {"country": "United Arab Emirates", "hdi": 0.911, "life_expectancy": 78.7, "expected_schooling": 15.7, "mean_schooling": 12.7, "gni_per_capita": 66205},
            {"country": "Spain", "hdi": 0.905, "life_expectancy": 83.3, "expected_schooling": 17.9, "mean_schooling": 10.1, "gni_per_capita": 38886},
            {"country": "France", "hdi": 0.903, "life_expectancy": 82.5, "expected_schooling": 15.5, "mean_schooling": 11.6, "gni_per_capita": 45223},
            {"country": "Italy", "hdi": 0.895, "life_expectancy": 83.1, "expected_schooling": 16.2, "mean_schooling": 10.6, "gni_per_capita": 42040},
            {"country": "Estonia", "hdi": 0.890, "life_expectancy": 77.2, "expected_schooling": 15.9, "mean_schooling": 13.1, "gni_per_capita": 34560},
            {"country": "Czech Republic", "hdi": 0.889, "life_expectancy": 78.0, "expected_schooling": 16.2, "mean_schooling": 12.9, "gni_per_capita": 33062},
            {"country": "Greece", "hdi": 0.887, "life_expectancy": 80.1, "expected_schooling": 18.0, "mean_schooling": 10.9, "gni_per_capita": 28779},
            {"country": "Poland", "hdi": 0.876, "life_expectancy": 75.6, "expected_schooling": 15.6, "mean_schooling": 12.3, "gni_per_capita": 30064},
            {"country": "Saudi Arabia", "hdi": 0.875, "life_expectancy": 75.1, "expected_schooling": 16.1, "mean_schooling": 10.1, "gni_per_capita": 53026},
            {"country": "Portugal", "hdi": 0.866, "life_expectancy": 81.0, "expected_schooling": 16.6, "mean_schooling": 9.3, "gni_per_capita": 32508},
            {"country": "Chile", "hdi": 0.860, "life_expectancy": 78.9, "expected_schooling": 16.3, "mean_schooling": 10.7, "gni_per_capita": 23920},
            {"country": "Turkey", "hdi": 0.838, "life_expectancy": 76.0, "expected_schooling": 18.3, "mean_schooling": 8.6, "gni_per_capita": 30876},
            {"country": "Uruguay", "hdi": 0.809, "life_expectancy": 75.4, "expected_schooling": 16.3, "mean_schooling": 9.0, "gni_per_capita": 21532},
            {"country": "Russia", "hdi": 0.822, "life_expectancy": 69.4, "expected_schooling": 15.0, "mean_schooling": 12.2, "gni_per_capita": 26289},
            {"country": "Argentina", "hdi": 0.842, "life_expectancy": 75.4, "expected_schooling": 17.8, "mean_schooling": 10.7, "gni_per_capita": 21587},
            {"country": "Brazil", "hdi": 0.760, "life_expectancy": 75.9, "expected_schooling": 15.6, "mean_schooling": 8.1, "gni_per_capita": 14695},
            {"country": "Mexico", "hdi": 0.758, "life_expectancy": 70.2, "expected_schooling": 14.1, "mean_schooling": 9.2, "gni_per_capita": 17875},
            {"country": "China", "hdi": 0.768, "life_expectancy": 78.2, "expected_schooling": 14.2, "mean_schooling": 8.1, "gni_per_capita": 17704},
            {"country": "Thailand", "hdi": 0.800, "life_expectancy": 77.7, "expected_schooling": 15.9, "mean_schooling": 8.6, "gni_per_capita": 17393},
            {"country": "Malaysia", "hdi": 0.803, "life_expectancy": 74.9, "expected_schooling": 14.0, "mean_schooling": 10.2, "gni_per_capita": 27931},
            {"country": "India", "hdi": 0.633, "life_expectancy": 67.2, "expected_schooling": 11.9, "mean_schooling": 6.5, "gni_per_capita": 6659},
            {"country": "South Africa", "hdi": 0.713, "life_expectancy": 64.1, "expected_schooling": 13.6, "mean_schooling": 10.6, "gni_per_capita": 12784},
            {"country": "Indonesia", "hdi": 0.705, "life_expectancy": 67.6, "expected_schooling": 13.0, "mean_schooling": 8.6, "gni_per_capita": 12022},
            {"country": "Philippines", "hdi": 0.699, "life_expectancy": 69.3, "expected_schooling": 12.6, "mean_schooling": 9.4, "gni_per_capita": 8397},
            {"country": "Vietnam", "hdi": 0.703, "life_expectancy": 73.6, "expected_schooling": 12.9, "mean_schooling": 8.4, "gni_per_capita": 7495},
            {"country": "Egypt", "hdi": 0.731, "life_expectancy": 70.2, "expected_schooling": 13.4, "mean_schooling": 7.4, "gni_per_capita": 11866},
            {"country": "Nigeria", "hdi": 0.535, "life_expectancy": 53.9, "expected_schooling": 10.1, "mean_schooling": 7.2, "gni_per_capita": 4961},
            {"country": "Pakistan", "hdi": 0.544, "life_expectancy": 66.1, "expected_schooling": 7.8, "mean_schooling": 5.2, "gni_per_capita": 4629},
            {"country": "Bangladesh", "hdi": 0.661, "life_expectancy": 72.4, "expected_schooling": 11.5, "mean_schooling": 6.4, "gni_per_capita": 5032},
            {"country": "Afghanistan", "hdi": 0.478, "life_expectancy": 62.0, "expected_schooling": 10.1, "mean_schooling": 3.0, "gni_per_capita": 1824},
        ],
        "indicators": [
            {"key": "hdi", "name": "Human Development Index", "unit": "0-1", "category": "Development", "higherIsBetter": True},
            {"key": "life_expectancy", "name": "Life Expectancy", "unit": "years", "category": "Health", "higherIsBetter": True},
            {"key": "expected_schooling", "name": "Expected Years of Schooling", "unit": "years", "category": "Education", "higherIsBetter": True},
            {"key": "mean_schooling", "name": "Mean Years of Schooling", "unit": "years", "category": "Education", "higherIsBetter": True},
            {"key": "gni_per_capita", "name": "GNI per Capita", "unit": "USD", "category": "Economy", "higherIsBetter": True},
        ]
    },
    "epi_2022": {
        "name": "Environmental Performance Index 2022",
        "source": "Yale / Columbia",
        "category": "Environment",
        "description": "Ecosystem vitality and environmental health scores.",
        "last_updated": "2022",
        "records": [
            {"country": "Denmark", "epi_score": 77.9, "environmental_health": 91.3, "ecosystem_vitality": 64.5, "air_quality": 95.3, "sanitation": 100.0, "biodiversity": 72.8},
            {"country": "United Kingdom", "epi_score": 77.7, "environmental_health": 91.5, "ecosystem_vitality": 64.0, "air_quality": 94.8, "sanitation": 100.0, "biodiversity": 71.2},
            {"country": "Finland", "epi_score": 76.5, "environmental_health": 94.1, "ecosystem_vitality": 59.0, "air_quality": 97.9, "sanitation": 100.0, "biodiversity": 68.3},
            {"country": "Malta", "epi_score": 75.2, "environmental_health": 94.5, "ecosystem_vitality": 55.9, "air_quality": 99.3, "sanitation": 100.0, "biodiversity": 65.1},
            {"country": "Sweden", "epi_score": 72.7, "environmental_health": 90.8, "ecosystem_vitality": 54.7, "air_quality": 93.2, "sanitation": 100.0, "biodiversity": 67.4},
            {"country": "Norway", "epi_score": 71.7, "environmental_health": 93.5, "ecosystem_vitality": 50.0, "air_quality": 96.7, "sanitation": 100.0, "biodiversity": 63.8},
            {"country": "Germany", "epi_score": 62.4, "environmental_health": 81.1, "ecosystem_vitality": 43.7, "air_quality": 78.2, "sanitation": 100.0, "biodiversity": 52.1},
            {"country": "Netherlands", "epi_score": 65.0, "environmental_health": 85.5, "ecosystem_vitality": 44.5, "air_quality": 84.1, "sanitation": 100.0, "biodiversity": 49.3},
            {"country": "Japan", "epi_score": 57.2, "environmental_health": 82.0, "ecosystem_vitality": 32.4, "air_quality": 78.6, "sanitation": 100.0, "biodiversity": 48.9},
            {"country": "Australia", "epi_score": 60.1, "environmental_health": 74.1, "ecosystem_vitality": 46.1, "air_quality": 72.3, "sanitation": 100.0, "biodiversity": 58.4},
            {"country": "Spain", "epi_score": 59.3, "environmental_health": 86.0, "ecosystem_vitality": 32.7, "air_quality": 85.3, "sanitation": 100.0, "biodiversity": 41.2},
            {"country": "France", "epi_score": 55.5, "environmental_health": 80.5, "ecosystem_vitality": 30.5, "air_quality": 76.8, "sanitation": 100.0, "biodiversity": 44.6},
            {"country": "Italy", "epi_score": 52.4, "environmental_health": 80.2, "ecosystem_vitality": 24.6, "air_quality": 73.1, "sanitation": 100.0, "biodiversity": 38.9},
            {"country": "South Korea", "epi_score": 46.4, "environmental_health": 66.2, "ecosystem_vitality": 26.7, "air_quality": 61.5, "sanitation": 100.0, "biodiversity": 35.2},
            {"country": "Canada", "epi_score": 50.6, "environmental_health": 78.9, "ecosystem_vitality": 22.3, "air_quality": 70.2, "sanitation": 100.0, "biodiversity": 51.7},
            {"country": "United States", "epi_score": 43.0, "environmental_health": 68.1, "ecosystem_vitality": 17.9, "air_quality": 62.3, "sanitation": 100.0, "biodiversity": 45.8},
            {"country": "China", "epi_score": 28.4, "environmental_health": 49.8, "ecosystem_vitality": 7.0, "air_quality": 38.1, "sanitation": 81.5, "biodiversity": 22.6},
            {"country": "India", "epi_score": 18.9, "environmental_health": 26.4, "ecosystem_vitality": 11.4, "air_quality": 18.2, "sanitation": 51.8, "biodiversity": 28.3},
            {"country": "Brazil", "epi_score": 46.4, "environmental_health": 62.3, "ecosystem_vitality": 30.5, "air_quality": 52.8, "sanitation": 75.6, "biodiversity": 58.9},
            {"country": "Russia", "epi_score": 38.5, "environmental_health": 61.5, "ecosystem_vitality": 15.5, "air_quality": 56.2, "sanitation": 88.4, "biodiversity": 46.1},
            {"country": "South Africa", "epi_score": 33.4, "environmental_health": 43.2, "ecosystem_vitality": 23.6, "air_quality": 37.5, "sanitation": 72.3, "biodiversity": 44.8},
            {"country": "Mexico", "epi_score": 39.5, "environmental_health": 57.8, "ecosystem_vitality": 21.2, "air_quality": 48.6, "sanitation": 78.4, "biodiversity": 42.3},
            {"country": "Indonesia", "epi_score": 37.8, "environmental_health": 51.4, "ecosystem_vitality": 24.2, "air_quality": 43.2, "sanitation": 72.5, "biodiversity": 48.6},
            {"country": "Turkey", "epi_score": 35.2, "environmental_health": 56.1, "ecosystem_vitality": 14.3, "air_quality": 46.8, "sanitation": 83.6, "biodiversity": 31.4},
            {"country": "Argentina", "epi_score": 41.5, "environmental_health": 68.2, "ecosystem_vitality": 14.8, "air_quality": 58.4, "sanitation": 82.1, "biodiversity": 39.5},
            {"country": "Nigeria", "epi_score": 24.6, "environmental_health": 24.8, "ecosystem_vitality": 24.4, "air_quality": 15.2, "sanitation": 32.4, "biodiversity": 45.2},
            {"country": "Pakistan", "epi_score": 24.1, "environmental_health": 24.5, "ecosystem_vitality": 23.7, "air_quality": 18.4, "sanitation": 38.6, "biodiversity": 41.8},
        ],
        "indicators": [
            {"key": "epi_score", "name": "EPI Score", "unit": "0-100", "category": "Environment", "higherIsBetter": True},
            {"key": "environmental_health", "name": "Environmental Health", "unit": "0-100", "category": "Environment", "higherIsBetter": True},
            {"key": "ecosystem_vitality", "name": "Ecosystem Vitality", "unit": "0-100", "category": "Environment", "higherIsBetter": True},
            {"key": "air_quality", "name": "Air Quality", "unit": "0-100", "category": "Environment", "higherIsBetter": True},
            {"key": "sanitation", "name": "Sanitation & Drinking Water", "unit": "0-100", "category": "Environment", "higherIsBetter": True},
            {"key": "biodiversity", "name": "Biodiversity & Habitat", "unit": "0-100", "category": "Environment", "higherIsBetter": True},
        ]
    },
    "peace_2023": {
        "name": "Global Peace Index 2023",
        "source": "Institute for Economics & Peace",
        "category": "Security",
        "description": "Peace index scores, safety, militarization, and ongoing conflict indicators.",
        "last_updated": "2023",
        "records": [
            {"country": "Iceland", "peace_index": 1.124, "safety_security": 1.155, "ongoing_conflict": 1.000, "militarization": 1.198, "homicide_rate": 0.964, "incarceration_rate": 0.462, "police_rate": 0.953, "terrorism_impact": 1.000, "weapons_exports": 1.706},
            {"country": "Denmark", "peace_index": 1.310, "safety_security": 1.367, "ongoing_conflict": 1.111, "militarization": 1.413, "homicide_rate": 0.984, "incarceration_rate": 0.605, "police_rate": 1.095, "terrorism_impact": 1.000, "weapons_exports": 1.706},
            {"country": "Ireland", "peace_index": 1.312, "safety_security": 1.384, "ongoing_conflict": 1.000, "militarization": 1.481, "homicide_rate": 0.889, "incarceration_rate": 0.589, "police_rate": 1.132, "terrorism_impact": 1.000, "weapons_exports": 1.706},
            {"country": "New Zealand", "peace_index": 1.323, "safety_security": 1.410, "ongoing_conflict": 1.000, "militarization": 1.483, "homicide_rate": 1.040, "incarceration_rate": 0.753, "police_rate": 1.202, "terrorism_impact": 1.000, "weapons_exports": 1.706},
            {"country": "Austria", "peace_index": 1.316, "safety_security": 1.378, "ongoing_conflict": 1.000, "militarization": 1.475, "homicide_rate": 0.954, "incarceration_rate": 0.811, "police_rate": 1.188, "terrorism_impact": 1.000, "weapons_exports": 1.706},
            {"country": "Singapore", "peace_index": 1.339, "safety_security": 1.400, "ongoing_conflict": 1.000, "militarization": 1.544, "homicide_rate": 0.225, "incarceration_rate": 1.290, "police_rate": 1.456, "terrorism_impact": 1.000, "weapons_exports": 1.706},
            {"country": "Portugal", "peace_index": 1.333, "safety_security": 1.417, "ongoing_conflict": 1.000, "militarization": 1.431, "homicide_rate": 0.712, "incarceration_rate": 1.187, "police_rate": 1.270, "terrorism_impact": 1.000, "weapons_exports": 1.706},
            {"country": "Slovenia", "peace_index": 1.341, "safety_security": 1.386, "ongoing_conflict": 1.000, "militarization": 1.577, "homicide_rate": 0.607, "incarceration_rate": 0.704, "police_rate": 1.388, "terrorism_impact": 1.000, "weapons_exports": 1.706},
            {"country": "Japan", "peace_index": 1.336, "safety_security": 1.454, "ongoing_conflict": 1.000, "militarization": 1.429, "homicide_rate": 0.265, "incarceration_rate": 0.522, "police_rate": 1.276, "terrorism_impact": 1.000, "weapons_exports": 1.706},
            {"country": "Switzerland", "peace_index": 1.350, "safety_security": 1.414, "ongoing_conflict": 1.000, "militarization": 1.534, "homicide_rate": 0.592, "incarceration_rate": 0.799, "police_rate": 1.237, "terrorism_impact": 1.000, "weapons_exports": 1.706},
            {"country": "Canada", "peace_index": 1.449, "safety_security": 1.500, "ongoing_conflict": 1.167, "militarization": 1.624, "homicide_rate": 0.718, "incarceration_rate": 0.870, "police_rate": 1.280, "terrorism_impact": 1.000, "weapons_exports": 1.706},
            {"country": "Czech Republic", "peace_index": 1.411, "safety_security": 1.476, "ongoing_conflict": 1.000, "militarization": 1.638, "homicide_rate": 0.715, "incarceration_rate": 1.957, "police_rate": 1.279, "terrorism_impact": 1.000, "weapons_exports": 1.706},
            {"country": "Netherlands", "peace_index": 1.432, "safety_security": 1.487, "ongoing_conflict": 1.111, "militarization": 1.626, "homicide_rate": 0.728, "incarceration_rate": 0.704, "police_rate": 1.280, "terrorism_impact": 1.000, "weapons_exports": 1.706},
            {"country": "Australia", "peace_index": 1.536, "safety_security": 1.539, "ongoing_conflict": 1.167, "militarization": 1.791, "homicide_rate": 0.850, "incarceration_rate": 1.474, "police_rate": 1.236, "terrorism_impact": 1.000, "weapons_exports": 1.706},
            {"country": "Germany", "peace_index": 1.456, "safety_security": 1.507, "ongoing_conflict": 1.222, "militarization": 1.562, "homicide_rate": 0.804, "incarceration_rate": 0.784, "police_rate": 1.243, "terrorism_impact": 1.000, "weapons_exports": 1.706},
            {"country": "Norway", "peace_index": 1.465, "safety_security": 1.504, "ongoing_conflict": 1.222, "militarization": 1.597, "homicide_rate": 0.565, "incarceration_rate": 0.603, "police_rate": 1.280, "terrorism_impact": 1.000, "weapons_exports": 1.706},
            {"country": "Sweden", "peace_index": 1.488, "safety_security": 1.536, "ongoing_conflict": 1.111, "militarization": 1.694, "homicide_rate": 1.112, "incarceration_rate": 0.676, "police_rate": 1.360, "terrorism_impact": 1.000, "weapons_exports": 1.706},
            {"country": "Finland", "peace_index": 1.474, "safety_security": 1.522, "ongoing_conflict": 1.222, "militarization": 1.615, "homicide_rate": 1.290, "incarceration_rate": 0.566, "police_rate": 1.403, "terrorism_impact": 1.000, "weapons_exports": 1.706},
            {"country": "Hungary", "peace_index": 1.502, "safety_security": 1.516, "ongoing_conflict": 1.000, "militarization": 1.789, "homicide_rate": 0.746, "incarceration_rate": 1.693, "police_rate": 1.456, "terrorism_impact": 1.000, "weapons_exports": 1.706},
            {"country": "Spain", "peace_index": 1.536, "safety_security": 1.546, "ongoing_conflict": 1.222, "militarization": 1.714, "homicide_rate": 0.612, "incarceration_rate": 1.291, "police_rate": 1.385, "terrorism_impact": 1.000, "weapons_exports": 1.706},
            {"country": "Belgium", "peace_index": 1.536, "safety_security": 1.552, "ongoing_conflict": 1.000, "militarization": 1.798, "homicide_rate": 0.889, "incarceration_rate": 0.880, "police_rate": 1.490, "terrorism_impact": 1.000, "weapons_exports": 1.706},
            {"country": "United Kingdom", "peace_index": 1.606, "safety_security": 1.596, "ongoing_conflict": 1.556, "militarization": 1.662, "homicide_rate": 1.035, "incarceration_rate": 1.312, "police_rate": 1.305, "terrorism_impact": 1.000, "weapons_exports": 1.706},
            {"country": "France", "peace_index": 1.629, "safety_security": 1.622, "ongoing_conflict": 1.444, "militarization": 1.764, "homicide_rate": 1.232, "incarceration_rate": 0.980, "police_rate": 1.355, "terrorism_impact": 1.000, "weapons_exports": 1.706},
            {"country": "United States", "peace_index": 2.448, "safety_security": 2.350, "ongoing_conflict": 2.333, "militarization": 2.586, "homicide_rate": 5.000, "incarceration_rate": 4.598, "police_rate": 2.350, "terrorism_impact": 2.000, "weapons_exports": 4.000},
            {"country": "Russia", "peace_index": 3.093, "safety_security": 3.000, "ongoing_conflict": 3.667, "militarization": 2.838, "homicide_rate": 5.000, "incarceration_rate": 3.522, "police_rate": 2.800, "terrorism_impact": 3.000, "weapons_exports": 4.000},
            {"country": "China", "peace_index": 2.101, "safety_security": 2.150, "ongoing_conflict": 1.667, "militarization": 2.322, "homicide_rate": 1.100, "incarceration_rate": 1.500, "police_rate": 1.900, "terrorism_impact": 1.500, "weapons_exports": 3.000},
            {"country": "Brazil", "peace_index": 2.470, "safety_security": 2.500, "ongoing_conflict": 1.667, "militarization": 3.000, "homicide_rate": 3.800, "incarceration_rate": 2.800, "police_rate": 2.400, "terrorism_impact": 2.000, "weapons_exports": 2.500},
            {"country": "India", "peace_index": 2.356, "safety_security": 2.400, "ongoing_conflict": 2.000, "militarization": 2.500, "homicide_rate": 3.000, "incarceration_rate": 2.100, "police_rate": 2.300, "terrorism_impact": 2.500, "weapons_exports": 2.000},
            {"country": "South Africa", "peace_index": 2.507, "safety_security": 2.600, "ongoing_conflict": 1.667, "militarization": 2.900, "homicide_rate": 4.200, "incarceration_rate": 3.000, "police_rate": 2.500, "terrorism_impact": 2.200, "weapons_exports": 2.500},
            {"country": "Nigeria", "peace_index": 2.845, "safety_security": 2.900, "ongoing_conflict": 2.667, "militarization": 3.100, "homicide_rate": 3.500, "incarceration_rate": 2.400, "police_rate": 2.800, "terrorism_impact": 3.000, "weapons_exports": 2.000},
        ],
        "indicators": [
            {"key": "peace_index", "name": "Overall Peace Index", "unit": "", "category": "Security", "higherIsBetter": False},
            {"key": "safety_security", "name": "Safety & Security", "unit": "", "category": "Security", "higherIsBetter": False},
            {"key": "ongoing_conflict", "name": "Ongoing Conflict", "unit": "", "category": "Security", "higherIsBetter": False},
            {"key": "militarization", "name": "Militarization", "unit": "", "category": "Security", "higherIsBetter": False},
            {"key": "homicide_rate", "name": "Homicide Rate", "unit": "", "category": "Security", "higherIsBetter": False},
            {"key": "incarceration_rate", "name": "Incarceration Rate", "unit": "", "category": "Security", "higherIsBetter": False},
            {"key": "police_rate", "name": "Police Rate", "unit": "", "category": "Security", "higherIsBetter": False},
            {"key": "terrorism_impact", "name": "Terrorism Impact", "unit": "", "category": "Security", "higherIsBetter": False},
            {"key": "weapons_exports", "name": "Weapons Exports", "unit": "", "category": "Security", "higherIsBetter": False},
        ]
    },
    "press_freedom_2023": {
        "name": "Press Freedom Index 2023",
        "source": "Reporters Without Borders",
        "category": "Governance",
        "description": "Press freedom scores, safety of journalists, and media independence.",
        "last_updated": "2023",
        "records": [
            {"country": "Norway", "press_freedom_score": 95.18, "political_context": 96.20, "economic_context": 95.56, "legal_framework": 97.30, "safety": 92.53, "sociocultural_context": 95.32},
            {"country": "Ireland", "press_freedom_score": 89.91, "political_context": 92.35, "economic_context": 88.89, "legal_framework": 92.06, "safety": 87.50, "sociocultural_context": 88.78},
            {"country": "Denmark", "press_freedom_score": 89.48, "political_context": 93.56, "economic_context": 88.24, "legal_framework": 93.33, "safety": 85.00, "sociocultural_context": 86.79},
            {"country": "Sweden", "press_freedom_score": 88.15, "political_context": 92.94, "economic_context": 86.11, "legal_framework": 91.18, "safety": 82.50, "sociocultural_context": 88.21},
            {"country": "Finland", "press_freedom_score": 87.94, "political_context": 92.94, "economic_context": 86.11, "legal_framework": 90.59, "safety": 82.50, "sociocultural_context": 88.21},
            {"country": "Netherlands", "press_freedom_score": 87.43, "political_context": 92.35, "economic_context": 85.00, "legal_framework": 90.59, "safety": 82.50, "sociocultural_context": 86.79},
            {"country": "Switzerland", "press_freedom_score": 89.13, "political_context": 93.56, "economic_context": 87.22, "legal_framework": 92.94, "safety": 85.00, "sociocultural_context": 88.04},
            {"country": "Jamaica", "press_freedom_score": 84.56, "political_context": 90.59, "economic_context": 82.78, "legal_framework": 87.65, "safety": 80.00, "sociocultural_context": 81.84},
            {"country": "New Zealand", "press_freedom_score": 86.12, "political_context": 91.18, "economic_context": 84.44, "legal_framework": 89.41, "safety": 82.50, "sociocultural_context": 83.31},
            {"country": "Portugal", "press_freedom_score": 85.77, "political_context": 90.59, "economic_context": 83.89, "legal_framework": 89.41, "safety": 80.00, "sociocultural_context": 85.04},
            {"country": "Germany", "press_freedom_score": 84.61, "political_context": 90.00, "economic_context": 82.22, "legal_framework": 88.24, "safety": 80.00, "sociocultural_context": 82.64},
            {"country": "Belgium", "press_freedom_score": 83.74, "political_context": 89.41, "economic_context": 81.67, "legal_framework": 87.06, "safety": 78.75, "sociocultural_context": 81.84},
            {"country": "Canada", "press_freedom_score": 83.62, "press_freedom_score": 83.62, "political_context": 89.41, "economic_context": 81.11, "legal_framework": 87.65, "safety": 77.50, "sociocultural_context": 82.64},
            {"country": "Australia", "press_freedom_score": 82.15, "political_context": 88.24, "economic_context": 79.44, "legal_framework": 85.88, "safety": 77.50, "sociocultural_context": 80.78},
            {"country": "Japan", "press_freedom_score": 76.59, "political_context": 82.94, "economic_context": 76.67, "legal_framework": 82.35, "safety": 70.00, "sociocultural_context": 70.93},
            {"country": "United Kingdom", "press_freedom_score": 71.58, "political_context": 78.24, "economic_context": 72.22, "legal_framework": 78.24, "safety": 67.50, "sociocultural_context": 62.73},
            {"country": "France", "press_freedom_score": 70.67, "political_context": 77.65, "economic_context": 71.11, "legal_framework": 77.06, "safety": 65.00, "sociocultural_context": 62.73},
            {"country": "United States", "press_freedom_score": 71.22, "political_context": 77.06, "economic_context": 71.67, "legal_framework": 77.06, "safety": 67.50, "sociocultural_context": 62.73},
            {"country": "South Korea", "press_freedom_score": 66.97, "political_context": 72.35, "economic_context": 66.67, "legal_framework": 72.35, "safety": 60.00, "sociocultural_context": 63.46},
            {"country": "Spain", "press_freedom_score": 73.65, "political_context": 80.00, "economic_context": 75.56, "legal_framework": 80.59, "safety": 65.00, "sociocultural_context": 67.19},
            {"country": "Italy", "press_freedom_score": 66.56, "political_context": 72.35, "economic_context": 65.56, "legal_framework": 72.35, "safety": 57.50, "sociocultural_context": 65.08},
            {"country": "Poland", "press_freedom_score": 60.98, "political_context": 66.47, "economic_context": 60.56, "legal_framework": 67.06, "safety": 52.50, "sociocultural_context": 58.30},
            {"country": "Brazil", "press_freedom_score": 52.44, "political_context": 56.47, "economic_context": 50.56, "legal_framework": 56.47, "safety": 47.50, "sociocultural_context": 51.26},
            {"country": "India", "press_freedom_score": 36.62, "political_context": 39.41, "economic_context": 34.44, "legal_framework": 40.59, "safety": 32.50, "sociocultural_context": 36.20},
            {"country": "Turkey", "press_freedom_score": 28.64, "political_context": 28.24, "economic_context": 28.89, "legal_framework": 28.24, "safety": 27.50, "sociocultural_context": 30.31},
            {"country": "Russia", "press_freedom_score": 28.82, "political_context": 28.24, "economic_context": 29.44, "legal_framework": 28.24, "safety": 27.50, "sociocultural_context": 30.68},
            {"country": "China", "press_freedom_score": 25.08, "political_context": 25.88, "economic_context": 24.44, "legal_framework": 25.88, "safety": 25.00, "sociocultural_context": 24.26},
            {"country": "Nigeria", "press_freedom_score": 26.72, "political_context": 26.47, "economic_context": 26.67, "legal_framework": 26.47, "safety": 25.00, "sociocultural_context": 29.03},
        ],
        "indicators": [
            {"key": "press_freedom_score", "name": "Press Freedom Score", "unit": "0-100", "category": "Governance", "higherIsBetter": True},
            {"key": "political_context", "name": "Political Context", "unit": "0-100", "category": "Governance", "higherIsBetter": True},
            {"key": "economic_context", "name": "Economic Context", "unit": "0-100", "category": "Governance", "higherIsBetter": True},
            {"key": "legal_framework", "name": "Legal Framework", "unit": "0-100", "category": "Governance", "higherIsBetter": True},
            {"key": "safety", "name": "Safety of Journalists", "unit": "0-100", "category": "Governance", "higherIsBetter": True},
            {"key": "sociocultural_context", "name": "Sociocultural Context", "unit": "0-100", "category": "Governance", "higherIsBetter": True},
        ]
    },
    "gini_ref_2022": {
        "name": "Income Inequality Reference Dataset",
        "source": "World Bank",
        "category": "Social",
        "description": "Gini index and income share held by various percentiles.",
        "last_updated": "2022",
        "records": [
            {"country": "South Africa", "gini": 63.0, "income_share_lowest_20": 2.5, "income_share_highest_10": 55.0, "income_share_highest_20": 68.0, "palma_ratio": 7.1},
            {"country": "Brazil", "gini": 53.4, "income_share_lowest_20": 3.2, "income_share_highest_10": 43.0, "income_share_highest_20": 57.0, "palma_ratio": 4.8},
            {"country": "Mexico", "gini": 45.4, "income_share_lowest_20": 4.5, "income_share_highest_10": 36.0, "income_share_highest_20": 51.0, "palma_ratio": 3.3},
            {"country": "United States", "gini": 41.5, "income_share_lowest_20": 5.2, "income_share_highest_10": 30.0, "income_share_highest_20": 46.0, "palma_ratio": 2.5},
            {"country": "China", "gini": 38.2, "income_share_lowest_20": 6.0, "income_share_highest_10": 28.0, "income_share_highest_20": 43.0, "palma_ratio": 2.1},
            {"country": "Russia", "gini": 36.0, "income_share_lowest_20": 6.5, "income_share_highest_10": 26.0, "income_share_highest_20": 41.0, "palma_ratio": 1.8},
            {"country": "Turkey", "gini": 41.9, "income_share_lowest_20": 5.5, "income_share_highest_10": 32.0, "income_share_highest_20": 48.0, "palma_ratio": 2.7},
            {"country": "Argentina", "gini": 42.3, "income_share_lowest_20": 4.8, "income_share_highest_10": 32.5, "income_share_highest_20": 49.0, "palma_ratio": 2.8},
            {"country": "India", "gini": 35.7, "income_share_lowest_20": 7.0, "income_share_highest_10": 26.5, "income_share_highest_20": 41.0, "palma_ratio": 1.7},
            {"country": "United Kingdom", "gini": 35.1, "income_share_lowest_20": 6.8, "income_share_highest_10": 27.0, "income_share_highest_20": 42.0, "palma_ratio": 1.8},
            {"country": "Germany", "gini": 31.7, "income_share_lowest_20": 8.2, "income_share_highest_10": 23.0, "income_share_highest_20": 37.0, "palma_ratio": 1.3},
            {"country": "France", "gini": 32.7, "income_share_lowest_20": 7.8, "income_share_highest_10": 24.0, "income_share_highest_20": 39.0, "palma_ratio": 1.4},
            {"country": "Japan", "gini": 32.9, "income_share_lowest_20": 7.5, "income_share_highest_10": 24.5, "income_share_highest_20": 39.0, "palma_ratio": 1.4},
            {"country": "South Korea", "gini": 31.4, "income_share_lowest_20": 8.0, "income_share_highest_10": 23.5, "income_share_highest_20": 38.0, "palma_ratio": 1.3},
            {"country": "Canada", "gini": 33.3, "income_share_lowest_20": 7.5, "income_share_highest_10": 25.0, "income_share_highest_20": 40.0, "palma_ratio": 1.5},
            {"country": "Australia", "gini": 34.3, "income_share_lowest_20": 7.2, "income_share_highest_10": 26.0, "income_share_highest_20": 41.0, "palma_ratio": 1.6},
            {"country": "Spain", "gini": 34.3, "income_share_lowest_20": 7.0, "income_share_highest_10": 26.0, "income_share_highest_20": 41.0, "palma_ratio": 1.6},
            {"country": "Italy", "gini": 35.2, "income_share_lowest_20": 6.8, "income_share_highest_10": 27.0, "income_share_highest_20": 42.0, "palma_ratio": 1.7},
            {"country": "Poland", "gini": 30.2, "income_share_lowest_20": 8.5, "income_share_highest_10": 22.0, "income_share_highest_20": 36.0, "palma_ratio": 1.2},
            {"country": "Sweden", "gini": 28.8, "income_share_lowest_20": 9.0, "income_share_highest_10": 21.0, "income_share_highest_20": 34.0, "palma_ratio": 1.0},
            {"country": "Norway", "gini": 27.6, "income_share_lowest_20": 9.5, "income_share_highest_10": 20.0, "income_share_highest_20": 33.0, "palma_ratio": 0.9},
            {"country": "Denmark", "gini": 27.7, "income_share_lowest_20": 9.4, "income_share_highest_10": 20.5, "income_share_highest_20": 33.0, "palma_ratio": 0.9},
            {"country": "Finland", "gini": 27.3, "income_share_lowest_20": 9.6, "income_share_highest_10": 20.0, "income_share_highest_20": 32.0, "palma_ratio": 0.9},
        ],
        "indicators": [
            {"key": "gini", "name": "Gini Index", "unit": "0-100", "category": "Social", "higherIsBetter": False},
            {"key": "income_share_lowest_20", "name": "Income Share Lowest 20%", "unit": "%", "category": "Social", "higherIsBetter": True},
            {"key": "income_share_highest_10", "name": "Income Share Highest 10%", "unit": "%", "category": "Social", "higherIsBetter": False},
            {"key": "income_share_highest_20", "name": "Income Share Highest 20%", "unit": "%", "category": "Social", "higherIsBetter": False},
            {"key": "palma_ratio", "name": "Palma Ratio", "unit": "", "category": "Social", "higherIsBetter": False},
        ]
    },
    "corruption_perceptions_2023": {
        "name": "Corruption Perceptions Index 2023",
        "source": "Transparency International",
        "category": "Governance",
        "description": "Perceived levels of public sector corruption across countries.",
        "last_updated": "2023",
        "records": [
            {"country": "Denmark", "cpi_score": 90, "rank": 1},
            {"country": "Finland", "cpi_score": 87, "rank": 2},
            {"country": "New Zealand", "cpi_score": 85, "rank": 3},
            {"country": "Norway", "cpi_score": 84, "rank": 4},
            {"country": "Singapore", "cpi_score": 83, "rank": 5},
            {"country": "Sweden", "cpi_score": 82, "rank": 6},
            {"country": "Switzerland", "cpi_score": 82, "rank": 7},
            {"country": "Netherlands", "cpi_score": 78, "rank": 8},
            {"country": "Germany", "cpi_score": 78, "rank": 9},
            {"country": "Australia", "cpi_score": 77, "rank": 10},
            {"country": "Canada", "cpi_score": 76, "rank": 11},
            {"country": "Iceland", "cpi_score": 72, "rank": 12},
            {"country": "Hong Kong", "cpi_score": 75, "rank": 14},
            {"country": "United Kingdom", "cpi_score": 71, "rank": 20},
            {"country": "Japan", "cpi_score": 73, "rank": 16},
            {"country": "France", "cpi_score": 71, "rank": 21},
            {"country": "United States", "cpi_score": 69, "rank": 24},
            {"country": "South Korea", "cpi_score": 63, "rank": 32},
            {"country": "Spain", "cpi_score": 60, "rank": 38},
            {"country": "Italy", "cpi_score": 56, "rank": 42},
            {"country": "Poland", "cpi_score": 54, "rank": 47},
            {"country": "China", "cpi_score": 42, "rank": 76},
            {"country": "India", "cpi_score": 39, "rank": 85},
            {"country": "Turkey", "cpi_score": 36, "rank": 98},
            {"country": "Russia", "cpi_score": 26, "rank": 141},
            {"country": "Brazil", "cpi_score": 36, "rank": 100},
            {"country": "South Africa", "cpi_score": 41, "rank": 83},
            {"country": "Mexico", "cpi_score": 31, "rank": 126},
            {"country": "Nigeria", "cpi_score": 25, "rank": 145},
            {"country": "Pakistan", "cpi_score": 29, "rank": 133},
            {"country": "Afghanistan", "cpi_score": 20, "rank": 162},
            {"country": "Argentina", "cpi_score": 37, "rank": 96},
        ],
        "indicators": [
            {"key": "cpi_score", "name": "CPI Score", "unit": "0-100", "category": "Governance", "higherIsBetter": True},
            {"key": "rank", "name": "Global Rank", "unit": "", "category": "Governance", "higherIsBetter": False},
        ]
    },
    "labor_rights_2023": {
        "name": "Global Rights Index 2023",
        "source": "ITUC",
        "category": "Labor",
        "description": "Workers' rights, collective bargaining, and trade union indicators.",
        "last_updated": "2023",
        "records": [
            {"country": "Denmark", "workers_rights_score": 95, "collective_bargaining": 98, "trade_union_rights": 96, "freedom_of_association": 95, "right_to_strike": 94, "right_to_collective_bargaining": 96},
            {"country": "Sweden", "workers_rights_score": 95, "collective_bargaining": 97, "trade_union_rights": 95, "freedom_of_association": 96, "right_to_strike": 94, "right_to_collective_bargaining": 96},
            {"country": "Finland", "workers_rights_score": 94, "collective_bargaining": 96, "trade_union_rights": 94, "freedom_of_association": 95, "right_to_strike": 93, "right_to_collective_bargaining": 95},
            {"country": "Norway", "workers_rights_score": 94, "collective_bargaining": 97, "trade_union_rights": 95, "freedom_of_association": 94, "right_to_strike": 93, "right_to_collective_bargaining": 95},
            {"country": "Netherlands", "workers_rights_score": 89, "collective_bargaining": 92, "trade_union_rights": 90, "freedom_of_association": 88, "right_to_strike": 85, "right_to_collective_bargaining": 92},
            {"country": "Germany", "workers_rights_score": 88, "collective_bargaining": 91, "trade_union_rights": 89, "freedom_of_association": 87, "right_to_strike": 84, "right_to_collective_bargaining": 90},
            {"country": "Austria", "workers_rights_score": 87, "collective_bargaining": 90, "trade_union_rights": 88, "freedom_of_association": 86, "right_to_strike": 83, "right_to_collective_bargaining": 89},
            {"country": "Belgium", "workers_rights_score": 87, "collective_bargaining": 90, "trade_union_rights": 88, "freedom_of_association": 86, "right_to_strike": 84, "right_to_collective_bargaining": 88},
            {"country": "France", "workers_rights_score": 82, "collective_bargaining": 85, "trade_union_rights": 83, "freedom_of_association": 81, "right_to_strike": 80, "right_to_collective_bargaining": 84},
            {"country": "United Kingdom", "workers_rights_score": 78, "collective_bargaining": 80, "trade_union_rights": 79, "freedom_of_association": 77, "right_to_strike": 75, "right_to_collective_bargaining": 80},
            {"country": "Canada", "workers_rights_score": 76, "collective_bargaining": 79, "trade_union_rights": 77, "freedom_of_association": 75, "right_to_strike": 74, "right_to_collective_bargaining": 77},
            {"country": "Australia", "workers_rights_score": 75, "collective_bargaining": 78, "trade_union_rights": 76, "freedom_of_association": 74, "right_to_strike": 73, "right_to_collective_bargaining": 76},
            {"country": "United States", "workers_rights_score": 72, "collective_bargaining": 74, "trade_union_rights": 73, "freedom_of_association": 71, "right_to_strike": 70, "right_to_collective_bargaining": 73},
            {"country": "Japan", "workers_rights_score": 70, "collective_bargaining": 73, "trade_union_rights": 71, "freedom_of_association": 69, "right_to_strike": 68, "right_to_collective_bargaining": 70},
            {"country": "South Korea", "workers_rights_score": 65, "collective_bargaining": 68, "trade_union_rights": 66, "freedom_of_association": 64, "right_to_strike": 62, "right_to_collective_bargaining": 66},
            {"country": "Spain", "workers_rights_score": 72, "collective_bargaining": 75, "trade_union_rights": 73, "freedom_of_association": 71, "right_to_strike": 70, "right_to_collective_bargaining": 72},
            {"country": "Italy", "workers_rights_score": 71, "collective_bargaining": 74, "trade_union_rights": 72, "freedom_of_association": 70, "right_to_strike": 69, "right_to_collective_bargaining": 71},
            {"country": "Poland", "workers_rights_score": 68, "collective_bargaining": 71, "trade_union_rights": 69, "freedom_of_association": 67, "right_to_strike": 65, "right_to_collective_bargaining": 68},
            {"country": "Brazil", "workers_rights_score": 52, "collective_bargaining": 55, "trade_union_rights": 53, "freedom_of_association": 51, "right_to_strike": 50, "right_to_collective_bargaining": 52},
            {"country": "Mexico", "workers_rights_score": 48, "collective_bargaining": 50, "trade_union_rights": 49, "freedom_of_association": 47, "right_to_strike": 46, "right_to_collective_bargaining": 48},
            {"country": "India", "workers_rights_score": 42, "collective_bargaining": 44, "trade_union_rights": 43, "freedom_of_association": 41, "right_to_strike": 40, "right_to_collective_bargaining": 42},
            {"country": "Turkey", "workers_rights_score": 35, "collective_bargaining": 37, "trade_union_rights": 36, "freedom_of_association": 34, "right_to_strike": 33, "right_to_collective_bargaining": 35},
            {"country": "Russia", "workers_rights_score": 30, "collective_bargaining": 32, "trade_union_rights": 31, "freedom_of_association": 29, "right_to_strike": 28, "right_to_collective_bargaining": 30},
            {"country": "China", "workers_rights_score": 25, "collective_bargaining": 27, "trade_union_rights": 26, "freedom_of_association": 24, "right_to_strike": 23, "right_to_collective_bargaining": 25},
            {"country": "Nigeria", "workers_rights_score": 38, "collective_bargaining": 40, "trade_union_rights": 39, "freedom_of_association": 37, "right_to_strike": 36, "right_to_collective_bargaining": 38},
        ],
        "indicators": [
            {"key": "workers_rights_score", "name": "Workers Rights Score", "unit": "0-100", "category": "Labor", "higherIsBetter": True},
            {"key": "collective_bargaining", "name": "Collective Bargaining", "unit": "0-100", "category": "Labor", "higherIsBetter": True},
            {"key": "trade_union_rights", "name": "Trade Union Rights", "unit": "0-100", "category": "Labor", "higherIsBetter": True},
            {"key": "freedom_of_association", "name": "Freedom of Association", "unit": "0-100", "category": "Labor", "higherIsBetter": True},
            {"key": "right_to_strike", "name": "Right to Strike", "unit": "0-100", "category": "Labor", "higherIsBetter": True},
            {"key": "right_to_collective_bargaining", "name": "Right to Collective Bargaining", "unit": "0-100", "category": "Labor", "higherIsBetter": True},
        ]
    },
    "cybersecurity_2023": {
        "name": "Global Cybersecurity Index 2023",
        "source": "ITU",
        "category": "Technology",
        "description": "Cybersecurity readiness, legal frameworks, and technical capacity.",
        "last_updated": "2023",
        "records": [
            {"country": "United States", "cybersecurity_score": 100.0, "legal": 20.0, "technical": 19.0, "organizational": 18.0, "capacity_building": 18.5, "cooperation": 18.0},
            {"country": "United Kingdom", "cybersecurity_score": 99.5, "legal": 19.5, "technical": 19.0, "organizational": 18.5, "capacity_building": 18.0, "cooperation": 18.5},
            {"country": "Saudi Arabia", "cybersecurity_score": 99.5, "legal": 19.5, "technical": 19.5, "organizational": 18.0, "capacity_building": 18.0, "cooperation": 18.5},
            {"country": "Estonia", "cybersecurity_score": 99.5, "legal": 19.5, "technical": 19.0, "organizational": 18.5, "capacity_building": 18.5, "cooperation": 18.5},
            {"country": "South Korea", "cybersecurity_score": 98.5, "legal": 19.5, "technical": 19.5, "organizational": 18.0, "capacity_building": 18.0, "cooperation": 18.5},
            {"country": "Singapore", "cybersecurity_score": 98.0, "legal": 19.5, "technical": 19.0, "organizational": 18.0, "capacity_building": 18.0, "cooperation": 18.0},
            {"country": "Spain", "cybersecurity_score": 97.5, "legal": 19.0, "technical": 18.5, "organizational": 18.0, "capacity_building": 18.0, "cooperation": 18.0},
            {"country": "Russia", "cybersecurity_score": 97.0, "legal": 18.5, "technical": 19.5, "organizational": 18.0, "capacity_building": 18.0, "cooperation": 18.0},
            {"country": "United Arab Emirates", "cybersecurity_score": 97.0, "legal": 19.0, "technical": 19.0, "organizational": 18.0, "capacity_building": 18.0, "cooperation": 18.0},
            {"country": "Malaysia", "cybersecurity_score": 96.5, "legal": 19.0, "technical": 18.5, "organizational": 17.5, "capacity_building": 18.0, "cooperation": 18.0},
            {"country": "Lithuania", "cybersecurity_score": 96.5, "legal": 19.0, "technical": 18.5, "organizational": 18.0, "capacity_building": 18.0, "cooperation": 18.0},
            {"country": "Japan", "cybersecurity_score": 96.0, "legal": 19.0, "technical": 18.5, "organizational": 17.5, "capacity_building": 18.0, "cooperation": 18.0},
            {"country": "Canada", "cybersecurity_score": 95.5, "legal": 18.5, "technical": 18.5, "organizational": 17.5, "capacity_building": 18.0, "cooperation": 18.0},
            {"country": "France", "cybersecurity_score": 95.0, "legal": 18.5, "technical": 18.5, "organizational": 17.5, "capacity_building": 18.0, "cooperation": 18.0},
            {"country": "Germany", "cybersecurity_score": 94.5, "legal": 18.5, "technical": 18.0, "organizational": 17.5, "capacity_building": 18.0, "cooperation": 18.0},
            {"country": "India", "cybersecurity_score": 93.5, "legal": 18.5, "technical": 18.0, "organizational": 17.5, "capacity_building": 18.0, "cooperation": 18.0},
            {"country": "Australia", "cybersecurity_score": 93.0, "legal": 18.0, "technical": 18.0, "organizational": 17.5, "capacity_building": 18.0, "cooperation": 18.0},
            {"country": "Brazil", "cybersecurity_score": 92.5, "legal": 18.0, "technical": 17.5, "organizational": 17.5, "capacity_building": 18.0, "cooperation": 18.0},
            {"country": "China", "cybersecurity_score": 92.0, "legal": 18.0, "technical": 18.0, "organizational": 17.5, "capacity_building": 18.0, "cooperation": 18.0},
            {"country": "Turkey", "cybersecurity_score": 91.0, "legal": 17.5, "technical": 17.5, "organizational": 17.5, "capacity_building": 18.0, "cooperation": 18.0},
            {"country": "South Africa", "cybersecurity_score": 89.0, "legal": 17.0, "technical": 17.0, "organizational": 17.0, "capacity_building": 17.5, "cooperation": 18.0},
            {"country": "Nigeria", "cybersecurity_score": 82.0, "legal": 15.5, "technical": 15.5, "organizational": 15.5, "capacity_building": 16.0, "cooperation": 17.0},
            {"country": "Pakistan", "cybersecurity_score": 80.0, "legal": 15.0, "technical": 15.0, "organizational": 15.0, "capacity_building": 15.5, "cooperation": 16.0},
        ],
        "indicators": [
            {"key": "cybersecurity_score", "name": "Cybersecurity Score", "unit": "0-100", "category": "Technology", "higherIsBetter": True},
            {"key": "legal", "name": "Legal Framework", "unit": "0-20", "category": "Technology", "higherIsBetter": True},
            {"key": "technical", "name": "Technical Measures", "unit": "0-20", "category": "Technology", "higherIsBetter": True},
            {"key": "organizational", "name": "Organizational Measures", "unit": "0-20", "category": "Technology", "higherIsBetter": True},
            {"key": "capacity_building", "name": "Capacity Building", "unit": "0-20", "category": "Technology", "higherIsBetter": True},
            {"key": "cooperation", "name": "Cooperation", "unit": "0-20", "category": "Technology", "higherIsBetter": True},
        ]
    },
    "food_security_2023": {
        "name": "Global Food Security Index 2022",
        "source": "Economist Impact",
        "category": "Food",
        "description": "Affordability, availability, quality, and safety of food.",
        "last_updated": "2022",
        "records": [
            {"country": "Finland", "food_security_score": 85.3, "affordability": 89.5, "availability": 82.1, "quality_safety": 87.4, "sustainability_adaptation": 82.2},
            {"country": "Ireland", "food_security_score": 84.0, "affordability": 87.2, "availability": 81.5, "quality_safety": 86.8, "sustainability_adaptation": 80.5},
            {"country": "Norway", "food_security_score": 83.8, "affordability": 86.5, "availability": 81.8, "quality_safety": 86.2, "sustainability_adaptation": 80.7},
            {"country": "France", "food_security_score": 82.5, "affordability": 85.8, "availability": 80.2, "quality_safety": 85.1, "sustainability_adaptation": 78.9},
            {"country": "Netherlands", "food_security_score": 82.1, "affordability": 85.2, "availability": 79.8, "quality_safety": 84.5, "sustainability_adaptation": 78.8},
            {"country": "Canada", "food_security_score": 81.5, "affordability": 84.8, "availability": 79.5, "quality_safety": 83.8, "sustainability_adaptation": 77.9},
            {"country": "Japan", "food_security_score": 80.2, "affordability": 83.5, "availability": 78.2, "quality_safety": 82.4, "sustainability_adaptation": 76.7},
            {"country": "Sweden", "food_security_score": 80.5, "affordability": 83.8, "availability": 78.5, "quality_safety": 82.8, "sustainability_adaptation": 76.9},
            {"country": "United Kingdom", "food_security_score": 79.8, "affordability": 83.2, "availability": 77.8, "quality_safety": 82.1, "sustainability_adaptation": 76.1},
            {"country": "United States", "food_security_score": 78.5, "affordability": 82.5, "availability": 76.5, "quality_safety": 80.8, "sustainability_adaptation": 74.2},
            {"country": "Germany", "food_security_score": 79.2, "affordability": 82.8, "availability": 77.2, "quality_safety": 81.5, "sustainability_adaptation": 75.4},
            {"country": "Australia", "food_security_score": 78.8, "affordability": 82.2, "availability": 76.8, "quality_safety": 81.2, "sustainability_adaptation": 75.0},
            {"country": "Austria", "food_security_score": 78.2, "affordability": 81.5, "availability": 76.2, "quality_safety": 80.5, "sustainability_adaptation": 74.5},
            {"country": "New Zealand", "food_security_score": 77.5, "affordability": 80.8, "availability": 75.5, "quality_safety": 79.8, "sustainability_adaptation": 73.8},
            {"country": "Switzerland", "food_security_score": 77.2, "affordability": 80.5, "availability": 75.2, "quality_safety": 79.5, "sustainability_adaptation": 73.5},
            {"country": "Denmark", "food_security_score": 77.8, "affordability": 81.2, "availability": 75.8, "quality_safety": 80.2, "sustainability_adaptation": 74.0},
            {"country": "South Korea", "food_security_score": 74.5, "affordability": 78.5, "availability": 72.5, "quality_safety": 77.2, "sustainability_adaptation": 70.1},
            {"country": "Spain", "food_security_score": 75.2, "affordability": 79.2, "availability": 73.2, "quality_safety": 77.8, "sustainability_adaptation": 70.5},
            {"country": "Italy", "food_security_score": 74.8, "affordability": 78.8, "availability": 72.8, "quality_safety": 77.5, "sustainability_adaptation": 70.2},
            {"country": "Chile", "food_security_score": 68.5, "affordability": 72.5, "availability": 66.2, "quality_safety": 71.2, "sustainability_adaptation": 64.2},
            {"country": "China", "food_security_score": 71.2, "affordability": 75.2, "availability": 69.5, "quality_safety": 74.2, "sustainability_adaptation": 66.0},
            {"country": "Brazil", "food_security_score": 65.8, "affordability": 68.5, "availability": 63.8, "quality_safety": 68.5, "sustainability_adaptation": 62.4},
            {"country": "Russia", "food_security_score": 66.2, "affordability": 69.2, "availability": 64.2, "quality_safety": 69.0, "sustainability_adaptation": 62.5},
            {"country": "Turkey", "food_security_score": 62.5, "affordability": 65.2, "availability": 60.5, "quality_safety": 65.2, "sustainability_adaptation": 59.0},
            {"country": "South Africa", "food_security_score": 58.2, "affordability": 60.5, "availability": 56.2, "quality_safety": 61.2, "sustainability_adaptation": 55.0},
            {"country": "India", "food_security_score": 55.8, "affordability": 58.2, "availability": 53.5, "quality_safety": 58.5, "sustainability_adaptation": 52.8},
            {"country": "Nigeria", "food_security_score": 48.5, "affordability": 50.2, "availability": 46.8, "quality_safety": 51.2, "sustainability_adaptation": 45.8},
            {"country": "Pakistan", "food_security_score": 46.2, "affordability": 48.5, "availability": 44.5, "quality_safety": 49.2, "sustainability_adaptation": 43.0},
        ],
        "indicators": [
            {"key": "food_security_score", "name": "Food Security Score", "unit": "0-100", "category": "Food", "higherIsBetter": True},
            {"key": "affordability", "name": "Affordability", "unit": "0-100", "category": "Food", "higherIsBetter": True},
            {"key": "availability", "name": "Availability", "unit": "0-100", "category": "Food", "higherIsBetter": True},
            {"key": "quality_safety", "name": "Quality & Safety", "unit": "0-100", "category": "Food", "higherIsBetter": True},
            {"key": "sustainability_adaptation", "name": "Sustainability & Adaptation", "unit": "0-100", "category": "Food", "higherIsBetter": True},
        ]
    },
    "innovation_2023": {
        "name": "Global Innovation Index 2023",
        "source": "WIPO",
        "category": "Innovation",
        "description": "Innovation capacity, outputs, institutions, and infrastructure.",
        "last_updated": "2023",
        "records": [
            {"country": "Switzerland", "innovation_score": 67.6, "institutions": 83.2, "human_capital": 68.5, "infrastructure": 69.2, "market_sophistication": 62.1, "business_sophistication": 67.8, "knowledge_tech_outputs": 72.5, "creative_outputs": 51.2},
            {"country": "Sweden", "innovation_score": 62.4, "institutions": 82.5, "human_capital": 66.2, "infrastructure": 67.5, "market_sophistication": 58.4, "business_sophistication": 62.5, "knowledge_tech_outputs": 68.2, "creative_outputs": 48.5},
            {"country": "United States", "innovation_score": 63.5, "institutions": 79.8, "human_capital": 62.5, "infrastructure": 65.8, "market_sophistication": 65.2, "business_sophistication": 64.8, "knowledge_tech_outputs": 75.2, "creative_outputs": 52.8},
            {"country": "United Kingdom", "innovation_score": 61.2, "institutions": 80.2, "human_capital": 65.8, "infrastructure": 64.2, "market_sophistication": 60.5, "business_sophistication": 61.8, "knowledge_tech_outputs": 66.5, "creative_outputs": 50.2},
            {"country": "South Korea", "innovation_score": 59.8, "institutions": 76.5, "human_capital": 72.5, "infrastructure": 74.2, "market_sophistication": 55.8, "business_sophistication": 62.5, "knowledge_tech_outputs": 70.5, "creative_outputs": 45.8},
            {"country": "Netherlands", "innovation_score": 58.5, "institutions": 81.2, "human_capital": 63.2, "infrastructure": 66.8, "market_sophistication": 58.2, "business_sophistication": 59.5, "knowledge_tech_outputs": 62.8, "creative_outputs": 47.5},
            {"country": "Germany", "innovation_score": 57.2, "institutions": 78.5, "human_capital": 60.8, "infrastructure": 64.5, "market_sophistication": 56.8, "business_sophistication": 60.2, "knowledge_tech_outputs": 65.8, "creative_outputs": 44.2},
            {"country": "Finland", "innovation_score": 56.8, "institutions": 83.5, "human_capital": 68.2, "infrastructure": 66.5, "market_sophistication": 54.2, "business_sophistication": 56.8, "knowledge_tech_outputs": 60.5, "creative_outputs": 46.2},
            {"country": "Singapore", "innovation_score": 55.8, "institutions": 84.2, "human_capital": 70.5, "infrastructure": 72.5, "market_sophistication": 58.5, "business_sophistication": 58.2, "knowledge_tech_outputs": 58.5, "creative_outputs": 42.5},
            {"country": "Denmark", "innovation_score": 55.2, "institutions": 82.8, "human_capital": 65.5, "infrastructure": 65.8, "market_sophistication": 55.5, "business_sophistication": 56.5, "knowledge_tech_outputs": 58.2, "creative_outputs": 44.8},
            {"country": "France", "innovation_score": 53.5, "institutions": 76.2, "human_capital": 58.5, "infrastructure": 62.8, "market_sophistication": 55.2, "business_sophistication": 57.5, "knowledge_tech_outputs": 60.2, "creative_outputs": 42.8},
            {"country": "China", "innovation_score": 55.3, "institutions": 68.5, "human_capital": 56.2, "infrastructure": 63.5, "market_sophistication": 52.8, "business_sophistication": 55.2, "knowledge_tech_outputs": 72.5, "creative_outputs": 40.5},
            {"country": "Japan", "innovation_score": 52.8, "institutions": 75.2, "human_capital": 62.5, "infrastructure": 65.2, "market_sophistication": 52.5, "business_sophistication": 55.8, "knowledge_tech_outputs": 62.5, "creative_outputs": 38.5},
            {"country": "Canada", "innovation_score": 51.5, "institutions": 78.5, "human_capital": 60.2, "infrastructure": 62.5, "market_sophistication": 52.8, "business_sophistication": 53.5, "knowledge_tech_outputs": 56.2, "creative_outputs": 41.5},
            {"country": "Australia", "innovation_score": 50.2, "institutions": 77.2, "human_capital": 62.5, "infrastructure": 60.8, "market_sophistication": 51.5, "business_sophistication": 52.2, "knowledge_tech_outputs": 54.8, "creative_outputs": 40.2},
            {"country": "Israel", "innovation_score": 50.5, "institutions": 72.5, "human_capital": 64.2, "infrastructure": 62.5, "market_sophistication": 52.8, "business_sophistication": 55.5, "knowledge_tech_outputs": 58.5, "creative_outputs": 39.5},
            {"country": "Austria", "innovation_score": 48.8, "institutions": 76.5, "human_capital": 58.2, "infrastructure": 60.5, "market_sophistication": 50.2, "business_sophistication": 52.8, "knowledge_tech_outputs": 52.5, "creative_outputs": 38.5},
            {"country": "Norway", "innovation_score": 47.5, "institutions": 80.5, "human_capital": 65.2, "infrastructure": 62.5, "market_sophistication": 48.5, "business_sophistication": 50.5, "knowledge_tech_outputs": 50.2, "creative_outputs": 37.5},
            {"country": "Ireland", "innovation_score": 48.2, "institutions": 78.2, "human_capital": 60.5, "infrastructure": 58.5, "market_sophistication": 54.2, "business_sophistication": 52.5, "knowledge_tech_outputs": 52.8, "creative_outputs": 37.2},
            {"country": "Belgium", "innovation_score": 47.5, "institutions": 75.8, "human_capital": 58.5, "infrastructure": 59.2, "market_sophistication": 50.5, "business_sophistication": 51.8, "knowledge_tech_outputs": 51.5, "creative_outputs": 36.8},
            {"country": "Spain", "innovation_score": 45.2, "institutions": 72.5, "human_capital": 55.2, "infrastructure": 56.5, "market_sophistication": 48.5, "business_sophistication": 50.2, "knowledge_tech_outputs": 48.5, "creative_outputs": 35.5},
            {"country": "Italy", "innovation_score": 44.8, "institutions": 70.2, "human_capital": 52.5, "infrastructure": 55.2, "market_sophistication": 47.8, "business_sophistication": 48.5, "knowledge_tech_outputs": 47.5, "creative_outputs": 34.2},
            {"country": "Poland", "innovation_score": 42.5, "institutions": 68.5, "human_capital": 50.2, "infrastructure": 52.5, "market_sophistication": 45.5, "business_sophistication": 45.8, "knowledge_tech_outputs": 45.2, "creative_outputs": 32.5},
            {"country": "India", "innovation_score": 40.5, "institutions": 55.2, "human_capital": 42.5, "infrastructure": 48.5, "market_sophistication": 42.5, "business_sophistication": 42.8, "knowledge_tech_outputs": 45.5, "creative_outputs": 30.2},
            {"country": "Brazil", "innovation_score": 38.5, "institutions": 52.5, "human_capital": 40.2, "infrastructure": 45.5, "market_sophistication": 40.5, "business_sophistication": 40.2, "knowledge_tech_outputs": 40.5, "creative_outputs": 28.5},
            {"country": "Russia", "innovation_score": 36.2, "institutions": 48.5, "human_capital": 58.5, "infrastructure": 52.5, "market_sophistication": 38.5, "business_sophistication": 38.2, "knowledge_tech_outputs": 42.5, "creative_outputs": 26.5},
            {"country": "Turkey", "innovation_score": 35.5, "institutions": 50.2, "human_capital": 48.5, "infrastructure": 48.2, "market_sophistication": 38.2, "business_sophistication": 38.5, "knowledge_tech_outputs": 40.2, "creative_outputs": 25.8},
            {"country": "South Africa", "innovation_score": 33.2, "institutions": 48.5, "human_capital": 38.2, "infrastructure": 42.5, "market_sophistication": 36.5, "business_sophistication": 35.8, "knowledge_tech_outputs": 38.5, "creative_outputs": 24.2},
        ],
        "indicators": [
            {"key": "innovation_score", "name": "Innovation Score", "unit": "0-100", "category": "Innovation", "higherIsBetter": True},
            {"key": "institutions", "name": "Institutions", "unit": "0-100", "category": "Innovation", "higherIsBetter": True},
            {"key": "human_capital", "name": "Human Capital & Research", "unit": "0-100", "category": "Innovation", "higherIsBetter": True},
            {"key": "infrastructure", "name": "Infrastructure", "unit": "0-100", "category": "Innovation", "higherIsBetter": True},
            {"key": "market_sophistication", "name": "Market Sophistication", "unit": "0-100", "category": "Innovation", "higherIsBetter": True},
            {"key": "business_sophistication", "name": "Business Sophistication", "unit": "0-100", "category": "Innovation", "higherIsBetter": True},
            {"key": "knowledge_tech_outputs", "name": "Knowledge & Tech Outputs", "unit": "0-100", "category": "Innovation", "higherIsBetter": True},
            {"key": "creative_outputs", "name": "Creative Outputs", "unit": "0-100", "category": "Innovation", "higherIsBetter": True},
        ]
    },
    "digital_competitiveness_2023": {
        "name": "IMD Digital Competitiveness 2023",
        "source": "IMD World Competitiveness Center",
        "category": "Digital",
        "description": "Knowledge, technology, and future readiness of digital economies.",
        "last_updated": "2023",
        "records": [
            {"country": "United States", "digital_rank": 1, "knowledge": 100.0, "technology": 97.5, "future_readiness": 95.2},
            {"country": "Netherlands", "digital_rank": 2, "knowledge": 95.2, "technology": 94.8, "future_readiness": 92.5},
            {"country": "Singapore", "digital_rank": 3, "knowledge": 94.5, "technology": 95.2, "future_readiness": 91.8},
            {"country": "Denmark", "digital_rank": 4, "knowledge": 93.8, "technology": 93.5, "future_readiness": 90.5},
            {"country": "Sweden", "digital_rank": 5, "knowledge": 92.5, "technology": 92.8, "future_readiness": 89.5},
            {"country": "Switzerland", "digital_rank": 6, "knowledge": 92.0, "technology": 91.5, "future_readiness": 88.8},
            {"country": "South Korea", "digital_rank": 7, "knowledge": 91.5, "technology": 93.2, "future_readiness": 87.5},
            {"country": "Taiwan", "digital_rank": 8, "knowledge": 90.5, "technology": 92.5, "future_readiness": 86.8},
            {"country": "United Kingdom", "digital_rank": 9, "knowledge": 89.5, "technology": 90.2, "future_readiness": 85.5},
            {"country": "Finland", "digital_rank": 10, "knowledge": 88.8, "technology": 89.5, "future_readiness": 84.8},
            {"country": "Canada", "digital_rank": 11, "knowledge": 87.5, "technology": 88.2, "future_readiness": 83.5},
            {"country": "Germany", "digital_rank": 12, "knowledge": 86.8, "technology": 87.5, "future_readiness": 82.8},
            {"country": "Australia", "digital_rank": 13, "knowledge": 85.5, "technology": 86.5, "future_readiness": 81.5},
            {"country": "Israel", "digital_rank": 14, "knowledge": 85.2, "technology": 85.8, "future_readiness": 80.8},
            {"country": "Norway", "digital_rank": 15, "knowledge": 84.5, "technology": 85.2, "future_readiness": 79.5},
            {"country": "Japan", "digital_rank": 16, "knowledge": 83.8, "technology": 86.5, "future_readiness": 78.8},
            {"country": "France", "digital_rank": 17, "knowledge": 82.5, "technology": 84.5, "future_readiness": 77.5},
            {"country": "Austria", "digital_rank": 18, "knowledge": 81.8, "technology": 83.2, "future_readiness": 76.5},
            {"country": "Estonia", "digital_rank": 19, "knowledge": 80.5, "technology": 82.5, "future_readiness": 75.8},
            {"country": "Belgium", "digital_rank": 20, "knowledge": 79.8, "technology": 81.5, "future_readiness": 74.5},
            {"country": "New Zealand", "digital_rank": 21, "knowledge": 78.5, "technology": 80.2, "future_readiness": 73.5},
            {"country": "Ireland", "digital_rank": 22, "knowledge": 77.8, "technology": 79.5, "future_readiness": 72.8},
            {"country": "Spain", "digital_rank": 23, "knowledge": 76.5, "technology": 78.5, "future_readiness": 71.5},
            {"country": "China", "digital_rank": 24, "knowledge": 75.2, "technology": 80.5, "future_readiness": 70.5},
            {"country": "Italy", "digital_rank": 25, "knowledge": 74.5, "technology": 77.2, "future_readiness": 69.5},
            {"country": "Poland", "digital_rank": 26, "knowledge": 72.8, "technology": 75.5, "future_readiness": 67.5},
            {"country": "Malaysia", "digital_rank": 27, "knowledge": 71.5, "technology": 74.5, "future_readiness": 66.5},
            {"country": "Chile", "digital_rank": 28, "knowledge": 70.2, "technology": 73.5, "future_readiness": 65.5},
            {"country": "United Arab Emirates", "digital_rank": 29, "knowledge": 69.5, "technology": 72.8, "future_readiness": 64.5},
            {"country": "Brazil", "digital_rank": 30, "knowledge": 68.2, "technology": 71.5, "future_readiness": 63.5},
            {"country": "India", "digital_rank": 31, "knowledge": 66.5, "technology": 70.2, "future_readiness": 62.5},
            {"country": "Turkey", "digital_rank": 32, "knowledge": 65.2, "technology": 69.5, "future_readiness": 61.5},
            {"country": "South Africa", "digital_rank": 33, "knowledge": 63.5, "technology": 68.2, "future_readiness": 60.5},
            {"country": "Russia", "digital_rank": 34, "knowledge": 62.5, "technology": 69.5, "future_readiness": 59.5},
            {"country": "Nigeria", "digital_rank": 35, "knowledge": 58.5, "technology": 62.5, "future_readiness": 55.5},
            {"country": "Pakistan", "digital_rank": 36, "knowledge": 56.5, "technology": 60.5, "future_readiness": 53.5},
        ],
        "indicators": [
            {"key": "digital_rank", "name": "Digital Rank", "unit": "", "category": "Digital", "higherIsBetter": False},
            {"key": "knowledge", "name": "Knowledge", "unit": "0-100", "category": "Digital", "higherIsBetter": True},
            {"key": "technology", "name": "Technology", "unit": "0-100", "category": "Digital", "higherIsBetter": True},
            {"key": "future_readiness", "name": "Future Readiness", "unit": "0-100", "category": "Digital", "higherIsBetter": True},
        ]
    },
}

# Endpoints for built-in datasets

@app.get("/api/sources/list")
def list_sources():
    """List all available data sources with metadata."""
    builtin = []
    for ds_id, ds in _BUILTIN_DATASETS.items():
        builtin.append({
            "id": ds_id,
            "name": ds["name"],
            "source": ds["source"],
            "category": ds["category"],
            "description": ds["description"],
            "last_updated": ds["last_updated"],
            "variable_count": len(ds["indicators"]),
        })
    # World Bank presets
    wb_presets = [
        {"id": "wb:economy", "name": "World Bank - Economy", "source": "World Bank API", "category": "Economy", "description": "GDP, growth, debt, trade, FDI", "variable_count": 25, "indicators": "gdp_current,gdp_per_capita,gdp_growth,gdp_ppp,gdp_ppp_per_capita,inflation,exports,imports,trade_balance,fdi_inflows,fdi_net_inflows,industry_value_added,services_value_added,agriculture_value_added,manufacturing_value_added,gov_debt,gov_expense,gov_revenue,tax_revenue,central_gov_debt,broad_money_growth,consumer_price_index,exchange_rate,business_extent,private_sector_credit"},
        {"id": "wb:health", "name": "World Bank - Health", "source": "World Bank API", "category": "Health", "description": "Life expectancy, mortality, health spending, disease", "variable_count": 25, "indicators": "life_expectancy,life_expectancy_female,life_expectancy_male,health_exp_per_capita,health_exp_gdp,health_exp_gov,health_exp_private,physicians_per_1000,nurses_per_1000,hospital_beds_per_1000,immunization_dpt,immunization_measles,infant_mortality,under5_mortality,maternal_mortality,neonatal_mortality,adolescent_fertility,contraceptive_prevalence,births_attended,sanitation,drinking_water,handwashing,stunting,wasting,overweight"},
        {"id": "wb:education", "name": "World Bank - Education", "source": "World Bank API", "category": "Education", "description": "Enrollment, literacy, years of schooling, spending", "variable_count": 20, "indicators": "primary_enrollment,primary_completion,secondary_enrollment,tertiary_enrollment,literacy_rate,literacy_rate_youth,literacy_rate_female,mean_years_schooling,expected_years_schooling,pupil_teacher_ratio_primary,school_enrollment_preprimary,out_of_school_primary,out_of_school_secondary,gov_education_exp,scholarship_travel,trained_teachers,children_out_of_school,youth_literacy,gov_education_exp_primary"},
        {"id": "wb:environment", "name": "World Bank - Environment", "source": "World Bank API", "category": "Environment", "description": "CO2, renewables, forest, water, emissions, biodiversity", "variable_count": 25, "indicators": "co2_per_capita,co2_total,co2_intensity,renewable_energy,fossil_fuel_energy,nuclear_energy,electricity_from_renewables,electricity_from_fossil,electricity_from_nuclear,energy_use_per_capita,energy_intensity,forest_cover,forest_area,terrestrial_protected_areas,marine_protected_areas,freshwater_withdrawal,renewable_freshwater_per_capita,water_productivity,agricultural_land,arable_land,cereal_yield,fertilizer_consumption,methane_emissions,nitrous_oxide_emissions,ghg_emissions_total"},
        {"id": "wb:social", "name": "World Bank - Social & Demographics", "source": "World Bank API", "category": "Social", "description": "Population, gender, inequality, migration", "variable_count": 20, "indicators": "population,population_density,population_growth,urban_population,rural_population,population_0_14,population_15_64,population_65_plus,dependency_ratio,fertility_rate,birth_rate,death_rate,migration_net,refugee_population,gini,poverty_headcount,poverty_gap,income_share_lowest_20,income_share_highest_10,vulnerable_employment"},
        {"id": "wb:governance", "name": "World Bank - Governance & Business", "source": "World Bank API", "category": "Governance", "description": "Business climate, taxes, legal, women's participation", "variable_count": 20, "indicators": "voice_accountability,political_stability,government_effectiveness,regulatory_quality,rule_of_law,control_of_corruption,procedures_to_start_business,time_to_start_business,cost_to_start_business,tax_payments,time_to_pay_taxes,total_tax_rate,property_registering_procedures,time_to_register_property,women_parliament_seats,women_minister_positions,gender_inequality_index,legal_framework_gender,assets_ownership_gender"},
        {"id": "wb:digital", "name": "World Bank - Digital & Infrastructure", "source": "World Bank API", "category": "Digital", "description": "Internet, broadband, ICT, e-government", "variable_count": 15, "indicators": "internet_users,fixed_broadband,mobile_subscriptions,secure_internet_servers,individuals_using_internet,researchers_per_million,technicians_in_rd,rd_expenditure,scientific_journal_articles,ict_goods_exports,ict_goods_imports,ict_services_exports,ict_services_imports,computer_communication_services,digital_government_index"},
        {"id": "wb:tourism", "name": "World Bank - Tourism", "source": "World Bank API", "category": "Tourism", "description": "International arrivals, tourism receipts", "variable_count": 5, "indicators": "international_tourist_arrivals,international_tourism_receipts,international_tourism_expenditures,tourism_receipts_percent_exports,tourism_receipts_percent_gdp"},
        {"id": "wb:full", "name": "World Bank - Full Indicator Set (120+)", "source": "World Bank API", "category": "All", "description": "Comprehensive set of 120+ World Bank development indicators", "variable_count": 120, "indicators": ",".join(list(WORLDBANK_INDICATORS.keys())[:120])},
    ]
    return {
        "sources": {
            "builtin_datasets": builtin,
            "worldbank_presets": wb_presets,
            "live_apis": [
                {"name": "World Bank", "endpoint": "/api/worldbank/fetch", "description": "120+ development indicators", "category": "All"},
                {"name": "Open-Meteo Climate", "endpoint": "/api/climate/fetch", "description": "Temperature & precipitation for 195+ capital cities", "category": "Environment"},
                {"name": "REST Countries", "endpoint": "/api/countries/list", "description": "Basic country metadata, flags, area, landlocked", "category": "General"},
            ],
        },
        "total_builtin": len(builtin),
        "total_wb_presets": len(wb_presets),
    }

@app.get("/api/datasets/builtin/{dataset_id}")
def get_builtin_dataset(dataset_id: str):
    """Get a built-in reference dataset by ID."""
    if dataset_id not in _BUILTIN_DATASETS:
        raise HTTPException(status_code=404, detail=f"Built-in dataset '{dataset_id}' not found")
    ds = _BUILTIN_DATASETS[dataset_id]
    return {
        "dataset_id": dataset_id,
        "name": ds["name"],
        "source": ds["source"],
        "category": ds["category"],
        "description": ds["description"],
        "last_updated": ds["last_updated"],
        "record_count": len(ds["records"]),
        "variable_count": len(ds["indicators"]),
        "indicators": ds["indicators"],
        "data": ds["records"],
    }

@app.get("/api/datasets/builtin")
def list_builtin_datasets():
    """List all built-in reference datasets."""
    result = []
    for ds_id, ds in _BUILTIN_DATASETS.items():
        result.append({
            "id": ds_id,
            "name": ds["name"],
            "source": ds["source"],
            "category": ds["category"],
            "description": ds["description"],
            "last_updated": ds["last_updated"],
            "variable_count": len(ds["indicators"]),
            "record_count": len(ds["records"]),
        })
    return {"datasets": result, "count": len(result)}

# ============================================================================
# NEWS GLOBE API β€” Real-time global news sentiment
# ============================================================================

GLOBE_COUNTRIES = [
    {"name": "United States", "lat": 39.8, "lon": -98.5, "articles": 12400, "sentiment": 12, "topic": "Economy", "trend": "up"},
    {"name": "China", "lat": 35.8, "lon": 104.0, "articles": 9800, "sentiment": -8, "topic": "Trade", "trend": "down"},
    {"name": "India", "lat": 22.5, "lon": 79.0, "articles": 7200, "sentiment": 18, "topic": "Tech", "trend": "up"},
    {"name": "Russia", "lat": 61.5, "lon": 105.0, "articles": 5400, "sentiment": -35, "topic": "Conflict", "trend": "down"},
    {"name": "United Kingdom", "lat": 54.0, "lon": -2.0, "articles": 4800, "sentiment": 5, "topic": "Politics", "trend": "flat"},
    {"name": "Germany", "lat": 51.0, "lon": 10.0, "articles": 4200, "sentiment": 8, "topic": "Energy", "trend": "up"},
    {"name": "France", "lat": 46.5, "lon": 2.0, "articles": 3900, "sentiment": 3, "topic": "Labor", "trend": "flat"},
    {"name": "Brazil", "lat": -14.2, "lon": -51.9, "articles": 3600, "sentiment": 15, "topic": "Climate", "trend": "up"},
    {"name": "Japan", "lat": 36.2, "lon": 138.2, "articles": 3300, "sentiment": 22, "topic": "Innovation", "trend": "up"},
    {"name": "Canada", "lat": 56.0, "lon": -106.0, "articles": 2900, "sentiment": 14, "topic": "Health", "trend": "up"},
    {"name": "Australia", "lat": -25.2, "lon": 133.7, "articles": 2600, "sentiment": 10, "topic": "Mining", "trend": "flat"},
    {"name": "South Korea", "lat": 36.5, "lon": 127.9, "articles": 2500, "sentiment": 28, "topic": "Semiconductors", "trend": "up"},
    {"name": "Mexico", "lat": 23.6, "lon": -102.5, "articles": 2200, "sentiment": -5, "topic": "Migration", "trend": "down"},
    {"name": "Italy", "lat": 42.8, "lon": 12.5, "articles": 2100, "sentiment": 6, "topic": "Tourism", "trend": "up"},
    {"name": "Spain", "lat": 40.4, "lon": -3.7, "articles": 1900, "sentiment": 9, "topic": "Tourism", "trend": "up"},
    {"name": "Indonesia", "lat": -2.5, "lon": 118.0, "articles": 1800, "sentiment": 20, "topic": "Infrastructure", "trend": "up"},
    {"name": "Saudi Arabia", "lat": 24.0, "lon": 45.0, "articles": 1700, "sentiment": -15, "topic": "Oil", "trend": "flat"},
    {"name": "Turkey", "lat": 39.0, "lon": 35.0, "articles": 1600, "sentiment": -22, "topic": "Inflation", "trend": "down"},
    {"name": "Nigeria", "lat": 9.0, "lon": 8.0, "articles": 1500, "sentiment": -18, "topic": "Security", "trend": "down"},
    {"name": "South Africa", "lat": -29.0, "lon": 24.0, "articles": 1400, "sentiment": -12, "topic": "Energy", "trend": "down"},
    {"name": "Argentina", "lat": -38.4, "lon": -63.6, "articles": 1300, "sentiment": -30, "topic": "Economy", "trend": "down"},
    {"name": "Egypt", "lat": 26.8, "lon": 30.8, "articles": 1200, "sentiment": -8, "topic": "Water", "trend": "flat"},
    {"name": "Thailand", "lat": 15.8, "lon": 100.9, "articles": 1100, "sentiment": 25, "topic": "Tourism", "trend": "up"},
    {"name": "Vietnam", "lat": 14.0, "lon": 108.2, "articles": 1050, "sentiment": 30, "topic": "Manufacturing", "trend": "up"},
    {"name": "Israel", "lat": 31.0, "lon": 34.8, "articles": 2000, "sentiment": -45, "topic": "Conflict", "trend": "down"},
    {"name": "Ukraine", "lat": 49.0, "lon": 32.0, "articles": 1800, "sentiment": -55, "topic": "War", "trend": "down"},
    {"name": "Iran", "lat": 32.4, "lon": 53.6, "articles": 1500, "sentiment": -38, "topic": "Nuclear", "trend": "down"},
    {"name": "Pakistan", "lat": 30.3, "lon": 69.3, "articles": 900, "sentiment": -10, "topic": "Politics", "trend": "flat"},
    {"name": "Philippines", "lat": 12.8, "lon": 121.7, "articles": 850, "sentiment": 8, "topic": "Maritime", "trend": "up"},
    {"name": "Ethiopia", "lat": 9.1, "lon": 40.4, "articles": 600, "sentiment": -15, "topic": "Drought", "trend": "down"},
    {"name": "Kenya", "lat": -1.2, "lon": 36.8, "articles": 700, "sentiment": 12, "topic": "Tech", "trend": "up"},
    {"name": "Chile", "lat": -35.6, "lon": -71.5, "articles": 750, "sentiment": 5, "topic": "Lithium", "trend": "up"},
    {"name": "Sweden", "lat": 62.0, "lon": 15.0, "articles": 800, "sentiment": 18, "topic": "Green Tech", "trend": "up"},
    {"name": "Norway", "lat": 60.4, "lon": 8.4, "articles": 720, "sentiment": 20, "topic": "Energy", "trend": "up"},
    {"name": "Finland", "lat": 64.9, "lon": 26.2, "articles": 650, "sentiment": 22, "topic": "Education", "trend": "up"},
    {"name": "Singapore", "lat": 1.3, "lon": 103.8, "articles": 1100, "sentiment": 35, "topic": "Finance", "trend": "up"},
    {"name": "UAE", "lat": 23.4, "lon": 53.8, "articles": 1000, "sentiment": 15, "topic": "Tourism", "trend": "up"},
    {"name": "Poland", "lat": 52.0, "lon": 19.0, "articles": 780, "sentiment": 10, "topic": "Agriculture", "trend": "up"},
    {"name": "Netherlands", "lat": 52.1, "lon": 5.3, "articles": 700, "sentiment": 16, "topic": "Agriculture", "trend": "up"},
    {"name": "Switzerland", "lat": 46.8, "lon": 8.2, "articles": 650, "sentiment": 25, "topic": "Pharma", "trend": "up"},
]

import random
import re
import feedparser
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer

# Free RSS feeds (no key required). Mix of major Anglophone + international outlets.
RSS_FEEDS = [
    ("BBC",        "http://feeds.bbci.co.uk/news/world/rss.xml"),
    ("Guardian",   "https://www.theguardian.com/world/rss"),
    ("Al Jazeera", "https://www.aljazeera.com/xml/rss/all.xml"),
    ("NYT",        "https://rss.nytimes.com/services/xml/rss/nyt/World.xml"),
    ("DW",         "https://rss.dw.com/rdf/rss-en-world"),
    ("CNN",        "http://rss.cnn.com/rss/edition_world.rss"),
    ("NPR",        "https://feeds.npr.org/1004/rss.xml"),
    ("Reuters",    "https://feeds.reuters.com/Reuters/worldNews"),
    ("AP",         "https://feeds.apnews.com/rss/apf-topnews"),
    ("France24",   "https://www.france24.com/en/rss"),
]

# Aliases for matching articles to countries. Lowercased.
# Multi-word / punctuated aliases are matched as substrings; single-word aliases
# use \b boundaries to avoid spurious hits (e.g. "us" inside "bus").
COUNTRY_ALIASES = {
    "United States":  ["united states", "u.s.", "usa", "american", "americans", "washington d.c.", "biden", "trump"],
    "China":          ["china", "chinese", "beijing", "xi jinping"],
    "India":          ["india", "indian", "indians", "new delhi", "modi"],
    "Russia":         ["russia", "russian", "russians", "moscow", "kremlin", "putin"],
    "United Kingdom": ["united kingdom", "u.k.", "britain", "british", "london", "downing street"],
    "Germany":        ["germany", "german", "germans", "berlin"],
    "France":         ["france", "french", "paris", "macron"],
    "Brazil":         ["brazil", "brazilian", "sao paulo", "rio de janeiro", "lula"],
    "Japan":          ["japan", "japanese", "tokyo"],
    "Canada":         ["canada", "canadian", "canadians", "ottawa", "toronto", "trudeau"],
    "Australia":      ["australia", "australian", "australians", "sydney", "canberra"],
    "South Korea":    ["south korea", "south korean", "seoul"],
    "Mexico":         ["mexico", "mexican", "mexicans", "mexico city"],
    "Italy":          ["italy", "italian", "italians", "rome", "meloni"],
    "Spain":          ["spain", "spanish", "madrid"],
    "Indonesia":      ["indonesia", "indonesian", "jakarta"],
    "Saudi Arabia":   ["saudi arabia", "saudis", "riyadh", "mohammed bin salman"],
    "Turkey":         ["turkey", "turkish", "ankara", "istanbul", "erdogan"],
    "Nigeria":        ["nigeria", "nigerian", "lagos", "abuja"],
    "South Africa":   ["south africa", "south african", "johannesburg", "cape town"],
    "Argentina":      ["argentina", "argentinian", "argentine", "buenos aires", "milei"],
    "Egypt":          ["egypt", "egyptian", "cairo", "sisi"],
    "Thailand":       ["thailand", "thai", "bangkok"],
    "Vietnam":        ["vietnam", "vietnamese", "hanoi"],
    "Israel":         ["israel", "israeli", "tel aviv", "jerusalem", "netanyahu"],
    "Ukraine":        ["ukraine", "ukrainian", "ukrainians", "kyiv", "kiev", "zelensky"],
    "Iran":           ["iran", "iranian", "iranians", "tehran"],
    "Pakistan":       ["pakistan", "pakistani", "islamabad", "karachi"],
    "Philippines":    ["philippines", "filipino", "manila", "marcos"],
    "Ethiopia":       ["ethiopia", "ethiopian", "addis ababa", "tigray"],
    "Kenya":          ["kenya", "kenyan", "nairobi"],
    "Chile":          ["chile", "chilean", "santiago", "boric"],
    "Sweden":         ["sweden", "swedish", "stockholm"],
    "Norway":         ["norway", "norwegian", "oslo"],
    "Finland":        ["finland", "finnish", "helsinki"],
    "Singapore":      ["singapore", "singaporean"],
    "UAE":            ["uae", "united arab emirates", "dubai", "abu dhabi"],
    "Poland":         ["poland", "polish", "warsaw"],
    "Netherlands":    ["netherlands", "dutch", "amsterdam", "the hague"],
    "Switzerland":    ["switzerland", "swiss", "zurich", "geneva", "bern"],
}

_vader = SentimentIntensityAnalyzer()
_alias_patterns: Dict[str, list] = {}  # cached compiled regex per country
def _compile_alias_patterns():
    for country, aliases in COUNTRY_ALIASES.items():
        _alias_patterns[country] = []
        for a in aliases:
            # Substring for multi-word or punctuated forms; word-boundary regex for single words
            if " " in a or "." in a:
                _alias_patterns[country].append(("substr", a))
            else:
                _alias_patterns[country].append(("regex", re.compile(r"\b" + re.escape(a) + r"\b")))
_compile_alias_patterns()


async def _fetch_rss_feed(session: aiohttp.ClientSession, source: str, url: str) -> list:
    """Pull one RSS feed and return a flat list of dicts."""
    try:
        async with session.get(url, timeout=aiohttp.ClientTimeout(total=8)) as resp:
            if resp.status != 200:
                return []
            text = await resp.text()
    except Exception:
        return []
    try:
        feed = feedparser.parse(text)
    except Exception:
        return []
    out = []
    for entry in feed.entries[:80]:
        title = (entry.get("title") or "").strip()
        if not title:
            continue
        desc = (entry.get("summary") or entry.get("description") or "").strip()
        # Strip HTML from descriptions (feedparser sometimes leaves tags in summary)
        desc_clean = re.sub(r"<[^>]+>", " ", desc)[:400]
        out.append({
            "title": title[:200],
            "description": desc_clean,
            "link": entry.get("link") or "",
            "published": entry.get("published") or entry.get("updated") or "",
            "source": source,
        })
    return out


def _match_country_for(article: dict) -> list:
    """Return list of country names mentioned in the article (haystack = title+desc, lowercased)."""
    haystack = (article["title"] + " " + article["description"]).lower()
    hits = []
    for country, patterns in _alias_patterns.items():
        for kind, p in patterns:
            if kind == "substr":
                if p in haystack:
                    hits.append(country)
                    break
            else:
                if p.search(haystack):
                    hits.append(country)
                    break
    return hits


def _score_articles(articles: list) -> tuple:
    """Compute avg VADER compound score and prepare top-5 headlines payload."""
    if not articles:
        return None, []
    scores = []
    payload = []
    for art in articles[:25]:
        text = (art["title"] + " " + art["description"][:240]).strip()
        score = _vader.polarity_scores(text)["compound"]  # -1..+1
        scores.append(score)
        payload.append({
            "title": art["title"][:160],
            "url": art["link"],
            "source": art["source"],
            "tone": round(score * 10, 2),     # 0..10 scale for parity with prior shape
            "seendate": art["published"],
            "_score": score,
        })
    avg = sum(scores) / len(scores)
    # Top-5 by absolute deviation from neutral (most polarised first) β€” visually richer
    payload.sort(key=lambda p: -abs(p["_score"]))
    for p in payload:
        p.pop("_score", None)
    return avg, payload[:5]


async def _fetch_all_rss() -> Dict[str, dict]:
    """Pull all RSS feeds in parallel, score, group by country."""
    async with aiohttp.ClientSession() as session:
        tasks = [_fetch_rss_feed(session, src, url) for src, url in RSS_FEEDS]
        results = await asyncio.gather(*tasks, return_exceptions=True)
    all_articles = []
    for r in results:
        if isinstance(r, list):
            all_articles.extend(r)
    if not all_articles:
        return {}
    by_country: Dict[str, list] = {}
    for art in all_articles:
        for country in _match_country_for(art):
            by_country.setdefault(country, []).append(art)
    out: Dict[str, dict] = {}
    for country, arts in by_country.items():
        if not arts:
            continue
        avg_score, headlines = _score_articles(arts)
        if avg_score is None:
            continue
        out[country] = {
            "country": country,
            "articles": len(arts),
            "sentiment_norm": round(max(-1.0, min(1.0, avg_score)), 3),
            "tone": round(avg_score * 10, 2),
            "headlines": headlines,
        }
    return out


# ---- The functions below (_fetch_gdelt_country, _build_globe_response,
# _background_fetch_and_cache, news_globe) keep their names for backward
# compatibility but now call the RSS pipeline. ----


async def _fetch_gdelt_country(session: aiohttp.ClientSession, country_name: str, fips: str) -> Optional[dict]:
    """Pull last 24h of articles for a single country from GDELT DOC API.
    Returns None on any failure (timeout, non-200, parse error, or 429)."""
    url = "https://api.gdeltproject.org/api/v2/doc/doc"
    params = {
        "query": f"sourcecountry:{fips}",
        "mode": "artlist",
        "maxrecords": "75",
        "timespan": "1day",
        "format": "json",
        "sort": "datedesc",
    }
    try:
        async with session.get(url, params=params, timeout=aiohttp.ClientTimeout(total=12)) as resp:
            if resp.status != 200:
                return None
            text = await resp.text()
            if not text.strip():
                return None
            try:
                payload = json.loads(text)
            except json.JSONDecodeError:
                return None
        articles = payload.get("articles") or []
        if not articles:
            return None
        tones = []
        for a in articles:
            t = a.get("tone")
            if t is None:
                continue
            try:
                tones.append(float(t))
            except (TypeError, ValueError):
                continue
        avg_tone = (sum(tones) / len(tones)) if tones else 0.0
        sentiment_norm = max(-1.0, min(1.0, avg_tone / 10.0))
        headlines = []
        for a in articles[:5]:
            title = (a.get("title") or "").strip()
            if not title:
                continue
            try:
                tone_val = float(a.get("tone") or 0)
            except (TypeError, ValueError):
                tone_val = 0.0
            headlines.append({
                "title": title[:160],
                "url": a.get("url") or "",
                "source": a.get("domain") or "",
                "tone": round(tone_val, 2),
                "seendate": a.get("seendate") or "",
            })
        return {
            "country": country_name,
            "articles": len(articles),
            "sentiment_norm": round(sentiment_norm, 3),
            "tone": round(avg_tone, 2),
            "headlines": headlines,
        }
    except Exception:
        return None


async def _fetch_all_gdelt_sequential() -> Dict[str, dict]:
    """
    Fetch GDELT countries one-by-one with delays to avoid 429 rate limiting.
    Updates the cache after EACH successful country fetch so users see live data
    appear progressively rather than waiting for all 40 to finish.
    """
    out: Dict[str, dict] = {}
    cache_key = "news_globe_gdelt_v1"
    async with aiohttp.ClientSession() as session:
        for c in GLOBE_COUNTRIES:
            fips = GDELT_COUNTRY_CODES.get(c["name"])
            if not fips:
                continue
            # 1 retry per country with a short backoff
            for attempt in range(2):
                r = await _fetch_gdelt_country(session, c["name"], fips)
                if r:
                    out[c["name"]] = r
                    # Progressive cache update β€” bump the cached response so
                    # subsequent /api/news/globe hits include this country.
                    _cache_set(cache_key, _build_globe_response(out), ttl=1800)
                    break
                await asyncio.sleep(4 + attempt * 5)
            # polite gap between countries
            await asyncio.sleep(1)
    return out


def _synthesized_country(c: dict) -> dict:
    """Single-country synthesized data with small per-request variation."""
    variation = random.gauss(0, 3)
    sentiment = max(-80, min(80, c["sentiment"] + variation))
    article_var = int(random.gauss(0, c["articles"] * 0.03))
    articles = max(100, c["articles"] + article_var)
    return {
        "name": c["name"],
        "lat": c["lat"],
        "lon": c["lon"],
        "articles": articles,
        "sentiment": round(sentiment, 1),
        "topic": c["topic"],
        "trend": c["trend"],
        "headlines": [],
        "source": "synthesized",
    }


def _build_globe_response(gdelt: Dict[str, dict]) -> dict:
    data = []
    for c in GLOBE_COUNTRIES:
        live = gdelt.get(c["name"])
        if live:
            data.append({
                "name": c["name"],
                "lat": c["lat"],
                "lon": c["lon"],
                "articles": live["articles"],
                "sentiment": round(live["sentiment_norm"] * 80, 1),
                "topic": c["topic"],
                "trend": c["trend"],
                "headlines": live["headlines"],
                "source": "gdelt",
            })
        else:
            data.append(_synthesized_country(c))
    total = sum(d["articles"] for d in data)
    avg_sentiment = round(sum(d["sentiment"] for d in data) / len(data), 1) if data else 0
    most_active = max(data, key=lambda x: x["articles"])["name"] if data else "--"
    live_count = sum(1 for d in data if d["source"] == "gdelt")
    return {
        "countries": data,
        "total_articles": total,
        "avg_sentiment": avg_sentiment,
        "most_active": most_active,
        "live_count": live_count,
        "total_count": len(data),
        "source": "gdelt" if live_count > len(data) // 2 else ("mixed" if live_count > 0 else "synthesized"),
        "updated_at": datetime.utcnow().isoformat() + "Z",
    }


# Module-level flag β€” prevents duplicate background fetches running at once.
_globe_fetch_in_progress = False

async def _background_fetch_and_cache():
    """Fetch all RSS feeds in parallel, score with VADER, cache result.
    Typical total time: 5–12s. Far faster than the GDELT-per-country approach
    it replaces, so the foreground endpoint can also wait on it when cache misses.
    """
    global _globe_fetch_in_progress
    if _globe_fetch_in_progress:
        return
    _globe_fetch_in_progress = True
    try:
        print("globe: RSS aggregator fetch started")
        live = await _fetch_all_rss()
        result = _build_globe_response(live)
        _cache_set("news_globe_gdelt_v1", result, ttl=1800)
        print(f"globe: RSS aggregator done β€” {result['live_count']}/{result['total_count']} live")
    except Exception as e:
        print(f"globe: RSS aggregator failed: {e}")
    finally:
        _globe_fetch_in_progress = False


@app.on_event("startup")
async def _globe_startup_prewarm():
    """Kick off a one-time GDELT prewarm shortly after app boot."""
    async def _delayed():
        await asyncio.sleep(8)
        await _background_fetch_and_cache()
    asyncio.create_task(_delayed())


@app.get("/api/news/globe")
async def news_globe():
    """
    Returns global news sentiment for the 3D globe.

    Cache-first; on cold cache, blocks the request (up to ~15s) while pulling
    RSS feeds from BBC/Guardian/Al Jazeera/NYT/DW/CNN/NPR/Reuters/AP/France24,
    scoring with VADER, and matching to countries. Startup prewarm means most
    users hit a warm cache.
    """
    cache_key = "news_globe_gdelt_v1"
    cached = _cache_get(cache_key)
    if cached:
        return cached
    # Cold cache. If another request is already fetching, wait for it.
    if _globe_fetch_in_progress:
        loop = asyncio.get_event_loop()
        deadline = loop.time() + 15
        while _globe_fetch_in_progress and loop.time() < deadline:
            await asyncio.sleep(0.25)
        cached = _cache_get(cache_key)
        if cached:
            return cached
    # Do the fetch ourselves (will also populate cache as side effect).
    try:
        await asyncio.wait_for(_background_fetch_and_cache(), timeout=15)
    except asyncio.TimeoutError:
        pass
    cached = _cache_get(cache_key)
    if cached:
        return cached
    # Last resort β€” synthesized.
    return _build_globe_response({})


# ----------------------------------------------------------------------------
# Sibling bridge: World Digest's published digest (contract/news_exchange.md).
# Best-effort + cached; surfaces the sibling's clustered LLM narrative for the
# globe's "Digest view". Never 500s β€” a sibling outage degrades to an empty,
# stale-flagged payload so the globe keeps rendering.
# ----------------------------------------------------------------------------
DIGEST_JSON_URL = os.environ.get(
    "DIGEST_JSON_URL",
    "https://raw.githubusercontent.com/colesr/World.alive/main/public/digest.json",
)

@app.get("/api/news/world-digest")
def news_world_digest():
    cache_key = "world_digest_v1"
    cached = _cache_get(cache_key)
    if cached:
        return cached
    fallback = {
        "schema": "world-digest/news-exchange@1",
        "clusters": [],
        "narrative": "",
        "stale": True,
    }
    try:
        resp = requests.get(DIGEST_JSON_URL, timeout=8)
        if not resp.ok:
            return fallback
        data = resp.json()
        if not isinstance(data, dict) or "clusters" not in data:
            return fallback
        data.setdefault("stale", False)
        _cache_set(cache_key, data, ttl=1800)
        return data
    except Exception as e:
        print(f"world-digest bridge failed: {e}")
        return fallback

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=7860)