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

import requests
from fastapi import FastAPI, UploadFile, File, Form, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from PIL import Image, ImageOps, ImageFilter
import pytesseract
import os

# Serve static files from outputs directory
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
from dotenv import load_dotenv
load_dotenv()

# Optional extractors for DOCX/PDF
try:
    from docx import Document  # python-docx
except Exception:
    Document = None

try:
    from pypdf import PdfReader
except Exception:
    PdfReader = None

try:
    from reportlab.pdfgen import canvas
    from reportlab.lib.pagesizes import letter
    from reportlab.lib import colors
    from reportlab.lib.utils import ImageReader
    import reportlab
except Exception as e:
    reportlab = None
    print(f"[WARN] reportlab import failed: {e}")

try:
    from pdf2image import convert_from_bytes  # requires poppler
except Exception:
    convert_from_bytes = None

from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity


# ✅ Gemini SDK
try:
    from google import genai
except Exception as e:
    genai = None
    print(f"[WARN] google-genai import failed: {e}")

# ✅ Google Cloud Vision SDK (for better handwritten OCR)
try:
    from google.cloud import vision
    from google.cloud.vision_v1 import types
    google_vision_available = True
except Exception as e:
    google_vision_available = False
    print(f"[WARN] google-cloud-vision import failed: {e}")



app = FastAPI()
app.mount("/files", StaticFiles(directory="outputs"), name="files")


outputs_dir = "outputs"
os.makedirs(outputs_dir, exist_ok=True)

app.mount("/outputs", StaticFiles(directory=outputs_dir), name="outputs")

# Create outputs directory if it doesn't exist
outputs_dir = os.path.join(os.path.dirname(__file__), "outputs")
os.makedirs(outputs_dir, exist_ok=True)

@app.get("/outputs/{filename}")
async def get_output_file(filename: str):
    """Serve files from the outputs directory."""
    filepath = os.path.join(outputs_dir, filename)
    if os.path.exists(filepath):
        return FileResponse(filepath)
    raise HTTPException(status_code=404, detail="File not found")

@app.get("/storage/{filename}")
async def get_storsge_file(filename:str):
    """Serve files from the storage directory."""
    filepath = os.path.join(outputs_dir, filename)
    if os.path.exists(filepath):
        return FileResponse(filepath)
    raise HTTPException(status_code=404, detail="File not found")

@app.get("/debug/erp-row")
async def debug_erp_row(homework_id: int, student_id: int):
    """Debug endpoint: shows the raw ERP row so you can see all field names."""
    try:
        row = fetch_student_record(homework_id, student_id)
        return {"erp_row": row, "keys": list(row.keys())}
    except Exception as e:
        return {"error": str(e)}

@app.get("/debug/env")
def debug_env():
    return {
        "has_gemini_keys": bool(GOOGLE_API_KEYS),
        "num_keys": len(GOOGLE_API_KEYS),
        "has_openai_key": bool(os.getenv("OPENAI_API_KEY")),
    }
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)



if os.name == "nt":
    pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
else:
    pytesseract.pytesseract.tesseract_cmd = "/usr/bin/tesseract"



ERP_BASE = os.getenv("ERP_BASE", "https://erp.triz.co.in/lms_data")
STORAGE_BASE = os.getenv("STORAGE_BASE", "https://erp.triz.co.in/storage/student/")
ERP_TOKEN = os.getenv("ERP_TOKEN", "")


def get_public_base_url() -> str:
    """
    Returns the public base URL of this server.
    Priority:
      1. SPACE_HOST  — set automatically by Hugging Face Spaces (most reliable)
      2. HF_SPACE    — manual fallback env var for HF
      3. APP_BASE_URL — custom deployment domain
      4. localhost   — local dev only
    """
    hf_host = os.getenv("SPACE_HOST", "").strip()
    if hf_host:
        return f"https://{hf_host}"

    hf_space = os.getenv("HF_SPACE", "").strip()
    if hf_space:
        return f"https://{hf_space}"

    custom = os.getenv("APP_BASE_URL", "").strip()
    if custom:
        return custom.rstrip("/")

    return "http://127.0.0.1:7860"


def build_pdf_url(filename: str) -> str:
    """Given a saved PDF filename, return its full public URL."""
    if not filename:
        return ""
    return f"{get_public_base_url()}/outputs/{filename}"


def make_question_marks(mcq_results: list) -> list:
    """
    Convert internal mcq_results into a clean list the frontend can use
    to show ✓ ✗ ○ next to each question number.

    Each item:
      {
        "qid":            "Q1",
        "mark":           "correct" | "wrong" | "unattempted",
        "student_answer": "A",          # what the student chose (empty if unattempted)
        "correct_answer": "B"           # the right answer (null if unknown)
      }
    """
    result = []
    for r in (mcq_results or []):
        if r.get('unattempted'):
            mark = "unattempted"
        elif r.get('correct') is True:
            mark = "correct"
        else:
            mark = "wrong"
        result.append({
            "qid":            r.get('qid', ''),
            "mark":           mark,
            "student_answer": r.get('chosen', ''),
            "correct_answer": r.get('correct_answer'),
        })
    return result


# API Key Rotation - Support multiple API keys for higher limits
GOOGLE_API_KEYS = []
for i in range(1, 10):  # Support up to 10 API keys
    key = os.getenv(f"GOOGLE_API_KEY_{i}", "").strip()
    if key:
        GOOGLE_API_KEYS.append(key)

# Fallback to single key if no multi-key config
GOOGLE_API_KEY = (os.getenv("GOOGLE_API_KEY") or "").strip()
if not GOOGLE_API_KEYS and GOOGLE_API_KEY:
    GOOGLE_API_KEYS = [GOOGLE_API_KEY]

# Track current key index and rate-limited keys
current_key_index = 0
rate_limited_keys = set()  # Track keys that are rate limited

GEMINI_MODEL = (os.getenv("GEMINI_MODEL", "models/gemini-flash-lite-latest") or "").strip()
if GEMINI_MODEL and not GEMINI_MODEL.startswith("models/"):
    GEMINI_MODEL = "models/" + GEMINI_MODEL


GOOGLE_CLOUD_VISION_API_KEY = (os.getenv("GCV_API_KEY") or "").strip()
# Fall back to Gemini API key if no separate Vision key provided
if not GOOGLE_CLOUD_VISION_API_KEY and GOOGLE_API_KEY:
    GOOGLE_CLOUD_VISION_API_KEY = GOOGLE_API_KEY

vision_client = None
if google_vision_available and GOOGLE_CLOUD_VISION_API_KEY:
    try:
        # Use API key authentication
        vision_client = vision.ImageAnnotatorClient(client_options={
            'api_key': GOOGLE_CLOUD_VISION_API_KEY
        })
        print("[INFO] Google Cloud Vision client initialized")
    except Exception as e:
        print(f"[WARN] Google Cloud Vision init failed: {e}")

gemini_client = None
GEMINI_LAST_ERROR = ""

def _init_gemini_client(key_index: int = 0) -> None:
    """Initialize Gemini client with the API key at the given index."""
    global gemini_client, GEMINI_LAST_ERROR, current_key_index
    
    current_key_index = key_index
    
    if not GOOGLE_API_KEYS:
        GEMINI_LAST_ERROR = "No GOOGLE_API_KEY configured"
        gemini_client = None
        return
    
    if key_index >= len(GOOGLE_API_KEYS):
        GEMINI_LAST_ERROR = "All API keys rate limited or exhausted"
        gemini_client = None
        return
    
    api_key = GOOGLE_API_KEYS[key_index]

    if not genai:
        GEMINI_LAST_ERROR = "google-genai not installed / import failed"
        gemini_client = None
        return

    if not api_key:
        GEMINI_LAST_ERROR = f"GOOGLE_API_KEY_{key_index + 1} not set"
        gemini_client = None
        return

    try:
        gemini_client = genai.Client(api_key=api_key)
        GEMINI_LAST_ERROR = ""
        print(f"[INFO] Gemini client initialized with key #{key_index + 1}")
    except Exception as e:
        gemini_client = None
        GEMINI_LAST_ERROR = str(e)
        print(f"[WARN] Gemini init failed: {GEMINI_LAST_ERROR}")


def _is_rate_limit_error(error_msg: str) -> bool:
    """Check if the error is a rate limit error (429) or service unavailable (503)."""
    if not error_msg:
        return False
    lower = error_msg.lower()
    return ("429" in lower or 
            "503" in lower or 
            "rate_limit" in lower or 
            "resource_exhausted" in lower or 
            "rate limit" in lower or
            "unavailable" in lower)


def _rotate_to_next_key() -> bool:
    """Rotate to the next available API key. Returns True if successful, False if all keys exhausted."""
    global current_key_index, rate_limited_keys
    
    if len(GOOGLE_API_KEYS) <= 1:
        return False
    
    # Mark current key as rate limited
    rate_limited_keys.add(current_key_index)
    print(f"[WARN] Key #{current_key_index + 1} rate limited, rotating to next key...")
    
    # Find next available key
    attempts = 0
    while attempts < len(GOOGLE_API_KEYS):
        current_key_index = (current_key_index + 1) % len(GOOGLE_API_KEYS)
        if current_key_index not in rate_limited_keys:
            _init_gemini_client(current_key_index)
            if gemini_client:
                print(f"[INFO] Rotated to API key #{current_key_index + 1}")
                return True
        attempts += 1
    
    # All keys exhausted
    GEMINI_LAST_ERROR = "All API keys are rate limited"
    print("[ERROR] All API keys are rate limited")
    return False


_init_gemini_client(0)


def parse_gemini_error(error_msg: str) -> dict:
    msg = (error_msg or "").strip()
    lower = msg.lower()

    if "service_disabled" in lower or "generativelanguage.googleapis.com" in lower:
        return {"ok": False, "error_type": "GEMINI_SERVICE_DISABLED", "message": msg}

    if "api key" in lower or "invalid" in lower or "permission" in lower or "unauthorized" in lower:
        return {"ok": False, "error_type": "GEMINI_KEY_OR_PERMISSION_ERROR", "message": msg}

    return {"ok": False, "error_type": "GEMINI_ERROR", "message": msg}



def extract_qid_from_prompt(prompt: str, erp_row: dict = None) -> str:
    """
    Extract the question number (e.g. 'Q5') from the ERP row or prompt string.
    Priority:
      1. Direct field in erp_row: question_no, q_no, sr_no, serial_no, qno, question_number, q_number, order, position
      2. Pattern match in prompt text: 'Q5:', 'Question 5:', '5.', '5)'
      3. Falls back to 'Q1' if nothing found.
    """
    import re as _re

    # Priority 1: check ERP row fields directly
    if erp_row and isinstance(erp_row, dict):
        for field in ("question_no", "q_no", "qno", "sr_no", "serial_no",
                      "question_number", "q_number", "order", "position",
                      "question_order", "q_order", "seq", "sequence", "index"):
            val = erp_row.get(field)
            if val is not None:
                try:
                    num = int(str(val).strip())
                    if 1 <= num <= 200:
                        print(f"[INFO] extract_qid: found Q{num} from erp_row['{field}']={val}")
                        return f"Q{num}"
                except (ValueError, TypeError):
                    pass

    # Priority 2: parse from prompt text
    p = (prompt or "").strip()
    m = _re.match(r'^[Qq]\s*(\d+)', p)
    if m:
        return f"Q{m.group(1)}"
    m2 = _re.match(r'^Question\s*(\d+)', p, _re.IGNORECASE)
    if m2:
        return f"Q{m2.group(1)}"
    m3 = _re.match(r'^(\d+)[.)\s]', p)
    if m3:
        return f"Q{m3.group(1)}"
    first_line = p.split('\n')[0]
    m4 = _re.search(r'[Qq]\s*(\d+)', first_line)
    if m4:
        return f"Q{m4.group(1)}"

    print(f"[WARN] extract_qid: could not determine question number from prompt={repr(p[:80])} erp_row_keys={list((erp_row or {}).keys())}")
    return "Q1"


