File size: 49,453 Bytes
d1bb193
1ddf149
 
 
428054b
 
7e49357
 
60a66d0
7e49357
 
c07129b
428054b
7e49357
 
428054b
c07129b
2aa2caf
 
621f326
 
1ddf149
7e49357
 
 
 
 
 
 
 
 
 
 
 
 
63e2ea5
 
2aa2caf
63e2ea5
428054b
63e2ea5
7e49357
 
 
60a66d0
7e49357
1ddf149
7e49357
 
428054b
63e2ea5
 
 
 
 
 
 
1ddf149
7e49357
 
 
 
 
 
 
 
0da7d43
 
7e49357
 
 
 
 
 
 
 
 
 
0da7d43
 
 
 
 
 
7e49357
 
 
 
 
 
 
 
 
b6190f0
7e49357
 
 
 
 
 
0da7d43
7e49357
 
 
 
0da7d43
7e49357
 
0da7d43
 
7e49357
 
0da7d43
7e49357
 
 
0da7d43
 
7e49357
0da7d43
 
7e49357
 
0da7d43
7e49357
 
 
0da7d43
7e49357
 
 
0da7d43
7e49357
 
 
0da7d43
7e49357
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b046fed
7e49357
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e531516
7e49357
 
 
 
 
 
 
e531516
b6190f0
428054b
 
b046fed
 
 
 
e531516
428054b
 
 
63e2ea5
 
7e49357
 
 
428054b
7e49357
63e2ea5
b046fed
b6190f0
60a66d0
 
0da7d43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e49357
 
 
 
 
0da7d43
7e49357
 
 
 
8354cbf
0da7d43
7e49357
 
 
 
 
 
 
8354cbf
0da7d43
7e49357
0da7d43
 
d1bb193
7e49357
 
 
0da7d43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e49357
 
 
 
0da7d43
 
7e49357
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60a66d0
63e2ea5
 
 
b046fed
63e2ea5
e531516
7e49357
b046fed
63e2ea5
b046fed
 
7e49357
 
b046fed
 
d1bb193
63e2ea5
ea68370
b8cd992
428054b
b046fed
 
 
60a66d0
0d6e382
7e49357
 
 
 
 
 
 
 
 
 
 
 
 
 
63e2ea5
ea68370
b8cd992
63e2ea5
0da7d43
 
 
b046fed
0da7d43
 
 
7e49357
428054b
0da7d43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b046fed
0da7d43
7e49357
0da7d43
7e49357
 
428054b
 
0da7d43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
428054b
0da7d43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60a66d0
0d6e382
7e49357
b046fed
 
 
 
 
 
 
 
 
 
 
 
 
 
7e49357
b046fed
 
 
428054b
7e49357
ceafaef
f001182
c07129b
63e2ea5
c07129b
428054b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c07129b
0d6e382
60a66d0
e531516
60a66d0
 
f001182
 
428054b
b046fed
7e49357
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ddf149
b8cd992
2aa2caf
7e49357
 
 
 
 
 
 
2aa2caf
 
 
 
7e49357
 
 
 
2aa2caf
7e49357
 
 
2aa2caf
7e49357
 
 
 
b8cd992
7e49357
2aa2caf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e49357
2aa2caf
 
 
 
 
 
 
 
 
 
 
 
7e49357
 
 
 
 
 
 
 
 
 
 
 
428054b
7e49357
428054b
2aa2caf
428054b
c07129b
428054b
7e49357
 
2aa2caf
 
c07129b
63e2ea5
7e49357
 
2aa2caf
7e49357
 
 
 
428054b
7e49357
428054b
7e49357
 
428054b
 
 
 
7e49357
428054b
 
 
7e49357
 
2aa2caf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e49357
 
 
 
 
 
2aa2caf
7e49357
 
 
 
 
 
 
 
 
 
 
2aa2caf
7e49357
 
 
 
b046fed
428054b
7e49357
b046fed
428054b
7e49357
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
428054b
b046fed
7e49357
 
 
b046fed
7e49357
 
 
0da7d43
7e49357
0da7d43
7e49357
 
 
 
 
0da7d43
 
7e49357
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0da7d43
7e49357
 
0da7d43
 
7e49357
 
 
 
 
 
 
 
 
 
 
 
 
b046fed
7e49357
63e2ea5
7e49357
 
b046fed
7e49357
 
b046fed
0da7d43
 
b046fed
7e49357
b046fed
7e49357
 
b046fed
7e49357
 
b046fed
7e49357
 
b046fed
0da7d43
 
63e2ea5
7e49357
 
b046fed
 
7e49357
b046fed
63e2ea5
7e49357
0da7d43
 
7e49357
 
 
 
428054b
b8cd992
7e49357
 
428054b
7e49357
63e2ea5
428054b
7e49357
 
 
 
 
 
 
63e2ea5
b046fed
7e49357
b046fed
b8cd992
7e49357
63e2ea5
b046fed
7e49357
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
428054b
7e49357
 
b8cd992
7e49357
428054b
7e49357
 
63e2ea5
7e49357
 
 
2aa2caf
 
7e49357
2aa2caf
 
 
7e49357
 
 
 
 
 
 
428054b
7e49357
 
 
 
 
 
 
428054b
 
2aa2caf
428054b
7e49357
 
2aa2caf
7e49357
b046fed
7e49357
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2aa2caf
 
 
7e49357
 
 
 
 
 
 
 
 
 
63e2ea5
428054b
b046fed
 
428054b
63e2ea5
 
428054b
 
 
 
 
2aa2caf
 
428054b
e531516
 
7e49357
 
 
428054b
7e49357
428054b
7e49357
 
 
b8cd992
7e49357
b8cd992
7e49357
 
 
 
 
 
621f326
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import io
import re
import base64
import gc
import tempfile
import uuid
import asyncio
from typing import List, Dict, Optional, Tuple
from collections import Counter
from concurrent.futures import ThreadPoolExecutor

from fastapi import FastAPI, File, UploadFile, Form, HTTPException, BackgroundTasks
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from starlette.requests import Request
import fitz  # PyMuPDF
import google.generativeai as genai
from PIL import Image
from fastapi import Query


# Azure Blob Storage
try:
    from azure.storage.blob import (
        BlobServiceClient,
        generate_blob_sas,
        BlobSasPermissions,
        ContentSettings
    )
    AZURE_AVAILABLE = True
except ImportError:
    AZURE_AVAILABLE = False
    print("Warning: azure-storage-blob not installed. Run: pip install azure-storage-blob")

# Google Gemini - optional import
try:

    GEMINI_AVAILABLE = True
except ImportError:
    GEMINI_AVAILABLE = False
    print("Warning: google-generativeai not installed. Image-based PDFs won't be supported.")

