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

Evaluates the production Hybrid Retrieval Pipeline (BGE-M3 + FTS + RRF k=60)
with Student gpt-oss-120b (Cerebras) and Teacher gpt-4o-mini (OpenAI).

Metric Suites:
  - Component-Level: Context Precision, Context Recall, Context Relevance, MRR
  - End-to-End: Faithfulness, Answer Correctness (0.7/0.3), Answer Relevance
  - PGC Logic: Temporal Adherence, Numerical Rigor, Constraint Satisfaction
  - Indonesian Terminology Nuance (5-case sub-suite)
  - Operational: Latency, TPS
  - Youden's J calibration (golden_retrieval_cases.json)

Thesis train/test split:
  - Calibration set: golden_retrieval_cases.json → thresholds, MRR
  - Test set (100+ cases from synthetic + human-adversarial) → all RAGAS metrics
"""

from __future__ import annotations

import asyncio
import json
import csv
import os
import re
import sys
import time
import warnings
from collections import defaultdict
from datetime import datetime
from pathlib import Path
from typing import Any, Dict, List, Optional, Set, Tuple

import numpy as np

sys.path.insert(0, str(Path(__file__).resolve().parent.parent))

# Ensure UTF-8 stdout for Windows console compatibility (emojis, arrows in print)
if hasattr(sys.stdout, 'reconfigure'):
    sys.stdout.reconfigure(encoding='utf-8')
    sys.stderr.reconfigure(encoding='utf-8')

from dotenv import load_dotenv
load_dotenv(Path(__file__).resolve().parent.parent / ".env")

# =============================================================================
# CONFIGURATION
# =============================================================================

RESULTS_DIR = Path(__file__).resolve().parent.parent / "results"
FIXTURES_DIR = Path(__file__).resolve().parent.parent / "tests" / "fixtures"
DATA_DIR = Path(__file__).resolve().parent.parent / "data"

OPENAI_MODEL = "gpt-4o-mini"
# ragas 0.4.3 InstructorLLM uses max_tokens which GPT-5 series rejects;
# gpt-4o-mini supports max_tokens and is the correct ragas critic model
RAGAS_CRITIC_MODEL = "gpt-4o-mini"
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY", "")
CEREBRAS_API_KEY = os.getenv("CEREBRAS_API_KEY", "")

# RAGAS metrics batch size per critic call
CRITIC_BATCH_SIZE = 3
# Semaphores for async API throttling
CEREBRAS_SEMAPHORE = asyncio.Semaphore(3)
OPENAI_SEMAPHORE = asyncio.Semaphore(5)

# Retry parameters for RAGAS API calls
MAX_RETRIES = 3
RETRY_BASE_DELAY = 2.0
CRITIC_TIMEOUT = 60.0

# Numerical Rigor tolerance
NUMERICAL_TOLERANCE = 0.5
NUMERICAL_PARAM_CUES = {
    "temperature": ["suhu", "temperature", "temp", "°c", "celsius", "fahrenheit", "kelvin"],
    "humidity": ["kelembaban", "humidity", "rh", "relative humidity"],
    "light": ["cahaya", "light", "lux", "lumen", "ppfd"],
}

# Youden's J calibration parameters
CALIB_THRESHOLD = 0.20
CALIB_COUNT = 20
HISTOGRAM_BINS = 15
HISTOGRAM_LOW = 0.40
HISTOGRAM_HIGH = 0.82
CANDIDATE_RANGE = range(45, 81)

# =============================================================================
# RETRY HELPER FOR RAGAS METRICS
# =============================================================================

async def _retry_ragas_call(metric_call, metric_name, timeout=None, max_retries=None):
    """
    Retry a RAGAS metric async call with exponential backoff and timeout.

    Args:
        metric_call: Async callable (e.g., lambda: f_metric.ascore(...))
        metric_name: String for logging
        timeout: Seconds per attempt (default: CRITIC_TIMEOUT)
        max_retries: Max retry attempts (default: MAX_RETRIES)

    Returns:
        float score

    Raises:
        Last exception if all retries fail.
    """
    timeout = timeout if timeout is not None else CRITIC_TIMEOUT
    max_retries = max_retries if max_retries is not None else MAX_RETRIES
    last_exception = None
    for attempt in range(1, max_retries + 1):
        try:
            score = await asyncio.wait_for(metric_call(), timeout=timeout)
            return float(score)
        except asyncio.TimeoutError:
            print(f"[RAGAS] {metric_name} attempt {attempt}/{max_retries} timed out ({timeout}s)")
            last_exception = asyncio.TimeoutError("timeout")
            if attempt < max_retries:
                delay = RETRY_BASE_DELAY * (2 ** (attempt - 1))
                print(f"[RAGAS] Retrying {metric_name} in {delay:.1f}s...")
                await asyncio.sleep(delay)
        except Exception as e:
            estr = str(e)
            print(f"[RAGAS] {metric_name} attempt {attempt}/{max_retries} failed: {estr[:120]}")
            last_exception = e
            if any(kw in estr.lower() for kw in ("api_key", "authorization", "invalid_api")):
                raise
            if attempt < max_retries:
                delay = RETRY_BASE_DELAY * (2 ** (attempt - 1))
                print(f"[RAGAS] Retrying {metric_name} in {delay:.1f}s...")
                await asyncio.sleep(delay)
    raise last_exception  # type: ignore[misc]


def _categorize_ragas_error(e: Exception) -> str:
    """Categorize a RAGAS exception into a short error string."""
    estr = str(e).lower()
    if "max_tokens" in estr or "length" in estr or "incomplete" in estr:
        return "error: max_tokens"
    if "timeout" in estr:
        return "error: timeout"
    if "rate" in estr and "limit" in estr:
        return "error: rate_limit"
    return f"error: {e}"


# =============================================================================
# PLANT FAMILY MAPPING (for Graded Relevance Scoring)
# =============================================================================

PLANT_ALIAS_MAP = {
    "bok_choy": "pak_choy",
    "amaranth": "spinach_amaranth",
    "spinach": "spinach_amaranth",
}

PLANT_FAMILY_MAP = {
    "lettuce": "Asteraceae",
    "pak_choy": "Brassicaceae",
    "spinach_amaranth": "Amaranthaceae",
    "mustard_greens": "Brassicaceae",
    "kailan": "Brassicaceae",
    "chinese_cabbage": "Brassicaceae",
    "water_spinach": "Convolvulaceae",
    "celery": "Apiaceae",
    "green_onion": "Amaryllidaceae",
    "chili_pepper": "Solanaceae",
    "tomato": "Solanaceae",
    "melon": "Cucurbitaceae",
    "watermelon": "Cucurbitaceae",
    "cucumber": "Cucurbitaceae",
    "eggplant": "Solanaceae",
    "pumpkin": "Cucurbitaceae",
    "cauliflower": "Brassicaceae",
    "shallot": "Amaryllidaceae",
    "papaya": "Caricaceae",
    "marigold": "Asteraceae",
    "cabbage": "Brassicaceae",
}


def _resolve_plant_id(plant_id: str) -> str:
    return PLANT_ALIAS_MAP.get(plant_id, plant_id)


def _get_plant_family(plant_id: str) -> str:
    resolved = _resolve_plant_id(plant_id)
    return PLANT_FAMILY_MAP.get(resolved, "")


def _get_source_category(source: str) -> str:
    src_lower = source.lower()
    if "sop" in src_lower:
        return "sop"
    if "handbook" in src_lower:
        return "handbook"
    if "juknis" in src_lower:
        return "juknis"
    if "buku" in src_lower or "book" in src_lower:
        return "buku"
    return "general"


def _build_source_plant_map(cases: list) -> dict:
    mapping = {}
    for case in cases:
        source = case.get("expected_source", "").strip()
        plant = case.get("expected_plant")
        if source and plant:
            mapping[source] = plant
    return mapping


# =============================================================================
# IMPORTS (with graceful fallbacks)
# =============================================================================

HAS_RAGAS = False
HAS_OPENAI = False

try:
    from ragas.llms import llm_factory
    from ragas.metrics.collections import (
        Faithfulness,
        ContextPrecision,
        ContextRecall,
        AnswerCorrectness,
    )
    try:
        from ragas.metrics.collections import ResponseRelevancy as _AnswerRelevanceMetric
        ResponseRelevancy = _AnswerRelevanceMetric  # expose for monkeypatching / tests
    except ImportError:
        from ragas.metrics.collections import AnswerRelevancy as _AnswerRelevanceMetric
        AnswerRelevancy = _AnswerRelevanceMetric  # expose for monkeypatching / tests

    _RAGAS_METRIC_CLASSES = [
        Faithfulness,
        ContextPrecision,
        ContextRecall,
        AnswerCorrectness,
        _AnswerRelevanceMetric,
    ]
    HAS_RAGAS_METRICS = True
    HAS_RAGAS = True
except ImportError:
    HAS_RAGAS = False
    warnings.warn("RAGAS or langchain-openai not installed. Install with: pip install ragas langchain-openai")

try:
    from openai import OpenAI as OpenAIClient
    HAS_OPENAI = True
except ImportError:
    warnings.warn("openai not installed. Install with: pip install openai")

# =============================================================================
# PROJECT IMPORTS
# =============================================================================

from app.ai_engine import generate_context_aware_response, call_llm_with_history
from app.retrieval_eval import load_golden_retrieval_cases


# =============================================================================
# LOGGING OPENAI WRAPPER (Critic Reasoning Capture)
# =============================================================================

class CriticReasoningLogger:
    """Logs every gpt-4o-mini critic call to a JSONL file for auditing.

    If a score is low, the user can inspect the reasoning to determine whether
    the Teacher (mini) misunderstood the agronomic context.
    """

    def __init__(self, log_path: Path):
        self.log_path = log_path
        self.log_path.parent.mkdir(parents=True, exist_ok=True)
        self._entries: List[dict] = []

    def log(self, entry: dict):
        entry["timestamp"] = datetime.utcnow().isoformat() + "Z"
        self._entries.append(entry)
        with open(self.log_path, "a", encoding="utf-8") as f:
            f.write(json.dumps(entry) + "\n")

    def flush(self):
        pass


class LoggingOpenAIClient(OpenAIClient):
    """OpenAI client subclass that logs every chat completion for auditing.

    Inherits from openai.OpenAI directly so llm_factory recognizes the type.
    Overrides chat.completions.create to log requests/responses.
    """

    def __init__(self, logger: CriticReasoningLogger, **kwargs):
        super().__init__(**kwargs)
        self._critic_logger = logger

    @property
    def chat(self):
        return _LoggingChatCompletions(super().chat.completions, self._critic_logger)


class _LoggingChatCompletions:
    """Wraps chat.completions to log requests/responses.

    Preserves the client.chat.completions.create() chain that Instructor expects.
    """

    def __init__(self, inner, logger: CriticReasoningLogger):
        self._inner = inner
        self._critic_logger = logger
        self.completions = self  # client.chat.completions.create() chain

    def create(self, *args, **kwargs):
        response = self._inner.create(*args, **kwargs)
        self._critic_logger.log({
            "event": "critic_call",
            "model": kwargs.get("model", ""),
            "messages_preview": [str(m)[:200] for m in kwargs.get("messages", [])],
            "response_preview": str(response.choices[0].message.content)[:500] if response.choices else "",
            "usage": response.usage.__dict__ if response.usage else {},
        })
        return response


# =============================================================================
# RANGE-AWARE NUMERICAL RIGOR CHECKER
# =============================================================================


def _requested_numeric_params(query: str) -> Set[str]:
    query_lower = query.lower()
    requested = {
        param
        for param, cues in NUMERICAL_PARAM_CUES.items()
        if any(cue in query_lower for cue in cues)
    }
    return requested or {"temperature", "humidity", "light"}


class NumericalRigorChecker:
    """Strict Data Rule: checks if numerical values in answer match ground truth.

    Pass condition (Agronomic Envelope Adherence):
      - Answer value is within ±0.5 of ground truth optimal value, OR
      - Answer value is within the ground truth safety range [min, max]

    Supports both range answers ("22 to 24 degrees") and single values ("23.5°C").
    """

    _TEMP_RANGE = re.compile(
        r'(\d+(?:\.\d+)?)\s*(?:[-\u2013tohingga]+)\s*(\d+(?:\.\d+)?)\s*(?:°C|derajat\s+celsius|degrees?\s*celsius)',
        re.IGNORECASE,
    )
    _TEMP_SINGLE = re.compile(
        r'(\d+(?:\.\d+)?)\s*(?:°C|derajat\s+celsius|degrees?\s*celsius)',
        re.IGNORECASE,
    )
    _RH_RANGE = re.compile(
        r'(\d+(?:\.\d+)?)\s*(?:[-\u2013tohingga]+)\s*(\d+(?:\.\d+)?)\s*(?:%|persen|percent)',
        re.IGNORECASE,
    )
    _RH_SINGLE = re.compile(
        r'(\d+(?:\.\d+)?)\s*(?:%|persen|percent)',
        re.IGNORECASE,
    )
    _LUX_RANGE = re.compile(
        r'([\d,\s\u00a0\u202f\u2009]+)\s*(?:[-\u2013tohingga]+)\s*([\d,\s\u00a0\u202f\u2009]+)\s*(?:lux|lux|lumen)',
        re.IGNORECASE,
    )
    _LUX_SINGLE = re.compile(
        r'([\d,\s\u00a0\u202f\u2009]+)\s*(?:lux|lux|lumen)',
        re.IGNORECASE,
    )

    @staticmethod
    def _parse_number(text: str) -> Optional[float]:
        normalized = (
            text.replace(",", "")
            .replace(" ", "")
            .replace("\u00a0", "")
            .replace("\u202f", "")
            .replace("\u2009", "")
        )
        if not normalized:
            return None
        try:
            return float(normalized)
        except ValueError:
            return None

    @staticmethod
    def _extract_all_params(
        answer: str, range_re: re.Pattern, single_re: re.Pattern,
    ) -> List[Tuple[str, List[float]]]:
        """Extract ALL param values found. Returns list of (type, values) tuples."""
        results = []
        for match in range_re.finditer(answer):
            lo, hi = match.group(1), match.group(2)
            lo_val = NumericalRigorChecker._parse_number(lo)
            hi_val = NumericalRigorChecker._parse_number(hi)
            if lo_val is not None and hi_val is not None:
                results.append(("range", [lo_val, hi_val]))
        for match in single_re.finditer(answer):
            val = match.group(1)
            parsed = NumericalRigorChecker._parse_number(val)
            if parsed is not None:
                results.append(("single", [parsed]))
        return results

    @classmethod
    def _check_value(cls, ans_val: float, label: str, gt_value: Optional[float], gt_min: Optional[float], gt_max: Optional[float]) -> Optional[Dict]:
        """Check one answer value against ground truth. Returns detail dict on PASS, None on fail."""
        # Check ±0.5 of optimal value
        if gt_value is not None and abs(ans_val - gt_value) <= NUMERICAL_TOLERANCE:
            return {"label": label, "ans_val": ans_val, "gt_val": gt_value, "method": "optimal_tolerance", "pass": True}
        # Check if within safety range
        if gt_min is not None and gt_max is not None and gt_min <= ans_val <= gt_max:
            return {"label": label, "ans_val": ans_val, "range": f"[{gt_min}, {gt_max}]", "method": "safety_range", "pass": True}
        return None

    @classmethod
    def evaluate_answer(
        cls,
        answer: str,
        ground_truth: Dict[str, Dict],
        requested_params: Optional[Set[str]] = None,
    ) -> Dict:
        """Evaluate all numerical parameters in the answer.

        Scans ALL temperature/humidity/light values in the answer.
        PASS if ANY value satisfies Agronomic Envelope Adherence:
          - Within ±0.5 of ground truth optimal, OR
          - Within ground truth safety range [min, max]

        ground_truth schema: {
            "temperature": {"value": 20.0, "min": 18.0, "max": 22.0},
            "humidity": {"value": 85.0, "min": 75.0, "max": 90.0},
            "light": {"value": 15000, "min": 12000, "max": 20000},
        }
        """
        results = {}
        requested = set(ground_truth.keys()) if requested_params is None else set(requested_params)
        all_pass = True

        param_configs = [
            ("temperature", cls._TEMP_RANGE, cls._TEMP_SINGLE),
            ("humidity", cls._RH_RANGE, cls._RH_SINGLE),
            ("light", cls._LUX_RANGE, cls._LUX_SINGLE),
        ]

        for param, range_re, single_re in param_configs:
            if param not in requested:
                continue
            gt = ground_truth.get(param, {})
            gt_value = gt.get("value")
            gt_min = gt.get("min")
            gt_max = gt.get("max")

            if gt_value is None and gt_min is None:
                continue  # No ground truth for this param, skip

            extracted = cls._extract_all_params(answer, range_re, single_re)
            if not extracted:
                results[param] = {"status": "NOT_FOUND", "reason": f"No {param} value found in answer"}
                all_pass = False
                continue

            # Try each extracted value; PASS if ANY matches
            any_pass = False
            passed_details = []
            for val_type, values in extracted:
                if val_type == "single":
                    detail = cls._check_value(values[0], param, gt_value, gt_min, gt_max)
                    if detail:
                        any_pass = True
                        passed_details.append(detail)
                elif val_type == "range":
                    ans_min, ans_max = values
                    # Check if ground truth optimal falls within answer range
                    if gt_value is not None and ans_min <= gt_value <= ans_max:
                        any_pass = True
                        passed_details.append({"method": "optimal_in_range", "ans_range": f"[{ans_min}, {ans_max}]"})
                    # Check if midpoint falls within answer range
                    elif gt_min is not None and gt_max is not None:
                        gt_mid = (gt_min + gt_max) / 2.0
                        if ans_min <= gt_mid <= ans_max:
                            any_pass = True
                            passed_details.append({"method": "midpoint_in_range", "ans_range": f"[{ans_min}, {ans_max}]"})

            if any_pass:
                results[param] = {"status": "PASS", "details": passed_details}
            else:
                results[param] = {"status": "FAIL", "extracted_values": extracted, "gt": gt}
                all_pass = False

        return {
            "applicable": bool(requested),
            "requested_params": sorted(requested),
            "param_results": results,
            "overall_pass": all_pass,
            "factual_score_override": 1.0 if all_pass else 0.0,
        }


# =============================================================================
# TEMPORAL ADHERENCE CHECKER
# =============================================================================

class TemporalAdherenceChecker:
    """Binary check: does the answer reference the correct day/night phase?

    Uses the resolved_phase from _evaluation_metadata:
    - "day": answer must reference "siklus siang" (ID) or "day" (EN)
    - "night": answer must reference "siklus malam" (ID) or "night" (EN)
    - None or "general": not applicable, always pass
    """

    _DAY_PATTERNS = re.compile(
        r'\bsiklus\s+siang\b|\bfase\s+siang\b|\bday\s+schedule\b|\bdaytime\b|\bday\s+cycle\b|\bsiang\s*hari\b',
        re.IGNORECASE,
    )
    _NIGHT_PATTERNS = re.compile(
        r'\bsiklus\s+malam\b|\bfase\s+malam\b|\bnight\s+schedule\b|\bnighttime\b|\bnight\s+cycle\b|\bmalam\s*hari\b',
        re.IGNORECASE,
    )

    @classmethod
    def check(cls, answer: str, resolved_phase: Optional[str]) -> Dict:
        if resolved_phase is None or resolved_phase == "general":
            return {"applicable": False, "status": "N/A", "pass": True}

        # If answer is a no-data disclaimer, skip temporal check
        no_data_indicators = ["not currently online", "no live chamber data", "tidak ada data sensor",
                              "chamber is not currently", "tidak terhubung", "tidak online"]
        if any(indicator in answer.lower() for indicator in no_data_indicators):
            return {"applicable": False, "status": "NO_LIVE_DATA", "pass": True}

        has_day = bool(cls._DAY_PATTERNS.search(answer))
        has_night = bool(cls._NIGHT_PATTERNS.search(answer))

        if resolved_phase == "day":
            passed = has_day
            expected = "day/siklus siang"
            found = "day" if has_day else "none"
        elif resolved_phase == "night":
            passed = has_night
            expected = "night/siklus malam"
            found = "night" if has_night else "none"
        else:
            passed = True
            expected = resolved_phase
            found = "unknown"

        return {
            "applicable": True,
            "resolved_phase": resolved_phase,
            "expected": expected,
            "found": found,
            "pass": passed,
        }


# =============================================================================
# CONSTRAINT SATISFACTION CHECKER
# =============================================================================

class ConstraintSatisfactionChecker:
    """Three-state context-aware constraint checker.

    State A — Qualitative/SOP mode (use_structured_params=False):
      Forbidden terms are ALLOWED because the AI is quoting verified documents.
      Always passes.

    State B — Guarded mode (use_structured_params=True, no explicit request):
      Forbidden terms are HALLUCINATIONS unless they appear only within the
      system-approved breadcrumb text (see BREADCRUMB_PATTERNS).  Fails if
      forbidden terms found outside breadcrumb.

    State C — Explicit request mode (use_structured_params=True, user asked):
      Forbidden terms are ALLOWED but the answer MUST contain the bifurcation
      warning (⚠️ + 'di luar kendali otomatis' / 'outside automatic control').
      Passes if warning present, fails if missing.
    """

    FORBIDDEN_TERMS = ["ph", "ec", "co2", "co\u2082", "o2", "o\u2082",
                       "fertilizer", "fertiliser", "pupuk",
                       "spacing", "jarak tanam", "ppm", "conductivity", "tds"]

    EXPLICIT_REQUEST_TERMS = ["ph", "ec", "nutrisi", "nutrient",
                              "pupuk", "fertilizer", "conductivity",
                              "ppm", "co2", "co\u2082", "o2", "o\u2082",
                              "karbon dioksida", "oksigen",
                              "kadar nutrisi", "ph air", "ec larutan",
                              "soil", "tanah", "larutan"]

    # System-approved breadcrumb text — forbidden terms inside this text are
    # intentionally placed by Rule 9 and do NOT count as hallucinations.
    BREADCRUMB_EXCERPTS = [
        # Indonesian breadcrumb — full text variations
        "seperti ph, ec, co\u2082, atau o\u2082",
        "seperti ph, ec, co2, atau o2",
        "seperti ph, ec, co\u2082, dan o\u2082",
        "(seperti ph, ec",
        "(seperti ph, ec, co",
        "panduan manual untuk nutrisi (seperti ph, ec",
        "panduan manual untuk nutrisi seperti ph, ec",
        "parameter terverifikasi pgc hanya mencakup suhu, kelembaban, dan cahaya",
        # English breadcrumb — full text variations
        "such as ph, ec, co\u2082, or o\u2082",
        "such as ph, ec, co2, or o2",
        "(such as ph, ec",
        "(such as ph, ec, co",
        "manual guidance for nutrition (such as ph, ec",
        "manual guidance for nutrition such as ph, ec",
        "pgc verified parameters cover temperature, humidity, and light only",
        # Extra catch-alls for leftover fragments
        "seperti ph, ec, co", "such as ph, ec, co",
        "ph, ec, atau o", "ph, ec, or o",
        "ph, ec, dan o", "ph, ec, dan",
        "ec, co\u2082, atau", "ec, co2, atau",
        "ec, co\u2082, dan", "ec, co2, dan",
        "ec, co\u2082, or", "ec, co2, or",
    ]

    # Patterns that indicate the AI is correctly stating a parameter's
    # unavailability rather than presenting it as a value.
    NOT_AVAILABLE_PATTERNS = [
        "tidak tersedia", "not available", "not found", "tidak ditemukan",
        "tidak ada di dokumen", "not in my document",
        "hanya menyimpan data suhu", "only stores temperature",
        "hanya menyimpan suhu, kelembaban",
        "only temp", "only temperature, humidity",
    ]

    BIFURCATION_WARNING_EXCERPTS = [
        "di luar kendali otomatis",
        "outside automatic control",
        "tidak dikendalikan oleh pgc",
        "not controlled by pgc",
        "panduan manual",
        "manual guidance",
        "bersifat panduan manual",
        "manual guidance only",
    ]

    # Forbidden terms that require word-boundary matching (avoid false positives
    # like "ph" inside "aphanadermatum" or "ec" inside "Perkecambahan").
    _FORBIDDEN_WORD_RE = None

    @classmethod
    def _compile_re(cls):
        if cls._FORBIDDEN_WORD_RE is not None:
            return cls._FORBIDDEN_WORD_RE
        # Build patterns — short terms (<4 chars) require word boundaries
        patterns = []
        for term in cls.FORBIDDEN_TERMS:
            if len(term) <= 3:
                patterns.append(r'\b' + re.escape(term) + r'\b')
            else:
                patterns.append(re.escape(term))
        cls._FORBIDDEN_WORD_RE = re.compile('|'.join(patterns), re.IGNORECASE)
        return cls._FORBIDDEN_WORD_RE

    @classmethod
    def _find_forbidden_terms(cls, text: str) -> list:
        """Find forbidden terms using word-boundary-aware matching."""
        regex = cls._compile_re()
        found = set()
        for m in regex.finditer(text):
            found.add(m.group().lower())
        return sorted(found)

    @classmethod
    def _strip_breadcrumb(cls, text: str) -> str:
        """Remove system-approved breadcrumb text so forbidden terms inside it
        are not counted as hallucinations."""
        for excerpt in cls.BREADCRUMB_EXCERPTS:
            text = text.replace(excerpt, "")
        return text

    @classmethod
    def _has_warning(cls, text: str) -> bool:
        lower = text.lower()
        return any(w in lower for w in cls.BIFURCATION_WARNING_EXCERPTS)

    @classmethod
    def _also_has_doc_citation(cls, answer: str) -> bool:
        """Check if answer contains verified document citations (📖)."""
        return bool(re.search(r'📖', answer))

    @classmethod
    def check(cls, answer: str, query: str, use_structured_params: bool) -> Dict:
        answer_lower = answer.lower()

        # State A: Qualitative/SOP — quoting documents is allowed
        if not use_structured_params:
            return {
                "applicable": True,
                "pass": True,
                "mode": "qualitative_quoted",
                "found_terms": [],
            }

        # Strip the system-approved breadcrumb before checking
        check_text = cls._strip_breadcrumb(answer_lower)

        found_terms = cls._find_forbidden_terms(check_text)

        # No forbidden terms outside breadcrumb → clean pass
        if not found_terms:
            return {
                "applicable": True,
                "pass": True,
                "mode": "guarded",
                "found_terms": [],
            }

        # If answer has verified document citations (📖), forbidden terms
        # are from the quoted document, not hallucinations.
        if cls._also_has_doc_citation(answer):
            return {
                "applicable": True,
                "pass": True,
                "mode": "document_quoted",
                "found_terms": found_terms,
            }

        # If all found forbidden terms appear only in an unavailability
        # disclaimer context (e.g., "pH is not available in documents"),
        # the answer is correctly acknowledging its limitations.
        if any(p in answer_lower for p in cls.NOT_AVAILABLE_PATTERNS):
            return {
                "applicable": True,
                "pass": True,
                "mode": "unavailable_disclaimed",
                "found_terms": found_terms,
            }

        # State C: User explicitly asked for out-of-scope metrics
        query_lower = query.lower()
        explicit_request = any(term in query_lower for term in cls.EXPLICIT_REQUEST_TERMS)

        if explicit_request:
            if cls._has_warning(answer_lower):
                return {
                    "applicable": True,
                    "pass": True,
                    "mode": "explicit_request_warned",
                    "found_terms": found_terms,
                }

            return {
                "applicable": True,
                "pass": False,
                "mode": "explicit_request_unwarned",
                "found_terms": found_terms,
                "reason": "User asked for out-of-scope params but AI omitted mandatory bifurcation warning",
            }

        # State B: AI hallucinated unprompted (outside breadcrumb)
        return {
            "applicable": True,
            "pass": False,
            "mode": "unprompted_hallucination",
            "found_terms": found_terms,
            "reason": "AI mentioned forbidden terms without explicit user request",
        }


# =============================================================================
# CITATION ACCURACY CHECKER (Emoji Audit)
# =============================================================================

class CitationAccuracyChecker:
    """Verifies correct emoji usage (📚, 📖, ⚠️) based on retrieval tier metadata.

    Rules:
    - If use_structured_params=True AND answer cites temp/humidity/light → expect 📚
    - If verified chunks present and alias filter passes → expect 📖 for each cited source
    - If no verified chunks or plant not in DB → expect ⚠️
    """

    _EMOJI_PATTERNS = {
        "db": re.compile(r'📚'),
        "doc": re.compile(r'📖'),
        "ai": re.compile(r'⚠️'),
    }

    @classmethod
    def check(cls, answer: str, metadata: Dict) -> Dict:
        has_db = bool(cls._EMOJI_PATTERNS["db"].search(answer))
        has_doc = bool(cls._EMOJI_PATTERNS["doc"].search(answer))
        has_ai = bool(cls._EMOJI_PATTERNS["ai"].search(answer))

        use_structured = metadata.get("use_structured_params", False)
        chunks = metadata.get("retrieved_chunks", [])
        aliases = metadata.get("plant_aliases")

        # Determine expected emojis
        expected = set()
        if use_structured:
            expected.add("📚")
        if chunks:
            # Check if any chunk is verified AND passes alias filter
            from app.vector_store import _is_verified, _chunk_mentions_plant
            verified_docs = any(
                _is_verified(c) and (aliases is None or _chunk_mentions_plant(c, aliases))
                for c in chunks
            )
            if verified_docs:
                expected.add("📖")
        if not expected or not (use_structured or any(_is_verified(c) for c in chunks)):
            # No verified sources → must have ⚠️
            pass  # ⚠️ is always acceptable

        found = set()
        if has_db:
            found.add("📚")
        if has_doc:
            found.add("📖")
        if has_ai:
            found.add("⚠️")

        issues = []
        # RULE: ⚠️ cannot coexist with 📚 (verified DB)
        if has_db and has_ai:
            issues.append("⚠️ mixed with 📚 — AI estimate cannot appear alongside verified database data")
        # Warning: if 📚 is found but shouldn't be
        if not use_structured and has_db:
            issues.append("Unexpected 📚 (use_structured_params=False)")
        # Warning: if 📖 is found but no verified docs
        if has_doc and not any(_is_verified(c) for c in chunks):
            issues.append("Unexpected 📖 (no verified chunks)")
        # Warning: if no ⚠️ but answer uses AI content
        if not has_ai and not use_structured and not chunks:
            issues.append("Missing ⚠️ (AI-generated content without disclaimer)")

        return {
            "found_emojis": list(found),
            "expected_emojis": list(expected),
            "issues": issues,
            "pass": len(issues) == 0,
        }


# =============================================================================
# INDONESIAN TERMINOLOGY NUANCE CHECKER
# =============================================================================

class TerminologyNuanceChecker:
    """5-case sub-suite for Indonesian agricultural terminology accuracy."""

    @staticmethod
    def check_kecambah_tunas(answer: str) -> Dict:
        """Case 1: Must distinguish k生长发育 from tunas correctly."""
        has_mungbean = any(t in answer.lower() for t in ["mung bean sprouts", "mung bean", "kacang hijau", "kecambah", "toge", "tauge"])
        has_tunas_as_plant = bool(re.search(r'(?:📚|📖)\s*S(?:ource|umber).*tunas', answer, re.IGNORECASE))
        has_vegetative = any(t in answer.lower() for t in ["vegetatif", "vegetative", "tunas"])
        return {
            "case": "kecambah_vs_tunas",
            "mungbean_identified": has_mungbean,
            "tunas_not_misidentified_as_plant": not has_tunas_as_plant,
            "tunas_as_vegetative": has_vegetative,
            "pass": has_mungbean and not has_tunas_as_plant and has_vegetative,
        }

    @staticmethod
    def check_layu_fusarium(answer: str) -> Dict:
        """Case 2: Must distinguish Fusarium wilt from drought wilt."""
        has_fusarium = "fusarium" in answer.lower()
        has_diagnosis = any(t in answer.lower() for t in ["pembuluh", "vascular", "bercak", "layu"])
        return {
            "case": "layu_fusarium_vs_kekeringan",
            "fusarium_mentioned": has_fusarium,
            "has_diagnostic_content": has_diagnosis,
            "pass": has_fusarium,
        }

    @staticmethod
    def check_busuk_akar_pythium(answer: str) -> Dict:
        """Case 3: Must explain busuk akar (symptom) vs Pythium (pathogen)."""
        has_pythium = "pythium" in answer.lower()
        has_hierarchy = any(t in answer.lower() for t in ["disebabkan", "caused by", "patogen", "pathogen", "jamur air", "water mold"])
        return {
            "case": "busuk_akar_vs_pythium",
            "pythium_mentioned": has_pythium,
            "has_hierarchy_explanation": has_hierarchy,
            "pass": has_pythium and has_hierarchy,
        }

    @staticmethod
    def check_kacang_hijau(answer: str) -> Dict:
        """Case 4: Must resolve kacang hijau to mung bean (not green beans/buncis)."""
        has_mung = "mung" in answer.lower() or "kacang hijau" in answer.lower()
        has_wrong = any(t in answer.lower() for t in ["buncis", "green bean", "snap bean", "string bean"])
        return {
            "case": "kacang_hijau",
            "correct_plant": has_mung,
            "wrong_plant": has_wrong,
            "pass": has_mung and not has_wrong,
        }

    @staticmethod
    def check_baginda_f1(answer: str) -> Dict:
        """Case 5: Must resolve Baginda F1 to watermelon."""
        has_watermelon = any(t in answer.lower() for t in ["watermelon", "semangka"])
        has_parameters = any(t in answer.lower() for t in ["°c", "derajat", "celsius", "lux", "%", "persen", "kelembaban"])
        return {
            "case": "baginda_f1",
            "watermelon_resolved": has_watermelon,
            "has_parameters": has_parameters,
            "pass": has_watermelon and has_parameters,
        }

    @staticmethod
    def evaluate_all(answer: str) -> Dict:
        results = {
            "kecambah_vs_tunas": TerminologyNuanceChecker.check_kecambah_tunas(answer),
            "layu_fusarium_vs_kekeringan": TerminologyNuanceChecker.check_layu_fusarium(answer),
            "busuk_akar_vs_pythium": TerminologyNuanceChecker.check_busuk_akar_pythium(answer),
            "kacang_hijau": TerminologyNuanceChecker.check_kacang_hijau(answer),
            "baginda_f1": TerminologyNuanceChecker.check_baginda_f1(answer),
        }
        passed = sum(1 for r in results.values() if r["pass"])
        total = len(results)
        return {
            "results": results,
            "total": total,
            "passed": passed,
            "accuracy": passed / total if total > 0 else 0,
        }


# =============================================================================
# GRADED RELEVANCE COMPUTATION (Phase 1)
# =============================================================================


def compute_relevance_grade(
    chunk_source: str,
    content: str,
    expected_source: str,
    expected_plant: str,
    expected_keywords: list,
    source_plant_map: dict,
) -> float:
    """Compute graded relevance (0.0, 0.25, 0.5, 1.0) for a retrieved chunk."""
    source_match = chunk_source == expected_source.strip()
    keyword_match = any(kw.lower() in content for kw in expected_keywords) if expected_keywords else False

    if source_match and keyword_match:
        return 1.0

    if not keyword_match:
        return 0.0

    expected_family = _get_plant_family(expected_plant) if expected_plant else ""
    if expected_family:
        chunk_plant = source_plant_map.get(chunk_source, "")
        chunk_family = _get_plant_family(chunk_plant) if chunk_plant else ""
        if chunk_family and chunk_family == expected_family:
            return 0.5

    expected_cat = _get_source_category(expected_source)
    chunk_cat = _get_source_category(chunk_source)
    if expected_cat != "general" and chunk_cat != "general" and expected_cat == chunk_cat:
        return 0.25

    return 0.0


# =============================================================================
# RETRIEVAL SPECIFICITY CLASSIFICATION (Phase 4)
# =============================================================================


def classify_top1_retrieval(
    chunk: dict,
    case: dict,
    source_plant_map: dict,
) -> str:
    """Classify the top-1 RRF result into Exact/Family/Topic/Irrelevant match."""
    chunk_source = chunk.get("source", "").strip()
    content = (chunk.get("content") or "").lower()
    expected_source = case.get("expected_source", "").strip()
    expected_keywords = case.get("expected_content_keywords") or []
    expected_plant = case.get("expected_plant", "")

    source_match = chunk_source == expected_source
    keyword_match = any(kw.lower() in content for kw in expected_keywords) if expected_keywords else False

    if source_match and keyword_match:
        return "Exact Match"

    if keyword_match:
        expected_family = _get_plant_family(expected_plant) if expected_plant else ""
        if expected_family:
            chunk_plant = source_plant_map.get(chunk_source, "")
            chunk_family = _get_plant_family(chunk_plant) if chunk_plant else ""
            if chunk_family and chunk_family == expected_family:
                return "Family Match"
        return "Topic Match"

    return "Irrelevant"


# =============================================================================
# SYSTEM PRECISION EVALUATOR (Phase 3)
# =============================================================================


class SystemPrecisionEvaluator:
    """End-to-end correctness audit of the full pipeline."""

    DIMENSIONS = ["numerical_rigor", "citation_accuracy", "constraint_satisfaction"]

    def __init__(self):
        self.results: List[Dict] = []

    async def evaluate_case(self, case: Dict) -> Dict:
        query = case["query"]
        case_id = case.get("case_id", "unknown")

        result = await generate_context_aware_response(
            query=query,
            sensors=None,
            has_live_sensors=False,
            plant_override=case.get("expected_plant"),
            stage_override=case.get("expected_stage"),
            history=None,
        )

        answer = result.get("response", "")
        metadata = result.get("_evaluation_metadata", {})
        retrieved_chunks = metadata.get("retrieved_chunks", [])
        use_structured = metadata.get("use_structured_params", False)

        gt_params = {}
        if case.get("expected_plant"):
            from app.local_plant_db import get_plant_parameters
            params = get_plant_parameters(case["expected_plant"], case.get("expected_stage") or "vegetative")
            if params:
                gt_params["temperature"] = {"value": params.get("ideal_temp_optimal"), "min": params.get("ideal_temp_min"), "max": params.get("ideal_temp_max")}
                gt_params["humidity"] = {"value": params.get("ideal_rh_optimal"), "min": params.get("ideal_rh_min"), "max": params.get("ideal_rh_max")}
                gt_params["light"] = {"value": params.get("ideal_light_optimal") or params.get("ideal_light_min"), "min": params.get("ideal_light_min"), "max": params.get("ideal_light_max")}

        should_score_numerical = (
            case.get("case_group") == "quantitative"
            and use_structured
            and bool(gt_params)
        )
        if should_score_numerical:
            numerical = NumericalRigorChecker.evaluate_answer(
                answer,
                gt_params,
                requested_params=_requested_numeric_params(query),
            )
        else:
            numerical = {"applicable": False, "overall_pass": True}
        constraint = ConstraintSatisfactionChecker.check(answer, query, use_structured)
        citation = CitationAccuracyChecker.check(answer, metadata)

        if not constraint["pass"]:
            print(f"    [DEBUG] Case {case_id} failed constraint: mode={constraint.get('mode','?')}, terms={constraint.get('found_terms',[])}")
        else:
            print(f"    [DEBUG] Case {case_id} constraint: mode={constraint.get('mode','?')}")

        eval_result = {
            "case_id": case_id,
            "query": query,
            "answer": answer[:300],
            "numerical_rigor": numerical["overall_pass"],
            "citation_accuracy": citation["pass"],
            "constraint_satisfaction": constraint["pass"],
        }
        self.results.append(eval_result)
        return eval_result

    def compute_precision(self) -> Dict:
        if not self.results:
            return {"system_precision": 0.0, "dimension_scores": {}, "n": 0}

        n = len(self.results)
        dim_scores = {}
        for dim in self.DIMENSIONS:
            passed = sum(1 for r in self.results if r.get(dim, False))
            dim_scores[dim] = round(passed / n, 4)

        overall = sum(dim_scores.values()) / len(self.DIMENSIONS)
        return {
            "system_precision": round(overall, 4),
            "dimension_scores": dim_scores,
            "n": n,
        }

    def print_report(self):
        summary = self.compute_precision()
        print()
        print("-" * 50)
        print("  SYSTEM PRECISION (End-to-End Audit)")
        print("-" * 50)
        print(f"  Cases evaluated: {summary['n']}")
        for dim, score in summary["dimension_scores"].items():
            label = dim.replace("_", " ").title()
            print(f"  {label:25} {score:.1%}")
        print(f"  {'System Precision':25} {summary['system_precision']:.1%}")
        print("-" * 50)

    def export(self, path: Path):
        with open(path, "w", encoding="utf-8") as f:
            json.dump({"results": self.results, "summary": self.compute_precision()}, f, indent=2)


# =============================================================================
# YOUDEN'S J CALIBRATION
# =============================================================================

class YoudenJCalibrator:
    """Youden's J = Sensitivity + Specificity - 1 for threshold optimization."""

    def __init__(self):
        self.records: List[Tuple[float, bool, bool, str, str]] = []  # (similarity, is_tp, is_cross_modal, category, case_id)
        self.graded_records: List[Tuple[float, float, bool, str, str]] = []  # (similarity, relevance_grade, is_cross_modal, category, case_id)
        self.category_records: Dict[str, List[Tuple[float, bool, str]]] = {}

    def add_record(self, similarity: float, is_tp: bool, is_cross_modal: bool, category: str = "unknown", case_id: str = ""):
        self.records.append((similarity, is_tp, is_cross_modal, category, case_id))
        if category not in self.category_records:
            self.category_records[category] = []
        self.category_records[category].append((similarity, is_tp, case_id))

    def add_record_graded(self, similarity: float, relevance_grade: float, is_cross_modal: bool, category: str = "unknown", case_id: str = ""):
        self.graded_records.append((similarity, relevance_grade, is_cross_modal, category, case_id))

    def compute_graded(self, cross_modal_only: bool = False) -> Tuple[float, float]:
        subset = [(s, rg) for s, rg, cm, _, _ in self.graded_records if not cross_modal_only or cm]
        if not subset:
            return 0.0, 0.0

        grade_pos = [(s, rg) for s, rg in subset if rg > 0]
        grade_neg = [(s, rg) for s, rg in subset if rg == 0]
        if not grade_pos or not grade_neg:
            return 0.0, 0.0

        total_possible_grade = sum(rg for _, rg in grade_pos)
        neg_count = len(grade_neg)

        best_t, best_j = 0.0, -99.0
        for ti in CANDIDATE_RANGE:
            t = ti / 100.0
            retrieved_grade = sum(rg for s, rg in grade_pos if s >= t)
            tpr_graded = retrieved_grade / total_possible_grade if total_possible_grade > 0 else 0
            fpr_graded = sum(1 for s, _ in grade_neg if s >= t) / neg_count if neg_count > 0 else 0
            j = tpr_graded - fpr_graded
            if j > best_j:
                best_j = j
                best_t = t
        return best_t, best_j

    def compute(self, cross_modal_only: bool = False) -> Tuple[float, float]:
        subset = [(s, tp) for s, tp, cm, _, _ in self.records if not cross_modal_only or cm]
        if not subset:
            return 0.0, 0.0

        tp_scores = [s for s, tp in subset if tp]
        tn_scores = [s for s, tp in subset if not tp]
        if not tp_scores or not tn_scores:
            return 0.0, 0.0

        best_t, best_j = 0.0, -99.0
        for ti in CANDIDATE_RANGE:
            t = ti / 100.0
            tpr = sum(1 for s in tp_scores if s >= t) / len(tp_scores)
            fpr = sum(1 for s in tn_scores if s >= t) / len(tn_scores)
            j = tpr - fpr
            if j > best_j:
                best_j = j
                best_t = t
        return best_t, best_j

    def report_per_category(self, threshold: float) -> str:
        """Build per-category accuracy report for thesis Results chapter."""
        lines = ["\n--- Per-Category Accuracy ---"]
        lines.append(f"{'Category':<25} {'Cases':>6} {'TP':>4} {'FP':>4} {'Acc':>6}")
        lines.append("-" * 50)
        for cat in sorted(self.category_records.keys()):
            records = self.category_records[cat]
            tp = sum(1 for s, is_tp, _ in records if is_tp and s >= threshold)
            fp = sum(1 for s, is_tp, _ in records if not is_tp and s >= threshold)
            total = len(records)
            hits = sum(1 for s, is_tp, _ in records if is_tp and s >= threshold)
            acc = hits / total if total > 0 else 0.0
            precision = tp / (tp + fp) if (tp + fp) > 0 else 0.0
            lines.append(f"{cat:<25} {total:>6} {tp:>4} {fp:>4} {acc:>6.0%}")
        return "\n".join(lines)

    def build_histogram(self) -> str:
        """Build ASCII histogram of TP vs TN score distributions."""
        if not self.records:
            return "(no data)"

        tp_scores = [s for s, tp, _, _, _ in self.records if tp]
        tn_scores = [s for s, tp, _, _, _ in self.records if not tp]
        lines = []

        lines.append(f"All dense chunks  (n={len(self.records)}, TP={len(tp_scores)}, TN={len(tn_scores)})")
        lines.append(f"{'Bucket':>14}   {'TP':>4} {'TN':>4}   {'TP (█)':25} {'TN (░)':25}")
        lines.append("-" * 75)

        bin_width = (HISTOGRAM_HIGH - HISTOGRAM_LOW) / HISTOGRAM_BINS
        max_count = max(
            max(
                sum(1 for s in tp_scores if HISTOGRAM_LOW + i * bin_width <= s < HISTOGRAM_LOW + (i + 1) * bin_width),
                sum(1 for s in tn_scores if HISTOGRAM_LOW + i * bin_width <= s < HISTOGRAM_LOW + (i + 1) * bin_width),
            )
            for i in range(HISTOGRAM_BINS)
        ) or 1

        for i in range(HISTOGRAM_BINS):
            lo = HISTOGRAM_LOW + i * bin_width
            hi = lo + bin_width
            tp_n = sum(1 for s in tp_scores if lo <= s < hi)
            tn_n = sum(1 for s in tn_scores if lo <= s < hi)
            tp_bar = "#" * int(tp_n / max_count * 24)
            tn_bar = "~" * int(tn_n / max_count * 24)
            lines.append(f"  {lo:.2f}{hi:.2f}    {tp_n:>4} {tn_n:>4}   {tp_bar:<25} {tn_bar}")

        return "\n".join(lines)