def generate_gemini_response(
    prompt: str,
    system_prompt: str = "",
    max_tokens: int = 650,
    temperature: float = 0.3,
) -> str:
    global GEMINI_LAST_ERROR, gemini_client, rate_limited_keys

    if gemini_client is None:
        if not GEMINI_LAST_ERROR:
            GEMINI_LAST_ERROR = "Gemini client not initialized"
        # Try to reinitialize if we have keys available
        if GOOGLE_API_KEYS and current_key_index not in rate_limited_keys:
            _init_gemini_client(current_key_index)
        if gemini_client is None:
            return ""

    try:
        contents = []
        if system_prompt:
            contents.append(system_prompt)
        contents.append(prompt)

        resp = gemini_client.models.generate_content(
            model=GEMINI_MODEL,
            contents=contents,
            config={"temperature": temperature, "max_output_tokens": max_tokens},
        )
        text = (getattr(resp, "text", "") or "").strip()
        if text:
            GEMINI_LAST_ERROR = ""
        return text
    except Exception as e:
        error_msg = str(e)
        print(f"[ERROR] Gemini call failed: {error_msg}")
        
        # Check if it's a rate limit error and try to rotate
        if _is_rate_limit_error(error_msg):
            GEMINI_LAST_ERROR = error_msg
            if _rotate_to_next_key():
                # Retry with new key
                return generate_gemini_response(prompt, system_prompt, max_tokens, temperature)
        
        GEMINI_LAST_ERROR = error_msg
        return ""

import time

def generate_gemini_with_retry(prompt: str, system_prompt: str, max_tokens=450, temperature=0.3, retries=3) -> str:
    last = ""
    for i in range(retries):
        text = generate_gemini_response(
            prompt=prompt,
            system_prompt=system_prompt,
            max_tokens=max_tokens,
            temperature=temperature,
        )
        if text:
            return text
        last = GEMINI_LAST_ERROR
        # small backoff
        time.sleep(1 + i)
    return ""

def cheap_overlap_score(student_text: str, prompt: str) -> int:
    # remove tiny words
    def tokens(s):
        return {w for w in re.findall(r"[a-zA-Z]{4,}", (s or "").lower())}
    s = tokens(student_text)
    p = tokens(prompt)
    if not s or not p:
        return 0
    overlap = len(s & p) / max(1, len(p))
    # map to a sane range
    return int(round(min(0.6, overlap) * 100))  # cap at 60



def _norm(s: str) -> str:
    return re.sub(r"\s+", " ", (s or "").strip().lower())


def cosine_sim(a: str, b: str) -> float:
    a = (a or "").strip().lower()
    b = (b or "").strip().lower()
    if not a or not b:
        return 0.0
    vec = TfidfVectorizer().fit([a, b])
    X = vec.transform([a, b])
    return float(cosine_similarity(X[0], X[1])[0][0])


def normalize_level(level: str) -> str:
    l = (level or "").strip().lower()
    if l in ("easy",):
        return "Easy"
    if l in ("hard",):
        return "Hard"
    if l in ("meadium", "mediam", "medium"):
        return "Medium"
    return "Medium"


def level_policy(student_level: str) -> dict:
    lvl = normalize_level(student_level).lower()
    if lvl == "easy":
        return {"w_sim": 0.8, "w_cov": 0.2, "verified": 65, "partial": 40, "kp_thr": 0.25}
    if lvl == "hard":
        return {"w_sim": 0.4, "w_cov": 0.6, "verified": 85, "partial": 65, "kp_thr": 0.40}
    return {"w_sim": 0.6, "w_cov": 0.4, "verified": 75, "partial": 55, "kp_thr": 0.20}


def mcq_partial_credit(student_level: str) -> dict:
    """
    Returns partial credit percentage for MCQ questions based on student level.
    This allows easier students to get partial marks even if they get some questions wrong.
    
    Returns dict with:
    - credit_per_question: percentage earned per correct answer
    - passing_threshold: minimum percentage needed to pass
    """
    lvl = normalize_level(student_level).lower()
    if lvl == "easy":
        # Easy students get 50% credit per correct answer
        return {"credit_per_question": 50, "passing_threshold": 50}
    if lvl == "hard":
        # Hard students need 100% - no partial credit
        return {"credit_per_question": 100, "passing_threshold": 100}
    # Medium students get 75% credit per correct answer
    return {"credit_per_question": 75, "passing_threshold": 75}


def keypoint_coverage(student_text: str, key_points: List[str], kp_threshold: float) -> Tuple[List[str], List[str], float]:
    covered, missing = [], []
    for kp in key_points:
        kp = (kp or "").strip()
        if not kp:
            continue
        s = cosine_sim(kp, student_text)
        if s >= kp_threshold:
            covered.append(kp)
        else:
            missing.append(kp)

    total = len(covered) + len(missing)
    coverage = (len(covered) / total) if total else 0.0
    return covered, missing, coverage



def infer_question_type_from_prompt(prompt: str, student_text: str = "") -> str:
    p = _norm(prompt)

    # Explicit markers - check for (mcq) first since it's common in parentheses
    if re.search(r"\(mcq\)", p) or re.search(r"\btype\s*:\s*mcq\b", p) or re.search(r"\bquestion_type\s*:\s*mcq\b", p):
        return "mcq"
    if re.search(r"\btype\s*:\s*narrative\b", p) or re.search(r"\bquestion_type\s*:\s*narrative\b", p):
        return "narrative"

    # Heuristic: options A/B/C/D exist in prompt -> likely MCQ
    if re.search(r"\b(a|b|c|d)\s*[\)\.]\s+", p) or "option a" in p or "option b" in p:
        return "mcq"
    
    # Check if prompt contains common MCQ keywords
    if re.search(r"\bchoose the correct|which is correct|select the right|multiple choice|single answer\b", p):
        return "mcq"

    # Check student answer for MCQ indicators if provided
    if student_text:
        s = _norm(student_text)
        # If student answer contains Option A/B/C/D, treat as MCQ
        if re.search(r"\boption\s*[a-d]\b", s) or re.search(r"^\(?\s*[a-d]\s*\)?$", s.strip()):
            return "mcq"
        # If answer starts with A. or B. etc.
        if re.search(r"^[a-d]\.\s+", s.strip()):
            return "mcq"

    return "narrative"


def parse_questions_from_prompt(prompt: str) -> List[Dict[str, Any]]:
    """
    Parse individual questions from the prompt, detecting MCQ vs Narrative for each.
    Returns list of dicts with: qid, type, question_text, correct_answer (for MCQ)
    """
    questions = []
    # Match patterns like "Q1:", "Q2.", "Question 1:", etc.
    q_pattern = re.compile(r'(Q\s*\d+[.:]\s*|Question\s*\d+[.:]\s*)(.*?)(?=(Q\s*\d|Question\s*\d|$))', re.IGNORECASE | re.DOTALL)
    
    # Alternative: split by Q1, Q2, etc.
    lines = prompt.split('\n')
    current_q = None
    current_type = None
    current_qid = None
    current_correct = None
    
    for line in lines:
        line = line.strip()
        if not line:
            continue
        
        # Detect new question
        q_match = re.match(r'^(Q\s*\d+|Question\s*\d+)[.:]\s*(.*)', line, re.IGNORECASE)
        if q_match:
            # Save previous question if exists
            if current_q is not None:
                questions.append({
                    'qid': current_qid,
                    'type': current_type,
                    'question': current_q,
                    'correct_answer': current_correct
                })
            # Start new question
            current_qid = q_match.group(1).strip()
            remaining = q_match.group(2).strip()
            current_q = remaining
            current_type = None
            current_correct = None
            
            # Check if this is MCQ or Narrative
            line_lower = line.lower()
            if '(mcq)' in line_lower or 'multiple choice' in line_lower or 'type: mcq' in line_lower:
                current_type = 'mcq'
            elif 'narrative' in line_lower or 'type: narrative' in line_lower:
                current_type = 'narrative'
        else:
            # This line belongs to current question
            if current_q is not None:
                current_q += ' ' + line
                
                # Check for type markers
                line_lower = line.lower()
                if current_type is None:
                    if '(mcq)' in line_lower or 'multiple choice' in line_lower or 'type: mcq' in line_lower:
                        current_type = 'mcq'
                    elif 'narrative' in line_lower or 'type: narrative' in line_lower:
                        current_type = 'narrative'
                
                # Check for correct answer (for MCQ)
                if current_type == 'mcq':
                    # First check: is this line "Correct Answer(s):" with nothing after it?
                    # If so, we need to look for the answer on the next line
                    if re.search(r'^correct\s*answer\s*\(?s\)?\s*[:\.]?\s*$', line, re.IGNORECASE):
                        # Set flag to look for answer on next line
                        current_q += ' [CORRECT_ANSWER_PENDING]'
                        continue
                    
                    # Check if we have a pending correct answer marker
                    if '[CORRECT_ANSWER_PENDING]' in current_q:
                        # This line should contain the answer like "A. Devdatta"
                        letter_match = re.search(r'^([A-D])\.?\s*', line)
                        if letter_match:
                            current_correct = letter_match.group(1).upper()
                            # Remove the pending marker from question text
                            current_q = current_q.replace(' [CORRECT_ANSWER_PENDING]', '')
                            continue
                        else:
                            # Not a letter, remove the pending marker
                            current_q = current_q.replace(' [CORRECT_ANSWER_PENDING]', '')
                    
                    # Look for "Correct Answer(s):" or "Correct:" or "Answer:" in same line
                    # Support formats: "Correct Answer(s): A.", "Correct: B", "Answer: C"
                    correct_match = re.search(r'(?:Correct\s*(?:Answer)?|Answer)[:.]\s*(?:[A-D]\.?\s*)?(.+)', line, re.IGNORECASE)
                    if correct_match and not current_correct:
                        # Extract just the letter (A, B, C, or D)
                        correct_text = correct_match.group(1).strip()
                        letter_match = re.search(r'^([A-D])\b', correct_text)
                        if letter_match:
                            current_correct = letter_match.group(1).upper()
                        else:
                            # Try to extract first letter
                            current_correct = correct_text[0].upper() if correct_text else None
    
    # Don't forget the last question
    if current_q is not None:
        questions.append({
            'qid': current_qid,
            'type': current_type,
            'question': current_q,
            'correct_answer': current_correct
        })
    
    # If no questions parsed, fall back to old behavior
    if not questions:
        qtype = infer_question_type_from_prompt(prompt)
        return [{'qid': extract_qid_from_prompt(prompt), 'type': qtype, 'question': prompt, 'correct_answer': None}]
    
    return questions


def extract_mcq_choice(text: str) -> str:
    """
    Extract chosen option from student text:
    supports: A, (B), Option C, Ans: D, Answer: B
    """
    t = _norm(text)

    m = re.search(r"\b(answer|ans|selected)\s*[:\-]?\s*\(?\s*([a-d])\s*\)?\b", t)
    if m:
        return m.group(2)

    m2 = re.search(r"\boption\s*([a-d])\b", t)
    if m2:
        return m2.group(1)

    m3 = re.search(r"^\(?\s*([a-d])\s*\)?$", t.strip())
    if m3:
        return m3.group(1)

    # last-resort: find first standalone A/B/C/D
    m4 = re.search(r"\b([a-d])\b", t)
    if m4:
        return m4.group(1)

    return ""


def extract_mcq_answers_with_qid(text: str) -> Dict[str, str]:
    """
    Extract MCQ answers WITH question numbers from student text.
    This handles shuffled answers where question numbers are needed to match.
    
    Supports patterns like:
    - "Q1: A, Q2: C, Q3: B"
    - "Q1. A Q2. C Q3. B"
    - "1) A 2) C 3) B"
    - "Answer 1: A Answer 2: C Answer 3: B"
    - "Q1 A Q2 C Q3 B" (space separated)
    
    Returns dict like: {"Q1": "A", "Q2": "C", "Q3": "B"}
    """
    results = {}
    t = (text or "").strip()
    
    if not t:
        return results

    # Pattern 1: Q1: A, Q2. B, Q3 - C, Question 4: D
    pattern1 = re.compile(r'(Q(?:uestion)?\s*(\d+))[:.\-\s]+([a-dA-D])', re.IGNORECASE)
    for match in pattern1.finditer(t):
        qnum = match.group(2)
        answer = match.group(3).upper()
        results[f"Q{qnum}"] = answer
    
    # Pattern 2: 1) A, 2) B, 3: C (numbered without Q prefix)
    pattern2 = re.compile(r'(?:^|\s)(\d+)\s*[\):\.]\s*([a-dA-D])(?:\s|$)', re.IGNORECASE)
    for match in pattern2.finditer(t):
        qnum = match.group(1)
        answer = match.group(2).upper()
        # Only add if not already found (Q pattern takes priority)
        if f"Q{qnum}" not in results:
            results[f"Q{qnum}"] = answer
    
    # Pattern 3: "Answer for Q1 is A", "Answer to question 2: B"
    pattern3 = re.compile(r'(?:answer|ans)\s*(?:for|to)?\s*(?:Q(?:uestion)?\s*)?(\d+)\s*(?:is|was)?\s*[:\-]?\s*([a-dA-D])', re.IGNORECASE)
    for match in pattern3.finditer(t):
        qnum = match.group(1)
        answer = match.group(2).upper()
        if f"Q{qnum}" not in results:
            results[f"Q{qnum}"] = answer
    
    # Pattern 4: Line by line format like "Q1 A" or "1 A" on same line
    pattern4 = re.compile(r'(?:^|\n)\s*(Q(?:uestion)?\s*)?(\d+)\s+([a-dA-D])\s*(?:\n|\s{2,}|$)', re.IGNORECASE)
    for match in pattern4.finditer(t):
        qnum = match.group(2)
        answer = match.group(3).upper()
        if f"Q{qnum}" not in results:
            results[f"Q{qnum}"] = answer
    
    return results