from datetime import datetime, timedelta

app = FastAPI(title="Invoice Splitter API with Azure Blob Storage - Optimized")

# Increase request body size limit
Request.max_body_size = 200 * 1024 * 1024  # 200MB

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# ============================================================================
# ⭐ CONFIGURATION FROM ENVIRONMENT VARIABLES (Hugging Face Secrets)
# ============================================================================

# Gemini API Key - REQUIRED for image-based PDFs
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY", "")

# Azure Blob Storage Configuration - REQUIRED for blob storage
AZURE_STORAGE_CONNECTION_STRING = os.environ.get(
    "AZURE_STORAGE_CONNECTION_STRING", "")
AZURE_STORAGE_ACCOUNT_NAME = os.environ.get("AZURE_STORAGE_ACCOUNT_NAME", "")
AZURE_STORAGE_ACCOUNT_KEY = os.environ.get("AZURE_STORAGE_ACCOUNT_KEY", "")

# Container name - can be configured or use default
AZURE_CONTAINER_NAME = os.environ.get("AZURE_CONTAINER_NAME", "invoice-splits")

# ⭐ FOLDER STRUCTURE CONFIGURATION
ROOT_FOLDER = os.environ.get("ROOT_FOLDER", "POD")  # Root folder name

# ⭐ PERFORMANCE CONFIGURATION
MAX_PARALLEL_GEMINI_CALLS = int(
    os.environ.get("MAX_PARALLEL_GEMINI_CALLS", "5"))
GEMINI_IMAGE_RESOLUTION = float(
    os.environ.get("GEMINI_IMAGE_RESOLUTION", "1.2"))
USE_SMART_SAMPLING = os.environ.get(
    "USE_SMART_SAMPLING", "false").lower() == "true"

# ⭐ SERVER CONFIGURATION
HOST = os.environ.get("HOST", "0.0.0.0")  # Hugging Face uses 0.0.0.0
PORT = int(os.environ.get("PORT", "7860"))  # Hugging Face default port

# ============================================================================
# GLOBAL VARIABLES
# ============================================================================

gemini_model = None
blob_service_client = None

# ============================================================================
# STARTUP VALIDATION
# ============================================================================


def validate_configuration():
    """Validate configuration and warn about missing credentials."""
    warnings = []
    errors = []

    # Check Gemini API Key
    if not GEMINI_API_KEY:
        warnings.append(
            "⚠️  GEMINI_API_KEY not set - image-based PDFs will not work")
    else:
        print(f"βœ… GEMINI_API_KEY configured ({len(GEMINI_API_KEY)} chars)")

    # Check Azure credentials
    if not AZURE_STORAGE_CONNECTION_STRING:
        if not (AZURE_STORAGE_ACCOUNT_NAME and AZURE_STORAGE_ACCOUNT_KEY):
            errors.append(
                "❌ Azure credentials missing - set AZURE_STORAGE_CONNECTION_STRING or both AZURE_STORAGE_ACCOUNT_NAME and AZURE_STORAGE_ACCOUNT_KEY")
        else:
            print(
                f"βœ… Azure credentials configured (account: {AZURE_STORAGE_ACCOUNT_NAME})")
    else:
        print(f"βœ… Azure connection string configured")

    # Print all warnings
    for warning in warnings:
        print(warning)

    # Print all errors
    for error in errors:
        print(error)

    if errors:
        print("\n⚠️  WARNING: Some required credentials are missing!")
        print("   Set them in Hugging Face Spaces Settings > Repository secrets")

    return len(errors) == 0


# ============================================================================
# AZURE BLOB STORAGE FUNCTIONS
# ============================================================================


def get_blob_service_client():
    """Get or create Azure Blob Service Client."""
    global blob_service_client

    if not AZURE_AVAILABLE:
        print("❌ Azure SDK not available")
        return None

    if blob_service_client is None:
        try:
            if AZURE_STORAGE_CONNECTION_STRING:
                blob_service_client = BlobServiceClient.from_connection_string(
                    AZURE_STORAGE_CONNECTION_STRING
                )
                print("βœ… Azure Blob Storage initialized with connection string")
            elif AZURE_STORAGE_ACCOUNT_NAME and AZURE_STORAGE_ACCOUNT_KEY:
                account_url = f"https://{AZURE_STORAGE_ACCOUNT_NAME}.blob.core.windows.net"
                blob_service_client = BlobServiceClient(
                    account_url=account_url,
                    credential=AZURE_STORAGE_ACCOUNT_KEY
                )
                print("βœ… Azure Blob Storage initialized with account key")
            else:
                print("⚠️ WARNING: No Azure credentials configured")
                return None
        except Exception as e:
            print(f"❌ Failed to initialize Azure Blob Storage: {e}")
            return None

    return blob_service_client


def ensure_container_exists(container_name: str = None):
    """Create container if it doesn't exist."""
    if container_name is None:
        container_name = AZURE_CONTAINER_NAME

    try:
        client = get_blob_service_client()
        if client:
            container_client = client.get_container_client(container_name)
            if not container_client.exists():
                container_client.create_container()
                print(f"βœ… Created container: {container_name}")
            else:
                print(f"βœ… Container exists: {container_name}")
    except Exception as e:
        print(f"⚠️ Container check error: {e}")