# =============================================================================
# DATASET LOADING
# =============================================================================

def load_golden_qa_cases() -> List[Dict]:
    path = FIXTURES_DIR / "golden_qa_cases.json"
    if not path.exists():
        raise FileNotFoundError(f"Golden QA cases not found at {path}")
    with open(path, encoding="utf-8") as f:
        return json.load(f)


def load_synthetic_qa_cases() -> List[Dict]:
    path = FIXTURES_DIR / "synthetic_qa_cases.json"
    if not path.exists():
        return []
    with open(path, encoding="utf-8") as f:
        return json.load(f)


def save_synthetic_qa_cases(cases: List[Dict]):
    path = FIXTURES_DIR / "synthetic_qa_cases.json"
    path.parent.mkdir(parents=True, exist_ok=True)
    with open(path, "w", encoding="utf-8") as f:
        json.dump(cases, f, indent=2, ensure_ascii=False)


def load_cross_lingual_cases() -> List[Dict]:
    path = FIXTURES_DIR / "cross_lingual_cases.json"
    if not path.exists():
        return []
    with open(path, encoding="utf-8") as f:
        return json.load(f)


async def generate_synthetic_dataset() -> List[Dict]:
    """Generate synthetic QA pairs using Ragas TestsetGenerator with gpt-4o-mini."""
    if not HAS_RAGAS:
        raise RuntimeError("RAGAS not installed. Cannot generate synthetic dataset.")

    print("[Synthetic] Generating synthetic QA dataset with gpt-4o-mini...")
    print("[Synthetic] This may take several minutes and cost ~$0.50-$1.00.")

    try:
        from ragas.testset.generator import TestsetGenerator
        from ragas.testset.evolutions import simple, reasoning, multi_context
    except ImportError:
        from ragas.testset import TestsetGenerator
        simple, reasoning, multi_context = None, None, None

    with open(DATA_DIR / "vector_database.json", encoding="utf-8") as f:
        raw_docs = json.load(f)

    from openai import OpenAI
    generator_llm = OpenAI(api_key=OPENAI_API_KEY)
    critic_llm = OpenAI(api_key=OPENAI_API_KEY)

    from ragas.llms import llm_factory
    gen_wrapper = llm_factory(OPENAI_MODEL, client=generator_llm)
    crit_wrapper = llm_factory(OPENAI_MODEL, client=critic_llm)

    generator = TestsetGenerator(
        generator_llm=gen_wrapper,
        critic_llm=crit_wrapper,
    )

    if simple is not None:
        distributions = {simple: 0.5, reasoning: 0.3, multi_context: 0.2}
    else:
        distributions = None

    testset = generator.generate_with_langchain_docs(
        raw_docs,
        test_size=60,
        distributions=distributions,
    )

    df = testset.to_pandas()
    cases = []
    for _, row in df.iterrows():
        cases.append({
            "case_id": f"synthetic_{str(row.get('question', ''))[:20]}",
            "case_group": "synthetic",
            "query": row.get("question", ""),
            "ground_truth": row.get("ground_truth", ""),
            "query_type": "plant_specific",
            "response_language": _detect_language(str(row.get("question", ""))),
            "expected_emojis": ["⚠️"],
        })

    save_synthetic_qa_cases(cases)
    print(f"[Synthetic] Generated {len(cases)} synthetic QA pairs.")
    return cases