def extract_correct_mcq_from_prompt(prompt: str) -> str:
    """
    This is IMPORTANT:
    Your prompt must contain correct option somewhere like:
      - Correct: B
      - Answer: C
      - correct_option: D
      - Correct Answer(s): A. Devdatta
    or JSON: {"correct_option":"B"}
    
    Supports formats:
      - "Correct Answer: A"
      - "Correct Answer(s): A. Devdatta"
      - "Correct: B"
      - "Answer: C"
    """
    p = (prompt or "").strip()
    if not p:
        return ""

    # JSON prompt support
    if p.startswith("{") and p.endswith("}"):
        try:
            obj = json.loads(p)
            for k in ("correct_option", "correct", "answer", "ans"):
                v = obj.get(k)
                if isinstance(v, str) and v.strip():
                    return extract_mcq_choice(v)
        except Exception:
            pass

    # Text prompt support - new format: "Correct Answer(s): A. Devdatta" or "Correct Answer: B"
    t = _norm(p)
    
    # Pattern 1: "Correct Answer(s): A. ..." or "Correct Answer: B. ..."
    # This handles format like "Correct Answer(s): A. Devdatta" or "Correct Answer(s):
    #    A. Devdatta"
    m1 = re.search(r"correct\s*answer\s*\(?s\)?\s*[:\.]\s*([a-d])\.?\s*", t)
    if m1:
        return m1.group(1)
    
    # Pattern 1b: Handle multi-line format where answer is on next line like:
    # "Correct Answer(s):\n   A. Devdatta"
    m1b = re.search(r"correct\s*answer\s*\(?s\)?\s*[:\.]\s*\n\s*([a-d])\.?", t)
    if m1b:
        return m1b.group(1)
    
    # Pattern 1c: Handle format with option text after letter like "Correct Answer(s): A. Devdatta"
    m1c = re.search(r"correct\s*answer\s*\(?s\)?\s*[:\.]\s*([a-d])\.", t)
    if m1c:
        return m1c.group(1)
    
    # Pattern 2: "Correct: A" or "Answer: B" (original pattern)
    m = re.search(r"\b(correct|answer|ans)\s*[:\-]?\s*\(?\s*([a-d])\s*\)?\b", t)
    if m:
        return m.group(2)

    return ""



def _erp_get(params: dict) -> list:
    headers = {}
    if ERP_TOKEN:
        headers["Authorization"] = f"Bearer {ERP_TOKEN}"

    r = requests.get(ERP_BASE, params=params, headers=headers, timeout=30)
    r.raise_for_status()
    data = r.json()
    if not isinstance(data, list):
        raise HTTPException(status_code=502, detail="ERP returned invalid JSON (expected list).")
    return data


def fetch_student_record(homework_id: int, student_id: int) -> Dict[str, Any]:
    data = _erp_get({"table": "homework", "filters[id]": homework_id, "filters[student_id]": student_id})
    if not data:
        raise HTTPException(status_code=404, detail="No ERP record found for this homework_id + student_id")
    return data[0]


def fetch_student_level_from_erp(row: Dict[str, Any]) -> str:
    """
    ERP field name is not guaranteed; try common ones.
    """
    for k in ("student_level", "level", "difficulty", "difficulty_level"):
        v = row.get(k)
        if isinstance(v, str) and v.strip():
            return normalize_level(v)
    return "Medium"



def _preprocess_for_ocr(img: Image.Image) -> Image.Image:
    """
    Enhanced preprocessing for better OCR on handwritten images.
    Includes adaptive thresholding, noise removal, and contrast enhancement.
    """
    # Convert to grayscale
    img = img.convert("L")
    
    w, h = img.size
    
    # Scale up for better detail (especially for handwritten)
    if max(w, h) < 2000:
        scale = 2000 / max(w, h)
        new_w = int(w * scale)
        new_h = int(h * scale)
        img = img.resize((new_w, new_h), Image.LANCZOS)
    
    # Apply adaptive thresholding for better handwritten recognition
    from PIL import ImageFilter
    
    # Try multiple preprocessing approaches and use the best
    img_enhanced = img
    
    # Method 1: Increase contrast significantly
    img_contrast = img.point(lambda p: 255 if p > 180 else int(p * 1.5))
    
    # Method 2: Apply sharpening twice for handwritten
    img_sharp = img.filter(ImageFilter.SHARPEN)
    img_sharp = img_sharp.filter(ImageFilter.SHARPEN)
    
    # Method 3: Apply unsharp mask for edge enhancement
    img_unsharp = img.filter(ImageFilter.UnsharpMask(radius=2, percent=150, threshold=3))
    
    # Use the sharpened version as primary
    img = img_sharp
    
    # Apply binary threshold with lower cutoff to capture lighter handwriting
    img = img.point(lambda p: 255 if p > 160 else 0)
    
    return img


def _extract_text_google_vision(image_bytes: bytes) -> str:
    """
    Extract text using Google Cloud Vision API - much better for handwriting.
    Returns empty string if API is not available.
    """
    global vision_client
    
    if not vision_client:
        return ""
    
    try:
        # Create image object
        image = vision.Image(content=image_bytes)
        
        # Use document text detection for better handwriting
        response = vision_client.document_text_detection(image=image)
        
        if response.texts:
            return "\n".join([t.description for t in response.texts])
        return ""
    except Exception as e:
        print(f"[WARN] Google Vision OCR failed: {e}")
        return ""


def _extract_text_gemini_vision(image_bytes: bytes, mime_type: str = "image/jpeg") -> str:
    """
    Use Gemini multimodal vision to extract handwritten or printed text from an image.
    This is significantly better than Tesseract for handwritten notes.
    """
    if not gemini_client:
        return ""

    import base64
    try:
        image_b64 = base64.b64encode(image_bytes).decode("utf-8")

        resp = gemini_client.models.generate_content(
            model=GEMINI_MODEL,
            contents=[
                {
                    "parts": [
                        {
                            "inline_data": {
                                "mime_type": mime_type,
                                "data": image_b64,
                            }
                        },
                        {
                            "text": (
                                "Extract ALL the text visible in this image exactly as written. "
                                "This may be handwritten notes, a printed document, or a scanned page. "
                                "Preserve all numbering, structure, headings, and content faithfully. "
                                "If it is a student answer sheet, extract every word the student has written. "
                                "Output only the extracted text — no commentary, no explanation."
                            )
                        },
                    ]
                }
            ],
            config={"temperature": 0.1, "max_output_tokens": 2000},
        )
        result = (getattr(resp, "text", "") or "").strip()
        print(f"[INFO] Gemini vision extracted {len(result)} chars from image")
        return result
    except Exception as e:
        print(f"[WARN] Gemini vision extraction failed: {e}")
        return ""


def extract_text_from_image(image_bytes: bytes, filename: str = "unknown") -> str:
    if not image_bytes or len(image_bytes) < 50:
        raise HTTPException(status_code=400, detail=f"Invalid file: '{filename}' - empty/too small")

    valid_image_signatures = {
        b"\xff\xd8\xff": "JPEG",
        b"\x89PNG\r\n\x1a\n": "PNG",
        b"GIF87a": "GIF",
        b"GIF89a": "GIF",
        b"BM": "BMP",
    }
    is_valid = any(image_bytes.startswith(sig) for sig in valid_image_signatures)
    if not is_valid:
        head = image_bytes[:12]
        raise HTTPException(status_code=400, detail=f"Invalid image format: '{filename}' (header={head})")

    # Detect MIME type for Gemini
    mime_type = "image/jpeg"
    if image_bytes.startswith(b"\x89PNG"):
        mime_type = "image/png"
    elif image_bytes.startswith(b"GIF"):
        mime_type = "image/gif"
    elif image_bytes.startswith(b"BM"):
        mime_type = "image/bmp"

    # 1. Try Gemini Vision first — best for handwriting and printed text
    if gemini_client:
        gv_text = _extract_text_gemini_vision(image_bytes, mime_type)
        if gv_text and len(gv_text.strip()) > 10:
            return _clean_extracted_text(gv_text)
        print("[WARN] Gemini vision returned no text, falling back to Google Cloud Vision / Tesseract")

    # 2. Fallback: Google Cloud Vision
    if vision_client:
        gv_text = _extract_text_google_vision(image_bytes)
        if gv_text and len(gv_text.strip()) > 10:
            return _clean_extracted_text(gv_text)

    # 3. Last resort: Tesseract with improved preprocessing
    try:
        img = Image.open(io.BytesIO(image_bytes))
    except Exception as e:
        raise HTTPException(status_code=400, detail=f"Invalid image '{filename}': {e}")

    img = _preprocess_for_ocr(img)

    # Try multiple OCR configurations for better handwritten recognition
    ocr_configs = [
        "--oem 3 --psm 6",  # Default
        "--oem 3 --psm 4",  # Treat as single column
        "--oem 1 --psm 3",  # Fully automatic
    ]

    best_text = ""
    for config in ocr_configs:
        try:
            text = pytesseract.image_to_string(img, lang="eng", config=config)
            if text and len(text.strip()) > len(best_text.strip()):
                best_text = text
        except Exception:
            continue

    if not best_text:
        # Final fallback
        try:
            best_text = pytesseract.image_to_string(img, lang="eng", config="--oem 3 --psm 6")
        except pytesseract.TesseractNotFoundError:
            raise HTTPException(status_code=500, detail="Tesseract OCR not found. Install it / fix path.")
        except Exception as e:
            raise HTTPException(status_code=500, detail=f"OCR failed: {e}")

    text = (best_text or "").strip()
    text = re.sub(r"[ \t]+", " ", text)
    return text


def _clean_extracted_text(text: str) -> str:
    text = (text or "").strip()
    text = re.sub(r"[ \t]+", " ", text)
    text = re.sub(r"\n{3,}", "\n\n", text)
    return text.strip()


def extract_text_from_docx(docx_bytes: bytes, filename: str = "unknown.docx") -> str:
    if Document is None:
        raise HTTPException(status_code=500, detail="DOCX support not installed. Add 'python-docx'.")
    try:
        doc = Document(io.BytesIO(docx_bytes))
        parts = []
        for p in doc.paragraphs:
            if p.text and p.text.strip():
                parts.append(p.text.strip())
        for t in doc.tables:
            for row in t.rows:
                cells = [c.text.strip() for c in row.cells if c.text and c.text.strip()]
                if cells:
                    parts.append(" | ".join(cells))
        return _clean_extracted_text("\n".join(parts))
    except Exception as e:
        raise HTTPException(status_code=400, detail=f"Unable to read DOCX '{filename}': {e}")


def extract_text_from_pdf(pdf_bytes: bytes, filename: str = "unknown.pdf") -> Dict[str, Any]:
    used_ocr = False
    extracted = ""

    if PdfReader is not None:
        try:
            reader = PdfReader(io.BytesIO(pdf_bytes))
            parts = []
            for page in reader.pages:
                t = page.extract_text() or ""
                if t.strip():
                    parts.append(t)
            extracted = _clean_extracted_text("\n\n".join(parts))
        except Exception:
            extracted = ""

    if len(extracted) < 50:
        if convert_from_bytes is None:
            return {"text": extracted, "used_ocr": False, "needs_ocr": True}
        try:
            used_ocr = True
            # Higher DPI for better handwritten OCR
            pages = convert_from_bytes(pdf_bytes, dpi=300)
            page_texts = []
            for img in pages:
                import io as _io
                # Try Gemini vision first (best for handwriting)
                if gemini_client:
                    buf = _io.BytesIO()
                    img.save(buf, format="PNG")
                    img_bytes = buf.getvalue()
                    t = _extract_text_gemini_vision(img_bytes, "image/png")
                    if t and len(t.strip()) > 20:
                        page_texts.append(t)
                        continue

                # Fallback: Tesseract with preprocessing
                img_proc = _preprocess_for_ocr(img)
                for config in ["--oem 3 --psm 6", "--oem 3 --psm 4", "--oem 1 --psm 3"]:
                    try:
                        t = pytesseract.image_to_string(img_proc, lang="eng", config=config) or ""
                        if t.strip() and len(t.strip()) > 20:
                            page_texts.append(t)
                            break
                    except Exception:
                        continue

            if page_texts:
                extracted = _clean_extracted_text("\n\n".join(page_texts))
            else:
                # Final fallback with default config
                img = pages[0] if pages else None
                if img:
                    img = _preprocess_for_ocr(img)
                    extracted = pytesseract.image_to_string(img, lang="eng", config="--oem 3 --psm 6") or ""
        except Exception as e:
            return {"text": extracted, "used_ocr": used_ocr, "needs_ocr": True, "ocr_error": str(e)}

    return {"text": extracted, "used_ocr": used_ocr, "needs_ocr": False}