def upload_raw_pdf_to_blob(
    pdf_bytes: bytes,
    filename: str,
    batch_id: str,
    container_name: str = None
) -> dict:
    """
    Upload original/raw PDF to Azure Blob Storage.

    Path structure: POD/{batch_id}/{filename}/Raw/{filename}
    """
    if container_name is None:
        container_name = AZURE_CONTAINER_NAME

    try:
        client = get_blob_service_client()
        if not client:
            raise HTTPException(
                status_code=500,
                detail="Azure Blob Storage not configured"
            )

        # Clean filename for folder name
        base_filename = os.path.splitext(filename)[0]
        safe_folder_name = re.sub(r'[<>:"/\\|?*]', '_', base_filename)

        blob_name = f"{ROOT_FOLDER}/{batch_id}/{safe_folder_name}/Raw/{filename}"

        # Get blob client
        blob_client = client.get_blob_client(
            container=container_name,
            blob=blob_name
        )

        # Upload PDF
        print(f"πŸ“€ Uploading raw PDF to: {blob_name}")
        blob_client.upload_blob(
            pdf_bytes,
            overwrite=True,
            content_settings=ContentSettings(content_type='application/pdf'),
            metadata={
                'batch_id': batch_id,
                'file_type': 'raw',
                'uploaded_at': datetime.now().isoformat(),
                'original_filename': filename
            }
        )

        # Generate SAS URL (valid for 24 hours)
        expiry_hours = 24
        sas_token = generate_blob_sas(
            account_name=AZURE_STORAGE_ACCOUNT_NAME,
            container_name=container_name,
            blob_name=blob_name,
            account_key=AZURE_STORAGE_ACCOUNT_KEY,
            permission=BlobSasPermissions(read=True),
            expiry=datetime.utcnow() + timedelta(hours=expiry_hours)
        )

        # Construct URLs
        blob_url = blob_client.url
        download_url = f"{blob_url}?{sas_token}"
        expires_at = (datetime.utcnow() +
                      timedelta(hours=expiry_hours)).isoformat() + "Z"

        print(f"βœ… Uploaded raw PDF: {blob_name}")

        return {
            "blob_name": blob_name,
            "blob_url": blob_url,
            "download_url": download_url,
            "expires_at": expires_at,
            "expires_in_hours": expiry_hours,
            "storage": "azure_blob",
            "folder_type": "raw",
            "container": container_name,
            "size_bytes": len(pdf_bytes),
            "size_mb": round(len(pdf_bytes) / (1024 * 1024), 2)
        }

    except Exception as e:
        print(f"❌ Raw PDF upload failed: {e}")
        raise HTTPException(
            status_code=500,
            detail=f"Azure Blob upload failed: {str(e)}"
        )


def upload_split_pdf_to_blob(
    pdf_bytes: bytes,
    invoice_filename: str,
    original_filename: str,
    batch_id: str,
    container_name: str = None
) -> dict:
    """
    Upload split invoice PDF to Azure Blob Storage.

    Path structure: POD/{batch_id}/{original_filename}/Splitted/{invoice_filename}
    """
    if container_name is None:
        container_name = AZURE_CONTAINER_NAME

    try:
        client = get_blob_service_client()
        if not client:
            raise HTTPException(
                status_code=500,
                detail="Azure Blob Storage not configured"
            )

        # Clean original filename for folder name
        base_filename = os.path.splitext(original_filename)[0]
        safe_folder_name = re.sub(r'[<>:"/\\|?*]', '_', base_filename)

        blob_name = f"{ROOT_FOLDER}/{batch_id}/{safe_folder_name}/Splitted/{invoice_filename}"

        # Get blob client
        blob_client = client.get_blob_client(
            container=container_name,
            blob=blob_name
        )

        # Upload PDF
        blob_client.upload_blob(
            pdf_bytes,
            overwrite=True,
            content_settings=ContentSettings(content_type='application/pdf'),
            metadata={
                'batch_id': batch_id,
                'file_type': 'split',
                'uploaded_at': datetime.now().isoformat(),
                'original_filename': original_filename,
                'invoice_filename': invoice_filename
            }
        )

        # Generate SAS URL (valid for 24 hours)
        expiry_hours = 24
        sas_token = generate_blob_sas(
            account_name=AZURE_STORAGE_ACCOUNT_NAME,
            container_name=container_name,
            blob_name=blob_name,
            account_key=AZURE_STORAGE_ACCOUNT_KEY,
            permission=BlobSasPermissions(read=True),
            expiry=datetime.utcnow() + timedelta(hours=expiry_hours)
        )

        # Construct URLs
        blob_url = blob_client.url
        download_url = f"{blob_url}?{sas_token}"
        expires_at = (datetime.utcnow() +
                      timedelta(hours=expiry_hours)).isoformat() + "Z"

        return {
            "blob_name": blob_name,
            "blob_url": blob_url,
            "download_url": download_url,
            "expires_at": expires_at,
            "expires_in_hours": expiry_hours,
            "storage": "azure_blob",
            "folder_type": "split",
            "container": container_name,
            "size_bytes": len(pdf_bytes),
            "size_mb": round(len(pdf_bytes) / (1024 * 1024), 2)
        }

    except Exception as e:
        print(f"❌ Split PDF upload failed: {e}")
        raise HTTPException(
            status_code=500,
            detail=f"Azure Blob upload failed: {str(e)}"
        )


async def cleanup_old_blobs(batch_id: str, container_name: str = None):
    """Delete all blobs for a specific batch_id."""
    if container_name is None:
        container_name = AZURE_CONTAINER_NAME

    try:
        client = get_blob_service_client()
        if not client:
            return

        container_client = client.get_container_client(container_name)

        prefix = f"{ROOT_FOLDER}/{batch_id}/"
        blobs = container_client.list_blobs(name_starts_with=prefix)

        deleted_count = 0
        for blob in blobs:
            blob_client = container_client.get_blob_client(blob.name)
            blob_client.delete_blob()
            deleted_count += 1

        print(f"🧹 Cleaned up {deleted_count} blobs for batch {batch_id}")

    except Exception as e:
        print(f"⚠️ Cleanup error: {e}")


# ============================================================================
# OPTIMIZED GEMINI FUNCTIONS WITH ASYNC PROCESSING
# ============================================================================

def get_gemini_model():
    """Get or create Gemini model instance."""
    global gemini_model

    if not GEMINI_AVAILABLE:
        return None

    if gemini_model is None:
        if not GEMINI_API_KEY:
            return None

        try:
            genai.configure(api_key=GEMINI_API_KEY)
            # Use Gemini 2.5 Flash
            gemini_model = genai.GenerativeModel('gemini-2.5-flash')
            print("βœ… Google Gemini 2.5 Flash initialized")
        except Exception as e:
            print(f"❌ Failed to initialize Gemini: {e}")
            return None

    return gemini_model


def _clean_gemini_invoice_text(text: str) -> Optional[str]:
    if not text:
        return None

    cleaned = text.strip()
    cleaned = cleaned.replace("*", "").replace("#", "")
    cleaned = re.sub(
        r"(?i)\b(invoice|inv|bill|document|doc|tax\s*invoice)\s*(no|number)?\b",
        "",
        cleaned
    )
    cleaned = re.sub(r"[:\-\(\)\[\]]", " ", cleaned)
    cleaned = re.sub(r"\s+", " ", cleaned).strip()

    # Extract candidates
    tokens = re.findall(r"[A-Z0-9][A-Z0-9\-\/]{2,}", cleaned.upper())

    # Prefer alphanumeric invoice IDs first
    for token in tokens:
        if any(c.isalpha() for c in token) and any(c.isdigit() for c in token):
            return token

    # Fallback to numeric-only (6-15 digits)
    for token in tokens:
        if token.isdigit() and 6 <= len(token) <= 15:
            return token

    return None