# =============================================================================
# OPERATIONAL METRICS
# =============================================================================

def compute_tps(usage: dict, latency_ms: float) -> float:
    """Compute tokens per second from usage and latency."""
    completion = usage.get("completion_tokens", 0)
    if completion > 0 and latency_ms > 0:
        return round(completion / (latency_ms / 1000.0), 1)
    return 0.0


def _detect_language(text: str) -> str:
    id_markers = {"suhu", "berapa", "kelembaban", "cahaya", "tanaman",
                  "membutuhkan", "pertumbuhan", "perkecambahan", "berapakah",
                  "kangkung", "selada", "bayam", "kailan", "cabai",
                  "pakcoy", "seledri", "terong", "pada", "fase", "yang",
                  "untuk", "dan", "dengan", "adalah", "secara"}
    en_markers = {"what", "how", "does", "need", "give", "is", "the",
                  "temperature", "humidity", "light", "for", "during"}
    words = set(re.findall(r'\b\w+\b', text.lower()))
    id_score = sum(1 for m in id_markers if m in words)
    en_score = sum(1 for m in en_markers if m in words)
    if id_score > en_score:
        return "id"
    if en_score > id_score:
        return "en"
    return "id" if id_score > 0 else "en"


# =============================================================================
# GROUND TRUTH VALIDATION
# =============================================================================