def get_question_positions_from_pdf(pdf_bytes: bytes) -> Dict[int, List[Dict]]:
    """
    Detect question number positions in a PDF.
    Strategy 1: pypdf text-layer visitor (fast, for PDFs with text layer).
    Strategy 2: pdf2image + pytesseract OCR (for image-based PDFs).
    Returns dict: page_num -> [{qid, y_pos, x_pos}] in PDF coords (origin bottom-left).
    """
    try:
        from pypdf import PdfReader
        from io import BytesIO
        reader = PdfReader(BytesIO(pdf_bytes))
        question_positions: Dict[int, List[Dict]] = {}

        def _normalise_ocr_qid(token: str):
            t = token.strip().rstrip('.')
            m = re.match(r'^[Qq]\s*(\d+)$', t)
            if m:
                return f"Q{m.group(1)}"
            ocr_map = {'i': '1', 'I': '1', 'l': '1', 'o': '0', 'O': '0',
                       'z': '2', 'Z': '2', 's': '5', 'S': '5', 'g': '9'}
            m2 = re.match(r'^[Qq]([a-zA-Z\d])$', t)
            if m2:
                digit = ocr_map.get(m2.group(1), m2.group(1))
                if digit.isdigit():
                    return f"Q{digit}"
            return None

        for page_num, page in enumerate(reader.pages):
            page_height = float(page.mediabox.height) if hasattr(page.mediabox, 'height') else 792
            page_width  = float(page.mediabox.width)  if hasattr(page.mediabox, 'width')  else 595
            found: List[Dict] = []
            existing_qids: set = set()

            # Strategy 1: text layer
            try:
                parts = []
                def _visitor(text, cm, tm, font_dict, font_size):
                    if text and text.strip():
                        parts.append((text.strip(), float(tm[4]) if tm else 0, float(tm[5]) if tm else 0))
                page.extract_text(visitor_text=_visitor)
                tl_patterns = [
                    re.compile(r'\bQ\s*(\d+)\b', re.IGNORECASE),
                    re.compile(r'\bQuestion\s*(\d+)\b', re.IGNORECASE),
                    re.compile(r'^(\d+)[.):\s]'),
                ]
                for text_frag, x, y in parts:
                    for pat in tl_patterns:
                        m = pat.match(text_frag)
                        if m:
                            qid = f"Q{m.group(1)}"
                            if qid not in existing_qids:
                                existing_qids.add(qid)
                                found.append({'qid': qid, 'y_pos': y, 'x_pos': x})
                            break
            except Exception as tl_err:
                print(f"[WARN] text-layer page {page_num}: {tl_err}")

            # Strategy 2: OCR fallback (image-based PDFs)
            if not found:
                try:
                    from pdf2image import convert_from_bytes as _c2b
                    import pytesseract
                    rendered = _c2b(pdf_bytes, dpi=72, first_page=page_num+1, last_page=page_num+1)
                    if rendered:
                        img = rendered[0]
                        img_w, img_h = img.size
                        scale_x = page_width  / img_w
                        scale_y = page_height / img_h
                        ocr_data = pytesseract.image_to_data(img, output_type=pytesseract.Output.DICT)
                        for i, token in enumerate(ocr_data['text']):
                            if not token or not token.strip() or int(ocr_data['conf'][i]) < 20:
                                continue
                            dm = re.match(r'^[Qq]\s*(\d+)[.:]?$', token.strip())
                            qid = f"Q{dm.group(1)}" if dm else _normalise_ocr_qid(token)
                            if qid and qid not in existing_qids:
                                pdf_x = ocr_data['left'][i] * scale_x
                                pdf_y = page_height - (ocr_data['top'][i] + ocr_data['height'][i] * 0.5) * scale_y
                                existing_qids.add(qid)
                                found.append({'qid': qid, 'y_pos': pdf_y, 'x_pos': pdf_x})
                except Exception as ocr_err:
                    print(f"[WARN] OCR fallback page {page_num}: {ocr_err}")

            if found:
                found.sort(key=lambda d: -d['y_pos'])
                question_positions[page_num] = found
        return question_positions
    except Exception as e:
        print(f"[WARN] Failed to get question positions: {e}")
        return {}