def extract_invoice_gemini_sync(page: fitz.Page) -> Optional[str]:
    """
    Optimized synchronous Gemini extraction for thread pool execution.
    - Reduced image resolution for faster processing
    - Simplified prompt for quicker responses
    - OCR fallback for better accuracy
    """
    model = get_gemini_model()
    if not model:
        return None

    img = None
    try:
        # Reduced resolution for faster processing
        pix = page.get_pixmap(matrix=fitz.Matrix(
            GEMINI_IMAGE_RESOLUTION, GEMINI_IMAGE_RESOLUTION))
        img_bytes = pix.tobytes("png")
        pix = None
        img = Image.open(io.BytesIO(img_bytes))

        # Updated prompt to prioritize labeled alphanumeric invoice numbers
        prompt = """Extract ONLY the invoice number from this image.
Prefer the value next to labels like: Invoice No, Invoice Number, Bill No, Document No.
Return ONLY the identifier (keep letters, e.g., A07966). If not found, return: NONE."""

        response = model.generate_content([prompt, img])
        if response and response.text:
            extracted_text = response.text.strip()
            candidate = _clean_gemini_invoice_text(extracted_text)
            if candidate and len(candidate) > 2:
                img.close()
                return candidate

        # OCR Fallback: Extract full text then run regex
        ocr_prompt = "Extract all text from this invoice image. Return the complete text content."
        ocr_response = model.generate_content([ocr_prompt, img])
        if ocr_response and ocr_response.text:
            inv = try_extract_invoice_from_text(ocr_response.text)
            if inv:
                img.close()
                return inv

        if img:
            img.close()
        return None

    except Exception as e:
        print(f"Gemini error: {e}")
        if img:
            img.close()
        return None


async def extract_invoices_batch_async(
    doc: fitz.Document,
    is_image_pdf: bool,
    batch_size: int = MAX_PARALLEL_GEMINI_CALLS
) -> List[Optional[str]]:
    """
    πŸš€ OPTIMIZED: Extract invoice numbers with parallel processing.

    For text PDFs: Fast sequential processing
    For image PDFs: Parallel Gemini API calls (5-10x faster)
    """
    page_invoice_nos = []

    if not is_image_pdf:
        # Fast text-based extraction (no parallelization needed)
        print(f"  πŸ“ Text-based extraction (sequential)")
        for i in range(doc.page_count):
            if i % 50 == 0:
                print(f"  Extracting... Page {i+1}/{doc.page_count}")
            page = doc.load_page(i)
            inv = extract_invoice_text_based(page)
            page_invoice_nos.append(inv)
            page = None
            if i % 100 == 0:
                gc.collect()
        return page_invoice_nos

    # Image-based PDF: Use parallel Gemini processing
    print(f"  πŸš€ Image-based extraction (parallel, batch_size={batch_size})")

    # Use ThreadPoolExecutor for parallel API calls
    with ThreadPoolExecutor(max_workers=batch_size) as executor:
        futures = []

        # Submit all pages to thread pool
        for i in range(doc.page_count):
            page = doc.load_page(i)
            # First try text extraction (fast)
            text_result = extract_invoice_text_based(page)
            if text_result:
                futures.append((i, None, text_result))
            else:
                # Submit to Gemini thread pool
                future = executor.submit(extract_invoice_gemini_sync, page)
                futures.append((i, future, None))

        # Collect results in order
        page_invoice_nos = [None] * doc.page_count
        completed = 0

        for i, future, text_result in futures:
            try:
                if text_result:
                    # Already extracted from text
                    page_invoice_nos[i] = text_result
                    completed += 1
                else:
                    # Wait for Gemini result
                    result = future.result(timeout=30)
                    page_invoice_nos[i] = result
                    completed += 1

                if completed % 5 == 0:
                    print(
                        f"  βœ“ Processed {completed}/{doc.page_count} pages...")

            except Exception as e:
                print(f"  ⚠️ Page {i+1} failed: {e}")
                page_invoice_nos[i] = None

            if completed % 20 == 0:
                gc.collect()

    print(f"  βœ… Extraction complete: {completed}/{doc.page_count} pages")
    return page_invoice_nos