def validate_ground_truths(human_cases: List[Dict]) -> List[str]:
    """Check hard-coded ground truths against live plants_database_day_night.json."""
    db_path = DATA_DIR / "plants_database_day_night.json"
    if not db_path.exists():
        return ["[WARN] plants_database_day_night.json not found -- skipping validation"]

    with open(db_path, encoding="utf-8") as f:
        plant_db = json.load(f)

    warnings_list = []
    for case in human_cases:
        plant_id = case.get("expected_plant")
        if not plant_id:
            continue
        if case.get("is_negative_test"):
            continue
        resolved_id = _resolve_plant_id(plant_id)
        for plant in plant_db.get("plants", []):
            if plant.get("id") == resolved_id:
                stage = case.get("expected_stage", "vegetative")
                lifecycle = plant.get("lifecycle", {})
                if stage not in lifecycle:
                    warnings_list.append(
                        f"[WARN] {case['case_id']}: stage '{stage}' not found in DB for {resolved_id}. "
                        f"Available: {list(lifecycle.keys())}"
                    )
                    continue
                stage_data = lifecycle[stage]
                db_day = stage_data.get("day", {})
                if db_day:
                    gt = case.get("ground_truth", "")
                    db_opt = db_day.get("temp_optimal_c")
                    if db_opt and str(db_opt) not in gt and str(int(db_opt)) not in gt:
                        warnings_list.append(
                            f"[WARN] {case['case_id']}: ground_truth may be stale. "
                            f"DB temp_optimal_c={db_opt} not found in ground_truth text."
                        )
    return warnings_list


# =============================================================================
# MAIN EVALUATION ENGINE
# =============================================================================