def create_annotated_pdf(
    original_pdf_bytes: bytes,
    mcq_results: List[Dict[str, Any]] = None,
    match_percentage: int = 0,
    status: str = "Needs Review",
    student_level: str = "Medium",
    question_type: str = "mcq"
) -> bytes:
    """
    Annotate every question number found in the PDF with a coloured mark:
      Correct     -> filled green circle + white tick  (✓)
      Wrong       -> filled red circle   + white cross (✗)
      Unattempted -> hollow orange circle              (○)

    Any question detected in the PDF that has NO entry in mcq_results is
    automatically treated as unattempted (hollow orange circle).
    """
    if not reportlab:
        print("[WARN] reportlab not available, returning original PDF")
        return original_pdf_bytes

    try:
        from pypdf import PdfWriter, PdfReader
        from io import BytesIO

        question_positions = get_question_positions_from_pdf(original_pdf_bytes)
        print(f"[INFO] Detected question positions: {question_positions}")

        qid_location: Dict[str, tuple] = {}
        for pg, items in question_positions.items():
            for item in items:
                qid_location[item["qid"]] = (pg, item["y_pos"], item["x_pos"])

        results_by_qid: Dict[str, Dict] = {}
        for r in (mcq_results or []):
            qid = r.get("qid", "")
            if qid:
                results_by_qid[qid] = r

        def _draw_mark(c, x, y, is_correct, is_unattempted, radius=14):
            if is_correct and not is_unattempted:
                # ✓ Green filled circle — correct only
                c.setStrokeColor(colors.Color(0.0, 0.65, 0.0))
                c.setFillColor(colors.Color(0.0, 0.65, 0.0))
                c.setLineWidth(2)
                c.circle(x, y, radius, fill=1)
                c.setFillColor(colors.white)
                c.setFont("Helvetica-Bold", int(radius * 1.5))
                c.drawString(x - radius * 0.5, y - radius * 0.45, "\u2713")
            else:
                # ✗ Red filled circle — wrong OR unattempted
                c.setStrokeColor(colors.Color(0.85, 0.1, 0.1))
                c.setFillColor(colors.Color(0.85, 0.1, 0.1))
                c.setLineWidth(2)
                c.circle(x, y, radius, fill=1)
                c.setFillColor(colors.white)
                c.setFont("Helvetica-Bold", int(radius * 1.5))
                c.drawString(x - radius * 0.5, y - radius * 0.45, "\u2717")

        MARK_RADIUS   = 14
        MARK_X_OFFSET = -(MARK_RADIUS + 4)

        original_reader = PdfReader(BytesIO(original_pdf_bytes))
        writer = PdfWriter()
        total_pages = len(original_reader.pages)

        for page_num, page in enumerate(original_reader.pages):
            page_width  = float(page.mediabox.width)
            page_height = float(page.mediabox.height)
            packet = BytesIO()
            c = canvas.Canvas(packet, pagesize=(page_width, page_height))

            # Draw a mark for every detected question on this page
            for item in question_positions.get(page_num, []):
                qid   = item["qid"]
                y_pos = item["y_pos"]
                x_pos = item["x_pos"]

                result         = results_by_qid.get(qid)
                is_unattempted = True   # default: no data → unattempted
                is_correct     = False

                if result is not None:
                    explicit_unattempted = result.get("unattempted")
                    chosen = result.get("chosen", "")
                    correct_val = result.get("correct")

                    if explicit_unattempted is True:
                        # Explicitly flagged as unattempted
                        is_unattempted = True
                        is_correct = False
                    elif not chosen or str(chosen).strip() == "":
                        # No answer recorded → treat as unattempted
                        is_unattempted = True
                        is_correct = False
                    else:
                        # Answer was given — mark correct or wrong
                        is_unattempted = False
                        is_correct = bool(correct_val)

                mark_x = max(MARK_RADIUS + 2, x_pos + MARK_X_OFFSET)
                mark_y = y_pos + MARK_RADIUS * 0.3
                _draw_mark(c, mark_x, mark_y, is_correct, is_unattempted, MARK_RADIUS)

            # Fallback: results whose qid was not detected in the PDF
            undetected = [r for r in (mcq_results or []) if r.get("qid") not in qid_location]
            if undetected:
                per_page  = max(1, (len(undetected) + total_pages - 1) // total_pages)
                start_idx = page_num * per_page
                page_slice = undetected[start_idx: start_idx + per_page]
                y_start   = page_height - 100
                y_spacing = max(20, (page_height - 130) / max(1, per_page))
                for i, result in enumerate(page_slice):
                    explicit_unattempted = result.get("unattempted")
                    chosen = result.get("chosen", "")
                    correct_val = result.get("correct")

                    if explicit_unattempted is True or not chosen or str(chosen).strip() == "":
                        is_unattempted = True
                        is_correct = False
                    else:
                        is_unattempted = False
                        is_correct = bool(correct_val)
                    y_pos = y_start - i * y_spacing
                    if y_pos < 30:
                        break
                    _draw_mark(c, 18, y_pos, is_correct, is_unattempted, 9)

            # Header bar on first page
            if page_num == 0:
                header_h = 58
                c.setFillColor(colors.Color(0.93, 0.93, 0.93))
                c.rect(0, page_height - header_h, page_width, header_h, fill=1, stroke=0)

                sc = (colors.Color(0.0, 0.65, 0.0) if status == "Verified"
                      else colors.Color(1.0, 0.55, 0.0) if status == "Partial"
                      else colors.Color(0.85, 0.1, 0.1))

                c.setFillColor(sc); c.setStrokeColor(sc)
                c.circle(18, page_height - 22, 8, fill=1)
                c.setFont("Helvetica-Bold", 14)
                c.drawString(34, page_height - 27, f"Status: {status}")

                c.setFillColor(colors.black)
                c.setFont("Helvetica-Bold", 14)
                c.drawString(page_width * 0.42, page_height - 27, f"Score: {match_percentage}%")
                c.drawString(page_width * 0.72, page_height - 27, f"Level: {student_level}")

                all_detected = [item["qid"] for pg_items in question_positions.values()
                                for item in pg_items]
                if all_detected or mcq_results:
                    correct_count   = sum(1 for r in (mcq_results or []) if r.get("correct"))
                    incorrect_count = sum(1 for r in (mcq_results or []) if not r.get("correct"))
                    total_count     = len(all_detected) or len(mcq_results or [])

                    c.setFont("Helvetica-Bold", 11)
                    c.drawString(18, page_height - 46,
                                 f"Questions: {correct_count} correct  |  {incorrect_count} wrong/unattempted  (of {total_count})")

                    lx = page_width - 200
                    c.setFont("Helvetica", 9)
                    c.setFillColor(colors.Color(0.0, 0.65, 0.0))
                    c.drawString(lx,      page_height - 46, "\u2713 Correct")
                    c.setFillColor(colors.Color(0.85, 0.1, 0.1))
                    c.drawString(lx + 72, page_height - 46, "\u2717 Wrong / Unattempted")
            c.save()
            packet.seek(0)
            overlay_reader = PdfReader(packet)
            if overlay_reader.pages:
                page.merge_page(overlay_reader.pages[0])
            writer.add_page(page)

        output = BytesIO()
        writer.write(output)
        output.seek(0)
        return output.read()

    except Exception as e:
        print(f"[ERROR] Failed to create annotated PDF: {e}")
        return original_pdf_bytes

async def extract_text_from_upload(file: UploadFile) -> Dict[str, Any]:
    filename = getattr(file, "filename", "") or "upload"
    content_type = (getattr(file, "content_type", "") or "").lower()
    data = await file.read()

    if not data or len(data) < 20:
        return {"text": "", "kind": "unknown", "used_ocr": False, "needs_ocr": False, "error": "empty"}

    ext = (os.path.splitext(filename)[1] or "").lower()

    is_image = content_type.startswith("image/") or ext in {".png", ".jpg", ".jpeg", ".bmp", ".gif", ".webp"}
    is_pdf = (content_type == "application/pdf") or ext == ".pdf"
    is_docx = (content_type in {
        "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
        "application/msword"
    }) or ext in {".docx", ".doc"}

    if is_image:
        try:
            return {"text": _clean_extracted_text(extract_text_from_image(data, filename=filename)),
                    "kind": "image", "used_ocr": True, "needs_ocr": False}
        except HTTPException as e:
            return {"text": "", "kind": "image", "used_ocr": True, "needs_ocr": False, "error": e.detail}

    if is_docx:
        try:
            return {"text": _clean_extracted_text(extract_text_from_docx(data, filename=filename)),
                    "kind": "docx", "used_ocr": False, "needs_ocr": False}
        except HTTPException as e:
            return {"text": "", "kind": "docx", "used_ocr": False, "needs_ocr": False, "error": e.detail}

    if is_pdf:
        info = extract_text_from_pdf(data, filename=filename)
        return {"text": info.get("text", ""), "kind": "pdf",
                "used_ocr": bool(info.get("used_ocr", False)),
                "needs_ocr": bool(info.get("needs_ocr", False)),
                "ocr_error": info.get("ocr_error")}

    # fallback: try as image
    try:
        return {"text": _clean_extracted_text(extract_text_from_image(data, filename=filename)),
                "kind": "unknown_as_image", "used_ocr": True, "needs_ocr": False}
    except Exception:
        return {"text": "", "kind": "unknown", "used_ocr": False, "needs_ocr": False,
                "error": f"Unsupported file type: {content_type or ext or 'unknown'}"}





@app.get("/health")
def health():
    return {"status": "ok"}


@app.get("/health/llm")
def health_llm():
    return {
        "ok": bool(gemini_client) and bool(GOOGLE_API_KEYS),
        "gemini": {
            "sdk_import_ok": genai is not None,
            "configured": bool(GOOGLE_API_KEYS),
            "num_keys_configured": len(GOOGLE_API_KEYS),
            "current_key_index": current_key_index + 1 if GOOGLE_API_KEYS else 0,
            "rate_limited_keys": list(rate_limited_keys),
            "client_ready": gemini_client is not None,
            "model": GEMINI_MODEL,
            "last_error": GEMINI_LAST_ERROR if GEMINI_LAST_ERROR else None,
        },
    }


@app.get("/homework/annotated-url/{homework_id}/{student_id}")
async def get_annotated_pdf_url(
    homework_id: int,
    student_id: int,
):
    """
    Get the URL for the annotated PDF.
    Returns JSON with the URL that can be used in your frontend.
    """
    base_url = get_public_base_url()
    return {
        "homework_id": homework_id,
        "student_id": student_id,
        "annotated_pdf_url": f"{base_url}/homework/annotated/{homework_id}/{student_id}"
    }
@app.get("/homework/annotated/{homework_id}/{student_id}")
async def get_annotated_pdf(
    homework_id: int,
    student_id: int,
):
    """
    Download the annotated PDF with tickmarks for a validated homework.
    This endpoint returns the PDF directly as a file download.
    """
    from fastapi.responses import Response
    
    try:
        # Fetch ERP record
        erp_row = fetch_student_record(homework_id, student_id)
        
        # Get submission image from ERP
        submission_image = erp_row.get("submission_image")
        if not submission_image:
            raise HTTPException(status_code=404, detail="No submission found")
        
        # Download the original file
        submission_url = STORAGE_BASE + submission_image
        resp = requests.get(submission_url, timeout=30)
        resp.raise_for_status()
        original_content = resp.content
        
        # Determine file type
        filename = submission_image.lower()
        is_pdf = filename.endswith('.pdf')
        
        if not is_pdf:
            raise HTTPException(status_code=400, detail="Annotated PDF only available for PDF submissions")
        
        # Get prompt and question type
        prompt = erp_row.get("prompt") or erp_row.get("question_prompt") or ""
        question_type = erp_row.get("question_type") or erp_row.get("type")
        student_level = fetch_student_level_from_erp(erp_row)
        
        # Extract text from PDF FIRST (needed for question type inference)
        pdf_info = extract_text_from_pdf(original_content, filename=submission_image)
        student_text = (pdf_info.get("text") or "").strip()
        
        if not student_text or len(student_text) < 10:
            raise HTTPException(status_code=400, detail="Could not extract text from PDF")
        
        final_question_type = (question_type or "").strip().lower()
        if final_question_type not in ("mcq", "narrative", "mixed"):
            final_question_type = infer_question_type_from_prompt(prompt, student_text)
        
        mcq_results = []
        status = "Needs Review"
        match_percentage = 0
        
        # Process based on question type
        if final_question_type == "mcq":
            correct = extract_correct_mcq_from_prompt(prompt)
            chosen = extract_mcq_choice(student_text)
            
            student_answers_by_qid = extract_mcq_answers_with_qid(student_text)
            
            if student_answers_by_qid:
                # Multiple MCQ
                parsed_questions = parse_questions_from_prompt(prompt)
                mcq_questions_with_answers = [q for q in parsed_questions if q.get('type') == 'mcq' and q.get('correct_answer')]
                
                for qid, student_ans in student_answers_by_qid.items():
                    matched = False
                    for pq in mcq_questions_with_answers:
                        pq_num = pq.get('qid', '').replace('Q', '').strip()
                        qid_num = qid.replace('Q', '').strip()
                        if pq_num == qid_num:
                            is_correct = student_ans.lower() == pq.get('correct_answer', '').lower()
                            mcq_results.append({
                                'qid': qid,
                                'chosen': student_ans,
                                'correct_answer': pq.get('correct_answer'),
                                'correct': is_correct,
                                'unattempted': False
                            })
                            matched = True
                            break
                    if not matched:
                        mcq_results.append({'qid': qid, 'chosen': student_ans, 'correct_answer': None, 'correct': False, 'unattempted': False})
                
                # Mark questions from the prompt that the student never answered
                answered_nums = {r['qid'].replace('Q', '').strip() for r in mcq_results}
                for pq in mcq_questions_with_answers:
                    pq_num = pq.get('qid', '').replace('Q', '').strip()
                    if pq_num not in answered_nums:
                        mcq_results.append({
                            'qid': pq.get('qid'),
                            'chosen': '',
                            'correct_answer': pq.get('correct_answer'),
                            'correct': False,
                            'unattempted': True
                        })
                
                if mcq_results:
                    correct_count = sum(1 for r in mcq_results if r.get('correct'))
                    mcq_credit = mcq_partial_credit(student_level)
                    match_percentage = int((correct_count * mcq_credit["credit_per_question"]) / max(1, len(mcq_results)))
                    status = "Verified" if match_percentage >= mcq_credit["passing_threshold"] else "Needs Review"
            elif correct and chosen:
                is_correct = (chosen == correct)
                mcq_credit = mcq_partial_credit(student_level)
                match_percentage = mcq_credit["credit_per_question"] if is_correct else 0
                status = "Verified" if match_percentage >= mcq_credit["passing_threshold"] else "Needs Review"
                _qid = extract_qid_from_prompt(prompt, erp_row)
                mcq_results = [{'qid': _qid, 'correct': is_correct, 'chosen': chosen, 'correct_answer': correct}]
        
        # For narrative, calculate score using AI
        if final_question_type == "narrative" and gemini_client:
            # Generate AI reference answer
            ai_prompt = (
                f"STUDENT_LEVEL: {student_level}\n"
                f"QUESTION:\n{prompt.strip()}\n\n"
                'Return ONLY valid JSON with keys: {"ai_reference_answer": string, "key_points": [string, ...]}.'
            )
            
            response_text = generate_gemini_response(
                prompt=ai_prompt,
                system_prompt="Generate a correct reference answer for homework evaluation. Keep it aligned with the student level. Output strict JSON only.",
                max_tokens=650,
                temperature=0.3,
            )
            
            if response_text:
                try:
                    import re
                    m = re.search(r'\{.*\}', response_text, flags=re.S)
                    payload = json.loads(m.group(0) if m else response_text)
                    
                    ai_reference_answer = (payload.get("ai_reference_answer") or "").strip()
                    key_points = payload.get("key_points") or []
                    
                    policy = level_policy(student_level)
                    sim = cosine_sim(student_text, ai_reference_answer)
                    covered, missing, coverage = keypoint_coverage(student_text, key_points, kp_threshold=policy["kp_thr"])
                    
                    final = policy["w_sim"] * sim + policy["w_cov"] * coverage
                    match_percentage = int(round(final * 100))
                    
                    if match_percentage >= policy["verified"]:
                        status = "Verified"
                    elif match_percentage >= policy["partial"]:
                        status = "Partial"
                    else:
                        status = "Needs Review"
                    
                    # Create result for narrative to show in PDF
                    if status == "Verified":
                        narrative_correct = True
                    elif status == "Partial":
                        narrative_correct = False
                    else:
                        narrative_correct = False
                    
                    _qid = extract_qid_from_prompt(prompt, erp_row)
                    mcq_results = [
                        {'qid': _qid, 'correct': narrative_correct, 'chosen': f'Score: {match_percentage}%', 'correct_answer': status}
                    ]
                except Exception as e:
                    print(f"[WARN] Failed to calculate narrative score: {e}")
        
        # Create annotated PDF
        annotated_pdf = create_annotated_pdf(
            original_pdf_bytes=original_content,
            mcq_results=mcq_results,
            match_percentage=match_percentage,
            status=status,
            student_level=student_level,
            question_type=final_question_type
        )
        
        # Return as file download
        return Response(
            content=annotated_pdf,
            media_type="application/pdf",
            headers={"Content-Disposition": f"inline; filename=annotated_homework_{homework_id}_{student_id}.pdf"}
        )
        
    except HTTPException:
        raise
    except Exception as e:
        print(f"[ERROR] Failed to generate annotated PDF: {e}")
        raise HTTPException(status_code=500, detail=f"Failed to generate PDF: {str(e)}")


def ai_evaluate_per_question(
    prompt: str,
    student_text: str,
    student_level: str = "Medium",
) -> list:
    """
    Uses Gemini AI to evaluate each question individually from the student's answer sheet.

    Logic:
      1. Send ALL questions from the prompt + the full student answer text to Gemini.
      2. Gemini identifies:
         - Which questions the student attempted (wrote any answer for)
         - For each attempted question: is the answer correct or wrong?
         - For unattempted questions: marks as unattempted
      3. Returns list of {qid, correct, unattempted, chosen, correct_answer}

    Marking rules (as per requirement):
      - No answer written            → unattempted  (orange ○)
      - Answer written, score < 35%  → wrong         (red ✗)
      - Answer written, score >= 35% → correct       (green ✓)
    """
    import re as _re

    # Parse question numbers from the homework prompt
    q_numbers = []
    for m in _re.finditer(r'\bQ(\d+)\s*[:\.]?', prompt, _re.IGNORECASE):
        n = int(m.group(1))
        if n not in q_numbers:
            q_numbers.append(n)
    q_numbers.sort()

    if not q_numbers:
        # Single question fallback — ask Gemini to evaluate it
        q_numbers = [1]

    total_q = len(q_numbers)
    qid_list = [f"Q{n}" for n in q_numbers]

    ai_prompt = f"""You are a strict homework evaluator. Your job is to evaluate each question individually.

STUDENT LEVEL: {student_level}
TOTAL QUESTIONS: {total_q}
QUESTION IDs: {', '.join(qid_list)}

HOMEWORK QUESTIONS (from teacher):
{prompt.strip()}

STUDENT'S ANSWER SHEET (OCR extracted):
{student_text.strip()}

INSTRUCTIONS:
For EACH question ({', '.join(qid_list)}), you must determine:
1. Did the student write ANY answer for this question? (check the student's answer sheet carefully)
2. If NO answer was written → mark as "unattempted"
3. If answer was written → evaluate if it is correct based on the homework question
   - score >= 35% similarity to correct answer → "correct"
   - score < 35% similarity → "wrong"

IMPORTANT RULES:
- Look carefully at student's answer text. The student may have written answers with question numbers like "1.", "Q1:", "1)", or just paragraph answers.
- If student's answer sheet is blank or has no relevant text for a specific question → unattempted
- Be strict: a vague or incomplete answer with < 35% match to expected answer = wrong
- A reasonably correct answer with >= 35% match = correct

Return ONLY valid JSON array, no extra text:
[
  {{"qid": "Q1", "status": "correct" | "wrong" | "unattempted", "student_answer_snippet": "brief snippet of what student wrote or empty string", "reason": "one line reason"}},
  ...one entry per question...
]"""

    response = generate_gemini_response(
        prompt=ai_prompt,
        system_prompt="You are a strict homework evaluator. Output only valid JSON array.",
        max_tokens=800,
        temperature=0.1,
    )

    results = []

    if response:
        try:
            # Extract JSON array from response
            m = re.search(r'\[.*\]', response, flags=re.S)
            if m:
                ai_data = json.loads(m.group(0))
                seen_qids = set()
                for item in ai_data:
                    qid = (item.get("qid") or "").strip()
                    status = (item.get("status") or "unattempted").strip().lower()
                    snippet = (item.get("student_answer_snippet") or "").strip()

                    if not qid:
                        continue
                    seen_qids.add(qid)

                    is_unattempted = status == "unattempted"
                    is_correct = status == "correct"

                    results.append({
                        "qid": qid,
                        "correct": is_correct,
                        "unattempted": is_unattempted,
                        "chosen": snippet[:80] if snippet else ("" if is_unattempted else "Answered"),
                        "correct_answer": "" if is_unattempted else ("Correct" if is_correct else "Wrong"),
                    })

                # Add any missing questions as unattempted
                for n in q_numbers:
                    qid = f"Q{n}"
                    if qid not in seen_qids:
                        results.append({
                            "qid": qid,
                            "correct": False,
                            "unattempted": True,
                            "chosen": "",
                            "correct_answer": "",
                        })

                # Sort by question number
                results.sort(key=lambda r: int(re.search(r'\d+', r.get("qid", "Q0")).group() or 0))
                return results

        except Exception as e:
            print(f"[WARN] ai_evaluate_per_question JSON parse failed: {e}, raw={response[:300]}")

    # ── Fallback: Gemini failed → use cosine similarity per question ──────────
    print("[WARN] ai_evaluate_per_question: Gemini failed, using cosine similarity fallback")
    return _cosine_fallback_per_question(prompt, student_text, q_numbers)


def _cosine_fallback_per_question(prompt: str, student_text: str, q_numbers: list) -> list:
    """
    Fallback when Gemini is unavailable.
    Extracts each question's answer from student text using regex segmentation,
    then scores with cosine similarity. < 35% = wrong, >= 35% = correct, no text = unattempted.
    """
    import re as _re

    WRONG_THRESHOLD = 0.35

    def q_start_regex(n):
        return _re.compile(
            rf'(?:q\s*{n}\s*[:.)\-]|(?<!\d){n}\s*[.):\-]\s|question\s*{n}\s*[:.)\-]?|ans(?:wer)?\s*{n}\s*[:.)\-]?)',
            _re.IGNORECASE
        )

    results = []
    text = student_text or ""

    for n in q_numbers:
        qid = f"Q{n}"
        pat = q_start_regex(n)
        m = pat.search(text)

        if not m:
            results.append({"qid": qid, "correct": False, "unattempted": True, "chosen": "", "correct_answer": ""})
            continue

        answer_start = m.end()
        answer_end = len(text)

        # Find where next question starts
        for other_n in q_numbers:
            if other_n == n:
                continue
            om = q_start_regex(other_n).search(text, answer_start)
            if om and om.start() < answer_end:
                answer_end = om.start()

        answer_text = text[answer_start:answer_end].strip()

        if not answer_text or len(answer_text.split()) < 2:
            results.append({"qid": qid, "correct": False, "unattempted": True, "chosen": "", "correct_answer": ""})
            continue

        # Score against full student text (rough proxy — no per-Q reference available here)
        sim = cosine_sim(answer_text, text)
        is_correct = sim >= WRONG_THRESHOLD

        results.append({
            "qid": qid,
            "correct": is_correct,
            "unattempted": False,
            "chosen": answer_text[:80],
            "correct_answer": "Correct" if is_correct else "Wrong",
        })

    return results


def build_per_question_results(
    prompt: str,
    student_text: str,
    overall_status: str,
    overall_score: int,
    ai_reference_answer: str = "",
    key_points: list = None,
    policy: dict = None,
    student_level: str = "Medium",
) -> list:
    """
    Main entry point for per-question evaluation.
    Delegates to ai_evaluate_per_question (Gemini) with cosine fallback.
    This replaces the old overall-status-based approach.
    """
    # Always use AI evaluation — it checks each question individually
    return ai_evaluate_per_question(prompt, student_text, student_level)


@app.post("/homework/validate")
async def homework_validate(
    student_id: int = Form(...),
    homework_id: int = Form(...),
    student_file: UploadFile = File(...),
):
    # 0) Fetch ERP record -> get all fields automatically
    erp_row = fetch_student_record(homework_id, student_id)
    
    # Extract fields from ERP record
    sub_institute_id = erp_row.get("sub_institute_id")
    syear = erp_row.get("syear")
    prompt = erp_row.get("prompt") or erp_row.get("question_prompt") or ""
    question_type = erp_row.get("question_type") or erp_row.get("type")
    
    student_level = fetch_student_level_from_erp(erp_row)
    policy = level_policy(student_level)

    # 2) Extract student text FIRST (needed for question type inference)
    student_info = await extract_text_from_upload(student_file)
    student_text = (student_info.get("text") or "").strip()
    
    # Keep a copy of the original file bytes for PDF annotation
    # Reset file cursor and read again
    await student_file.seek(0)
    original_file_bytes = await student_file.read()
    await student_file.seek(0)  # Reset for any further processing
    
    # Decide final question type: respect request value if valid, else infer using student text
    final_question_type = (question_type or "").strip().lower()
    if final_question_type not in ("mcq", "narrative", "mixed"):
        final_question_type = infer_question_type_from_prompt(prompt, student_text)

    # 1) Infer question_type from prompt automatically (NO EXTRA FIELD)
    # Try to parse mixed questions first
    parsed_questions = parse_questions_from_prompt(prompt)
    has_mcq = any(q.get('type') == 'mcq' for q in parsed_questions)
    has_narrative = any(q.get('type') == 'narrative' for q in parsed_questions)
    
    # Detect submission kind
    submission_kind = student_info.get("kind", "")  # "pdf", "image", "docx", etc.
    is_pdf_submission   = submission_kind == "pdf"
    is_image_submission = submission_kind == "image" or submission_kind == "unknown_as_image"
    is_docx_submission  = submission_kind == "docx"
    can_annotate        = is_pdf_submission or is_image_submission or is_docx_submission

    # ── Converters: image/docx → PDF bytes so create_annotated_pdf can process them ──
    def _image_bytes_to_pdf(img_bytes: bytes) -> bytes:
        """Wrap a raw image inside a single-page PDF using reportlab."""
        try:
            from reportlab.pdfgen import canvas as rl_canvas
            from reportlab.lib.utils import ImageReader
            from PIL import Image as PILImage
            import io as _io
            img = PILImage.open(_io.BytesIO(img_bytes))
            iw, ih = img.size
            buf = _io.BytesIO()
            c = rl_canvas.Canvas(buf, pagesize=(iw, ih))
            c.drawImage(ImageReader(img), 0, 0, iw, ih)
            c.save()
            buf.seek(0)
            return buf.read()
        except Exception as e:
            print(f"[WARN] _image_bytes_to_pdf failed: {e}")
            return b""

    def _docx_bytes_to_pdf(docx_bytes: bytes) -> bytes:
        """
        Convert DOCX → PDF.
        Tries LibreOffice (soffice) first — available in most Linux envs.
        Falls back to building a simple reportlab PDF with the extracted text.
        """
        import subprocess, tempfile, shutil, os as _os, io as _io
        # Try LibreOffice
        try:
            with tempfile.TemporaryDirectory() as tmpdir:
                docx_path = _os.path.join(tmpdir, "input.docx")
                with open(docx_path, "wb") as f:
                    f.write(docx_bytes)
                result = subprocess.run(
                    ["soffice", "--headless", "--convert-to", "pdf", "--outdir", tmpdir, docx_path],
                    timeout=30, capture_output=True
                )
                pdf_path = docx_path.replace(".docx", ".pdf")
                if _os.path.exists(pdf_path):
                    with open(pdf_path, "rb") as f:
                        return f.read()
        except Exception as e:
            print(f"[WARN] LibreOffice docx→pdf failed: {e}")

        # Fallback: extract text and build a simple PDF with reportlab
        try:
            from reportlab.pdfgen import canvas as rl_canvas
            from reportlab.lib.pagesizes import A4
            from docx import Document as DocxDoc
            doc = DocxDoc(_io.BytesIO(docx_bytes))
            text_lines = [p.text for p in doc.paragraphs if p.text.strip()]
            buf = _io.BytesIO()
            page_w, page_h = A4
            c = rl_canvas.Canvas(buf, pagesize=A4)
            c.setFont("Helvetica", 11)
            y = page_h - 50
            for line in text_lines:
                # Word-wrap long lines
                while len(line) > 90:
                    c.drawString(40, y, line[:90])
                    line = line[90:]
                    y -= 16
                    if y < 50:
                        c.showPage()
                        c.setFont("Helvetica", 11)
                        y = page_h - 50
                c.drawString(40, y, line)
                y -= 16
                if y < 50:
                    c.showPage()
                    c.setFont("Helvetica", 11)
                    y = page_h - 50
            c.save()
            buf.seek(0)
            return buf.read()
        except Exception as e:
            print(f"[WARN] Fallback docx→pdf failed: {e}")
            return b""

    def _get_pdf_bytes_for_annotation() -> bytes:
        """
        Returns PDF bytes ready for annotation, converting from image/docx if needed.
        """
        if is_pdf_submission:
            return original_file_bytes
        if is_image_submission:
            pdf = _image_bytes_to_pdf(original_file_bytes)
            if pdf:
                return pdf
        if is_docx_submission:
            pdf = _docx_bytes_to_pdf(original_file_bytes)
            if pdf:
                return pdf
        return b""

    # Initialize annotated PDF filename
    annotated_pdf_filename = None
    annotated_pdf_url = None

    # Function to save annotated PDF — returns (filename, public_url)
    def save_annotated_pdf(pdf_bytes, hw_id, stud_id, results, score, stat, lvl, qtype="mcq"):
        if not pdf_bytes or len(pdf_bytes) < 100:
            return None, None
        try:
            outputs_dir = os.path.join(os.path.dirname(__file__), "outputs")
            os.makedirs(outputs_dir, exist_ok=True)
            ts = int(time.time())
            filename = f"marked_{hw_id}_{stud_id}_{ts}.pdf"
            filepath = os.path.join(outputs_dir, filename)

            # Convert image/docx → PDF if needed, then annotate
            annotation_input = _get_pdf_bytes_for_annotation()
            if not annotation_input:
                print(f"[WARN] Could not get PDF bytes for annotation (kind={submission_kind})")
                return None, None

            annotated = create_annotated_pdf(
                original_pdf_bytes=annotation_input,
                mcq_results=results,
                match_percentage=score,
                status=stat,
                student_level=lvl,
                question_type=qtype
            )

            with open(filepath, "wb") as f:
                f.write(annotated)
            return filename, build_pdf_url(filename)
        except Exception as e:
            print(f"[WARN] Failed to save annotated PDF: {e}")
            return None, None

    MIN_WORDS = 3 if final_question_type == "mcq" else 8
    if len(student_text.split()) < MIN_WORDS:
        # Save annotated PDF even for unreadable (with status shown)
        if can_annotate and original_file_bytes:
            # Show circle mark for unreadable
            unreadable_result = [{'qid': extract_qid_from_prompt(prompt, erp_row), 'correct': None, 'chosen': 'Unreadable', 'correct_answer': 'N/A'}]
            annotated_pdf_filename, annotated_pdf_url = save_annotated_pdf(
                original_file_bytes, homework_id, student_id, unreadable_result, 0, "Unreadable", student_level
            )
        return {
            "student_id": student_id,
            "homework_id": homework_id,
            "sub_institute_id": sub_institute_id,
            "syear": syear,
            "question_type": final_question_type,
            "student_level": student_level,
            "status": "Unreadable",
            "match_percentage": 0,
            "submission_remarks": None,
            "rule_based_remark": "Answer text could not be read clearly. Please upload a clearer file.",
            "student_extracted_text": student_text,
            "llm_used": False,
            "question_marks": make_question_marks([]),
            "annotated_pdf": annotated_pdf_filename,
            "extraction": {"student": {k: v for k, v in student_info.items() if k != "text"}},
        }

    if student_info.get("needs_ocr") and not student_text:
        # Save annotated PDF even for unreadable (with status shown)
        if can_annotate and original_file_bytes:
            # Show circle mark for scanned PDF that needs OCR
            ocr_result = [{'qid': extract_qid_from_prompt(prompt, erp_row), 'correct': None, 'chosen': 'Needs OCR', 'correct_answer': 'N/A'}]
            annotated_pdf_filename, annotated_pdf_url = save_annotated_pdf(
                original_file_bytes, homework_id, student_id, ocr_result, 0, "Unreadable", student_level
            )
        return {
            "student_id": student_id,
            "homework_id": homework_id,
            "sub_institute_id": sub_institute_id,
            "syear": syear,
            "question_type": final_question_type,
            "student_level": student_level,
            "status": "Unreadable",
            "match_percentage": 0,
            "submission_remarks": None,
            "rule_based_remark": "This PDF looks scanned. OCR is required (install pdf2image + poppler) or upload a clearer file.",
            "student_extracted_text": student_text,
            "llm_used": False,
            "question_marks": make_question_marks([]),
            "annotated_pdf": annotated_pdf_filename,
            "extraction": {"student": {k: v for k, v in student_info.items() if k != "text"}},
        }

    
    if final_question_type == "mixed":
        # Process each question type separately and combine results
        mcq_results = []
        narrative_results = []
        
        # Extract ALL MCQ answers from student text with question numbers
        student_answers_by_qid = extract_mcq_answers_with_qid(student_text)
        
        # Extract MCQ answers from student text for each MCQ question
        for q in parsed_questions:
            if q.get('type') == 'mcq':
                qid = q.get('qid', '')
                q_num = qid.replace('Q', '').strip() if qid else ''
                
                # Try to get answer by question number first
                chosen = student_answers_by_qid.get(qid) or student_answers_by_qid.get(f"Q{q_num}")
                
                # Fallback to old method if no question number found
                if not chosen:
                    chosen = extract_mcq_choice(student_text)
                
                correct = q.get('correct_answer') or extract_correct_mcq_from_prompt(q.get('question', ''))
                
                if correct and chosen:
                    is_correct = (chosen.lower().strip() == correct.lower().strip())
                    mcq_results.append({
                        'qid': qid,
                        'correct': is_correct,
                        'chosen': chosen,
                        'correct_answer': correct,
                        'unattempted': False
                    })
                elif correct and not chosen:
                    # Student didn't answer this question at all
                    mcq_results.append({
                        'qid': qid,
                        'correct': False,
                        'chosen': '',
                        'correct_answer': correct,
                        'unattempted': True
                    })
        
        # For narrative questions, use AI to generate reference
        narrative_questions = [q for q in parsed_questions if q.get('type') == 'narrative']
        
        if narrative_questions and gemini_client:
            # Combine narrative questions into one prompt for AI
            narrative_prompt_text = "\n".join([
                f"{q.get('qid')}: {q.get('question')}" for q in narrative_questions
            ])
            
            ai_prompt = (
                f"STUDENT_LEVEL: {student_level}\n"
                f"QUESTIONS:\n{narrative_prompt_text}\n\n"
                'Return ONLY valid JSON with keys: {"ai_reference_answer": string, "key_points": [string, ...]}.'
            )
            
            response_text = generate_gemini_response(
                prompt=ai_prompt,
                system_prompt=(
                    "Generate correct reference answers for homework evaluation. "
                    "Keep it aligned with the student level. Output strict JSON only."
                ),
                max_tokens=650,
                temperature=0.3,
            )
            
            if response_text:
                try:
                    m = re.search(r'\{.*\}', response_text, flags=re.S)
                    payload = json.loads(m.group(0) if m else response_text)
                    
                    ai_reference_answer = (payload.get("ai_reference_answer") or "").strip()
                    key_points = payload.get("key_points") or []
                    
                    if isinstance(key_points, list):
                        key_points = [str(x).strip() for x in key_points if str(x).strip()]
                    
                    sim = cosine_sim(student_text, ai_reference_answer)
                    covered, missing, coverage = keypoint_coverage(
                        student_text, key_points, kp_threshold=policy["kp_thr"]
                    )
                    
                    final = policy["w_sim"] * sim + policy["w_cov"] * coverage
                    match_pct = int(round(final * 100))
                    
                    narrative_results = {
                        'similarity': sim,
                        'coverage': coverage,
                        'match_percentage': match_pct,
                        'key_points_covered': covered,
                        'key_points_missing': missing
                    }
                except Exception as e:
                    narrative_results = {'error': str(e)}
        
        # Calculate combined score with level-based partial credit for MCQ
        total_mcq = len(mcq_results)
        correct_mcq = sum(1 for r in mcq_results if r.get('correct'))
        
        # Get level-based credit per question
        mcq_credit = mcq_partial_credit(student_level)
        credit_per_q = mcq_credit["credit_per_question"]
        passing_threshold = mcq_credit["passing_threshold"]
        
        # Calculate MCQ score based on level (not just binary correct/incorrect)
        mcq_score = (correct_mcq * credit_per_q) / max(1, total_mcq)
        
        narrative_score = narrative_results.get('match_percentage', 0) if narrative_results else 0
        
        # Weight: 50% MCQ, 50% Narrative (if both exist)
        if total_mcq > 0 and narrative_results and 'error' not in narrative_results:
            final_score = int((mcq_score + narrative_score) / 2)
        elif total_mcq > 0:
            final_score = mcq_score
        elif narrative_results and 'error' not in narrative_results:
            final_score = narrative_score
        else:
            final_score = 0
        
        # Determine status
        if final_score >= policy["verified"]:
            status = "Verified"
        elif final_score >= policy["partial"]:
            status = "Partial"
        else:
            status = "Needs Review"
        
        # Save annotated PDF
        if can_annotate and original_file_bytes and mcq_results:
            annotated_pdf_filename, annotated_pdf_url = save_annotated_pdf(
                original_file_bytes, homework_id, student_id, mcq_results, final_score, status, student_level
            )
        
        return {
            "student_id": student_id,
            "homework_id": homework_id,
            "sub_institute_id": sub_institute_id,
            "syear": syear,
            "question_type": "mixed",
            "student_level": student_level,
            "status": status,
            "match_percentage": final_score,
            "submission_remarks": None,
            "rule_based_remark": f"MCQ: {correct_mcq}/{total_mcq} correct. Narrative score: {narrative_score}%. (Level: {student_level}, Credit per Q: {credit_per_q}%)",
            "llm_used": bool(narrative_results and 'error' not in narrative_results),
            "student_extracted_text": student_text,
            "mcq_results": mcq_results,
            "narrative_results": narrative_results,
            "question_marks": make_question_marks(mcq_results),
            "annotated_pdf": annotated_pdf_filename,
            "extraction": {"student": {k: v for k, v in student_info.items() if k != "text"}},
            "debug": {
                "erp_row_fields": list(erp_row.keys()) if erp_row else [],
                "erp_student_level_raw": erp_row.get("student_level") or erp_row.get("level") or erp_row.get("difficulty") or erp_row.get("difficulty_level"),
                "mcq_credit_per_q": credit_per_q,
            },
        }

    elif final_question_type == "mcq":
        correct = extract_correct_mcq_from_prompt(prompt)
        chosen = extract_mcq_choice(student_text)
        
        # Try to extract multiple MCQ answers (for numbered questions like "1 A", "2 B")
        student_answers_by_qid = extract_mcq_answers_with_qid(student_text)
        has_multiple_mcq = len(student_answers_by_qid) > 1

        # Smart fallback: if answer looks like narrative (not MCQ), treat as narrative instead
        # This handles cases where question type is MCQ but student answered in narrative format
        # BUT if the answer contains Option A/B/C/D, treat as MCQ
        answer_has_mcq_option = bool(re.search(r"\b(option|answer|ans)\s*[:\-]?\s*[a-d]\b", _norm(student_text)))
        
        answer_looks_like_narrative = (
            len(student_text.split()) > 15 and  # More than 15 words
            not has_multiple_mcq and  # Not multiple numbered MCQ answers
            not answer_has_mcq_option  # No explicit option markers
        )

        # If answer looks like narrative, redirect to narrative processing
        if answer_looks_like_narrative and gemini_client:
            final_question_type = "narrative"
            redirect_to_narrative = True
        else:
            redirect_to_narrative = False
            
        # Handle multiple MCQ answers - grade each one
        if has_multiple_mcq:
            # Parse prompt for multiple correct answers
            parsed_questions = parse_questions_from_prompt(prompt)
            mcq_questions_with_answers = [q for q in parsed_questions if q.get('type') == 'mcq' and q.get('correct_answer')]
            
            # If we have correct answers for multiple questions, grade them
            if mcq_questions_with_answers:
                correct_count = 0
                total_count = len(student_answers_by_qid)
                mcq_results = []
                
                for qid, student_ans in student_answers_by_qid.items():
                    # Find matching correct answer
                    matched = False
                    for pq in mcq_questions_with_answers:
                        pq_num = pq.get('qid', '').replace('Q', '').strip()
                        qid_num = qid.replace('Q', '').strip()
                        if pq_num == qid_num:
                            is_correct = student_ans.lower() == pq.get('correct_answer', '').lower()
                            if is_correct:
                                correct_count += 1
                            mcq_results.append({
                                'qid': qid,
                                'chosen': student_ans,
                                'correct_answer': pq.get('correct_answer'),
                                'correct': is_correct,
                                'unattempted': False
                            })
                            matched = True
                            break
                    if not matched:
                        mcq_results.append({
                            'qid': qid,
                            'chosen': student_ans,
                            'correct_answer': None,
                            'correct': False,
                            'unattempted': False
                        })
                
                # Add any questions from the prompt that the student never answered
                answered_nums = {r['qid'].replace('Q', '').strip() for r in mcq_results}
                for pq in mcq_questions_with_answers:
                    pq_num = pq.get('qid', '').replace('Q', '').strip()
                    if pq_num not in answered_nums:
                        mcq_results.append({
                            'qid': pq.get('qid'),
                            'chosen': '',
                            'correct_answer': pq.get('correct_answer'),
                            'correct': False,
                            'unattempted': True
                        })
                
                # Calculate score based on level
                mcq_credit = mcq_partial_credit(student_level)
                credit_per_q = mcq_credit["credit_per_question"]
                match_percentage = int((correct_count * credit_per_q) / max(1, total_count))
                passing_threshold = mcq_credit["passing_threshold"]
                status = "Verified" if match_percentage >= passing_threshold else "Needs Review"
                
                # Save annotated PDF
                if can_annotate and original_file_bytes:
                    annotated_pdf_filename, annotated_pdf_url = save_annotated_pdf(
                        original_file_bytes, homework_id, student_id, mcq_results, match_percentage, status, student_level
                    )
                
                return {
                    "student_id": student_id,
                    "homework_id": homework_id,
                    "sub_institute_id": sub_institute_id,
                    "syear": syear,
                    "question_type": "mcq",
                    "student_level": student_level,
                    "status": status,
                    "match_percentage": match_percentage,
                    "submission_remarks": None,
                    "rule_based_remark": f"Multiple MCQ: {correct_count}/{total_count} correct. Score: {match_percentage}% (Level: {student_level})",
                    "student_extracted_text": student_text,
                    "llm_used": False,
                    "question_marks": make_question_marks(mcq_results),
            "annotated_pdf": annotated_pdf_filename,
                    "debug": {"student_answers": student_answers_by_qid, "mcq_results": mcq_results},
                    "extraction": {"student": {k: v for k, v in student_info.items() if k != "text"}},
                }
            else:
                # No correct answers in prompt - return needs review with extracted answers
                # Save annotated PDF with circle mark
                if can_annotate and original_file_bytes:
                    no_answer_result = [{'qid': extract_qid_from_prompt(prompt, erp_row), 'correct': None, 'chosen': 'No Answer Key', 'correct_answer': 'N/A'}]
                    annotated_pdf_filename, annotated_pdf_url = save_annotated_pdf(
                        original_file_bytes, homework_id, student_id, no_answer_result, 0, "Needs Review", student_level
                    )
                return {
                    "student_id": student_id,
                    "homework_id": homework_id,
                    "sub_institute_id": sub_institute_id,
                    "syear": syear,
                    "question_type": "mcq",
                    "student_level": student_level,
                    "status": "Needs Review",
                    "match_percentage": 0,
                    "submission_remarks": None,
                    "rule_based_remark": f"Found {len(student_answers_by_qid)} MCQ answers but no correct answers in prompt. Include 'Correct: B' for each question.",
                    "student_extracted_text": student_text,
                    "llm_used": False,
                    "question_marks": make_question_marks([]),
            "annotated_pdf": annotated_pdf_filename,
                    "debug": {"student_answers": student_answers_by_qid, "correct_answers_in_prompt": False},
                    "extraction": {"student": {k: v for k, v in student_info.items() if k != "text"}},
                }

        if redirect_to_narrative:
            pass  # Will continue to narrative handling
        elif not correct:
            # Save annotated PDF with circle mark
            if can_annotate and original_file_bytes:
                no_correct_result = [{'qid': extract_qid_from_prompt(prompt, erp_row), 'correct': None, 'chosen': 'Not Found', 'correct_answer': 'N/A'}]
                annotated_pdf_filename, annotated_pdf_url = save_annotated_pdf(
                    original_file_bytes, homework_id, student_id, no_correct_result, 0, "Needs Review", student_level
                )
            return {
                "student_id": student_id,
                "homework_id": homework_id,
                "sub_institute_id": sub_institute_id,
                "syear": syear,
                "question_type": "mcq",
                "student_level": student_level,
                "status": "Needs Review",
                "match_percentage": 0,
                "submission_remarks": None,
                "rule_based_remark": "MCQ correct option not found in prompt. Include 'Correct: B' or similar in prompt.",
                "student_extracted_text": student_text,
                "llm_used": False,
                "question_marks": make_question_marks([]),
            "annotated_pdf": annotated_pdf_filename,
                "debug": {"correct": correct, "chosen": chosen},
                "extraction": {"student": {k: v for k, v in student_info.items() if k != "text"}},
            }
        elif not chosen:
            # Save annotated PDF with circle mark
            if can_annotate and original_file_bytes:
                no_chosen_result = [{'qid': extract_qid_from_prompt(prompt, erp_row), 'correct': None, 'chosen': 'Not Detected', 'correct_answer': correct or 'N/A'}]
                annotated_pdf_filename, annotated_pdf_url = save_annotated_pdf(
                    original_file_bytes, homework_id, student_id, no_chosen_result, 0, "Needs Review", student_level
                )
            return {
                "student_id": student_id,
                "homework_id": homework_id,
                "sub_institute_id": sub_institute_id,
                "syear": syear,
                "question_type": "mcq",
                "student_level": student_level,
                "status": "Needs Review",
                "match_percentage": 0,
                "submission_remarks": None,
                "rule_based_remark": "Student option (A/B/C/D) not detected clearly.",
                "student_extracted_text": student_text,
                "llm_used": False,
                "question_marks": make_question_marks([]),
            "annotated_pdf": annotated_pdf_filename,
                "debug": {"correct": correct, "chosen": chosen},
                "extraction": {"student": {k: v for k, v in student_info.items() if k != "text"}},
            }

        # Only process MCQ validation if not redirecting to narrative
        if not redirect_to_narrative:
            is_correct = (chosen == correct)
            
            # Get level-based credit
            mcq_credit = mcq_partial_credit(student_level)
            credit_per_q = mcq_credit["credit_per_question"]
            
            # Calculate score based on level
            match_percentage = credit_per_q if is_correct else 0
            
            # Determine status based on level threshold
            passing_threshold = mcq_credit["passing_threshold"]
            status = "Verified" if match_percentage >= passing_threshold else "Needs Review"
            
            # Save annotated PDF
            _qid = extract_qid_from_prompt(prompt, erp_row)
            mcq_results_single = [{'qid': _qid, 'correct': is_correct, 'chosen': chosen, 'correct_answer': correct}]
            if can_annotate and original_file_bytes:
                annotated_pdf_filename, annotated_pdf_url = save_annotated_pdf(
                    original_file_bytes, homework_id, student_id, mcq_results_single, match_percentage, status, student_level
                )
            
            return {
                "student_id": student_id,
                "homework_id": homework_id,
                "sub_institute_id": sub_institute_id,
                "syear": syear,
                "question_type": "mcq",
                "student_level": student_level,
                "status": status,
                "match_percentage": match_percentage,
                "submission_remarks": None,
                "rule_based_remark": f"{'Correct' if is_correct else 'Incorrect'}. Score: {match_percentage}% (Level: {student_level}, Credit per Q: {credit_per_q}%)",
                "student_extracted_text": student_text,
                "llm_used": False,
                "question_marks": make_question_marks(mcq_results_single),
            "annotated_pdf": annotated_pdf_filename,
                "debug": {"correct": correct, "chosen": chosen, "level": student_level, "credit_per_q": credit_per_q},
                "extraction": {"student": {k: v for k, v in student_info.items() if k != "text"}},
            }

 
    if gemini_client is None:
        # Save annotated PDF
        if can_annotate and original_file_bytes:
            annotated_pdf_filename, annotated_pdf_url = save_annotated_pdf(
                original_file_bytes, homework_id, student_id, [], 0, "Needs Review", student_level
            )
        return {
            "student_id": student_id,
            "homework_id": homework_id,
            "sub_institute_id": sub_institute_id,
            "syear": syear,
            "question_type": "narrative",
            "student_level": student_level,
            "status": "Needs Review",
            "match_percentage": 0,
            "submission_remarks": None,
            "rule_based_remark": "Gemini not configured. Check /health/llm.",
            "llm_used": False,
            "llm_error": parse_gemini_error(GEMINI_LAST_ERROR),
            "student_extracted_text": student_text,
            "question_marks": make_question_marks([]),
            "annotated_pdf": annotated_pdf_filename,
            "extraction": {"student": {k: v for k, v in student_info.items() if k != "text"}},
        }

    user_prompt = (
        f"STUDENT_LEVEL: {student_level}\n"
        f"QUESTION:\n{prompt.strip()}\n\n"
        'Return ONLY valid JSON with keys: {"ai_reference_answer": string, "key_points": [string, ...]}.'
    )

    response_text = generate_gemini_response(
        prompt=user_prompt,
        system_prompt=(
            "Generate a correct reference answer for homework evaluation. "
            "Keep it aligned with the student level. Output strict JSON only."
        ),
        max_tokens=650,
        temperature=0.3,
    )

    if not response_text:
        # Save annotated PDF
        if can_annotate and original_file_bytes:
            annotated_pdf_filename, annotated_pdf_url = save_annotated_pdf(
                original_file_bytes, homework_id, student_id, [], 0, "Needs Review", student_level
            )
        return {
            "student_id": student_id,
            "homework_id": homework_id,
            "sub_institute_id": sub_institute_id,
            "syear": syear,
            "question_type": "narrative",
            "student_level": student_level,
            "status": "Needs Review",
            "match_percentage": 0,
            "submission_remarks": None,
            "rule_based_remark": "Gemini failed. Check /health/llm.",
            "llm_used": False,
            "llm_error": parse_gemini_error(GEMINI_LAST_ERROR),
            "student_extracted_text": student_text,
            "question_marks": make_question_marks([]),
            "annotated_pdf": annotated_pdf_filename,
            "extraction": {"student": {k: v for k, v in student_info.items() if k != "text"}},
        }

    try:
        m = re.search(r"\{.*\}", response_text, flags=re.S)
        payload = json.loads(m.group(0) if m else response_text)
    except Exception as e:
        # Save annotated PDF
        if can_annotate and original_file_bytes:
            annotated_pdf_filename, annotated_pdf_url = save_annotated_pdf(
                original_file_bytes, homework_id, student_id, [], 0, "Needs Review", student_level
            )
        return {
            "student_id": student_id,
            "homework_id": homework_id,
            "sub_institute_id": sub_institute_id,
            "syear": syear,
            "question_type": "narrative",
            "student_level": student_level,
            "status": "Needs Review",
            "match_percentage": 0,
            "submission_remarks": None,
            "rule_based_remark": "Gemini returned non-JSON output.",
            "llm_used": False,
            "llm_error": {"ok": False, "error_type": "GEMINI_BAD_JSON", "message": str(e), "raw": response_text[:800]},
            "student_extracted_text": student_text,
            "question_marks": make_question_marks([]),
            "annotated_pdf": annotated_pdf_filename,
            "extraction": {"student": {k: v for k, v in student_info.items() if k != "text"}},
        }

    ai_reference_answer = (payload.get("ai_reference_answer") or "").strip()
    key_points = payload.get("key_points") or []
    if not isinstance(key_points, list):
        key_points = []
    key_points = [str(x).strip() for x in key_points if str(x).strip()]

    if not ai_reference_answer:
        # Save annotated PDF
        if can_annotate and original_file_bytes:
            annotated_pdf_filename, annotated_pdf_url = save_annotated_pdf(
                original_file_bytes, homework_id, student_id, [], 0, "Needs Review", student_level
            )
        return {
            "student_id": student_id,
            "homework_id": homework_id,
            "sub_institute_id": sub_institute_id,
            "syear": syear,
            "question_type": "narrative",
            "student_level": student_level,
            "status": "Needs Review",
            "match_percentage": 0,
            "submission_remarks": None,
            "rule_based_remark": "AI returned empty reference answer.",
            "llm_used": True,
            "student_extracted_text": student_text,
            "question_marks": make_question_marks([]),
            "annotated_pdf": annotated_pdf_filename,
            "extraction": {"student": {k: v for k, v in student_info.items() if k != "text"}},
        }

    sim = cosine_sim(student_text, ai_reference_answer)
    covered, missing, coverage = keypoint_coverage(student_text, key_points, kp_threshold=policy["kp_thr"])

    final = policy["w_sim"] * sim + policy["w_cov"] * coverage
    match_pct = int(round(final * 100))

    if match_pct >= policy["verified"]:
        status = "Verified"
    elif match_pct >= policy["partial"]:
        status = "Partial"
    else:
        status = "Needs Review"

    # Short remark (Gemini), fallback to rule-based
    remark_prompt = (
        f"Student level: {student_level}\n"
        f"Match: {match_pct}%\n"
        f"Missing key points: {missing[:6]}\n\n"
        "Write a short, factual teacher remark (2-4 lines). No marks. No overpraise."
    )

    resp2_prompt = (
        f"REFERENCE ANSWER:\n{ai_reference_answer[:900]}\n\n"
        f"STUDENT ANSWER:\n{student_text[:900]}\n\n"
        f"{remark_prompt}"
    )

    submission_remark = generate_gemini_response(
        prompt=resp2_prompt,
        system_prompt="You are a strict, helpful teacher. Be concise and factual.",
        max_tokens=140,
        temperature=0.6,
    )

    rule_based_remark = None
    remark_llm_used = bool(submission_remark)
    remark_llm_error = None if submission_remark else (GEMINI_LAST_ERROR or "Unknown LLM error")

    if not submission_remark:
        if status == "Verified":
            rule_based_remark = "Homework matches the expected answer well. Good coverage of the key ideas."
        elif status == "Partial":
            rule_based_remark = "Homework is partially correct. Improve coverage of missing key points and make the explanation clearer."
        else:
            rule_based_remark = "Homework does not match the expected answer enough. Please review the topic and resubmit with clearer, complete points."

    # Save annotated PDF — evaluate EACH question individually against student text
    per_question_results = build_per_question_results(
        prompt, student_text, status, match_pct,
        ai_reference_answer=ai_reference_answer,
        key_points=key_points,
        policy=policy,
        student_level=student_level,
    )
    if can_annotate and original_file_bytes:
        annotated_pdf_filename, annotated_pdf_url = save_annotated_pdf(
            original_file_bytes, homework_id, student_id, per_question_results, match_pct, status, student_level, "narrative"
        )

    return {
        "student_id": student_id,
        "homework_id": homework_id,
        "sub_institute_id": sub_institute_id,
        "syear": syear,
        "question_type": "narrative",
        "student_level": student_level,
        "status": status,
        "match_percentage": match_pct,
        "submission_remarks": submission_remark if submission_remark else None,
        "rule_based_remark": rule_based_remark,
        "llm_used": True,
        "remark_llm_used": remark_llm_used,
        "remark_llm_error": remark_llm_error,
        "student_extracted_text": student_text,
        "ai_reference_answer": ai_reference_answer,
        "key_points": key_points,
        "key_points_covered": covered,
        "key_points_missing": missing,
        "question_marks": make_question_marks(per_question_results),
        "annotated_pdf": annotated_pdf_filename,
        "debug": {
            "similarity": sim,
            "coverage": coverage,
            "policy": policy,
            "per_question_results": per_question_results,
            "erp_row_fields": list(erp_row.keys()) if erp_row else [],
            "erp_student_level_raw": erp_row.get("student_level") or erp_row.get("level") or erp_row.get("difficulty") or erp_row.get("difficulty_level"),
        },
        "extraction": {"student": {k: v for k, v in student_info.items() if k != "text"}},
    }