def extract_invoices_smart_sampling(doc: fitz.Document, is_image_pdf: bool) -> List[Optional[str]]:
    """
    ⚑ FASTEST: Smart sampling strategy for large PDFs.
    """
    print(f"  ⚑ Smart sampling mode (faster, ~95% accurate)")

    page_invoice_nos = [None] * doc.page_count

    # Always extract from first page
    page = doc.load_page(0)
    page_invoice_nos[0] = extract_invoice_no_from_page(page, is_image_pdf)
    print(f"  βœ“ Page 1: {page_invoice_nos[0]}")

    # Sample every Nth page to detect changes
    sample_interval = max(3, doc.page_count // 20)
    print(f"  Sampling interval: every {sample_interval} pages")

    for i in range(sample_interval, doc.page_count, sample_interval):
        page = doc.load_page(i)
        inv = extract_invoice_no_from_page(page, is_image_pdf)
        page_invoice_nos[i] = inv

        if i % 10 == 0:
            print(f"  Sampling page {i+1}/{doc.page_count}...")

        # If invoice changed, extract nearby pages to find exact boundary
        prev_known_idx = i - sample_interval
        while prev_known_idx >= 0 and page_invoice_nos[prev_known_idx] is None:
            prev_known_idx -= 1

        if prev_known_idx >= 0 and inv != page_invoice_nos[prev_known_idx]:
            print(f"  πŸ” Boundary detected near page {i+1}, refining...")
            for offset in range(-3, 4):
                idx = i + offset
                if 0 <= idx < doc.page_count and page_invoice_nos[idx] is None:
                    page = doc.load_page(idx)
                    page_invoice_nos[idx] = extract_invoice_no_from_page(
                        page, is_image_pdf)

    # Also check last page
    if page_invoice_nos[-1] is None:
        page = doc.load_page(doc.page_count - 1)
        page_invoice_nos[-1] = extract_invoice_no_from_page(page, is_image_pdf)
        print(f"  βœ“ Last page: {page_invoice_nos[-1]}")

    # Forward-fill gaps
    last_known = page_invoice_nos[0]
    filled = 0
    for i in range(len(page_invoice_nos)):
        if page_invoice_nos[i] is not None:
            last_known = page_invoice_nos[i]
        else:
            page_invoice_nos[i] = last_known
            filled += 1

    print(f"  βœ… Smart sampling complete: forward-filled {filled} pages")
    return page_invoice_nos


# ============================================================================
# PDF PROCESSING FUNCTIONS
# ============================================================================

def is_image_based_pdf(doc: fitz.Document, sample_pages: int = 3) -> Tuple[bool, float]:
    total_text_length = 0
    pages_to_check = min(sample_pages, doc.page_count)

    for i in range(pages_to_check):
        text = doc.load_page(i).get_text("text") or ""
        total_text_length += len(text.strip())

    avg_text_length = total_text_length / pages_to_check
    is_image_based = avg_text_length < 50

    print(f"  PDF Type: {'IMAGE-BASED' if is_image_based else 'TEXT-BASED'}")
    print(f"  Avg text per page: {avg_text_length:.1f} chars")
    return is_image_based, avg_text_length


def normalize_text_for_search(s: str) -> str:
    if not s:
        return s
    s = s.replace("\u00A0", " ")
    s = re.sub(r"[\r\n\t]+", " ", s)
    s = re.sub(r"[ ]{2,}", " ", s).strip()
    return s


def is_valid_invoice_number(candidate: str) -> bool:
    if not candidate or len(candidate) < 3:
        return False
    if len(candidate) == 15 and re.match(r'^[0-9A-Z]{15}$', candidate.upper()):
        return False
    if re.match(r'^\d+$', candidate):
        return 6 <= len(candidate) <= 15
    if re.match(r'^\d+\.\d{2,}$', candidate):
        return False
    has_letter = any(c.isalpha() for c in candidate)
    has_digit = any(c.isdigit() for c in candidate)
    return has_letter and has_digit


def try_extract_invoice_from_text(text: str) -> Optional[str]:
    if not text:
        return None
    text_norm = normalize_text_for_search(text)
    
    # DEBUG: Print first 600 chars
    print(f"\nπŸ” DEBUG - Extracted text (first 600 chars):\n{text_norm[:600]}\n")

    # PRIORITY 1: Look for CREDIT number (14 digits, common in pharma invoices)
    credit_match = re.search(
        r"CREDIT\s*(?:NO|NUMBER|#)?\s*[:\-]?\s*(\d{12,20})",
        text_norm, re.IGNORECASE
    )
    if credit_match:
        credit_num = credit_match.group(1).strip()
        print(f"βœ“ Found CREDIT number: {credit_num}")
        if 12 <= len(credit_num) <= 20:
            return credit_num.upper()

    # PRIORITY 2: Look for "Invoice No" or "Bill No" followed by long numeric (12-20 digits)
    invoice_patterns = [
        r"Invoice\s*(?:No|Number)\.?\s*[:\-]?\s*(\d{12,20})",
        r"Bill\s*(?:No|Number)\.?\s*[:\-]?\s*(\d{12,20})",
        r"Tax\s*Invoice\s*(?:No|Number)\.?\s*[:\-]?\s*(\d{12,20})",
    ]
    
    for pattern in invoice_patterns:
        match = re.search(pattern, text_norm, re.IGNORECASE)
        if match:
            num = match.group(1).strip()
            print(f"βœ“ Found labeled long numeric invoice: {num}")
            return num.upper()

    # PRIORITY 3: Look for "Invoice No" with alphanumeric (but EXCLUDE batch patterns)
    label_patterns = [
        r"Invoice\s*No\.?\s*[:\-]\s*([A-Z][A-Z0-9\-\/]{2,20})",
        r"Bill\s*No\.?\s*[:\-]\s*([A-Z][A-Z0-9\-\/]{2,20})",
    ]
    
    for pattern in label_patterns:
        match = re.search(pattern, text_norm, re.IGNORECASE)
        if match:
            invoice_num = match.group(1).strip()
            
            # EXCLUDE batch number patterns (single letter + 6 digits: F500256, I500734, etc.)
            if re.match(r'^[A-Z]\d{6}$', invoice_num, re.IGNORECASE):
                print(f"⚠️ Skipping (batch pattern): {invoice_num}")
                continue
            
            # EXCLUDE license patterns (KA-MY2-157424)
            if re.match(r'^[A-Z]{2,3}-[A-Z0-9]+-\d+$', invoice_num, re.IGNORECASE):
                print(f"⚠️ Skipping (license pattern): {invoice_num}")
                continue
                
            print(f"βœ“ Found labeled alphanumeric: {invoice_num}")
            if any(c.isalpha() for c in invoice_num) and any(c.isdigit() for c in invoice_num):
                if 3 <= len(invoice_num) <= 20:
                    return invoice_num.upper()

    # PRIORITY 4: Look for long numeric values (12-20 digits) in top area
    top_text = text_norm[:1000]
    long_numerics = re.findall(r'\b(\d{12,20})\b', top_text)
    
    if long_numerics:
        # Take the longest one (most likely to be invoice number)
        longest = max(long_numerics, key=len)
        print(f"βœ“ Found long numeric value: {longest}")
        return longest.upper()

    # PRIORITY 5: Look near "Invoice" label for tokens, EXCLUDE batch patterns
    label_match = re.search(
        r"(?:Invoice|Bill|Tax\s*Invoice)\s*(?:No|Number|#|\.|:\s*)",
        text_norm, re.IGNORECASE
    )
    if label_match:
        start_idx = label_match.end()
        candidate_text = text_norm[start_idx:start_idx + 100]
        print(f"πŸ” Text after label: '{candidate_text[:50]}...'")
        
        tokens = re.findall(r"\b([A-Z0-9][A-Z0-9\-\/]{2,20})\b", candidate_text, re.IGNORECASE)
        print(f"πŸ” Tokens found: {tokens}")
        
        for token in tokens:
            token = token.strip(".,;:-*")
            
            # Skip common words
            if token.upper() in ("ORDER", "REF", "NO", "DATE", "DT", "INV", "BILL", "ACCOUNT", "PO", "COPY", "OF"):
                continue
            
            # EXCLUDE batch patterns (F500256, I500734)
            if re.match(r'^[A-Z]\d{6}$', token, re.IGNORECASE):
                print(f"⚠️ Skipping (batch pattern): {token}")
                continue
            
            # EXCLUDE license patterns
            if re.match(r'^[A-Z]{2,3}-[A-Z0-9]+-\d+$', token, re.IGNORECASE):
                print(f"⚠️ Skipping (license pattern): {token}")
                continue
                
            if any(c.isalpha() for c in token) and any(c.isdigit() for c in token):
                if 3 <= len(token) <= 20:
                    print(f"βœ“ Selected token: {token}")
                    return token.upper()

    # PRIORITY 6: Medium-length numeric (10-15 digits)
    medium_numerics = re.findall(r'\b(\d{10,15})\b', top_text)
    for num in medium_numerics:
        # Exclude phone numbers (10 digits starting with 6-9)
        if len(num) == 10 and num[0] in '6789':
            continue
        # Exclude dates (8 digits starting with 20)
        if len(num) == 8 and num.startswith('20'):
            continue
        print(f"βœ“ Found medium numeric value: {num}")
        return num.upper()

    print("βœ— No invoice number found")
    return None

def extract_invoice_text_based(page: fitz.Page) -> Optional[str]:
    text = page.get_text("text") or ""
    inv = try_extract_invoice_from_text(text)
    if inv:
        return inv
    for block in (page.get_text("blocks") or []):
        block_text = block[4] if len(block) > 4 else ""
        if block_text:
            inv = try_extract_invoice_from_text(block_text)
            if inv:
                return inv
    return None


def extract_invoice_no_from_page(page: fitz.Page, is_image_pdf: bool) -> Optional[str]:
    """Extract invoice number from a single page (used by smart sampling)."""
    text_result = extract_invoice_text_based(page)
    if text_result:
        return text_result
    if is_image_pdf:
        return extract_invoice_gemini_sync(page)
    return None


def build_pdf_from_pages(src_doc: fitz.Document, page_indices: List[int]) -> bytes:
    out = fitz.open()
    try:
        for i in page_indices:
            out.insert_pdf(src_doc, from_page=i, to_page=i)
        pdf_bytes = out.tobytes(garbage=4, deflate=True)
        return pdf_bytes
    finally:
        out.close()


def remove_file(path: str):
    try:
        if os.path.exists(path):
            os.remove(path)
    except Exception as e:
        print(f"⚠️ Cleanup warning: {e}")


# ============================================================================
# API ENDPOINTS
# ============================================================================

@app.post("/split-invoices")
async def split_invoices(
    background_tasks: BackgroundTasks,
    file: UploadFile = File(...),

    # ⭐ REQUIRED: Batch ID
    batch_id: str = Form(...,
                         description="Batch ID (required) - used for folder structure"),

    # Blob Storage options
    use_blob_storage: bool = Form(
        True, description="Upload PDFs to Azure Blob Storage"),
    blob_container: Optional[str] = Form(
        None, description="Custom Azure container (optional)"),

    # Response options
    include_base64: bool = Form(
        False, description="Include base64 in response"),

    # Performance options
    parallel_batch_size: int = Form(
        MAX_PARALLEL_GEMINI_CALLS, description="Parallel Gemini API calls (1-10)"),
    use_smart_sampling: bool = Form(
        USE_SMART_SAMPLING, description="Use smart sampling (faster, ~95% accurate)"),

    # File size limit
    max_file_size_mb: int = Form(200, description="Maximum file size in MB"),
):
    """
    ⭐ OPTIMIZED INVOICE SPLITTER - SUPPORTS PDF AND IMAGES

    Performance Improvements:
    - Parallel Gemini API calls (5-10x faster for image PDFs)
    - Smart sampling option for large PDFs
    - Reduced image resolution for faster processing
    - Optimized prompts for quicker responses

    File Support:
    - PDF files (text-based or image-based)
    - Image files (PNG, JPG, JPEG, TIFF, BMP) - auto-converted to PDF

    Folder Structure in Blob Storage:
    POD/
      └── {batch_id}/
           └── {filename}/
                β”œβ”€β”€ Raw/ (original uploaded file)
                └── Splitted/ (individual split invoice PDFs)

    Required Parameters:
    - file: PDF or image file to upload
    - batch_id: Batch identifier (used for folder structure)

    Returns:
    - All invoice URLs with proper folder paths
    """

    # ============================================================================
    # ENHANCED VALIDATION - ACCEPT PDF AND IMAGES
    # ============================================================================
    
    if not file.filename:
        raise HTTPException(status_code=400, detail="No filename provided")
    
    filename_lower = file.filename.lower()
    
    # Supported formats
    SUPPORTED_EXTENSIONS = ['.pdf', '.png', '.jpg', '.jpeg', '.tiff', '.tif', '.bmp']
    
    file_extension = None
    for ext in SUPPORTED_EXTENSIONS:
        if filename_lower.endswith(ext):
            file_extension = ext
            break
    
    if not file_extension:
        raise HTTPException(
            status_code=400, 
            detail=f"Unsupported file format. Supported: PDF, PNG, JPG, JPEG, TIFF, BMP"
        )
    
    is_image_file = file_extension in ['.png', '.jpg', '.jpeg', '.tiff', '.tif', '.bmp']
    
    # Check PIL availability for image files
    if is_image_file and not GEMINI_AVAILABLE:
        raise HTTPException(
            status_code=500,
            detail="Image processing requires PIL. Install: pip install Pillow"
        )

    # Check blob storage
    if use_blob_storage and not get_blob_service_client():
        raise HTTPException(
            status_code=500, detail="Azure Blob Storage not configured")

    # Container
    container_name = blob_container if blob_container else AZURE_CONTAINER_NAME

    # Ensure container exists
    if use_blob_storage:
        ensure_container_exists(container_name)

    # Stream upload to temp file
    max_size_bytes = max_file_size_mb * 1024 * 1024
    fd, temp_path = tempfile.mkstemp(suffix=file_extension)
    os.close(fd)

    doc = None
    original_pdf_bytes = None
    start_time = datetime.now()
    pdf_path = temp_path
    original_filename = file.filename

    try:
        print(f"\n{'='*70}")
        print(f"πŸ“₯ Processing: {file.filename}")
        print(f"   File Type: {'IMAGE' if is_image_file else 'PDF'}")
        print(f"   Batch ID: {batch_id}")
        print(
            f"   Performance Mode: {'Smart Sampling' if use_smart_sampling else f'Parallel ({parallel_batch_size} workers)'}")
        print(f"{'='*70}")

        total_size = 0
        with open(temp_path, "wb") as buffer:
            chunk_read_size = 5 * 1024 * 1024
            while content := await file.read(chunk_read_size):
                total_size += len(content)
                if total_size > max_size_bytes:
                    remove_file(temp_path)
                    raise HTTPException(
                        status_code=413, detail=f"File too large. Max: {max_file_size_mb}MB")
                buffer.write(content)

        file_size_mb = total_size / (1024 * 1024)
        print(f"πŸ’Ύ File size: {file_size_mb:.2f}MB")

        # ============================================================================
        # IMAGE TO PDF CONVERSION
        # ============================================================================
        
        if is_image_file:
            print(f"πŸ–ΌοΈ  Converting image to PDF...")
            try:
                from PIL import Image as PILImage
                
                # Open image and convert to PDF
                img = PILImage.open(temp_path)
                
                # Convert to RGB if necessary (for RGBA, grayscale, etc.)
                if img.mode != 'RGB':
                    img = img.convert('RGB')
                
                # Create PDF path
                pdf_path = temp_path.replace(file_extension, '.pdf')
                
                # Save as PDF
                img.save(pdf_path, 'PDF', resolution=100.0)
                img.close()
                
                print(f"βœ… Image converted to PDF")
                
                # Update filename for storage
                file.filename = file.filename.replace(file_extension, '.pdf')
                
            except Exception as e:
                print(f"❌ Image conversion failed: {e}")
                raise HTTPException(
                    status_code=500, 
                    detail=f"Failed to convert image to PDF: {str(e)}"
                )

        # Read PDF bytes (either original or converted)
        with open(pdf_path, "rb") as f:
            original_pdf_bytes = f.read()

        # Upload original PDF to Raw folder
        raw_pdf_info = None
        if use_blob_storage:
            try:
                print(f"\nπŸ“€ Uploading original {'PDF' if not is_image_file else 'converted PDF'} to Raw folder...")
                raw_pdf_info = upload_raw_pdf_to_blob(
                    original_pdf_bytes,
                    file.filename,
                    batch_id,
                    container_name
                )
                print(f"βœ… Original PDF uploaded: {raw_pdf_info['blob_name']}")
            except Exception as e:
                print(f"⚠️ Failed to upload raw PDF: {e}")

        # Open PDF for processing
        doc = fitz.open(pdf_path)
        if doc.page_count == 0:
            raise HTTPException(status_code=400, detail="Empty PDF")

        print(f"πŸ“„ Total pages: {doc.page_count}")

        # Detect PDF type
        is_image_pdf, _ = is_image_based_pdf(doc)
        if is_image_pdf and not get_gemini_model():
            raise HTTPException(
                status_code=500, detail="Image PDF detected but Gemini not configured")

        # ⚑ OPTIMIZED EXTRACTION
        print(f"\nπŸ“Š Extracting invoice numbers...")
        extraction_start = datetime.now()

        if use_smart_sampling and doc.page_count > 10:
            # Smart sampling for large PDFs
            page_invoice_nos = extract_invoices_smart_sampling(
                doc, is_image_pdf)
        else:
            # Parallel extraction (async batch processing)
            page_invoice_nos = await extract_invoices_batch_async(
                doc,
                is_image_pdf,
                batch_size=parallel_batch_size
            )

        extraction_time = (datetime.now() - extraction_start).total_seconds()
        print(f"βœ… Extraction completed in {extraction_time:.1f} seconds")
        print(f"   Speed: {doc.page_count / extraction_time:.1f} pages/second")

        # ============================================================================
        # πŸ”§ CORRECTED GROUPING LOGIC - NO AGGRESSIVE FILTERING
        # ============================================================================

        print(f"\nπŸ”§ Grouping invoices...")

        # DEBUG: Show raw extraction results
        print(f"\nπŸ” DEBUG - Raw extraction results:")
        for idx, inv in enumerate(page_invoice_nos[:min(10, len(page_invoice_nos))]):
            print(f"   Page {idx+1}: {inv if inv else '(not found)'}")
        if len(page_invoice_nos) > 10:
            print(
                f"   ... (showing first 10 of {len(page_invoice_nos)} pages)")

        # Step 1: Normalize extracted invoice numbers (only filter GST numbers)
        page_invoice_nos_normalized = []
        for v in page_invoice_nos:
            if v and v.upper().startswith("GST"):
                # Filter out GST numbers (not invoice numbers)
                page_invoice_nos_normalized.append(None)
            elif v:
                # Normalize: uppercase, remove spaces/underscores
                normalized = v.upper().strip().replace(" ", "").replace("_", "")
                page_invoice_nos_normalized.append(normalized)
            else:
                page_invoice_nos_normalized.append(None)

        # Step 2: Smart forward-fill for failed extractions
        # Only fill None values, DON'T remove any extracted invoice numbers
        page_invoice_nos_filled = []
        last_known_invoice = None

        for idx, inv in enumerate(page_invoice_nos_normalized):
            if inv is not None:
                # Valid invoice number found
                last_known_invoice = inv
                page_invoice_nos_filled.append(inv)
            else:
                # Extraction failed - use last known invoice
                page_invoice_nos_filled.append(last_known_invoice)

        # Count how many pages were forward-filled
        filled_count = sum(1 for i in range(len(page_invoice_nos_normalized))
                           if page_invoice_nos_normalized[i] is None and page_invoice_nos_filled[i] is not None)

        # Debug: Count unique invoice numbers
        unique_invoices = set(
            [v for v in page_invoice_nos_filled if v is not None])
        print(f"\n   πŸ“Š Found {len(unique_invoices)} unique invoice numbers:")
        for inv_no in sorted(unique_invoices) if unique_invoices else []:
            page_count = sum(1 for v in page_invoice_nos_filled if v == inv_no)
            print(f"      β€’ {inv_no}: {page_count} pages")

        # Step 3: Group consecutive pages by invoice number
        groups = []
        current_group = []
        current_invoice = None

        for idx, inv in enumerate(page_invoice_nos_filled):
            if idx == 0:
                # First page
                current_invoice = inv
                current_group = [idx]
            else:
                if inv != current_invoice:
                    # Invoice number changed - save current group and start new one
                    groups.append({
                        "invoice_no": current_invoice,
                        "pages": current_group[:]
                    })
                    print(
                        f"   πŸ“„ Group {len(groups)}: Invoice {current_invoice or 'UNKNOWN'} - Pages {current_group[0]+1}-{current_group[-1]+1} ({len(current_group)} pages)")
                    current_invoice = inv
                    current_group = [idx]
                else:
                    # Same invoice - add to current group
                    current_group.append(idx)

        # Don't forget the last group
        if current_group:
            groups.append({
                "invoice_no": current_invoice,
                "pages": current_group[:]
            })
            print(
                f"   πŸ“„ Group {len(groups)}: Invoice {current_invoice or 'UNKNOWN'} - Pages {current_group[0]+1}-{current_group[-1]+1} ({len(current_group)} pages)")

        # Handle edge case: entire PDF has no invoice numbers
        if len(groups) == 1 and groups[0]["invoice_no"] is None:
            groups = [{
                "invoice_no": None,
                "pages": list(range(doc.page_count))
            }]

        print(f"\nβœ… Created {len(groups)} invoice groups")
        print(
            f"   Forward-filled {filled_count} pages with missing invoice numbers")

        # Build and upload split PDFs
        print(f"\nπŸ”¨ Building and uploading split invoices...")
        all_parts = []

        for idx, g in enumerate(groups):
            if (idx + 1) % 20 == 0:
                print(f"  Processing {idx + 1}/{len(groups)} invoices...")

            # Build PDF
            part_bytes = build_pdf_from_pages(doc, g["pages"])

            # Generate filename
            invoice_no = g["invoice_no"] if g["invoice_no"] else f"NO_NUMBER_{idx + 1}"
            safe_invoice_no = re.sub(r'[<>:"/\\|?*]', '_', invoice_no)
            invoice_filename = f"invoice_{safe_invoice_no}.pdf"

            # Prepare invoice info
            invoice_info = {
                "invoice_no": g["invoice_no"],
                "pages": [p + 1 for p in g["pages"]],
                "page_range": f"{g['pages'][0]+1}-{g['pages'][-1]+1}" if len(g['pages']) > 1 else f"{g['pages'][0]+1}",
                "num_pages": len(g["pages"]),
                "size_bytes": len(part_bytes),
                "size_mb": round(len(part_bytes) / (1024 * 1024), 2),
            }

            # Upload to Splitted folder
            if use_blob_storage:
                try:
                    blob_info = upload_split_pdf_to_blob(
                        part_bytes,
                        invoice_filename,
                        file.filename,
                        batch_id,
                        container_name
                    )
                    invoice_info["storage"] = blob_info
                    invoice_info["pdf_url"] = blob_info["download_url"]
                    invoice_info["blob_name"] = blob_info["blob_name"]
                    invoice_info["expires_at"] = blob_info["expires_at"]
                except Exception as e:
                    print(f"  ⚠️ Failed to upload invoice {idx+1}: {e}")
                    invoice_info["upload_error"] = str(e)

            # Include base64 if requested
            if include_base64:
                invoice_info["pdf_base64"] = base64.b64encode(
                    part_bytes).decode("ascii")

            all_parts.append(invoice_info)
            del part_bytes

            if idx % 50 == 0:
                gc.collect()

        print(f"βœ… Processed all {len(all_parts)} invoices")

        # ⭐ SAVE VALUES BEFORE CLOSING DOCUMENT
        total_pages_count = doc.page_count

        # Close document
        doc.close()
        doc = None
        
        # Clean up temp files
        remove_file(temp_path)
        if pdf_path != temp_path:
            remove_file(pdf_path)
        
        gc.collect()

        # Calculate total processing time
        total_time = (datetime.now() - start_time).total_seconds()

        # Return response
        response_data = {
            "success": True,
            "batch_id": batch_id,
            "folder_structure": {
                "root": ROOT_FOLDER,
                "path": f"{ROOT_FOLDER}/{batch_id}/{os.path.splitext(file.filename)[0]}",
                "raw_folder": f"{ROOT_FOLDER}/{batch_id}/{os.path.splitext(file.filename)[0]}/Raw",
                "split_folder": f"{ROOT_FOLDER}/{batch_id}/{os.path.splitext(file.filename)[0]}/Splitted"
            },
            "source_file": {
                "name": file.filename,
                "original_name": original_filename,
                "size_mb": round(file_size_mb, 2),
                "total_pages": total_pages_count,
                "pdf_type": "image-based" if is_image_pdf else "text-based",
                "was_converted": is_image_file,
                "raw_pdf": raw_pdf_info
            },
            "summary": {
                "total_invoices": len(all_parts),
                "unique_invoice_numbers": len(unique_invoices),
                "extraction_method": "gemini" if is_image_pdf else "text",
                "pages_forward_filled": filled_count,
                "storage_type": "azure_blob" if use_blob_storage else "base64"
            },
            "performance": {
                "total_time_seconds": round(total_time, 2),
                "extraction_time_seconds": round(extraction_time, 2),
                "pages_per_second": round(total_pages_count / extraction_time, 2) if extraction_time > 0 else 0,
                "parallel_batch_size": parallel_batch_size,
                "smart_sampling_used": use_smart_sampling and total_pages_count > 10
            },
            "invoices": all_parts
        }

        print(f"\n{'='*70}")
        print(f"βœ… SUCCESS!")
        print(f"   Batch ID: {batch_id}")
        print(f"   Original File: {original_filename}")
        if is_image_file:
            print(f"   βœ“ Image converted to PDF")
        print(
            f"   Raw PDF: {raw_pdf_info['blob_name'] if raw_pdf_info else 'Not uploaded'}")
        print(f"   Split invoices: {len(all_parts)}")
        print(f"   Unique invoice numbers: {len(unique_invoices)}")
        print(f"   Total time: {total_time:.1f}s")
        print(
            f"   Extraction time: {extraction_time:.1f}s ({total_pages_count / extraction_time:.1f} pages/sec)")
        print(f"{'='*70}\n")

        return JSONResponse(response_data)

    except HTTPException:
        raise
    except Exception as e:
        print(f"\nβœ— Error: {str(e)}")
        import traceback
        traceback.print_exc()
        raise HTTPException(status_code=500, detail=str(e))
    finally:
        if doc:
            doc.close()
        remove_file(temp_path)
        if pdf_path != temp_path:
            remove_file(pdf_path)
        gc.collect()


@app.post("/cleanup-batch/{batch_id}")
async def cleanup_batch(
    batch_id: str,
    background_tasks: BackgroundTasks,
    container_name: Optional[str] = Form(None)
):
    """Delete all blobs for a specific batch (entire POD/{batch_id}/ folder)."""
    if container_name is None:
        container_name = AZURE_CONTAINER_NAME

    background_tasks.add_task(cleanup_old_blobs, batch_id, container_name)

    return JSONResponse({
        "success": True,
        "message": f"Cleanup started for batch {batch_id}",
        "batch_id": batch_id,
        "folder_path": f"{ROOT_FOLDER}/{batch_id}/",
        "container": container_name
    })


    # ============================================================
# GENERATE DOWNLOAD URL FROM BLOB PATH (PERMANENT ACCESS)
# ============================================================


@app.get("/blob/download-url")
def generate_download_url(
    blob_name: str = Query(..., description="Blob path like POD/BATCH.../file.pdf"),
    container: str = Query(AZURE_CONTAINER_NAME),
    expiry_minutes: int = Query(60)
):
    """
    Returns fresh SAS download URL.
    This prevents expired links problem.
    """

    try:
        sas_token = generate_blob_sas(
            account_name=AZURE_STORAGE_ACCOUNT_NAME,
            container_name=container,
            blob_name=blob_name,
            account_key=AZURE_STORAGE_ACCOUNT_KEY,
            permission=BlobSasPermissions(read=True),
            expiry=datetime.utcnow() + timedelta(minutes=expiry_minutes)
        )

        download_url = f"https://{AZURE_STORAGE_ACCOUNT_NAME}.blob.core.windows.net/{container}/{blob_name}?{sas_token}"

        return {
            "success": True,
            "download_url": download_url,
            "expires_in_minutes": expiry_minutes
        }

    except Exception as e:
        return {"success": False, "error": str(e)}