class EvaluationEngine:
    def __init__(self, results_dir: Path = RESULTS_DIR):
        self.results_dir = results_dir
        self.results_dir.mkdir(parents=True, exist_ok=True)
        self.critic_logger = CriticReasoningLogger(results_dir / "critic_reasoning_log.jsonl")
        self.all_results: List[Dict] = []
        self.cost_tracker = {"cases_completed": 0, "total_estimated_cost": 0.0}

    async def evaluate_single_case(self, case: Dict) -> Dict:
        query = case["query"]
        ground_truth = case.get("ground_truth", "")
        acceptable_answers = case.get("acceptable_answers", [])
        response_language = case.get("response_language")
        temporal_context = case.get("temporal_context")

        t_start = time.perf_counter()
        result = await generate_context_aware_response(
            query=query,
            sensors=None,
            has_live_sensors=False,
            plant_override=case.get("expected_plant"),
            stage_override=case.get("expected_stage"),
            history=None,
            response_language=response_language,
            temporal_context=temporal_context,
        )

        t_generation = time.perf_counter()
        answer = result.get("response", "")
        metadata = result.get("_evaluation_metadata", {})
        meta_latency = metadata.get("latency_ms", 0)
        latency_ms = meta_latency if meta_latency > 0 else round((t_generation - t_start) * 1000, 1)

        retrieved_chunks = metadata.get("retrieved_chunks", [])
        contexts = [c.get("content", "") for c in retrieved_chunks if c.get("content")]
        use_structured = metadata.get("use_structured_params", False)
        resolved_phase = metadata.get("resolved_phase")
        model_used = metadata.get("model_used", "unknown")
        usage = metadata.get("token_usage", {})
        semantic_scores = metadata.get("semantic_scores", [])
        fts_scores = metadata.get("fts_scores", [])
        rrf_ranks = metadata.get("rrf_ranks", [])
        bge_top_doc = metadata.get("bge_top_doc", "")
        fts_top_doc = metadata.get("fts_top_doc", "")
        tie_breaker_flag = metadata.get("tie_breaker_flag", False)

        gt_params = {}
        if case.get("expected_plant"):
            from app.local_plant_db import get_plant_parameters
            params = get_plant_parameters(case["expected_plant"], case.get("expected_stage") or "vegetative")
            if params:
                gt_params["temperature"] = {"value": params.get("ideal_temp_optimal"), "min": params.get("ideal_temp_min"), "max": params.get("ideal_temp_max")}
                gt_params["humidity"] = {"value": params.get("ideal_rh_optimal"), "min": params.get("ideal_rh_min"), "max": params.get("ideal_rh_max")}
                gt_params["light"] = {"value": params.get("ideal_light_optimal") or params.get("ideal_light_min"), "min": params.get("ideal_light_min"), "max": params.get("ideal_light_max")}
        should_score_numerical = (
            case.get("case_group") == "quantitative"
            and use_structured
            and bool(gt_params)
        )
        requested_params = _requested_numeric_params(query) if should_score_numerical else set()

        if should_score_numerical:
            numerical_result = NumericalRigorChecker.evaluate_answer(
                answer,
                gt_params,
                requested_params=requested_params,
            )
        else:
            numerical_result = {
                "applicable": False,
                "status": "NOT_APPLICABLE",
                "requested_params": [],
                "param_results": {},
                "overall_pass": True,
                "factual_score_override": 1.0,
            }
        temporal_result = TemporalAdherenceChecker.check(answer, resolved_phase)
        constraint_result = ConstraintSatisfactionChecker.check(answer, query, use_structured)
        citation_result = CitationAccuracyChecker.check(answer, metadata)
        terminology_result = TerminologyNuanceChecker.evaluate_all(answer) if case.get("risk_flag") else None
        tps = compute_tps(usage, latency_ms)
        ragas_scores = await self._compute_ragas_scores(
            question=query, answer=answer, contexts=contexts,
            ground_truth=ground_truth, acceptable_answers=acceptable_answers,
        )

        # D1 Guardrail: retry low-faith cases with strict document synthesis
        _guardrail_applied = False
        try:
            raw_faith = ragas_scores.get("faithfulness", "")
            if isinstance(raw_faith, (int, float)) and float(raw_faith) < 0.3 and len(contexts) > 0:
                case_id = case.get("case_id", "?")
                print(f"[Guardrail] Low faith ({raw_faith:.4f}) with {len(contexts)} chunks — retrying {case_id}")
                # Build strict context-only instruction
                ctx_text = "\n\n".join(
                    f"--- Document {i+1} ---\n{c[:1000]}"
                    for i, c in enumerate(contexts[:5])
                )
                guardrail_prompt = (
                    "You are Veridia, an agricultural assistant. Answer the user's question "
                    "using ONLY the provided context below. Follow these rules strictly:\n"
                    "1. If the context contains the answer, summarize it directly.\n"
                    "2. Do NOT say 'tidak ditemukan dalam dokumen' or 'not found in documents'.\n"
                    "3. Do NOT use your own training knowledge — only the context below.\n"
                    "4. If the context does not contain relevant information, say: "
                    "'The available documents do not contain this specific information.'\n\n"
                    f"CONTEXT:\n{ctx_text}"
                )
                retry_answer_raw = await call_llm_with_history(
                    system_prompt=guardrail_prompt,
                    user_message=query,
                    temperature=0.3,
                )
                retry_answer = retry_answer_raw if isinstance(retry_answer_raw, str) else retry_answer_raw.get("content", "")
                if retry_answer and len(retry_answer) > 50:
                    retry_scores = await self._compute_ragas_scores(
                        question=query, answer=retry_answer, contexts=contexts,
                        ground_truth=ground_truth, acceptable_answers=acceptable_answers,
                    )
                    retry_faith = retry_scores.get("faithfulness", "")
                    if (isinstance(retry_faith, (int, float))
                        and float(retry_faith) > float(raw_faith)):
                        print(f"[Guardrail] Improved faith: {raw_faith:.4f} -> {retry_faith:.4f}")
                        ragas_scores = retry_scores
                        answer = retry_answer
                        _guardrail_applied = True
        except Exception as guardrail_err:
            print(f"[Guardrail] Error during retry: {guardrail_err}")

        prompt_tokens = usage.get("prompt_tokens", 0)
        completion_tokens = usage.get("completion_tokens", 0)
        generation_cost = (prompt_tokens * 0.15 + completion_tokens * 0.60) / 1_000_000
        critic_cost = (len(ragas_scores.get("_metrics_computed", [])) * 2000 * 0.75 + 300 * 4.50) / 1_000_000
        estimated_cost = round(generation_cost + critic_cost, 6)

        eval_result = {
            "case_id": case["case_id"],
            "case_group": case.get("case_group", "unknown"),
            "query": query,
            "answer": answer,
            "ground_truth": ground_truth,
            "latency_ms": latency_ms,
            "model_used": model_used,
            "tps": tps,
            "token_usage": usage,
            "estimated_cost_usd": estimated_cost,
            "retrieved_chunks_count": len(retrieved_chunks),
            "retrieval_mode": metadata.get("retrieval_mode", "unknown"),
            "parent_expansion": metadata.get("parent_expansion", False),
            "category": case.get("category", "unknown"),
            "semantic_scores": semantic_scores,
            "fts_scores": fts_scores,
            "rrf_ranks": rrf_ranks,
            "bge_top_doc": bge_top_doc,
            "fts_top_doc": fts_top_doc,
            "tie_breaker_flag": tie_breaker_flag,
            "numerical_rigor": numerical_result,
            "temporal_adherence": temporal_result,
            "constraint_satisfaction": constraint_result,
            "citation_accuracy": citation_result,
            "terminology_nuance": terminology_result,
            "ragas_scores": ragas_scores,
            "guardrail_applied": _guardrail_applied,
        }
        self.all_results.append(eval_result)
        self.cost_tracker["cases_completed"] += 1
        self.cost_tracker["total_estimated_cost"] += estimated_cost
        return eval_result

    async def _compute_ragas_scores(self, question, answer, contexts, ground_truth, acceptable_answers=None) -> Dict:
        if not HAS_RAGAS or not HAS_OPENAI:
            return {"error": "RAGAS or OpenAI not available", "_metrics_computed": []}
        try:
            from openai import AsyncOpenAI as _AsyncOAI
            async_client = _AsyncOAI(api_key=OPENAI_API_KEY)
            critic_llm = llm_factory(RAGAS_CRITIC_MODEL, client=async_client, max_tokens=4096)

            ctx = contexts if isinstance(contexts, list) else [contexts] if contexts else []

            result = {}
            computed = []

            # Faithfulness: answer grounded in retrieved context (skip when no chunks)
            f_metric = Faithfulness(llm=critic_llm)
            try:
                if not ctx:
                    result["faithfulness"] = "skipped: no retrieved context"
                else:
                    # Truncate contexts to avoid max_tokens issues in critic LLM generation
                    truncated_ctx = [c[:1200] for c in ctx][:7]
                    score = await _retry_ragas_call(
                        lambda q=question, a=answer, tc=truncated_ctx: f_metric.ascore(
                            user_input=q, response=a, retrieved_contexts=tc
                        ),
                        "faithfulness",
                    )
                    result["faithfulness"] = round(score, 4)
                    computed.append("faithfulness")
            except Exception as e:
                result["faithfulness"] = _categorize_ragas_error(e)

            # Context Precision: ranking quality (needs ground_truth + contexts)
            cp_metric = ContextPrecision(llm=critic_llm)
            try:
                if not ctx:
                    result["context_precision"] = "skipped: no retrieved context"
                elif not ground_truth:
                    result["context_precision"] = "skipped: no ground_truth"
                else:
                    truncated_ctx = [c[:1200] for c in ctx][:7]
                    score = await _retry_ragas_call(
                        lambda q=question, gt=ground_truth, tc=truncated_ctx: cp_metric.ascore(
                            user_input=q, reference=gt, retrieved_contexts=tc
                        ),
                        "context_precision",
                    )
                    result["context_precision"] = round(score, 4)
                    computed.append("context_precision")
            except Exception as e:
                result["context_precision"] = _categorize_ragas_error(e)

            # Context Recall: did we find all needed facts (needs ground_truth + contexts)
            cr_metric = ContextRecall(llm=critic_llm)
            try:
                if not ctx:
                    result["context_recall"] = "skipped: no retrieved context"
                elif not ground_truth:
                    result["context_recall"] = "skipped: no ground_truth"
                else:
                    truncated_ctx = [c[:1200] for c in ctx][:7]
                    score = await _retry_ragas_call(
                        lambda q=question, gt=ground_truth, tc=truncated_ctx: cr_metric.ascore(
                            user_input=q, retrieved_contexts=tc, reference=gt
                        ),
                        "context_recall",
                    )
                    result["context_recall"] = round(score, 4)
                    computed.append("context_recall")
            except Exception as e:
                result["context_recall"] = _categorize_ragas_error(e)

            # Answer Correctness: LLM-based only (weights=[1.0, 0.0] disables embedding component)
            try:
                ac_metric = AnswerCorrectness(llm=critic_llm, weights=[1.0, 0.0])
                _all_gts = [ground_truth] + (acceptable_answers or [])
                _all_gts = [gt for gt in _all_gts if gt]
                if _all_gts:
                    best_ac = 0.0
                    for i, gt_candidate in enumerate(_all_gts):
                        score = await _retry_ragas_call(
                            lambda q=question, a=answer, gt=gt_candidate: ac_metric.ascore(
                                user_input=q, response=a, reference=gt
                            ),
                            f"answer_correctness[{i}]",
                        )
                        if score > best_ac:
                            best_ac = score
                    result["answer_correctness"] = round(best_ac, 4)
                    computed.append("answer_correctness")
                else:
                    result["answer_correctness"] = "skipped: no ground_truth"
            except Exception as e:
                result["answer_correctness"] = _categorize_ragas_error(e)

            # Answer Relevance: needs ragas-native OpenAI embeddings (not langchain's)
            try:
                import sys as _sys
                from ragas.embeddings import OpenAIEmbeddings as _RagasEmbed
                _ar_cls = (
                    getattr(_sys.modules[__name__], "ResponseRelevancy", None)
                    or getattr(_sys.modules[__name__], "AnswerRelevancy", None)
                    or _AnswerRelevanceMetric
                )
                _embeddings = _RagasEmbed(client=async_client)
                ar_metric = _ar_cls(llm=critic_llm, embeddings=_embeddings)
                score = await _retry_ragas_call(
                    lambda q=question, a=answer: ar_metric.ascore(
                        user_input=q, response=a
                    ),
                    "answer_relevance",
                )
                result["answer_relevance"] = round(score, 4)
                computed.append("answer_relevance")
            except Exception as e:
                result["answer_relevance"] = _categorize_ragas_error(e)

            # Strict Data Rule override for custom Numerical Rigor
            if self.all_results and self.all_results[-1].get("numerical_rigor", {}).get("overall_pass") is False:
                result["_numerical_rigor_override"] = True

            result["_metrics_computed"] = computed
            return result
        except Exception as e:
            print(f"[RAGAS] Error computing metrics: {e}")
            return {"error": str(e), "_metrics_computed": []}

    def export_csv(self):
        path = self.results_dir / "results_detail.csv"
        if not self.all_results:
            print("[Export] No results to export.")
            return

        rows = []
        for r in self.all_results:
            ragas = r.get("ragas_scores", {})
            row = {
                "case_id": r["case_id"],
                "case_group": r["case_group"],
                "category": r.get("category", "unknown"),
                "latency_ms": r["latency_ms"],
                "tps": r["tps"],
                "model_used": r["model_used"],
                "retrieved_chunks": r["retrieved_chunks_count"],
                "retrieval_mode": r["retrieval_mode"],
                "parent_expansion": r["parent_expansion"],
                "bge_top_doc": r.get("bge_top_doc", ""),
                "fts_top_doc": r.get("fts_top_doc", ""),
                "tie_breaker_flag": r.get("tie_breaker_flag", False),
                "numerical_rigor_pass": r["numerical_rigor"]["overall_pass"],
                "temporal_adherence_pass": r["temporal_adherence"]["pass"],
                "constraint_satisfaction_pass": r["constraint_satisfaction"]["pass"],
                "citation_accuracy_pass": r["citation_accuracy"]["pass"],
                "faithfulness": ragas.get("faithfulness", ""),
                "context_precision": ragas.get("context_precision", ""),
                "context_recall": ragas.get("context_recall", ""),
                "answer_correctness": ragas.get("answer_correctness", ""),
                "answer_relevance": ragas.get("answer_relevance", ""),
                "estimated_cost_usd": r["estimated_cost_usd"],
            }
            # Add terminology nuance scores
            tn = r.get("terminology_nuance")
            if tn:
                row["terminology_accuracy"] = tn["accuracy"]
                for key, sub in tn["results"].items():
                    row[f"tn_{key}"] = sub["pass"]

            rows.append(row)

        with open(path, "w", newline="", encoding="utf-8") as f:
            if rows:
                all_keys = list(dict.fromkeys(k for row in rows for k in row.keys()))
                writer = csv.DictWriter(f, fieldnames=all_keys, restval="")
                writer.writeheader()
                writer.writerows(rows)

        print(f"[Export] Results written to {path}")

    def export_summary(self):
        """Export summary JSON and methodology note."""
        if not self.all_results:
            return

        # Overall metrics
        rag_scores = [r.get("ragas_scores", {}) for r in self.all_results if r.get("ragas_scores")]
        metrics_computed = set()
        for rs in rag_scores:
            metrics_computed.update(rs.get("_metrics_computed", []))

        latencies = [r["latency_ms"] for r in self.all_results if r["latency_ms"] > 0]
        latencies_sorted = sorted(latencies) if latencies else [0]
        nr_results = [r["numerical_rigor"] for r in self.all_results]
        nr_applicable = [r for r in nr_results if r.get("applicable", True)]
        nr_pass = sum(1 for r in nr_applicable if r.get("overall_pass"))

        summary = {
            "total_cases": len(self.all_results),
            "total_estimated_cost_usd": round(self.cost_tracker["total_estimated_cost"], 4),
            "metrics_computed": list(metrics_computed),
            "latency_ms": {
                "p50": round(latencies_sorted[len(latencies_sorted) // 2], 1) if latencies_sorted else 0,
                "p95": round(latencies_sorted[int(len(latencies_sorted) * 0.95)], 1) if len(latencies_sorted) > 1 else 0,
                "p99": round(latencies_sorted[int(len(latencies_sorted) * 0.99)], 1) if len(latencies_sorted) > 1 else 0,
                "avg": round(sum(latencies) / len(latencies), 1) if latencies else 0,
            },
            "avg_tps": round(sum(r["tps"] for r in self.all_results) / len(self.all_results), 1) if self.all_results else 0,
            "numerical_rigor_pass_rate": round(
                nr_pass / len(nr_applicable) * 100, 1
            ) if nr_applicable else 0.0,
            "numerical_rigor_applicable_count": len(nr_applicable),
            "numerical_rigor_skipped_count": len(nr_results) - len(nr_applicable),
            "temporal_adherence_pass_rate": round(
                sum(1 for r in self.all_results if r["temporal_adherence"]["pass"]) / len(self.all_results) * 100, 1
            ) if self.all_results else 0,
            "constraint_satisfaction_pass_rate": round(
                sum(1 for r in self.all_results if r["constraint_satisfaction"]["pass"]) / len(self.all_results) * 100, 1
            ) if self.all_results else 0,
            "citation_accuracy_pass_rate": round(
                sum(1 for r in self.all_results if r["citation_accuracy"]["pass"]) / len(self.all_results) * 100, 1
            ) if self.all_results else 0,
        }

        # Terminology nuance summary
        tn_results = [r.get("terminology_nuance") for r in self.all_results if r.get("terminology_nuance")]
        if tn_results:
            avg_acc = sum(tn["accuracy"] for tn in tn_results) / len(tn_results)
            summary["terminology_accuracy_avg"] = round(avg_acc, 3)

        # Answer-level RAGAS metric averages with explicit accounting
        ac_vals = [s.get("answer_correctness") for s in rag_scores if isinstance(s.get("answer_correctness"), (int, float))]
        ac_skipped = sum(1 for s in rag_scores if s.get("answer_correctness") == "skipped: no ground_truth")
        ar_vals = [s.get("answer_relevance") for s in rag_scores if isinstance(s.get("answer_relevance"), (int, float))]
        summary["answer_correctness_avg"] = round(sum(ac_vals) / len(ac_vals), 4) if ac_vals else 0
        summary["answer_correctness_case_count"] = len(ac_vals)
        summary["answer_correctness_skipped_count"] = ac_skipped
        summary["answer_relevance_avg"] = round(sum(ar_vals) / len(ar_vals), 4) if ar_vals else 0
        summary["answer_relevance_case_count"] = len(ar_vals)

        path = self.results_dir / "operational_metrics.json"
        with open(path, "w", encoding="utf-8") as f:
            json.dump(summary, f, indent=2)
        print(f"[Export] Summary written to {path}")

        # Methodology note
        note_path = self.results_dir / "methodology_note.txt"
        calib_cases = load_golden_retrieval_cases()
        qa_cases = load_golden_qa_cases()
        synthetic_cases = load_synthetic_qa_cases()
        with open(note_path, "w", encoding="utf-8") as f:
            f.write("PGC RAGAS Evaluation — Methodology Note\n")
            f.write("=" * 50 + "\n\n")
            f.write("Train/Test Split:\n")
            f.write(f"  Calibration set: golden_retrieval_cases.json ({len(calib_cases)} cases)\n")
            f.write("    → Youden's J threshold calibration + retrieval MRR only.\n")
            f.write("    → NOT used for any end-to-end RAGAS metrics.\n\n")
            f.write(
                f"  Test set: golden_qa_cases.json ({len(qa_cases)} cases)"
                f" + synthetic_qa_cases.json ({len(synthetic_cases)} cases)\n"
            )
            f.write("    → All reported Faithfulness, Context Precision, Context Recall,\n")
            f.write("      Temporal Adherence, Numerical Rigor, Citation Accuracy,\n")
            f.write("      and Constraint Satisfaction scores.\n")
            f.write("    → Answer Correctness is computed only for cases with a populated\n")
            f.write("      ground_truth field; cases without references are excluded from\n")
            f.write("      that average (see answer_correctness_skipped_count in JSON).\n")
            f.write("    → Answer Relevance is reference-free and computed for all cases.\n\n")
            f.write(f"  Thresholds tested: Dense 0.70, Hybrid 0.70\n")
            f.write(f"  Numerical tolerance: ±{NUMERICAL_TOLERANCE} units\n\n")
            f.write(f"  Total estimated cost: ${self.cost_tracker['total_estimated_cost']:.2f}\n")
            f.write(f"  Student model: gpt-oss-120b via Cerebras\n")
            f.write(f"  Teacher model: {OPENAI_MODEL} via OpenAI\n\n")
            f.write("  Calibration categories:\n")
            f.write("    - standard, tie_breaker, species_mismatch, phase_mismatch,\n")
            f.write("      unit_conversion, out_of_scope, negative_control\n")
            f.write("  Per-category accuracy reported in per-category table above.\n")
        print(f"[Export] Methodology note written to {note_path}")

    def print_cost_summary(self):
        total = self.cost_tracker["total_estimated_cost"]
        completed = self.cost_tracker["cases_completed"]
        print(f"\n{'='*50}")
        print(f"  EVALUATION COST SUMMARY")
        print(f"{'='*50}")
        print(f"  Cases completed:   {completed}")
        print(f"  Estimated cost:    ${total:.4f}")
        remaining_budget = 5.00 - total
        print(f"  Remaining budget:  ${remaining_budget:.4f}")
        if remaining_budget < 0:
            print(f"  ⚠️  BUDGET EXCEEDED! Estimated cost exceeds $5.00 grant.")
        print(f"{'='*50}\n")

    def export_results_log(self):
        """Export full per-case results to results_log.json for debugging."""
        if not self.all_results:
            print("[Export] No results to export.")
            return
        path = self.results_dir / "results_log.json"
        with open(path, "w", encoding="utf-8") as f:
            json.dump(self.all_results, f, indent=2, ensure_ascii=False)
        print(f"[Export] Full results log written to {path}")

    def export_results_data(self):
        """Export flattened per-case metrics to results_data.csv for charting."""
        if not self.all_results:
            print("[Export] No results to export.")
            return

        rows = []
        for r in self.all_results:
            ragas = r.get("ragas_scores", {})
            nr = r.get("numerical_rigor", {})
            ta = r.get("temporal_adherence", {})
            cs = r.get("constraint_satisfaction", {})
            ca = r.get("citation_accuracy", {})
            tn = r.get("terminology_nuance")

            row = {
                "case_id": r["case_id"],
                "case_group": r["case_group"],
                "category": r.get("category", ""),
                "query_type": r.get("query_type", ""),
                "latency_ms": r["latency_ms"],
                "tps": r["tps"],
                "model_used": r["model_used"],
                "retrieved_chunks": r["retrieved_chunks_count"],
                "retrieval_mode": r["retrieval_mode"],
                "parent_expansion": r["parent_expansion"],
                "estimated_cost_usd": r["estimated_cost_usd"],
                "prompt_tokens": r.get("token_usage", {}).get("prompt_tokens", 0),
                "completion_tokens": r.get("token_usage", {}).get("completion_tokens", 0),
                "bge_top_doc": r.get("bge_top_doc", ""),
                "fts_top_doc": r.get("fts_top_doc", ""),
                "tie_breaker_flag": r.get("tie_breaker_flag", False),
                "numerical_rigor_pass": nr.get("overall_pass", True),
                "numerical_rigor_score_override": nr.get("factual_score_override", 1.0),
                "temporal_adherence_pass": ta.get("pass", True),
                "temporal_adherence_phase": ta.get("resolved_phase", ""),
                "constraint_satisfaction_pass": cs.get("pass", True),
                "constraint_satisfaction_mode": cs.get("mode", ""),
                "citation_accuracy_pass": ca.get("pass", True),
                "faithfulness": ragas.get("faithfulness", ""),
                "context_precision": ragas.get("context_precision", ""),
                "context_recall": ragas.get("context_recall", ""),
                "answer_correctness": ragas.get("answer_correctness", ""),
                "answer_relevance": ragas.get("answer_relevance", ""),
            }
            if tn:
                row["terminology_accuracy"] = tn["accuracy"]
                for key, sub in tn.get("results", {}).items():
                    row[f"tn_{key}"] = sub["pass"]

            rows.append(row)

        path = self.results_dir / "results_data.csv"
        with open(path, "w", newline="", encoding="utf-8") as f:
            if rows:
                all_keys = list(dict.fromkeys(k for row in rows for k in row.keys()))
                writer = csv.DictWriter(f, fieldnames=all_keys, restval="")
                writer.writeheader()
                writer.writerows(rows)
        print(f"[Export] Chart data written to {path}")

    def export_thesis_tables(self, calib_result: Optional[Dict] = None, cross_result: Optional[Dict] = None, only: Optional[str] = None):
        r"""Export LaTeX tabular environments for direct \input{} inclusion."""
        if not self.all_results:
            print("[Export] No results to export.")
            return

        n = len(self.all_results)
        nr_pass = sum(1 for r in self.all_results if r["numerical_rigor"]["overall_pass"])
        ta_pass = sum(1 for r in self.all_results if r["temporal_adherence"]["pass"])
        cs_pass = sum(1 for r in self.all_results if r["constraint_satisfaction"]["pass"])
        ca_pass = sum(1 for r in self.all_results if r["citation_accuracy"]["pass"])

        latencies = [r["latency_ms"] for r in self.all_results if r["latency_ms"] > 0]
        latencies_sorted = sorted(latencies) if latencies else [0]
        p50 = round(latencies_sorted[len(latencies_sorted) // 2], 1) if latencies_sorted else 0
        p95 = round(latencies_sorted[int(len(latencies_sorted) * 0.95)], 1) if len(latencies_sorted) > 1 else 0
        avg_lat = round(sum(latencies) / len(latencies), 1) if latencies else 0
        avg_tps = round(sum(r["tps"] for r in self.all_results) / n, 1) if n else 0

        ragas_scores = [r.get("ragas_scores", {}) for r in self.all_results]
        faithfulness_vals = [s.get("faithfulness", "") for s in ragas_scores if isinstance(s.get("faithfulness"), (int, float))]
        ctx_prec_vals = [s.get("context_precision", "") for s in ragas_scores if isinstance(s.get("context_precision"), (int, float))]
        ctx_recall_vals = [s.get("context_recall", "") for s in ragas_scores if isinstance(s.get("context_recall"), (int, float))]
        ac_vals = [s.get("answer_correctness") for s in ragas_scores if isinstance(s.get("answer_correctness"), (int, float))]
        ar_vals = [s.get("answer_relevance") for s in ragas_scores if isinstance(s.get("answer_relevance"), (int, float))]
        avg_faith = round(sum(faithfulness_vals) / len(faithfulness_vals), 4) if faithfulness_vals else 0
        avg_cp = round(sum(ctx_prec_vals) / len(ctx_prec_vals), 4) if ctx_prec_vals else 0
        avg_cr = round(sum(ctx_recall_vals) / len(ctx_recall_vals), 4) if ctx_recall_vals else 0
        avg_ac = round(sum(ac_vals) / len(ac_vals), 4) if ac_vals else 0
        avg_ar = round(sum(ar_vals) / len(ar_vals), 4) if ar_vals else 0

        lines = [
            "% Thesis Result Tables — auto-generated by evaluate_ragas.py",
            "% \\input{} each table into your LaTeX document.",
            "",
        ]

        # Table 1: Overall Performance Summary
        lines.append(r"\begin{table}[ht]")
        lines.append(r"\centering")
        if only == "rag":
            caption = r"\caption{RAG Evaluation Performance Summary}"
        elif only == "adversarial":
            caption = r"\caption{Adversarial Evaluation Performance Summary}"
        else:
            caption = (
                r"\caption{Overall System Performance Summary. "
                r"Ground-truth reference used for Answer Correctness; "
                r"cases without populated references are excluded from that average.}"
            )
        lines.append(caption)
        lines.append(r"\label{tab:overall_performance}")
        lines.append(r"\begin{tabular}{lcc}")
        lines.append(r"\toprule")
        lines.append(r"Metric & Value & Notes \\")
        lines.append(r"\midrule")
        lines.append(f"Total Cases & {n} & Human-adversarial + synthetic \\\\")
        if only == "rag":
            lines.append(f"Average Faithfulness & {avg_faith:.3f} & RAGAS metric \\\\")
            lines.append(f"Average Context Precision & {avg_cp:.3f} & RAGAS metric \\\\")
            lines.append(f"Average Context Recall & {avg_cr:.3f} & RAGAS metric \\\\")
            lines.append(f"Average Answer Correctness & {avg_ac:.3f} & RAGAS metric \\\\")
            lines.append(f"Average Answer Relevance & {avg_ar:.3f} & RAGAS metric \\\\")
        elif only == "adversarial":
            lines.append(f"Numerical Rigor Pass Rate & {nr_pass / n * 100:.1f}\\% & $\\pm${NUMERICAL_TOLERANCE}°C tolerance \\\\")
            lines.append(f"Temporal Adherence Pass Rate & {ta_pass / n * 100:.1f}\\% & Day/night phase correctness \\\\")
            lines.append(f"Constraint Satisfaction Pass Rate & {cs_pass / n * 100:.1f}\\% & 3-state context-aware \\\\")
            lines.append(f"Citation Accuracy Pass Rate & {ca_pass / n * 100:.1f}\\% & Emoji prefix audit \\\\")
        else:
            lines.append(f"Numerical Rigor Pass Rate & {nr_pass / n * 100:.1f}\\% & $\\pm${NUMERICAL_TOLERANCE}°C tolerance \\\\")
            lines.append(f"Temporal Adherence Pass Rate & {ta_pass / n * 100:.1f}\\% & Day/night phase correctness \\\\")
            lines.append(f"Constraint Satisfaction Pass Rate & {cs_pass / n * 100:.1f}\\% & 3-state context-aware \\\\")
            lines.append(f"Citation Accuracy Pass Rate & {ca_pass / n * 100:.1f}\\% & Emoji prefix audit \\\\")
            lines.append(f"Average Faithfulness & {avg_faith:.3f} & RAGAS metric \\\\")
            lines.append(f"Average Context Precision & {avg_cp:.3f} & RAGAS metric \\\\")
            lines.append(f"Average Context Recall & {avg_cr:.3f} & RAGAS metric \\\\")
            lines.append(f"Average Answer Correctness & {avg_ac:.3f} & RAGAS metric \\\\")
            lines.append(f"Average Answer Relevance & {avg_ar:.3f} & RAGAS metric \\\\")
        lines.append(f"Average Latency & {avg_lat:,}ms & P50: {p50:,}ms, P95: {p95:,}ms \\\\")
        lines.append(f"Average Throughput & {avg_tps} TPS & Tokens per second \\\\")
        lines.append(r"\bottomrule")
        lines.append(r"\end{tabular}")
        lines.append(r"\end{table}")
        lines.append("")

        # Table 2: System Precision by Dimension
        dims = {"Numerical Rigor": nr_pass, "Citation Accuracy": ca_pass, "Constraint Satisfaction": cs_pass}
        lines.append(r"\begin{table}[ht]")
        lines.append(r"\centering")
        lines.append(r"\caption{System Precision by Dimension}")
        lines.append(r"\label{tab:system_precision}")
        lines.append(r"\begin{tabular}{lcc}")
        lines.append(r"\toprule")
        lines.append(r"Dimension & Pass Rate & Description \\")
        lines.append(r"\midrule")
        for label, passed in dims.items():
            pct = passed / n * 100
            lines.append(f"{label} & {pct:.1f}\\% & See Section~\\\\")
        overall_sp = sum(passed / n for passed in dims.values()) / len(dims) * 100
        lines.append(r"\midrule")
        lines.append(f"System Precision & {overall_sp:.1f}\\% & Average of three dimensions \\\\")
        lines.append(r"\bottomrule")
        lines.append(r"\end{tabular}")
        lines.append(r"\end{table}")
        lines.append("")

        if only != "adversarial":
            # Table 3: Youden's J Calibration
            lines.append(r"\begin{table}[ht]")
            lines.append(r"\centering")
            lines.append(r"\caption{Youden's J Threshold Calibration}")
            lines.append(r"\label{tab:youden_calibration}")
            lines.append(r"\begin{tabular}{lccc}")
            lines.append(r"\toprule")
            lines.append(r"Mode & Optimal Threshold & Youden's J & Deployed \\")
            lines.append(r"\midrule")
            if calib_result:
                jd = calib_result.get("youden_dense", {})
                jg = calib_result.get("youden_graded_dense", {})
                jh = calib_result.get("youden_hybrid", {})
                lines.append(f"Binary Dense & {jd.get('optimal_threshold', 0):.2f} & ${jd.get('j', 0):+.3f}$ & 0.70 \\\\")
                lines.append(f"Graded Dense & {jg.get('optimal_threshold', 0):.2f} & ${jg.get('j', 0):+.3f}$ & --- \\\\")
                lines.append(f"Hybrid (Dense+FTS) & {jh.get('optimal_threshold', 0):.2f} & ${jh.get('j', 0):+.3f}$ & 0.70 \\\\")
            else:
                lines.append(r"% WARNING: No calibration data -- values are placeholders")
                lines.append(r"Binary Dense & --- & --- & 0.70 \\\\")
                lines.append(r"Graded Dense & --- & --- & --- \\\\")
                lines.append(r"Hybrid (Dense+FTS) & --- & --- & 0.70 \\\\")
            lines.append(r"\bottomrule")
            lines.append(r"\end{tabular}")
            lines.append(r"\end{table}")
            lines.append("")

        if only != "adversarial":
            # Table 4: Cross-Lingual MRR
            lines.append(r"\begin{table}[ht]")
            lines.append(r"\centering")
            lines.append(r"\caption{Cross-Lingual MRR Results}")
            lines.append(r"\label{tab:cross_lingual_mrr}")
            lines.append(r"\begin{tabular}{lcc}")
            lines.append(r"\toprule")
            lines.append(r"Language Group & MRR & Cases \\")
            lines.append(r"\midrule")
            if cross_result:
                lines.append(f"Indonesian $\\rightarrow$ English & {cross_result.get('id_mrr', 0):.3f} & {cross_result.get('id_cases', 0)} \\\\")
                lines.append(f"English $\\rightarrow$ English & {cross_result.get('en_mrr', 0):.3f} & {cross_result.get('en_cases', 0)} \\\\")
                lines.append(f"$\\Delta$MRR & {cross_result.get('delta_mrr', 0):.3f} & --- \\\\")
            else:
                lines.append(r"% WARNING: No cross-lingual data -- values are placeholders")
                lines.append(r"Indonesian $\rightarrow$ English & --- & --- \\\\")
                lines.append(r"English $\rightarrow$ English & --- & --- \\\\")
                lines.append(r"$\Delta$MRR & --- & --- \\\\")
            lines.append(r"\bottomrule")
            lines.append(r"\end{tabular}")
            lines.append(r"\end{table}")
            lines.append("")

        # Table 5: Retrieval Specificity
        total_cost = round(self.cost_tracker["total_estimated_cost"], 4)
        lines.append(r"\begin{table}[ht]")
        lines.append(r"\centering")
        lines.append(r"\caption{Evaluation Cost Breakdown}")
        lines.append(r"\label{tab:evaluation_cost}")
        lines.append(r"\begin{tabular}{lc}")
        lines.append(r"\toprule")
        lines.append(r"Item & Value \\")
        lines.append(r"\midrule")
        lines.append(f"Total Cases & {n} \\\\")
        lines.append(f"Total Estimated Cost & \\${total_cost:.4f} \\\\")
        lines.append(f"Teacher Model & {OPENAI_MODEL} \\\\")
        lines.append(r"Student Model & \texttt{gpt-oss-120b} (Cerebras) \\\\")
        lines.append(r"\bottomrule")
        lines.append(r"\end{tabular}")
        lines.append(r"\end{table}")
        lines.append("")

        body = "\n".join(lines)
        path = self.results_dir / "thesis_tables.tex"
        with open(path, "w", encoding="utf-8") as f:
            f.write(body)
        print(f"[Export] Thesis LaTeX tables written to {path}")


# =============================================================================
# YOUDEN'S J RUNNER (Calibration)
# =============================================================================

async def run_youden_calibration() -> Dict:
    """Run Youden's J calibration on the held-out calibration set (30 cases)."""
    from app.vector_store import search_knowledge, search_knowledge_fts

    cases = load_golden_retrieval_cases()
    total_cases = len(cases)
    rag_cases = [
        c for c in cases
        if c.get("expected_mode") == "vector_rag" and c.get("expected_found") and c.get("expected_source")
    ]
    print(f"\n[Calibration] Running Youden's J on golden_retrieval_cases.json ({total_cases} total, {len(rag_cases)} vector_rag)...\n")

    calibrator = YoudenJCalibrator()
    source_plant_map = _build_source_plant_map(cases)

    grade_debug = {"1.0_Exact": 0, "0.5_Family": 0, "0.25_Topic": 0, "0.0_None": 0}
    per_case_grade = defaultdict(lambda: {"1.0_Exact": 0, "0.5_Family": 0, "0.25_Topic": 0, "0.0_None": 0})

    for case in rag_cases:
        query = str(case["query"])
        source = str(case["expected_source"])
        keywords: list = case.get("expected_content_keywords") or []
        case_id = str(case["case_id"])
        category = case.get("category", "unknown")
        expected_plant = case.get("expected_plant", "")

        dense_chunks = await search_knowledge(
            query=query, match_threshold=CALIB_THRESHOLD,
            match_count=CALIB_COUNT, query_label=f"calib:{case_id}",
        )
        fts_chunks = await search_knowledge_fts(query=query, match_count=CALIB_COUNT)
        fts_keys = {(c.get("filename"), c.get("page_number")) for c in fts_chunks}

        for chunk in dense_chunks:
            sim = chunk.get("similarity", 0.0)
            key = (chunk.get("filename"), chunk.get("page_number"))
            is_cm = key in fts_keys
            chunk_source = chunk.get("source", "").strip()
            content = (chunk.get("content") or "").lower()
            source_match = chunk_source == source.strip()
            is_tp = source_match and any(kw.lower() in content for kw in keywords)
            calibrator.add_record(sim, is_tp, is_cm, category, case_id)

            relevance_grade = compute_relevance_grade(
                chunk_source=chunk_source,
                content=content,
                expected_source=source,
                expected_plant=expected_plant,
                expected_keywords=keywords,
                source_plant_map=source_plant_map,
            )
            calibrator.add_record_graded(sim, relevance_grade, is_cm, category, case_id)

            # Track grade distribution
            if relevance_grade >= 1.0:
                grade_debug["1.0_Exact"] += 1
                per_case_grade[case_id]["1.0_Exact"] += 1
            elif relevance_grade >= 0.5:
                grade_debug["0.5_Family"] += 1
                per_case_grade[case_id]["0.5_Family"] += 1
            elif relevance_grade >= 0.25:
                grade_debug["0.25_Topic"] += 1
                per_case_grade[case_id]["0.25_Topic"] += 1
            else:
                grade_debug["0.0_None"] += 1
                per_case_grade[case_id]["0.0_None"] += 1

    # Binary Youden's J
    dense_t, dense_j = calibrator.compute(cross_modal_only=False)
    hybrid_t, hybrid_j = calibrator.compute(cross_modal_only=True)

    # Graded Youden's J
    graded_dense_t, graded_dense_j = calibrator.compute_graded(cross_modal_only=False)

    histogram = calibrator.build_histogram()
    print(histogram)

    # Grade distribution debug
    total_graded = sum(grade_debug.values())
    print("\n--- Relevance Grade Distribution (all chunks) ---")
    print(f"  {'Grade':<20} {'Count':>6} {'Pct':>8}")
    print(f"  {'-'*36}")
    for label in ["1.0_Exact", "0.5_Family", "0.25_Topic", "0.0_None"]:
        cnt = grade_debug[label]
        pct = cnt / total_graded * 100 if total_graded > 0 else 0
        print(f"  {label:<20} {cnt:>6} ({pct:>5.1f}%)")
    print(f"  {'TOTAL':<20} {total_graded:>6}")
    print()

    # Per-case grade debug
    print("--- Per-Case Grade Breakdown ---")
    print(f"  {'Case ID':<35} {'Exact':>6} {'Family':>6} {'Topic':>6} {'None':>6}")
    print(f"  {'-'*60}")
    for case_id in sorted(per_case_grade.keys()):
        g = per_case_grade[case_id]
        print(f"  {case_id:<35} {g['1.0_Exact']:>6} {g['0.5_Family']:>6} {g['0.25_Topic']:>6} {g['0.0_None']:>6}")
    print()

    print(f"\n[Calibration] Youden's J Results:")
    print(f"  Binary Dense threshold:      t={dense_t:.2f}, J={dense_j:+.3f}")
    print(f"  Graded Dense threshold:      t={graded_dense_t:.2f}, J={graded_dense_j:+.3f}")
    print(f"  Hybrid threshold:            t={hybrid_t:.2f}, J={hybrid_j:+.3f}")
    print(f"  Deployed:                    Dense=0.70, Hybrid=0.70")

    # Print per-category accuracy at deployed thresholds
    print(calibrator.report_per_category(0.70))
    print()

    print(f"  Tie-Breaker Summary: {sum(1 for r in calibrator.records if r[3]=='tie_breaker')} records")
    print(f"  Species Mismatch:     {sum(1 for r in calibrator.records if r[3]=='species_mismatch')} records")
    print(f"  Phase Mismatch:       {sum(1 for r in calibrator.records if r[3]=='phase_mismatch')} records")
    print(f"  Unit Conversion:      {sum(1 for r in calibrator.records if r[3]=='unit_conversion')} records")
    print(f"  Negative Control:     {sum(1 for r in calibrator.records if r[3]=='negative_control')} records")

    # Phase 4: Retrieval Specificity Breakdown
    print("\n--- Retrieval Specificity (Top-1 RRF per case) ---")
    spec_counts = {"Exact Match": 0, "Family Match": 0, "Topic Match": 0, "Irrelevant": 0}
    for case in rag_cases:
        query = str(case["query"])
        source = str(case["expected_source"])
        keywords: list = case.get("expected_content_keywords") or []
        case_id = str(case["case_id"])
        expected_plant = case.get("expected_plant", "")

        dense_chunks = await search_knowledge(
            query=query, match_threshold=CALIB_THRESHOLD,
            match_count=1, query_label=f"spec:{case_id}",
        )
        if dense_chunks:
            top_chunk = dense_chunks[0]
            classification = classify_top1_retrieval(top_chunk, case, source_plant_map)
            spec_counts[classification] += 1

    total_spec = sum(spec_counts.values())
    print(f"  {'Classification':<20} {'Count':>6} {'Pct':>8}")
    print(f"  {'-'*36}")
    for cls, count in spec_counts.items():
        pct = count / total_spec * 100 if total_spec > 0 else 0
        print(f"  {cls:<20} {count:>4}/{total_spec} ({pct:>5.0f}%)")
    print()

    # Phase 3: System Precision
    print("[Calibration] Running System Precision evaluation on all cases...")
    system_evaluator = SystemPrecisionEvaluator()
    for i, case in enumerate(rag_cases, 1):
        print(f"  [{i}/{len(rag_cases)}] {case.get('case_id', '')}...")
        try:
            await system_evaluator.evaluate_case(case)
        except Exception as e:
            print(f"    ERROR: {e}")
    system_evaluator.print_report()
    system_evaluator.export(RESULTS_DIR / "system_precision.json")

    result = {
        "calibration_cases": len(rag_cases),
        "total_data_points": len(calibrator.records),
        "youden_dense": {"optimal_threshold": round(dense_t, 2), "j": round(dense_j, 3)},
        "youden_graded_dense": {"optimal_threshold": round(graded_dense_t, 2), "j": round(graded_dense_j, 3)},
        "youden_hybrid": {"optimal_threshold": round(hybrid_t, 2), "j": round(hybrid_j, 3)},
        "deployed_thresholds": {"dense": 0.70, "hybrid": 0.70},
        "retrieval_specificity": spec_counts,
        "system_precision": system_evaluator.compute_precision(),
    }

    # Save histogram
    with open(RESULTS_DIR / "similarity_histogram.txt", "w", encoding="utf-8") as f:
        f.write(histogram)

    # Save calibration JSON
    with open(RESULTS_DIR / "threshold_calibration.json", "w", encoding="utf-8") as f:
        json.dump(result, f, indent=2)

    print(f"\n[Calibration] Results saved to {RESULTS_DIR}")
    return result


# =============================================================================
# CROSS-LINGUAL MRR EXPERIMENT (Phase 2)
# =============================================================================

async def run_cross_lingual_experiment() -> Dict:
    """Run cross-lingual MRR experiment: ID→EN vs EN→EN retrieval."""
    from app.vector_store import search_knowledge, search_knowledge_fts

    cases = load_cross_lingual_cases()
    if not cases:
        print("[Cross-Lingual] No cross-lingual cases found. Skipping.")
        return {"error": "no cases"}

    print(f"\n[Cross-Lingual] Running ΔMRR experiment on {len(cases)} cases...")

    id_cases = [c for c in cases if c.get("query_lang") == "id"]
    en_cases = [c for c in cases if c.get("query_lang") == "en"]

    async def compute_mrr(case_list: list) -> float:
        if not case_list:
            return 0.0
        reciprocal_ranks = []
        for case in case_list:
            query = str(case["query"])
            source = str(case["expected_source"])
            keywords: list = case.get("expected_content_keywords") or []

            dense_chunks = await search_knowledge(
                query=query, match_threshold=CALIB_THRESHOLD,
                match_count=20, query_label=f"cross:{case.get('case_id','')}",
            )

            rank = None
            for idx, chunk in enumerate(dense_chunks):
                chunk_source = chunk.get("source", "").strip()
                content = (chunk.get("content") or "").lower()
                source_match = chunk_source == source.strip()
                keyword_match = any(kw.lower() in content for kw in keywords) if keywords else False
                if source_match and keyword_match:
                    rank = idx + 1
                    break

            if rank is not None:
                reciprocal_ranks.append(1.0 / rank)
            else:
                reciprocal_ranks.append(0.0)

        return sum(reciprocal_ranks) / len(reciprocal_ranks) if reciprocal_ranks else 0.0

    id_mrr = await compute_mrr(id_cases)
    en_mrr = await compute_mrr(en_cases)
    delta_mrr = abs(id_mrr - en_mrr)

    print()
    print("-" * 50)
    print("  CROSS-LINGUAL MRR RESULTS")
    print("-" * 50)
    print(f"  Indonesian → English MRR:  {id_mrr:.3f}  ({len(id_cases)} cases)")
    print(f"  English → English MRR:     {en_mrr:.3f}  ({len(en_cases)} cases)")
    print(f"  ΔMRR:                      {delta_mrr:.3f}")
    if delta_mrr < 0.15:
        print(f"  ✅ ΔMRR < 0.15 → Cross-lingual robustness demonstrated")
    else:
        print(f"  ⚠️  ΔMRR >= 0.15 → Cross-lingual gap may need mitigation")
    print("-" * 50)
    print()

    result = {
        "total_cases": len(cases),
        "id_cases": len(id_cases),
        "en_cases": len(en_cases),
        "id_mrr": round(id_mrr, 4),
        "en_mrr": round(en_mrr, 4),
        "delta_mrr": round(delta_mrr, 4),
    }

    with open(RESULTS_DIR / "cross_lingual_results.json", "w", encoding="utf-8") as f:
        json.dump(result, f, indent=2)

    print(f"[Cross-Lingual] Results saved to {RESULTS_DIR / 'cross_lingual_results.json'}")
    return result


# =============================================================================
# DRY RUN
# =============================================================================

async def run_dry_run(engine: EvaluationEngine):
    """Run full metric suite on 3 representative cases to verify pipeline."""
    print("\n" + "=" * 60)
    print("  DRY RUN MODE — 3 Cases, Full Pipeline")
    print("=" * 60)

    dry_cases = [
        {
            "case_id": "dry_quantitative",
            "case_group": "quantitative",
            "query": "Berapa suhu optimal selada fase vegetatif?",
            "ground_truth": "Suhu optimal untuk selada fase vegetatif adalah 20°C dengan rentang 18-24°C.",
            "expected_plant": "lettuce",
            "expected_stage": "vegetative",
            "response_language": "id",
            "expected_emojis": ["📚"],
        },
        {
            "case_id": "dry_phase_aware",
            "case_group": "phase_aware",
            "query": "Apa kondisi ideal chamber sekarang?",
            "ground_truth": "Chamber sedang dalam siklus malam dengan parameter suhu 18-22°C...",
            "expected_plant": None,
            "expected_stage": None,
            "response_language": "id",
            "temporal_context": {"local_hour": 23, "startNight": 22, "startDay": 6},
            "expected_emojis": ["⚠️"],
        },
        {
            "case_id": "dry_nuance_kecambah",
            "case_group": "linguistic",
            "query": "Apa perbedaan perawatan kecambah dan tunas pada fase awal?",
            "ground_truth": "Kecambah merujuk pada mung bean sprouts yang memerlukan...",
            "expected_plant": "mung_bean_sprouts",
            "expected_stage": "germination",
            "response_language": "id",
            "expected_emojis": ["📚", "📖"],
            "risk_flag": "high_risk",
        },
    ]

    for i, case in enumerate(dry_cases, 1):
        print(f"\n{'-'*50}")
        print(f"  DRY RUN CASE {i}: {case['case_id']}")
        print(f"  Query: {case['query']}")
        print(f"{'-'*50}")

        result = await engine.evaluate_single_case(case)

        print(f"\n  Answer: {result['answer'][:200]}...")
        print(f"\n  Metadata:")
        print(f"    Model: {result['model_used']}")
        print(f"    Latency: {result['latency_ms']}ms")
        print(f"    Tokens: {result['token_usage']}")
        print(f"    Chunks: {result['retrieved_chunks_count']}")
        print(f"    Parent Expansion: {result['parent_expansion']}")
        print(f"\n  Audits:")
        print(f"    Numerical Rigor: {'[OK] PASS' if result['numerical_rigor']['overall_pass'] else '[FAIL] FAIL'}")
        print(f"    Temporal Adherence: {'[OK] PASS' if result['temporal_adherence']['pass'] else '[FAIL] FAIL'}")
        print(f"    Constraint Satisfaction: {'[OK] PASS' if result['constraint_satisfaction']['pass'] else '[FAIL] FAIL'}")
        print(f"    Citation Accuracy: {'[OK] PASS' if result['citation_accuracy']['pass'] else '[FAIL] FAIL'}")

        ragas = result.get("ragas_scores", {})
        if "error" not in ragas:
            for metric in ragas.get("_metrics_computed", []):
                print(f"    RAGAS {metric}: {ragas.get(metric, 'N/A')}")
        else:
            print(f"    RAGAS: {ragas.get('error', 'N/A')}")

        tn = result.get("terminology_nuance")
        if tn:
            print(f"    Terminology Accuracy: {tn['accuracy']:.0%} ({tn['passed']}/{tn['total']})")

        print(f"    Est. Cost: ${result['estimated_cost_usd']:.6f}")

    print(f"\n{'='*60}")
    print("  DRY RUN COMPLETE")
    engine.print_cost_summary()

    # Check for anomalous critic reasoning
    log_path = RESULTS_DIR / "critic_reasoning_log.jsonl"
    if log_path.exists():
        with open(log_path, encoding="utf-8") as f:
            entries = [json.loads(line) for line in f if line.strip()]
        if len(entries) > 0:
            print(f"  Critic calls logged: {len(entries)}")
            print(f"  Check {log_path} for detailed reasoning.")

    engine.export_results_log()
    engine.export_results_data()
    engine.export_thesis_tables()

    print("\n  WARNING: Review critic reasoning for Indonesian terminology interpretation.")
    print("  If reasoning sounds inconsistent, consider upgrading to gpt-5.5 for final run.\n")


# =============================================================================
# FULL EVALUATION RUN
# =============================================================================

async def run_full_evaluation(engine: EvaluationEngine, only: Optional[str] = None):
    """Run full evaluation on all available test cases."""
    print(f"\n{'='*60}")
    print(f"  FULL EVALUATION RUN")
    print(f"{'='*60}")

    all_cases = []
    human_cases = load_golden_qa_cases()
    synthetic_cases = load_synthetic_qa_cases()

    if only in (None, "human"):
        all_cases.extend(human_cases)
    elif only == "rag":
        all_cases.extend([c for c in human_cases if c.get("case_group") == "rag_qualitative"])
    elif only == "adversarial":
        all_cases.extend([c for c in human_cases if c.get("case_group") != "rag_qualitative"])
    if only in (None, "synthetic"):
        all_cases.extend(synthetic_cases)

    if not all_cases:
        print("[Evaluation] No test cases found. Use --regenerate-synthetic to generate synthetic cases.")
        return

    print(f"\n  Loaded {len(human_cases)} human-adversarial + {len(synthetic_cases)} synthetic cases.")
    print(f"  Total: {len(all_cases)} cases.")

    # Validate ground truths
    warnings_gt = validate_ground_truths(human_cases)
    for w in warnings_gt:
        print(f"  {w}")

    # Run calibration first
    calib_result = await run_youden_calibration() if only != "adversarial" else None

    cross_result = await run_cross_lingual_experiment() if only != "adversarial" else None

    print(f"\n{'-'*50}")
    print(f"  Starting evaluation of {len(all_cases)} cases...")
    print(f"{'-'*50}")

    for i, case in enumerate(all_cases, 1):
        print(f"\n  [{i}/{len(all_cases)}] {case.get('case_id', case['query'][:40])}...")
        await engine.evaluate_single_case(case)
        if i % 10 == 0:
            engine.print_cost_summary()

    engine.export_csv()
    engine.export_summary()
    engine.export_results_log()
    engine.export_results_data()
    engine.export_thesis_tables(calib_result, cross_result, only=only)
    engine.print_cost_summary()

    print(f"\n{'='*60}")
    print("  EVALUATION COMPLETE")
    print(f"  Results: {RESULTS_DIR}")
    print(f"{'='*60}\n")


# =============================================================================
# CLI ENTRY POINT
# =============================================================================

async def run_calibration_only():
    """Run Youden calibration + cross-lingual experiment."""
    calib_result = await run_youden_calibration()
    cross_result = await run_cross_lingual_experiment()

    print()
    print("=" * 50)
    print("  CALIBRATION SUMMARY")
    print("=" * 50)
    jd = calib_result.get("youden_dense", {})
    jg = calib_result.get("youden_graded_dense", {})
    sp = calib_result.get("system_precision", {})
    cr = cross_result
    print(f"  Retriever Youden's J (binary):          {jd.get('j', 0):+.3f}")
    print(f"  Retriever Youden's J (graded):          {jg.get('j', 0):+.3f}")
    print(f"  System Precision:                       {sp.get('system_precision', 0):.2%}")
    print(f"  Cross-Lingual ΔMRR:                     {cr.get('delta_mrr', 0):.3f}")
    print("=" * 50)
    print()

    return calib_result


def main():
    import argparse
    parser = argparse.ArgumentParser(
        description="PGC RAGAS Evaluation Framework (Cerebras Edition)",
    )
    parser.add_argument("--mode", choices=["dry_run", "full", "calibrate"], default="dry_run",
                        help="dry_run (3 cases), full (all 100+ cases), calibrate (J + cross-lingual only)")
    parser.add_argument("--only", choices=["human", "synthetic", "rag", "adversarial"], default=None,
                        help="Restrict evaluation arm: 'human' (all 65 golden cases), 'synthetic', "
                             "'rag' (20 rag_qualitative cases only), 'adversarial' (original 45 non-RAG cases)")
    parser.add_argument("--output-dir", default=None,
                        help="Override output directory (default: results/<only>/ or results/)")
    parser.add_argument("--regenerate-synthetic", action="store_true",
                        help="Force regenerating the synthetic dataset")
    parser.add_argument("--run-cross-lingual", action="store_true",
                        help="Also run cross-lingual MRR experiment")
    args = parser.parse_args()

    if not OPENAI_API_KEY:
        print("ERROR: OPENAI_API_KEY environment variable not set.")
        print("Add it to AI Chatbot/.env or set it as an environment variable.")
        print("This is required for the gpt-4o-mini critic (Teacher model).")
        sys.exit(1)

    if not CEREBRAS_API_KEY:
        print("WARNING: CEREBRAS_API_KEY not set. Generation calls will fail.")
        print("Add it to AI Chatbot/.env")

    if args.regenerate_synthetic:
        if not HAS_RAGAS:
            print("ERROR: Cannot regenerate synthetic dataset. RAGAS not installed.")
            sys.exit(1)
        asyncio.run(generate_synthetic_dataset())

    # Determine output directory: --output-dir overrides, then auto-subdirectory for named arms
    if args.output_dir:
        results_dir = Path(args.output_dir)
    elif args.only in ("rag", "adversarial", "synthetic"):
        results_dir = RESULTS_DIR / args.only
    else:
        results_dir = RESULTS_DIR  # default: results/ (unchanged for --only human or unset)

    engine = EvaluationEngine(results_dir=results_dir)
    if args.mode == "dry_run":
        asyncio.run(run_dry_run(engine))
    elif args.mode == "calibrate":
        asyncio.run(run_calibration_only())
    else:
        asyncio.run(run_full_evaluation(engine, only=args.only))


if __name__ == "__main__":
    main()