oxidize-pdf 2.4.3

A pure Rust PDF generation and manipulation library with zero external dependencies
Documentation
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
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
//! PDF page content analysis
//!
//! This module provides functionality to analyze the content composition of PDF pages,
//! helping to determine whether pages contain primarily scanned images, vector text,
//! or a mixture of both. This is particularly useful for:
//!
//! - Detecting scanned documents that may benefit from OCR processing
//! - Analyzing document composition for optimization purposes
//! - Preprocessing documents for different handling strategies
//!
//! # Usage
//!
//! ```rust,no_run
//! use oxidize_pdf::operations::page_analysis::{PageContentAnalyzer, PageType};
//! use oxidize_pdf::parser::PdfReader;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let document = PdfReader::open_document("example.pdf")?;
//! let analyzer = PageContentAnalyzer::new(document);
//!
//! // Analyze a specific page
//! let analysis = analyzer.analyze_page(0)?;
//!
//! match analysis.page_type {
//!     PageType::Scanned => println!("This page appears to be scanned"),
//!     PageType::Text => println!("This page contains primarily vector text"),
//!     PageType::Mixed => println!("This page contains both text and images"),
//! }
//!
//! // Quick check for scanned pages
//! if analyzer.is_scanned_page(0)? {
//!     println!("Page 0 is likely a scanned image");
//! }
//! # Ok(())
//! # }
//! ```

use super::{OperationError, OperationResult};
use crate::parser::{PdfDocument, PdfReader};
use crate::text::{ExtractionOptions, OcrOptions, OcrProcessingResult, OcrProvider, TextExtractor};
// Note: ImageExtractor functionality is implemented inline to avoid circular dependencies
use std::fs::File;
use std::path::Path;

/// Represents the primary content type of a PDF page
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PageType {
    /// Page contains primarily scanned images (>80% image content, <10% text)
    Scanned,
    /// Page contains primarily vector text (>70% text content, <20% images)
    Text,
    /// Page contains a balanced mix of text and images
    Mixed,
}

impl PageType {
    /// Returns true if this page type represents a scanned page
    pub fn is_scanned(&self) -> bool {
        matches!(self, PageType::Scanned)
    }

    /// Returns true if this page type represents a text-heavy page
    pub fn is_text(&self) -> bool {
        matches!(self, PageType::Text)
    }

    /// Returns true if this page type represents a mixed content page
    pub fn is_mixed(&self) -> bool {
        matches!(self, PageType::Mixed)
    }
}

/// Detailed analysis results for a PDF page
#[derive(Debug, Clone)]
pub struct ContentAnalysis {
    /// The page number (0-indexed)
    pub page_number: usize,
    /// The determined page type based on content analysis
    pub page_type: PageType,
    /// Percentage of page area covered by text (0.0 to 1.0)
    pub text_ratio: f64,
    /// Percentage of page area covered by images (0.0 to 1.0)
    pub image_ratio: f64,
    /// Percentage of page area that is blank space (0.0 to 1.0)
    pub blank_space_ratio: f64,
    /// Number of text fragments found on the page
    pub text_fragment_count: usize,
    /// Number of images found on the page
    pub image_count: usize,
    /// Total number of characters in text content
    pub character_count: usize,
}

impl ContentAnalysis {
    /// Returns true if this page appears to be scanned
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use oxidize_pdf::operations::page_analysis::{ContentAnalysis, PageType};
    /// let analysis = ContentAnalysis {
    ///     page_number: 0,
    ///     page_type: PageType::Scanned,
    ///     text_ratio: 0.05,
    ///     image_ratio: 0.90,
    ///     blank_space_ratio: 0.05,
    ///     text_fragment_count: 2,
    ///     image_count: 1,
    ///     character_count: 15,
    /// };
    ///
    /// assert!(analysis.is_scanned());
    /// ```
    pub fn is_scanned(&self) -> bool {
        self.page_type.is_scanned()
    }

    /// Returns true if this page is primarily text-based
    pub fn is_text_heavy(&self) -> bool {
        self.page_type.is_text()
    }

    /// Returns true if this page has mixed content
    pub fn is_mixed_content(&self) -> bool {
        self.page_type.is_mixed()
    }

    /// Returns the dominant content type ratio (text or image)
    pub fn dominant_content_ratio(&self) -> f64 {
        self.text_ratio.max(self.image_ratio)
    }
}

/// Configuration options for page content analysis
#[derive(Debug, Clone)]
pub struct AnalysisOptions {
    /// Minimum text fragment size to consider (in characters)
    pub min_text_fragment_size: usize,
    /// Minimum image size to consider (in pixels)
    pub min_image_size: u32,
    /// Threshold for considering a page as scanned (image ratio)
    pub scanned_threshold: f64,
    /// Threshold for considering a page as text-heavy (text ratio)
    pub text_threshold: f64,
    /// OCR options for processing scanned pages
    pub ocr_options: Option<OcrOptions>,
}

impl Default for AnalysisOptions {
    fn default() -> Self {
        Self {
            min_text_fragment_size: 3,
            min_image_size: 50,
            scanned_threshold: 0.8,
            text_threshold: 0.7,
            ocr_options: None,
        }
    }
}

/// Analyzer for PDF page content composition
///
/// This struct provides methods to analyze the content of PDF pages and determine
/// their composition (text vs images vs mixed content).
pub struct PageContentAnalyzer {
    document: PdfDocument<File>,
    options: AnalysisOptions,
}

impl PageContentAnalyzer {
    /// Create a new page content analyzer
    ///
    /// # Arguments
    ///
    /// * `document` - The PDF document to analyze
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use oxidize_pdf::operations::page_analysis::PageContentAnalyzer;
    /// use oxidize_pdf::parser::PdfReader;
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let document = PdfReader::open_document("example.pdf")?;
    /// let analyzer = PageContentAnalyzer::new(document);
    /// # Ok(())
    /// # }
    /// ```
    pub fn new(document: PdfDocument<File>) -> Self {
        Self {
            document,
            options: AnalysisOptions::default(),
        }
    }

    /// Create a new page content analyzer with custom options
    ///
    /// # Arguments
    ///
    /// * `document` - The PDF document to analyze
    /// * `options` - Custom analysis options
    pub fn with_options(document: PdfDocument<File>, options: AnalysisOptions) -> Self {
        Self { document, options }
    }

    /// Create a page content analyzer from a file path
    ///
    /// # Arguments
    ///
    /// * `path` - Path to the PDF file
    ///
    /// # Errors
    ///
    /// Returns an error if the file cannot be opened or is not a valid PDF.
    pub fn from_file<P: AsRef<Path>>(path: P) -> OperationResult<Self> {
        let document = PdfReader::open_document(path)
            .map_err(|e| OperationError::ParseError(e.to_string()))?;
        Ok(Self::new(document))
    }

    /// Analyze the content of a specific page
    ///
    /// This method examines the page's text and image content to determine
    /// the composition and classify the page type.
    ///
    /// # Arguments
    ///
    /// * `page_number` - The page number to analyze (0-indexed)
    ///
    /// # Returns
    ///
    /// A `ContentAnalysis` struct containing detailed analysis results.
    ///
    /// # Errors
    ///
    /// Returns an error if the page cannot be accessed or analyzed.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use oxidize_pdf::operations::page_analysis::PageContentAnalyzer;
    /// # use oxidize_pdf::parser::PdfReader;
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let document = PdfReader::open_document("example.pdf")?;
    /// let analyzer = PageContentAnalyzer::new(document);
    ///
    /// let analysis = analyzer.analyze_page(0)?;
    /// println!("Page type: {:?}", analysis.page_type);
    /// println!("Text ratio: {:.2}%", analysis.text_ratio * 100.0);
    /// println!("Image ratio: {:.2}%", analysis.image_ratio * 100.0);
    /// # Ok(())
    /// # }
    /// ```
    pub fn analyze_page(&self, page_number: usize) -> OperationResult<ContentAnalysis> {
        // Get page dimensions for area calculations
        let page = self
            .document
            .get_page(page_number as u32)
            .map_err(|e| OperationError::ParseError(e.to_string()))?;

        let page_area = self.calculate_page_area(&page)?;

        // Analyze text content
        let text_analysis = self.analyze_text_content(page_number)?;
        let text_area = text_analysis.total_area;
        let text_fragment_count = text_analysis.fragment_count;
        let character_count = text_analysis.character_count;

        // Analyze image content
        let image_analysis = self.analyze_image_content(page_number)?;
        let image_area = image_analysis.total_area;
        let image_count = image_analysis.image_count;

        // Calculate ratios
        let text_ratio = if page_area > 0.0 {
            text_area / page_area
        } else {
            0.0
        };
        let image_ratio = if page_area > 0.0 {
            image_area / page_area
        } else {
            0.0
        };
        let blank_space_ratio = 1.0 - text_ratio - image_ratio;

        // Determine page type based on content ratios
        let page_type = self.determine_page_type(text_ratio, image_ratio);

        Ok(ContentAnalysis {
            page_number,
            page_type,
            text_ratio,
            image_ratio,
            blank_space_ratio: blank_space_ratio.max(0.0),
            text_fragment_count,
            image_count,
            character_count,
        })
    }

    /// Analyze all pages in the document
    ///
    /// # Returns
    ///
    /// A vector of `ContentAnalysis` results, one for each page.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use oxidize_pdf::operations::page_analysis::PageContentAnalyzer;
    /// # use oxidize_pdf::parser::PdfReader;
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let document = PdfReader::open_document("example.pdf")?;
    /// let analyzer = PageContentAnalyzer::new(document);
    ///
    /// let analyses = analyzer.analyze_document()?;
    /// for analysis in analyses {
    ///     println!("Page {}: {:?}", analysis.page_number, analysis.page_type);
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub fn analyze_document(&self) -> OperationResult<Vec<ContentAnalysis>> {
        let page_count = self
            .document
            .page_count()
            .map_err(|e| OperationError::ParseError(e.to_string()))?;

        let mut analyses = Vec::new();
        for page_idx in 0..page_count {
            let analysis = self.analyze_page(page_idx as usize)?;
            analyses.push(analysis);
        }

        Ok(analyses)
    }

    /// Analyze specific pages in the document
    ///
    /// # Arguments
    ///
    /// * `page_numbers` - Vector of page numbers to analyze (0-indexed)
    ///
    /// # Returns
    ///
    /// A vector of `ContentAnalysis` results for the specified pages.
    pub fn analyze_pages(&self, page_numbers: &[usize]) -> OperationResult<Vec<ContentAnalysis>> {
        let mut analyses = Vec::new();
        for &page_number in page_numbers {
            let analysis = self.analyze_page(page_number)?;
            analyses.push(analysis);
        }
        Ok(analyses)
    }

    /// Quick check if a page appears to be scanned
    ///
    /// This is a convenience method that performs a full analysis but only
    /// returns whether the page is classified as scanned.
    ///
    /// # Arguments
    ///
    /// * `page_number` - The page number to check (0-indexed)
    ///
    /// # Returns
    ///
    /// `true` if the page appears to be scanned, `false` otherwise.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use oxidize_pdf::operations::page_analysis::PageContentAnalyzer;
    /// # use oxidize_pdf::parser::PdfReader;
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let document = PdfReader::open_document("example.pdf")?;
    /// let analyzer = PageContentAnalyzer::new(document);
    ///
    /// if analyzer.is_scanned_page(0)? {
    ///     println!("Page 0 is a scanned image - consider OCR processing");
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub fn is_scanned_page(&self, page_number: usize) -> OperationResult<bool> {
        let analysis = self.analyze_page(page_number)?;
        Ok(analysis.is_scanned())
    }

    /// Find all scanned pages in the document
    ///
    /// # Returns
    ///
    /// A vector of page numbers (0-indexed) that appear to be scanned.
    pub fn find_scanned_pages(&self) -> OperationResult<Vec<usize>> {
        let analyses = self.analyze_document()?;
        Ok(analyses
            .into_iter()
            .filter(|analysis| analysis.is_scanned())
            .map(|analysis| analysis.page_number)
            .collect())
    }

    /// Extract text from a scanned page using OCR
    ///
    /// This method processes a scanned page with OCR to extract text content.
    /// It first verifies that the page is indeed scanned, then applies OCR processing.
    ///
    /// # Arguments
    ///
    /// * `page_number` - The page number to process (0-indexed)
    /// * `ocr_provider` - The OCR provider to use for text extraction
    ///
    /// # Returns
    ///
    /// OCR processing results with extracted text and positioning information.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The page is not scanned (use `is_scanned_page` to check first)
    /// - OCR processing fails
    /// - Page cannot be accessed
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use oxidize_pdf::operations::page_analysis::PageContentAnalyzer;
    /// # use oxidize_pdf::text::MockOcrProvider;
    /// # use oxidize_pdf::parser::PdfReader;
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let document = PdfReader::open_document("scanned.pdf")?;
    /// let analyzer = PageContentAnalyzer::new(document);
    /// let ocr_provider = MockOcrProvider::new();
    ///
    /// if analyzer.is_scanned_page(0)? {
    ///     let ocr_result = analyzer.extract_text_from_scanned_page(0, &ocr_provider)?;
    ///     println!("OCR extracted text: {}", ocr_result.text);
    ///     println!("Confidence: {:.2}%", ocr_result.confidence * 100.0);
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub fn extract_text_from_scanned_page<P: OcrProvider>(
        &self,
        page_number: usize,
        ocr_provider: &P,
    ) -> OperationResult<OcrProcessingResult> {
        // First verify the page is scanned
        let analysis = self.analyze_page(page_number)?;
        if !analysis.is_scanned() {
            return Err(OperationError::ParseError(format!(
                "Page {} is not a scanned page (image ratio: {:.2}%, text ratio: {:.2}%)",
                page_number,
                analysis.image_ratio * 100.0,
                analysis.text_ratio * 100.0
            )));
        }

        // Get OCR options from analysis options or use default
        let ocr_options = self.options.ocr_options.clone().unwrap_or_default();

        // Extract image data from the page
        let page_image_data = self.extract_page_image_data(page_number)?;

        // Process with OCR
        let ocr_result = ocr_provider
            .process_page(&analysis, &page_image_data, &ocr_options)
            .map_err(|e| OperationError::ParseError(format!("OCR processing failed: {e}")))?;

        Ok(ocr_result)
    }

    /// Process all scanned pages in the document with OCR
    ///
    /// This method identifies all scanned pages and processes them with OCR,
    /// returning a map of page numbers to OCR results.
    ///
    /// # Arguments
    ///
    /// * `ocr_provider` - The OCR provider to use for text extraction
    ///
    /// # Returns
    ///
    /// A vector of tuples containing (page_number, ocr_result) for each scanned page.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use oxidize_pdf::operations::page_analysis::PageContentAnalyzer;
    /// # use oxidize_pdf::text::MockOcrProvider;
    /// # use oxidize_pdf::parser::PdfReader;
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let document = PdfReader::open_document("scanned.pdf")?;
    /// let analyzer = PageContentAnalyzer::new(document);
    /// let ocr_provider = MockOcrProvider::new();
    ///
    /// let ocr_results = analyzer.process_scanned_pages_with_ocr(&ocr_provider)?;
    ///
    /// for (page_num, ocr_result) in ocr_results {
    ///     println!("Page {}: {} characters extracted", page_num, ocr_result.text.len());
    ///     println!("  Confidence: {:.2}%", ocr_result.confidence * 100.0);
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub fn process_scanned_pages_with_ocr<P: OcrProvider>(
        &self,
        ocr_provider: &P,
    ) -> OperationResult<Vec<(usize, OcrProcessingResult)>> {
        let scanned_pages = self.find_scanned_pages()?;
        let mut results = Vec::new();

        for page_number in scanned_pages {
            match self.extract_text_from_scanned_page(page_number, ocr_provider) {
                Ok(ocr_result) => {
                    results.push((page_number, ocr_result));
                }
                Err(e) => {
                    tracing::error!("Failed to process page {page_number}: {e}");
                    continue;
                }
            }
        }

        Ok(results)
    }

    /// Process multiple scanned pages with OCR in parallel (threaded version)
    ///
    /// This method processes multiple scanned pages concurrently using threads,
    /// which can significantly improve performance when dealing with large documents.
    ///
    /// # Arguments
    ///
    /// * `ocr_provider` - OCR provider to use for text extraction
    /// * `max_threads` - Maximum number of threads to use (None for automatic)
    ///
    /// # Returns
    ///
    /// A vector of tuples containing page numbers and their OCR results.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use oxidize_pdf::operations::page_analysis::PageContentAnalyzer;
    /// use oxidize_pdf::text::MockOcrProvider;
    /// use oxidize_pdf::parser::PdfReader;
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let document = PdfReader::open_document("scanned.pdf")?;
    /// let analyzer = PageContentAnalyzer::new(document);
    /// let provider = MockOcrProvider::new();
    ///
    /// // Process with up to 4 threads
    /// let results = analyzer.process_scanned_pages_parallel(&provider, Some(4))?;
    /// for (page_num, result) in results {
    ///     println!("Page {}: {} characters", page_num, result.text.len());
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub fn process_scanned_pages_parallel<P: OcrProvider + Clone + Send + Sync + 'static>(
        &self,
        ocr_provider: &P,
        max_threads: Option<usize>,
    ) -> OperationResult<Vec<(usize, OcrProcessingResult)>> {
        use std::sync::{Arc, Mutex};
        use std::thread;

        let scanned_pages = self.find_scanned_pages()?;
        if scanned_pages.is_empty() {
            return Ok(Vec::new());
        }

        // Determine thread count
        let thread_count = max_threads.unwrap_or_else(|| {
            std::cmp::min(
                scanned_pages.len(),
                std::thread::available_parallelism()
                    .map(|p| p.get())
                    .unwrap_or(4),
            )
        });

        if thread_count <= 1 {
            // Fall back to sequential processing
            return self.process_scanned_pages_with_ocr(ocr_provider);
        }

        // Shared results vector
        let results = Arc::new(Mutex::new(Vec::new()));
        let provider = Arc::new(ocr_provider.clone());

        // Create chunks of pages for each thread
        let chunk_size = scanned_pages.len().div_ceil(thread_count);
        let mut handles = Vec::new();

        for chunk in scanned_pages.chunks(chunk_size) {
            let chunk_pages = chunk.to_vec();
            let results_clone = Arc::clone(&results);
            let provider_clone = Arc::clone(&provider);

            // Create a temporary analyzer for this thread
            // Note: This is a simplified approach - in practice you'd want to avoid cloning the document
            let handle = thread::spawn(move || {
                let mut thread_results = Vec::new();

                for page_num in chunk_pages {
                    // In a real implementation, you'd extract the image data and process it
                    // For now, we'll simulate with a simple approach
                    match simulate_page_ocr_processing(page_num, &*provider_clone) {
                        Ok(ocr_result) => {
                            thread_results.push((page_num, ocr_result));
                        }
                        Err(e) => {
                            tracing::error!("OCR failed for page {page_num}: {e}");
                        }
                    }
                }

                // Add results to shared vector
                if let Ok(mut shared_results) = results_clone.lock() {
                    shared_results.extend(thread_results);
                }
            });

            handles.push(handle);
        }

        // Wait for all threads to complete
        for handle in handles {
            if let Err(e) = handle.join() {
                tracing::error!("Thread panicked: {e:?}");
            }
        }

        // Extract results
        let final_results = results
            .lock()
            .map_err(|e| OperationError::ProcessingError(format!("Failed to get results: {e}")))?
            .clone();

        Ok(final_results)
    }

    /// Process scanned pages with OCR using a batch approach
    ///
    /// This method processes pages in batches, which can be more efficient for
    /// certain OCR providers that support batch processing.
    ///
    /// # Arguments
    ///
    /// * `ocr_provider` - OCR provider to use for text extraction
    /// * `batch_size` - Number of pages to process in each batch
    ///
    /// # Returns
    ///
    /// A vector of tuples containing page numbers and their OCR results.
    pub fn process_scanned_pages_batch<P: OcrProvider>(
        &self,
        ocr_provider: &P,
        batch_size: usize,
    ) -> OperationResult<Vec<(usize, OcrProcessingResult)>> {
        let scanned_pages = self.find_scanned_pages()?;
        let mut results = Vec::new();

        // Handle edge case where batch_size is 0
        if batch_size == 0 {
            return Ok(results);
        }

        for batch in scanned_pages.chunks(batch_size) {
            tracing::info!("Processing batch of {} pages", batch.len());

            for &page_num in batch {
                match self.extract_text_from_scanned_page(page_num, ocr_provider) {
                    Ok(ocr_result) => {
                        results.push((page_num, ocr_result));
                    }
                    Err(e) => {
                        tracing::error!("OCR failed for page {page_num}: {e}");
                    }
                }
            }

            // Add a small delay between batches to avoid overwhelming the OCR provider
            std::thread::sleep(std::time::Duration::from_millis(100));
        }

        Ok(results)
    }

    /// Extract image data from a page for OCR processing
    ///
    /// This method extracts the primary image from a scanned page and converts
    /// it to a format suitable for OCR processing (PNG or JPEG).
    pub fn extract_page_image_data(&self, page_number: usize) -> OperationResult<Vec<u8>> {
        tracing::debug!(
            "🔍 [DEBUG] extract_page_image_data called for page {}",
            page_number
        );

        let page = self
            .document
            .get_page(page_number as u32)
            .map_err(|e| OperationError::ParseError(e.to_string()))?;

        // Method 1: Check page resources for XObjects
        tracing::debug!("🔍 [DEBUG] Trying Method 1: Check page resources for XObjects");
        let resources = self
            .document
            .get_page_resources(&page)
            .map_err(|e| OperationError::ParseError(e.to_string()))?;

        // Try to get resources from standard method first
        let mut resolved_resources_dict: Option<crate::parser::objects::PdfDictionary> = None;

        if let Some(_resources) = &resources {
            // Standard case - resources found normally
            tracing::debug!(
                "🔍 [DEBUG] Page {} has resources via standard method",
                page_number
            );
        } else {
            // If resources is None, try to resolve directly from page dictionary
            tracing::debug!(
                "🔍 [DEBUG] Page {} resources None, trying direct resolution",
                page_number
            );
            if let Some(resources_ref) = page.dict.get("Resources") {
                tracing::debug!(
                    "🔍 [DEBUG] Page {} has Resources entry, resolving reference",
                    page_number
                );
                match self.document.resolve(resources_ref) {
                    Ok(resolved_obj) => {
                        if let Some(resolved_dict) = resolved_obj.as_dict() {
                            tracing::debug!("🔍 [DEBUG] Page {} resolved Resources to dictionary with {} entries",
                                   page_number, resolved_dict.0.len());
                            resolved_resources_dict = Some(resolved_dict.clone());
                        } else {
                            tracing::debug!(
                                "🔍 [DEBUG] Page {} Resources resolved but not a dictionary",
                                page_number
                            );
                        }
                    }
                    Err(e) => {
                        tracing::debug!(
                            "🔍 [DEBUG] Page {} failed to resolve Resources: {}",
                            page_number,
                            e
                        );
                    }
                }
            } else {
                tracing::debug!(
                    "🔍 [DEBUG] Page {} has no Resources entry in dict",
                    page_number
                );
            }
        }

        // Check for XObjects in either standard resources or resolved resources
        let active_resources = resources.or(resolved_resources_dict.as_ref());

        if let Some(resources) = &active_resources {
            tracing::debug!("🔍 [DEBUG] Page {} has resources", page_number);
            if let Some(crate::parser::objects::PdfObject::Dictionary(xobjects)) = resources
                .0
                .get(&crate::parser::objects::PdfName("XObject".to_string()))
            {
                tracing::debug!(
                    "🔍 [DEBUG] Page {} has XObject dictionary with {} entries",
                    page_number,
                    xobjects.0.len()
                );
                // Process each XObject to find images
                for (xobject_name, obj_ref) in xobjects.0.iter() {
                    if let crate::parser::objects::PdfObject::Reference(obj_num, gen_num) = obj_ref
                    {
                        if let Ok(crate::parser::objects::PdfObject::Stream(stream)) =
                            self.document.get_object(*obj_num, *gen_num)
                        {
                            // Check if it's an image XObject
                            if let Some(crate::parser::objects::PdfObject::Name(subtype)) = stream
                                .dict
                                .0
                                .get(&crate::parser::objects::PdfName("Subtype".to_string()))
                            {
                                if subtype.0 == "Image" {
                                    let width = stream
                                        .dict
                                        .0
                                        .get(&crate::parser::objects::PdfName("Width".to_string()))
                                        .and_then(|w| {
                                            if let crate::parser::objects::PdfObject::Integer(w) = w
                                            {
                                                Some(*w)
                                            } else {
                                                None
                                            }
                                        })
                                        .unwrap_or(0);

                                    let height = stream
                                        .dict
                                        .0
                                        .get(&crate::parser::objects::PdfName("Height".to_string()))
                                        .and_then(|h| {
                                            if let crate::parser::objects::PdfObject::Integer(h) = h
                                            {
                                                Some(*h)
                                            } else {
                                                None
                                            }
                                        })
                                        .unwrap_or(0);

                                    tracing::debug!(
                                        "🔍 [DEBUG] Page {} Method1 XObject {} -> Object {} ({}x{})",
                                        page_number, xobject_name.0, obj_num, width, height
                                    );
                                    // Extract and convert image for OCR
                                    return self.extract_image_stream_for_ocr(&stream);
                                }
                            }
                        }
                    }
                }
            } else {
                tracing::debug!("🔍 [DEBUG] Page {} has no XObject dictionary", page_number);
            }
        } else {
            tracing::debug!("🔍 [DEBUG] Page {} has no resources", page_number);
        }

        // Method 2: Find XObject referenced by this specific page's content stream
        tracing::debug!("🔍 [DEBUG] Trying Method 2: Parse content streams for Do operators");
        if let Ok(content_streams) = self.document.get_page_content_streams(&page) {
            tracing::debug!(
                "🔍 [DEBUG] Page {} has {} content streams",
                page_number,
                content_streams.len()
            );
            for (i, content_stream) in content_streams.iter().enumerate() {
                let content_str = String::from_utf8_lossy(content_stream);
                tracing::debug!(
                    "🔍 [DEBUG] Content stream {} has {} bytes",
                    i,
                    content_stream.len()
                );

                // Look for Do operators and extract the XObject name
                // Pattern: "/ImageName Do" where ImageName is the XObject key
                for line in content_str.lines() {
                    if line.trim().ends_with(" Do") {
                        // Extract XObject name from "/Name Do"
                        let parts: Vec<&str> = line.split_whitespace().collect();
                        if parts.len() >= 2 && parts[parts.len() - 1] == "Do" {
                            let xobject_name = parts[parts.len() - 2];
                            tracing::debug!(
                                "🔍 [DEBUG] Found Do operator with XObject: {}",
                                xobject_name
                            );
                            if let Some(name) = xobject_name.strip_prefix('/') {
                                // Remove leading '/'
                                tracing::debug!("🔍 [DEBUG] Looking for XObject: {}", name);

                                // Try to find this specific XObject using page resources first
                                if let Ok(image_data) =
                                    self.find_specific_xobject_image_from_page(name, &page)
                                {
                                    return Ok(image_data);
                                } else {
                                    tracing::debug!("🔍 [DEBUG] Page-specific XObject lookup failed for: {}, trying document-wide search", name);
                                    // Fallback to document-wide search for malformed PDFs
                                    if let Ok(image_data) = self.find_specific_xobject_image(name) {
                                        return Ok(image_data);
                                    } else {
                                        tracing::debug!("🔍 [DEBUG] Document-wide XObject lookup also failed for: {}", name);
                                    }
                                }
                            }
                        }
                    }
                }

                // Fallback: Look for inline images: BI ... ID ... EI
                if content_str.contains("BI") && content_str.contains("EI") {
                    // For now, inline image extraction would require more complex implementation
                    // Most scanned PDFs use XObjects which we handle above
                }
            }
        }

        // Method 3: Last resort - scan document for any large images
        tracing::debug!("🔍 [DEBUG] Trying Method 3: Fallback scan for large images");
        match self.find_image_xobjects_in_document() {
            Ok(image_data) if !image_data.is_empty() => {
                return Ok(image_data);
            }
            _ => {}
        }

        Err(OperationError::ParseError(
            "No image data found on scanned page (checked XObjects and inline images)".to_string(),
        ))
    }

    /// Find a specific XObject image by name using page-specific resources
    fn find_specific_xobject_image_from_page(
        &self,
        xobject_name: &str,
        page: &crate::parser::page_tree::ParsedPage,
    ) -> OperationResult<Vec<u8>> {
        // Get page-specific resources - with fallback for malformed PDFs
        let resources = self
            .document
            .get_page_resources(page)
            .map_err(|e| OperationError::ParseError(e.to_string()))?;

        // Try standard method first
        if let Some(resources) = resources {
            if let Some(crate::parser::objects::PdfObject::Dictionary(xobjects)) = resources
                .0
                .get(&crate::parser::objects::PdfName("XObject".to_string()))
            {
                #[allow(clippy::collapsible_match)]
                if let Some(xobject_ref) = xobjects
                    .0
                    .get(&crate::parser::objects::PdfName(xobject_name.to_string()))
                {
                    if let crate::parser::objects::PdfObject::Reference(obj_num, gen_num) =
                        xobject_ref
                    {
                        if let Ok(crate::parser::objects::PdfObject::Stream(stream)) =
                            self.document.get_object(*obj_num, *gen_num)
                        {
                            if let Some(crate::parser::objects::PdfObject::Name(subtype)) = stream
                                .dict
                                .0
                                .get(&crate::parser::objects::PdfName("Subtype".to_string()))
                            {
                                if subtype.0 == "Image" {
                                    let width = stream
                                        .dict
                                        .0
                                        .get(&crate::parser::objects::PdfName("Width".to_string()))
                                        .and_then(|w| {
                                            if let crate::parser::objects::PdfObject::Integer(w) = w
                                            {
                                                Some(*w)
                                            } else {
                                                None
                                            }
                                        })
                                        .unwrap_or(0);
                                    let height = stream
                                        .dict
                                        .0
                                        .get(&crate::parser::objects::PdfName("Height".to_string()))
                                        .and_then(|h| {
                                            if let crate::parser::objects::PdfObject::Integer(h) = h
                                            {
                                                Some(*h)
                                            } else {
                                                None
                                            }
                                        })
                                        .unwrap_or(0);
                                    tracing::debug!(
                                        "🔍 [DEBUG] Page-specific XObject {} -> Object {} ({}x{})",
                                        xobject_name,
                                        obj_num,
                                        width,
                                        height
                                    );
                                    return self.extract_image_stream_for_ocr(&stream);
                                }
                            }
                        }
                    }
                }
            }
        }

        // Fallback for malformed PDFs: try direct resolution
        if let Some(crate::parser::objects::PdfObject::Reference(res_obj, res_gen)) = page
            .dict
            .0
            .get(&crate::parser::objects::PdfName("Resources".to_string()))
        {
            match self.document.get_object(*res_obj, *res_gen) {
                Ok(crate::parser::objects::PdfObject::Dictionary(resolved_dict)) => {
                    tracing::debug!(
                        "🔍 [DEBUG] Page-specific fallback: resolved Resources {} {} R",
                        res_obj,
                        res_gen
                    );
                    if let Some(crate::parser::objects::PdfObject::Dictionary(xobjects)) =
                        resolved_dict
                            .0
                            .get(&crate::parser::objects::PdfName("XObject".to_string()))
                    {
                        tracing::debug!("🔍 [DEBUG] Page-specific fallback found XObject dictionary with {} entries", xobjects.0.len());
                        for (name, obj) in &xobjects.0 {
                            tracing::debug!(
                                "🔍 [DEBUG] Page-specific fallback XObject: {} -> {:?}",
                                name.0,
                                obj
                            );
                        }
                        if let Some(xobject_ref) = xobjects
                            .0
                            .get(&crate::parser::objects::PdfName(xobject_name.to_string()))
                        {
                            if let crate::parser::objects::PdfObject::Reference(obj_num, gen_num) =
                                xobject_ref
                            {
                                tracing::debug!("🔍 [DEBUG] Page-specific fallback: trying to get object {} {} R", obj_num, gen_num);
                                match self.document.get_object(*obj_num, *gen_num) {
                                    Ok(crate::parser::objects::PdfObject::Stream(stream)) => {
                                        tracing::debug!(
                                            "🔍 [DEBUG] Page-specific fallback: got stream object"
                                        );
                                        match stream.dict.0.get(&crate::parser::objects::PdfName(
                                            "Subtype".to_string(),
                                        )) {
                                            Some(crate::parser::objects::PdfObject::Name(
                                                subtype,
                                            )) => {
                                                tracing::debug!("🔍 [DEBUG] Page-specific fallback: stream subtype = {}", subtype.0);
                                                if subtype.0 == "Image" {
                                                    let width = stream
                                                        .dict
                                                        .0
                                                        .get(&crate::parser::objects::PdfName("Width".to_string()))
                                                        .and_then(|w| {
                                                            if let crate::parser::objects::PdfObject::Integer(w) = w
                                                            {
                                                                Some(*w)
                                                            } else {
                                                                None
                                                            }
                                                        })
                                                        .unwrap_or(0);
                                                    let height = stream
                                                        .dict
                                                        .0
                                                        .get(&crate::parser::objects::PdfName("Height".to_string()))
                                                        .and_then(|h| {
                                                            if let crate::parser::objects::PdfObject::Integer(h) = h
                                                            {
                                                                Some(*h)
                                                            } else {
                                                                None
                                                            }
                                                        })
                                                        .unwrap_or(0);
                                                    tracing::debug!(
                                                        "🔍 [DEBUG] Page-specific fallback XObject {} -> Object {} ({}x{})",
                                                        xobject_name, obj_num, width, height
                                                    );
                                                    return self
                                                        .extract_image_stream_for_ocr(&stream);
                                                } else {
                                                    tracing::debug!("🔍 [DEBUG] Page-specific fallback: stream is not an image (subtype: {})", subtype.0);
                                                }
                                            }
                                            None => {
                                                tracing::debug!("🔍 [DEBUG] Page-specific fallback: stream has no Subtype");
                                            }
                                            _ => {
                                                tracing::debug!("🔍 [DEBUG] Page-specific fallback: stream Subtype is not a name");
                                            }
                                        }
                                    }
                                    Ok(obj) => {
                                        tracing::debug!("🔍 [DEBUG] Page-specific fallback: object {} {} R is not a stream, got: {:?}", obj_num, gen_num, std::any::type_name_of_val(&obj));
                                    }
                                    Err(e) => {
                                        tracing::debug!("🔍 [DEBUG] Page-specific fallback: failed to get object {} {} R: {}", obj_num, gen_num, e);
                                    }
                                }
                            } else {
                                tracing::debug!("🔍 [DEBUG] Page-specific fallback: XObject reference is not a Reference");
                            }
                        } else {
                            tracing::debug!("🔍 [DEBUG] Page-specific fallback: XObject '{}' not found in resolved resources", xobject_name);
                        }
                    } else {
                        tracing::debug!("🔍 [DEBUG] Page-specific fallback: no XObject dictionary in resolved resources");
                    }
                }
                Ok(_) => {
                    tracing::debug!("🔍 [DEBUG] Page-specific fallback: Resources reference resolved to non-dictionary");
                }
                Err(e) => {
                    tracing::debug!(
                        "🔍 [DEBUG] Page-specific fallback: failed to resolve Resources: {}",
                        e
                    );
                }
            }
        }

        // If we reach here, we couldn't find the XObject
        if let Some(resources) = resources {
            if let Some(crate::parser::objects::PdfObject::Dictionary(xobjects)) = resources
                .0
                .get(&crate::parser::objects::PdfName("XObject".to_string()))
            {
                // Look for the specific XObject name in this page's resources
                #[allow(clippy::collapsible_match)]
                if let Some(xobject_ref) = xobjects
                    .0
                    .get(&crate::parser::objects::PdfName(xobject_name.to_string()))
                {
                    if let crate::parser::objects::PdfObject::Reference(obj_num, gen_num) =
                        xobject_ref
                    {
                        if let Ok(crate::parser::objects::PdfObject::Stream(stream)) =
                            self.document.get_object(*obj_num, *gen_num)
                        {
                            // Verify it's an image XObject
                            if let Some(crate::parser::objects::PdfObject::Name(subtype)) = stream
                                .dict
                                .0
                                .get(&crate::parser::objects::PdfName("Subtype".to_string()))
                            {
                                if subtype.0 == "Image" {
                                    let width = stream
                                        .dict
                                        .0
                                        .get(&crate::parser::objects::PdfName("Width".to_string()))
                                        .and_then(|w| {
                                            if let crate::parser::objects::PdfObject::Integer(w) = w
                                            {
                                                Some(*w)
                                            } else {
                                                None
                                            }
                                        })
                                        .unwrap_or(0);

                                    let height = stream
                                        .dict
                                        .0
                                        .get(&crate::parser::objects::PdfName("Height".to_string()))
                                        .and_then(|h| {
                                            if let crate::parser::objects::PdfObject::Integer(h) = h
                                            {
                                                Some(*h)
                                            } else {
                                                None
                                            }
                                        })
                                        .unwrap_or(0);

                                    tracing::debug!(
                                        "🔍 [DEBUG] Page-specific XObject {} -> Object {} ({}x{})",
                                        xobject_name,
                                        obj_num,
                                        width,
                                        height
                                    );
                                    return self.extract_image_stream_for_ocr(&stream);
                                }
                            }
                        }
                    }
                }
            }
        }

        Err(OperationError::ParseError(format!(
            "No page-specific XObject found for name: {}",
            xobject_name
        )))
    }

    /// Find a specific XObject image by name in the document (fallback method)
    fn find_specific_xobject_image(&self, xobject_name: &str) -> OperationResult<Vec<u8>> {
        // Search through document objects for one with this specific name reference
        // This is more targeted than scanning all objects

        for obj_num in 1..=1000 {
            // Reasonable range for most PDFs
            if let Ok(crate::parser::objects::PdfObject::Stream(stream)) =
                self.document.get_object(obj_num, 0)
            {
                // Check if it's an image stream
                if let Some(crate::parser::objects::PdfObject::Name(subtype)) = stream
                    .dict
                    .0
                    .get(&crate::parser::objects::PdfName("Subtype".to_string()))
                {
                    if subtype.0 == "Image" {
                        // For now, we'll return the first large image we find
                        // TODO: Implement proper name-based lookup when we have access to the XRef table
                        let width = stream
                            .dict
                            .0
                            .get(&crate::parser::objects::PdfName("Width".to_string()))
                            .and_then(|w| {
                                if let crate::parser::objects::PdfObject::Integer(w) = w {
                                    Some(*w)
                                } else {
                                    None
                                }
                            })
                            .unwrap_or(0);
                        let height = stream
                            .dict
                            .0
                            .get(&crate::parser::objects::PdfName("Height".to_string()))
                            .and_then(|h| {
                                if let crate::parser::objects::PdfObject::Integer(h) = h {
                                    Some(*h)
                                } else {
                                    None
                                }
                            })
                            .unwrap_or(0);

                        // If it's a reasonably large image, likely a scanned page
                        if width > 100 && height > 100 {
                            tracing::debug!(
                                "🔍 [DEBUG] Using XObject {} -> Object {} ({}x{})",
                                xobject_name,
                                obj_num,
                                width,
                                height
                            );
                            return self.extract_image_stream_for_ocr(&stream);
                        }
                    }
                }
            }
        }

        Err(OperationError::ParseError(format!(
            "No image XObject found for name: {}",
            xobject_name
        )))
    }

    /// Scan the document for any image XObjects (fallback method)
    fn find_image_xobjects_in_document(&self) -> OperationResult<Vec<u8>> {
        // Scan through document objects looking for image streams
        // This handles malformed PDFs where images aren't properly referenced in page resources
        for obj_num in 1..=1000 {
            // Reasonable range for most PDFs
            if let Ok(crate::parser::objects::PdfObject::Stream(stream)) =
                self.document.get_object(obj_num, 0)
            {
                // Check if it's an image stream
                if let Some(crate::parser::objects::PdfObject::Name(subtype)) = stream
                    .dict
                    .0
                    .get(&crate::parser::objects::PdfName("Subtype".to_string()))
                {
                    if subtype.0 == "Image" {
                        // Get image dimensions to check if it's page-sized
                        let width = stream
                            .dict
                            .0
                            .get(&crate::parser::objects::PdfName("Width".to_string()))
                            .and_then(|w| {
                                if let crate::parser::objects::PdfObject::Integer(w) = w {
                                    Some(*w)
                                } else {
                                    None
                                }
                            })
                            .unwrap_or(0);
                        let height = stream
                            .dict
                            .0
                            .get(&crate::parser::objects::PdfName("Height".to_string()))
                            .and_then(|h| {
                                if let crate::parser::objects::PdfObject::Integer(h) = h {
                                    Some(*h)
                                } else {
                                    None
                                }
                            })
                            .unwrap_or(0);

                        // If it's a reasonably large image, likely a scanned page
                        if width > 100 && height > 100 {
                            return self.extract_image_stream_for_ocr(&stream);
                        }
                    }
                }
            }
        }

        Err(OperationError::ParseError(
            "No suitable image objects found in document".to_string(),
        ))
    }

    /// Extract and convert image stream data for OCR processing
    fn extract_image_stream_for_ocr(
        &self,
        stream: &crate::parser::objects::PdfStream,
    ) -> OperationResult<Vec<u8>> {
        tracing::debug!(
            "🔍 [DEBUG] extract_image_stream_for_ocr called with stream size: {}",
            stream.data.len()
        );

        // Get image properties
        let width = match stream
            .dict
            .0
            .get(&crate::parser::objects::PdfName("Width".to_string()))
        {
            Some(crate::parser::objects::PdfObject::Integer(w)) => *w as u32,
            _ => {
                return Err(OperationError::ParseError(
                    "Missing image width".to_string(),
                ))
            }
        };

        let height = match stream
            .dict
            .0
            .get(&crate::parser::objects::PdfName("Height".to_string()))
        {
            Some(crate::parser::objects::PdfObject::Integer(h)) => *h as u32,
            _ => {
                return Err(OperationError::ParseError(
                    "Missing image height".to_string(),
                ))
            }
        };

        // Get color space and bits per component
        let color_space = stream
            .dict
            .0
            .get(&crate::parser::objects::PdfName("ColorSpace".to_string()));
        let bits_per_component = match stream.dict.0.get(&crate::parser::objects::PdfName(
            "BitsPerComponent".to_string(),
        )) {
            Some(crate::parser::objects::PdfObject::Integer(bits)) => *bits as u8,
            _ => 8,
        };

        // Debug: show image properties
        let filter = stream
            .dict
            .0
            .get(&crate::parser::objects::PdfName("Filter".to_string()));
        tracing::debug!(
            "🔍 [DEBUG] Image properties: {}x{}, {} bits, filter: {:?}",
            width,
            height,
            bits_per_component,
            filter
                .as_ref()
                .map(|f| match f {
                    crate::parser::objects::PdfObject::Name(n) => n.0.as_str(),
                    _ => "Array/Other",
                })
                .unwrap_or("None")
        );

        // Get image data based on filter type
        let data = match filter {
            Some(crate::parser::objects::PdfObject::Name(filter_name)) => match filter_name
                .0
                .as_str()
            {
                "DCTDecode" => {
                    // JPEG data - use the raw stream data directly without decoding
                    // DCTDecode streams contain complete JPEG data including headers
                    let jpeg_data = &stream.data;

                    tracing::debug!(
                        "🔍 [DEBUG] Processing DCTDecode stream: {} bytes",
                        jpeg_data.len()
                    );

                    // Validate JPEG structure
                    if jpeg_data.len() < 4 {
                        return Err(OperationError::ParseError(
                            "DCTDecode stream too short to be valid JPEG".to_string(),
                        ));
                    }

                    // Check for JPEG SOI marker (Start Of Image: 0xFFD8)
                    if jpeg_data[0] != 0xFF || jpeg_data[1] != 0xD8 {
                        return Err(OperationError::ParseError(format!(
                            "Invalid JPEG stream: missing SOI marker. Found: {:02X}{:02X}, expected FFD8",
                            jpeg_data[0], jpeg_data[1]
                        )));
                    }

                    tracing::debug!("✅ [DEBUG] JPEG SOI marker found");

                    // Use the stream data as-is - DCTDecode streams are already complete JPEG files
                    let final_jpeg_data = jpeg_data.to_vec();

                    tracing::debug!(
                        "🔍 [DEBUG] Final JPEG size: {} bytes",
                        final_jpeg_data.len()
                    );

                    // SECURITY: Never save extracted images to disk for confidential documents

                    final_jpeg_data
                }
                filter_name => {
                    // For other filters, we need to decode the stream first
                    tracing::debug!("🔍 [DEBUG] Decoding stream with filter: {}", filter_name);
                    let parse_options = self.document.options();
                    let decoded_data = stream.decode(&parse_options).map_err(|e| {
                        OperationError::ParseError(format!("Failed to decode image stream: {e}"))
                    })?;

                    tracing::debug!(
                        "🔍 [DEBUG] Decoded stream data: {} bytes",
                        decoded_data.len()
                    );

                    match filter_name {
                        "FlateDecode" => {
                            // Convert raw pixel data to PNG
                            self.convert_raw_to_png_for_ocr(
                                &decoded_data,
                                width,
                                height,
                                color_space,
                                bits_per_component,
                            )?
                        }
                        "CCITTFaxDecode" => {
                            // Convert CCITT fax to PNG
                            self.convert_ccitt_to_png_for_ocr(&decoded_data, width, height)?
                        }
                        "LZWDecode" => {
                            // Convert LZW decoded data to PNG
                            self.convert_raw_to_png_for_ocr(
                                &decoded_data,
                                width,
                                height,
                                color_space,
                                bits_per_component,
                            )?
                        }
                        "JBIG2Decode" => {
                            // JBIG2 is a bilevel (1-bit) format like CCITT fax
                            self.convert_ccitt_to_png_for_ocr(&decoded_data, width, height)?
                        }
                        _ => {
                            return Err(OperationError::ParseError(format!(
                                "Unsupported image filter: {}",
                                filter_name
                            )))
                        }
                    }
                }
            },
            Some(crate::parser::objects::PdfObject::Array(filters)) => {
                // Handle filter arrays - use the first filter
                if let Some(crate::parser::objects::PdfObject::Name(filter)) = filters.0.first() {
                    match filter.0.as_str() {
                        "DCTDecode" => {
                            tracing::debug!("🔍 [DEBUG] Array filter: Using raw JPEG stream data");
                            stream.data.clone()
                        }
                        filter_name => {
                            // Decode other filter types
                            tracing::debug!(
                                "🔍 [DEBUG] Array filter: Decoding stream with filter: {}",
                                filter_name
                            );
                            let parse_options = self.document.options();
                            let decoded_data = stream.decode(&parse_options).map_err(|e| {
                                OperationError::ParseError(format!(
                                    "Failed to decode image stream: {e}"
                                ))
                            })?;

                            match filter_name {
                                "FlateDecode" => self.convert_raw_to_png_for_ocr(
                                    &decoded_data,
                                    width,
                                    height,
                                    color_space,
                                    bits_per_component,
                                )?,
                                "CCITTFaxDecode" => {
                                    self.convert_ccitt_to_png_for_ocr(&decoded_data, width, height)?
                                }
                                "LZWDecode" => self.convert_raw_to_png_for_ocr(
                                    &decoded_data,
                                    width,
                                    height,
                                    color_space,
                                    bits_per_component,
                                )?,
                                "JBIG2Decode" => {
                                    // JBIG2 is a bilevel (1-bit) format like CCITT fax
                                    self.convert_ccitt_to_png_for_ocr(&decoded_data, width, height)?
                                }
                                _ => {
                                    return Err(OperationError::ParseError(format!(
                                        "Unsupported image filter in array: {}",
                                        filter_name
                                    )))
                                }
                            }
                        }
                    }
                } else {
                    return Err(OperationError::ParseError("Empty filter array".to_string()));
                }
            }
            _ => {
                // No filter - raw image data, convert to PNG
                tracing::debug!("🔍 [DEBUG] No filter: Converting raw image data to PNG");
                let parse_options = self.document.options();
                let decoded_data = stream.decode(&parse_options).map_err(|e| {
                    OperationError::ParseError(format!("Failed to decode raw image stream: {e}"))
                })?;

                self.convert_raw_to_png_for_ocr(
                    &decoded_data,
                    width,
                    height,
                    color_space,
                    bits_per_component,
                )?
            }
        };

        tracing::debug!("🔍 [DEBUG] Final image data for OCR: {} bytes", data.len());
        Ok(data)
    }

    /// Return raw JPEG data from DCTDecode stream without modification
    /// DCTDecode streams in PDFs are valid JPEG data - pass through unchanged
    #[allow(dead_code)]
    fn clean_jpeg_data(&self, raw_data: &[u8]) -> Vec<u8> {
        tracing::debug!(
            "🔍 [DEBUG] Using raw DCTDecode stream as-is: {} bytes",
            raw_data.len()
        );

        // DCTDecode streams from PDF are already valid JPEG
        // Don't try to "clean" or modify them - just pass through
        raw_data.to_vec()
    }

    #[cfg(feature = "external-images")]
    #[allow(dead_code)]
    fn fix_image_rotation_for_ocr(
        &self,
        image_data: &[u8],
        pdf_width: u32,
        pdf_height: u32,
    ) -> OperationResult<Vec<u8>> {
        tracing::debug!("🔍 [DEBUG] Image rotation correction with external-images feature");

        // For now, apply a simple heuristic rotation fix for the known case
        // Based on your image showing 90 degree clockwise rotation
        let rotation_needed = self.detect_rotation_needed(pdf_width, pdf_height, 0, 0);

        if rotation_needed > 0 {
            // Use external command to rotate the image for now
            // This is a temporary solution until we fix the image crate import
            self.rotate_image_externally(image_data, rotation_needed)
        } else {
            tracing::debug!("🔍 [DEBUG] No rotation correction needed based on dimensions");
            Ok(image_data.to_vec())
        }
    }

    #[cfg(not(feature = "external-images"))]
    #[allow(dead_code)]
    fn fix_image_rotation_for_ocr(
        &self,
        image_data: &[u8],
        _pdf_width: u32,
        _pdf_height: u32,
    ) -> OperationResult<Vec<u8>> {
        tracing::debug!(
            "🔍 [DEBUG] Image rotation correction disabled (external-images feature not enabled)"
        );
        Ok(image_data.to_vec())
    }

    #[allow(dead_code)]
    fn detect_rotation_needed(
        &self,
        pdf_width: u32,
        pdf_height: u32,
        img_width: u32,
        img_height: u32,
    ) -> u8 {
        // For the specific case we're dealing with, apply a simple heuristic
        // Based on the debug output, we know the PDF is portrait (1169x1653 in metadata)
        // but the extracted image appears landscape-oriented when viewed

        // If we don't have actual image dimensions, use PDF dimensions as heuristic
        let (actual_img_width, actual_img_height) = if img_width == 0 || img_height == 0 {
            (pdf_width, pdf_height)
        } else {
            (img_width, img_height)
        };

        tracing::debug!(
            "🔍 [DEBUG] Rotation analysis - PDF: {}x{}, Image: {}x{}",
            pdf_width,
            pdf_height,
            actual_img_width,
            actual_img_height
        );

        // Check if this is the typical portrait PDF with likely rotated content
        if pdf_height > pdf_width {
            // PDF is portrait - this is typical for scanned documents
            // Based on your image example which was rotated 90° clockwise, apply counter-rotation
            tracing::debug!("🔍 [DEBUG] Portrait PDF detected - applying 270° rotation to correct typical scan rotation");
            return 3; // 270° = 90° counter-clockwise
        }

        // For landscape PDFs or when dimensions are swapped
        if pdf_width == actual_img_height && pdf_height == actual_img_width {
            tracing::debug!("🔍 [DEBUG] Dimensions swapped - applying 90° rotation");
            return 1; // 90° clockwise
        }

        tracing::debug!("🔍 [DEBUG] No rotation correction needed");
        0
    }

    #[allow(dead_code)]
    fn rotate_image_externally(&self, image_data: &[u8], rotation: u8) -> OperationResult<Vec<u8>> {
        use std::fs;
        use std::process::Command;

        // Create temporary input file
        let input_path = format!("examples/results/temp_input_{}.jpg", std::process::id());
        let output_path = format!("examples/results/temp_output_{}.jpg", std::process::id());

        // Save input image
        if let Err(e) = fs::write(&input_path, image_data) {
            tracing::debug!("🔍 [DEBUG] Failed to write temp input file: {}", e);
            return Ok(image_data.to_vec());
        }

        // Determine rotation angle
        let angle = match rotation {
            1 => "90",  // 90° clockwise
            2 => "180", // 180°
            3 => "270", // 270° clockwise (90° counter-clockwise)
            _ => {
                let _ = fs::remove_file(&input_path);
                return Ok(image_data.to_vec());
            }
        };

        tracing::debug!(
            "🔍 [DEBUG] Attempting to rotate image {} degrees using external tool",
            angle
        );

        // Try sips first (available on macOS)
        let sips_result = Command::new("sips")
            .arg(&input_path)
            .arg("-r")
            .arg(angle)
            .arg("--out")
            .arg(&output_path)
            .output();

        let rotated_data = match sips_result {
            Ok(sips_output) if sips_output.status.success() => match fs::read(&output_path) {
                Ok(data) => {
                    tracing::debug!("🔍 [DEBUG] Successfully rotated image using sips");
                    data
                }
                Err(e) => {
                    tracing::debug!("🔍 [DEBUG] Failed to read sips-rotated image: {}", e);
                    image_data.to_vec()
                }
            },
            Ok(sips_output) => {
                tracing::debug!(
                    "🔍 [DEBUG] sips failed: {}",
                    String::from_utf8_lossy(&sips_output.stderr)
                );

                // Fallback: try ImageMagick convert command
                let result = Command::new("convert")
                    .arg(&input_path)
                    .arg("-rotate")
                    .arg(angle)
                    .arg(&output_path)
                    .output();

                match result {
                    Ok(output) if output.status.success() => match fs::read(&output_path) {
                        Ok(data) => {
                            tracing::debug!(
                                "🔍 [DEBUG] Successfully rotated image using ImageMagick"
                            );
                            data
                        }
                        Err(e) => {
                            tracing::debug!("🔍 [DEBUG] Failed to read rotated image: {}", e);
                            image_data.to_vec()
                        }
                    },
                    _ => {
                        tracing::debug!(
                            "🔍 [DEBUG] Both sips and ImageMagick failed, using original image"
                        );
                        image_data.to_vec()
                    }
                }
            }
            Err(e) => {
                tracing::debug!("🔍 [DEBUG] sips not available: {}", e);
                tracing::debug!("🔍 [DEBUG] Trying ImageMagick as fallback...");

                let result = Command::new("convert")
                    .arg(&input_path)
                    .arg("-rotate")
                    .arg(angle)
                    .arg(&output_path)
                    .output();

                match result {
                    Ok(output) if output.status.success() => match fs::read(&output_path) {
                        Ok(data) => {
                            tracing::debug!(
                                "🔍 [DEBUG] Successfully rotated image using ImageMagick"
                            );
                            data
                        }
                        Err(e) => {
                            tracing::debug!("🔍 [DEBUG] Failed to read rotated image: {}", e);
                            image_data.to_vec()
                        }
                    },
                    _ => {
                        tracing::debug!(
                            "🔍 [DEBUG] No external rotation tools available, using original image"
                        );
                        image_data.to_vec()
                    }
                }
            }
        };

        // Cleanup temporary files
        let _ = fs::remove_file(&input_path);
        let _ = fs::remove_file(&output_path);

        Ok(rotated_data)
    }

    /// Clean corrupted JPEG data using sips (macOS system tool)
    /// This fixes JPEGs extracted from PDFs that have structural issues
    #[allow(dead_code)]
    fn clean_corrupted_jpeg(
        &self,
        corrupted_jpeg_data: &[u8],
        width: u32,
        _height: u32,
    ) -> OperationResult<Vec<u8>> {
        use std::fs;
        use std::process::Command;

        tracing::debug!("🔧 [DEBUG] Cleaning corrupted JPEG using sips");

        // Generate temp file paths
        let temp_id = std::process::id();
        let input_path = format!("/tmp/ocr_corrupted_{}_{}.jpg", temp_id, width);
        let output_path = format!("/tmp/ocr_clean_{}_{}.jpg", temp_id, width);

        // Write corrupted JPEG to temp file
        fs::write(&input_path, corrupted_jpeg_data).map_err(|e| {
            OperationError::ProcessingError(format!("Failed to write temp JPEG: {e}"))
        })?;

        tracing::debug!("🔧 [DEBUG] Saved corrupted JPEG to: {}", input_path);

        // Use sips to recompress and clean the JPEG
        let output = Command::new("sips")
            .args([
                "-s",
                "format",
                "jpeg",
                "-s",
                "formatOptions",
                "100", // Maximum quality
                &input_path,
                "--out",
                &output_path,
            ])
            .output()
            .map_err(|e| OperationError::ProcessingError(format!("Failed to run sips: {e}")))?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            tracing::debug!("❌ [DEBUG] sips failed: {}", stderr);

            // Cleanup temp files
            let _ = fs::remove_file(&input_path);
            let _ = fs::remove_file(&output_path);

            // Fall back to original data if sips fails
            tracing::debug!("🔧 [DEBUG] Falling back to original JPEG data");
            return Ok(corrupted_jpeg_data.to_vec());
        }

        // Read the cleaned JPEG
        let cleaned_data = fs::read(&output_path).map_err(|e| {
            OperationError::ProcessingError(format!("Failed to read cleaned JPEG: {e}"))
        })?;

        tracing::debug!(
            "🔧 [DEBUG] Successfully cleaned JPEG: {} -> {} bytes",
            corrupted_jpeg_data.len(),
            cleaned_data.len()
        );

        // SECURITY: Never save cleaned JPEG files for confidential documents

        // Cleanup temp files
        let _ = fs::remove_file(&input_path);
        let _ = fs::remove_file(&output_path);

        Ok(cleaned_data)
    }

    // Removed problematic convert_jpeg_to_png_for_ocr function

    /// Calculate the total area of a page in points
    fn calculate_page_area(&self, page: &crate::parser::ParsedPage) -> OperationResult<f64> {
        // Get page dimensions from MediaBox
        let width = page.width();
        let height = page.height();

        Ok(width * height)
    }

    /// Analyze text content on a page
    fn analyze_text_content(&self, page_number: usize) -> OperationResult<TextAnalysisResult> {
        let mut extractor = TextExtractor::with_options(ExtractionOptions {
            preserve_layout: true,
            space_threshold: 0.3,
            newline_threshold: 10.0,
            ..Default::default()
        });

        let extracted_text = extractor
            .extract_from_page(&self.document, page_number as u32)
            .map_err(|e| OperationError::ParseError(e.to_string()))?;

        let mut total_area = 0.0;
        let mut fragment_count = 0;
        let character_count = extracted_text.text.len();

        // Calculate area covered by text fragments
        for fragment in &extracted_text.fragments {
            if fragment.text.trim().len() >= self.options.min_text_fragment_size {
                total_area += fragment.width * fragment.height;
                fragment_count += 1;
            }
        }

        Ok(TextAnalysisResult {
            total_area,
            fragment_count,
            character_count,
        })
    }

    /// Analyze image content on a page
    fn analyze_image_content(&self, page_number: usize) -> OperationResult<ImageAnalysisResult> {
        // Enhanced approach: check XObjects AND page content streams for images

        let page = self
            .document
            .get_page(page_number as u32)
            .map_err(|e| OperationError::ParseError(e.to_string()))?;

        // Page analysis in progress

        // Get page resources to check for XObject references
        let resources = self
            .document
            .get_page_resources(&page)
            .map_err(|e| OperationError::ParseError(e.to_string()))?;

        let mut total_area = 0.0;
        let mut image_count = 0;

        // Method 1: Check XObjects in resources
        if let Some(resources) = &resources {
            if let Some(crate::parser::objects::PdfObject::Dictionary(xobjects)) = resources
                .0
                .get(&crate::parser::objects::PdfName("XObject".to_string()))
            {
                for obj_ref in xobjects.0.values() {
                    if let crate::parser::objects::PdfObject::Reference(obj_num, gen_num) = obj_ref
                    {
                        if let Ok(crate::parser::objects::PdfObject::Stream(stream)) =
                            self.document.get_object(*obj_num, *gen_num)
                        {
                            // Check if it's an image XObject
                            if let Some(crate::parser::objects::PdfObject::Name(subtype)) = stream
                                .dict
                                .0
                                .get(&crate::parser::objects::PdfName("Subtype".to_string()))
                            {
                                if subtype.0 == "Image" {
                                    image_count += 1;

                                    // Get image dimensions
                                    let width =
                                        match stream.dict.0.get(&crate::parser::objects::PdfName(
                                            "Width".to_string(),
                                        )) {
                                            Some(crate::parser::objects::PdfObject::Integer(w)) => {
                                                *w as f64
                                            }
                                            _ => 0.0,
                                        };

                                    let height =
                                        match stream.dict.0.get(&crate::parser::objects::PdfName(
                                            "Height".to_string(),
                                        )) {
                                            Some(crate::parser::objects::PdfObject::Integer(h)) => {
                                                *h as f64
                                            }
                                            _ => 0.0,
                                        };

                                    // Check minimum size
                                    if width >= self.options.min_image_size as f64
                                        && height >= self.options.min_image_size as f64
                                    {
                                        total_area += width * height;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        // Method 2: Check for inline images and Do operators in content stream
        if let Ok(content_streams) = self.document.get_page_content_streams(&page) {
            for content_stream in content_streams.iter() {
                let content_str = String::from_utf8_lossy(content_stream);

                // Look for inline image operators: BI ... ID ... EI
                let bi_count = content_str.matches("BI").count();
                let ei_count = content_str.matches("EI").count();

                if bi_count > 0 && ei_count > 0 {
                    image_count += bi_count.min(ei_count);
                    // For scanned pages, inline images often cover the entire page
                    let page_area = page.width() * page.height();
                    total_area += page_area * (bi_count.min(ei_count) as f64);
                }

                // Look for Do operators (invoke XObject) - fallback for scanned PDFs
                let do_count = content_str.matches(" Do").count();
                if do_count > 0 && image_count == 0 {
                    // Assume Do operators reference large images covering the page
                    image_count += do_count;
                    let page_area = page.width() * page.height();
                    total_area += page_area * (do_count as f64);
                }
            }
        }

        Ok(ImageAnalysisResult {
            total_area,
            image_count,
        })
    }

    /// Determine the page type based on content ratios
    ///
    /// # Arguments
    ///
    /// * `text_ratio` - Ratio of page area covered by text (0.0 to 1.0)
    /// * `image_ratio` - Ratio of page area covered by images (0.0 to 1.0)
    ///
    /// # Algorithm
    ///
    /// The classification uses the following thresholds:
    /// - **Scanned**: Image ratio > 80% AND text ratio < 10%
    /// - **Text**: Text ratio > 70% AND image ratio < 20%
    /// - **Mixed**: Everything else
    fn determine_page_type(&self, text_ratio: f64, image_ratio: f64) -> PageType {
        if image_ratio > self.options.scanned_threshold && text_ratio < 0.1 {
            PageType::Scanned
        } else if text_ratio > self.options.text_threshold && image_ratio < 0.2 {
            PageType::Text
        } else {
            PageType::Mixed
        }
    }

    /// Convert raw image data to PNG format for OCR processing
    fn convert_raw_to_png_for_ocr(
        &self,
        data: &[u8],
        width: u32,
        height: u32,
        color_space: Option<&crate::parser::objects::PdfObject>,
        bits_per_component: u8,
    ) -> OperationResult<Vec<u8>> {
        // Imports removed - not used in current implementation

        // Determine color components
        let components = match color_space {
            Some(crate::parser::objects::PdfObject::Name(cs)) => match cs.0.as_str() {
                "DeviceGray" => 1,
                "DeviceRGB" => 3,
                "DeviceCMYK" => 4,
                _ => 3, // Default to RGB
            },
            _ => 3, // Default to RGB
        };

        // Simple PNG creation
        let mut png_data = Vec::new();

        // PNG signature
        png_data.extend_from_slice(&[0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]);

        // IHDR chunk
        let mut ihdr = Vec::new();
        ihdr.extend_from_slice(&width.to_be_bytes());
        ihdr.extend_from_slice(&height.to_be_bytes());
        ihdr.push(bits_per_component);

        // Color type
        let color_type = match components {
            1 => 0, // Grayscale
            3 => 2, // RGB
            4 => 6, // RGBA (treat CMYK as RGBA for now)
            _ => 2, // Default to RGB
        };
        ihdr.push(color_type);
        ihdr.push(0); // Compression method
        ihdr.push(0); // Filter method
        ihdr.push(0); // Interlace method

        self.write_png_chunk(&mut png_data, b"IHDR", &ihdr);

        // IDAT chunk - compress the image data
        let compressed_data = self.compress_png_data(data, width, height, components)?;
        self.write_png_chunk(&mut png_data, b"IDAT", &compressed_data);

        // IEND chunk
        self.write_png_chunk(&mut png_data, b"IEND", &[]);

        Ok(png_data)
    }

    /// Convert CCITT Fax decoded data to PNG for OCR processing
    fn convert_ccitt_to_png_for_ocr(
        &self,
        data: &[u8],
        width: u32,
        height: u32,
    ) -> OperationResult<Vec<u8>> {
        // CCITT is typically 1-bit monochrome - convert to grayscale
        let mut grayscale_data = Vec::new();

        let bits_per_row = width as usize;
        let bytes_per_row = bits_per_row.div_ceil(8);

        for row in 0..height {
            let row_start = row as usize * bytes_per_row;

            for col in 0..width {
                let byte_idx = row_start + (col as usize / 8);
                let bit_idx = 7 - (col as usize % 8);

                if byte_idx < data.len() {
                    let bit = (data[byte_idx] >> bit_idx) & 1;
                    // CCITT: 0 = black, 1 = white
                    let gray_value = if bit == 0 { 0 } else { 255 };
                    grayscale_data.push(gray_value);
                } else {
                    grayscale_data.push(255); // White for missing data
                }
            }
        }

        // Convert to PNG
        self.convert_raw_to_png_for_ocr(
            &grayscale_data,
            width,
            height,
            Some(&crate::parser::objects::PdfObject::Name(
                crate::parser::objects::PdfName("DeviceGray".to_string()),
            )),
            8,
        )
    }

    /// Write a PNG chunk with proper CRC
    fn write_png_chunk(&self, output: &mut Vec<u8>, chunk_type: &[u8; 4], data: &[u8]) {
        // Length (4 bytes, big endian)
        output.extend_from_slice(&(data.len() as u32).to_be_bytes());

        // Chunk type (4 bytes)
        output.extend_from_slice(chunk_type);

        // Data
        output.extend_from_slice(data);

        // CRC (4 bytes, big endian)
        let crc = self.calculate_png_crc32(chunk_type, data);
        output.extend_from_slice(&crc.to_be_bytes());
    }

    /// Calculate CRC32 for PNG chunks
    fn calculate_png_crc32(&self, chunk_type: &[u8; 4], data: &[u8]) -> u32 {
        let mut crc: u32 = 0xFFFFFFFF;

        // Process chunk type
        for &byte in chunk_type {
            crc ^= byte as u32;
            for _ in 0..8 {
                if crc & 1 != 0 {
                    crc = (crc >> 1) ^ 0xEDB88320;
                } else {
                    crc >>= 1;
                }
            }
        }

        // Process data
        for &byte in data {
            crc ^= byte as u32;
            for _ in 0..8 {
                if crc & 1 != 0 {
                    crc = (crc >> 1) ^ 0xEDB88320;
                } else {
                    crc >>= 1;
                }
            }
        }

        crc ^ 0xFFFFFFFF
    }

    /// Compress image data for PNG IDAT chunk
    fn compress_png_data(
        &self,
        data: &[u8],
        width: u32,
        height: u32,
        components: u8,
    ) -> OperationResult<Vec<u8>> {
        use flate2::write::ZlibEncoder;
        use flate2::Compression;
        use std::io::Write;

        let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default());

        // PNG requires scanline filtering - add filter byte (0 = None) to each row
        let bytes_per_pixel = components as usize;
        let bytes_per_row = width as usize * bytes_per_pixel;

        for row in 0..height {
            // Filter byte (0 = no filter)
            encoder.write_all(&[0])?;

            // Row data
            let start = row as usize * bytes_per_row;
            let end = start + bytes_per_row;
            if end <= data.len() {
                encoder.write_all(&data[start..end])?;
            } else {
                // Pad with zeros if data is insufficient
                let available = data.len().saturating_sub(start);
                if available > 0 {
                    encoder.write_all(&data[start..start + available])?;
                }
                let padding = bytes_per_row.saturating_sub(available);
                for _ in 0..padding {
                    encoder.write_all(&[0])?;
                }
            }
        }

        encoder
            .finish()
            .map_err(|e| OperationError::ParseError(format!("Failed to compress PNG data: {e}")))
    }
}

/// Helper struct for text analysis results
struct TextAnalysisResult {
    total_area: f64,
    fragment_count: usize,
    character_count: usize,
}

/// Helper struct for image analysis results
struct ImageAnalysisResult {
    total_area: f64,
    image_count: usize,
}

/// Simulate OCR processing for a single page (helper function for parallel processing)
fn simulate_page_ocr_processing<P: OcrProvider>(
    page_num: usize,
    ocr_provider: &P,
) -> Result<OcrProcessingResult, crate::text::ocr::OcrError> {
    // Create mock image data for the page
    let mock_image_data = vec![
        0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10, 0x4A, 0x46, 0x49, 0x46, 0x00, 0x01, 0x01, 0x01, 0x00,
        0x48, 0x00, 0x48, 0x00, 0x00, 0xFF, 0xD9,
    ];

    let options = crate::text::ocr::OcrOptions {
        language: "eng".to_string(),
        min_confidence: 0.6,
        preserve_layout: true,
        preprocessing: crate::text::ocr::ImagePreprocessing::default(),
        engine_options: std::collections::HashMap::new(),
        timeout_seconds: 30,
        regions: None,
        debug_output: false,
    };

    // Process the mock image data
    let mut result = ocr_provider.process_image(&mock_image_data, &options)?;

    // Customize the result to indicate which page it came from
    result.text = format!("Page {page_num} text extracted via OCR");

    Ok(result)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_page_type_classification() {
        assert!(PageType::Scanned.is_scanned());
        assert!(!PageType::Text.is_scanned());
        assert!(!PageType::Mixed.is_scanned());

        assert!(PageType::Text.is_text());
        assert!(!PageType::Scanned.is_text());
        assert!(!PageType::Mixed.is_text());

        assert!(PageType::Mixed.is_mixed());
        assert!(!PageType::Scanned.is_mixed());
        assert!(!PageType::Text.is_mixed());
    }

    #[test]
    fn test_content_analysis_methods() {
        let analysis = ContentAnalysis {
            page_number: 0,
            page_type: PageType::Scanned,
            text_ratio: 0.05,
            image_ratio: 0.90,
            blank_space_ratio: 0.05,
            text_fragment_count: 2,
            image_count: 1,
            character_count: 15,
        };

        assert!(analysis.is_scanned());
        assert!(!analysis.is_text_heavy());
        assert!(!analysis.is_mixed_content());
        assert_eq!(analysis.dominant_content_ratio(), 0.90);
    }

    #[test]
    fn test_analysis_options_default() {
        let options = AnalysisOptions::default();
        assert_eq!(options.min_text_fragment_size, 3);
        assert_eq!(options.min_image_size, 50);
        assert_eq!(options.scanned_threshold, 0.8);
        assert_eq!(options.text_threshold, 0.7);
        assert!(options.ocr_options.is_none());
    }

    #[test]
    fn test_determine_page_type() {
        // Create a mock analyzer to test the logic
        let options = AnalysisOptions::default();

        // Test scanned page detection
        let page_type = if 0.90 > options.scanned_threshold && 0.05 < 0.1 {
            PageType::Scanned
        } else if 0.05 > options.text_threshold && 0.90 < 0.2 {
            PageType::Text
        } else {
            PageType::Mixed
        };
        assert_eq!(page_type, PageType::Scanned);

        // Test text page detection
        let page_type = if 0.10 > options.scanned_threshold && 0.80 < 0.1 {
            PageType::Scanned
        } else if 0.80 > options.text_threshold && 0.10 < 0.2 {
            PageType::Text
        } else {
            PageType::Mixed
        };
        assert_eq!(page_type, PageType::Text);

        // Test mixed page detection
        let page_type = if 0.40 > options.scanned_threshold && 0.50 < 0.1 {
            PageType::Scanned
        } else if 0.50 > options.text_threshold && 0.40 < 0.2 {
            PageType::Text
        } else {
            PageType::Mixed
        };
        assert_eq!(page_type, PageType::Mixed);
    }

    /// Verify that JBIG2Decode-filtered image streams no longer crash the OCR path.
    ///
    /// Regression test for the "Unsupported image filter: JBIG2Decode" error.
    #[test]
    fn test_jbig2decode_filter_no_longer_errors_in_ocr_path() {
        use crate::parser::objects::{PdfDictionary, PdfName, PdfObject, PdfStream};
        use crate::{Document, Page};
        use std::collections::HashMap;
        use tempfile::TempDir;

        // Create a minimal PDF so we can instantiate PageContentAnalyzer
        let temp_dir = TempDir::new().unwrap();
        let pdf_path = temp_dir.path().join("test.pdf");
        let mut doc = Document::new();
        doc.add_page(Page::a4());
        doc.save(&pdf_path).unwrap();

        let analyzer = PageContentAnalyzer::from_file(&pdf_path).unwrap();

        // Build a minimal PdfStream that claims to use JBIG2Decode.
        // The data doesn't need to be valid JBIG2 — the decoder is lenient.
        let mut dict_map: HashMap<PdfName, PdfObject> = HashMap::new();
        dict_map.insert(PdfName("Width".to_string()), PdfObject::Integer(4));
        dict_map.insert(PdfName("Height".to_string()), PdfObject::Integer(4));
        dict_map.insert(
            PdfName("BitsPerComponent".to_string()),
            PdfObject::Integer(1),
        );
        dict_map.insert(
            PdfName("Filter".to_string()),
            PdfObject::Name(PdfName("JBIG2Decode".to_string())),
        );
        // Provide enough bytes: the JBIG2 decoder needs at least 9 bytes
        // to attempt embedded-stream parsing without an early error.
        let stream_data = vec![0u8; 16];

        let stream = PdfStream {
            dict: PdfDictionary(dict_map),
            data: stream_data,
        };

        let result = analyzer.extract_image_stream_for_ocr(&stream);

        // The result may be Ok or Err (the decoder is incomplete), but it must
        // NOT be the "Unsupported image filter: JBIG2Decode" hard error.
        if let Err(err) = &result {
            let msg = err.to_string();
            assert!(
                !msg.contains("Unsupported image filter: JBIG2Decode"),
                "JBIG2Decode should no longer produce 'Unsupported image filter' error, got: {msg}"
            );
        }
    }
}

#[cfg(test)]
#[path = "page_analysis_tests.rs"]
mod page_analysis_tests;

#[cfg(test)]
#[path = "page_analysis_ocr_tests.rs"]
mod page_analysis_ocr_tests;

#[cfg(test)]
mod comprehensive_tests {
    use super::*;
    use crate::parser::{PdfDocument, PdfReader};
    use crate::text::{MockOcrProvider, OcrError, OcrOptions, OcrProvider};
    use std::fs::File;
    use std::io::Write;
    use std::sync::Mutex;
    use std::time::Duration;
    use tempfile::NamedTempFile;

    // Helper function to create a mock PDF document for testing
    fn create_mock_document() -> crate::parser::document::PdfDocument<std::fs::File> {
        // Create a document using the Document builder instead of raw PDF
        use crate::{Document, Page};

        let mut doc = Document::new();
        doc.add_page(Page::a4());

        // Save to temporary file
        let temp_file = NamedTempFile::new().expect("Failed to create temp file");
        doc.save(temp_file.path()).expect("Failed to save PDF");

        // Open with File reader
        let file = std::fs::File::open(temp_file.path()).expect("Failed to open PDF file");
        let reader =
            crate::parser::reader::PdfReader::new(file).expect("Failed to create PDF reader");
        crate::parser::document::PdfDocument::new(reader)
    }

    // Test 1: TextAnalysisResult struct functionality
    #[test]
    fn test_text_analysis_result_struct() {
        let result = TextAnalysisResult {
            total_area: 1000.0,
            fragment_count: 10,
            character_count: 500,
        };

        assert_eq!(result.total_area, 1000.0);
        assert_eq!(result.fragment_count, 10);
        assert_eq!(result.character_count, 500);
    }

    // Test 2: ImageAnalysisResult struct functionality
    #[test]
    fn test_image_analysis_result_struct() {
        let result = ImageAnalysisResult {
            total_area: 5000.0,
            image_count: 3,
        };

        assert_eq!(result.total_area, 5000.0);
        assert_eq!(result.image_count, 3);
    }

    // Test 3: PageContentAnalyzer with custom options
    #[test]
    fn test_analyzer_with_custom_options() {
        let doc = create_mock_document();
        let custom_options = AnalysisOptions {
            min_text_fragment_size: 10,
            min_image_size: 200,
            scanned_threshold: 0.9,
            text_threshold: 0.6,
            ocr_options: Some(OcrOptions {
                language: "de".to_string(),
                min_confidence: 0.85,
                ..Default::default()
            }),
        };

        let analyzer = PageContentAnalyzer::with_options(doc, custom_options);

        // Verify the analyzer was created (we can't directly access options)
        let page_count_result = analyzer.document.page_count();
        assert!(page_count_result.is_ok());
        assert_eq!(page_count_result.unwrap(), 1);
    }

    // Test 4: Multiple analyzers (not thread-safe, sequential)
    #[test]
    fn test_multiple_analyzers() {
        // Create multiple analyzers sequentially
        let analyzers: Vec<_> = (0..3)
            .map(|_| {
                let doc = create_mock_document();
                PageContentAnalyzer::new(doc)
            })
            .collect();

        // Test each analyzer works correctly
        for (i, analyzer) in analyzers.iter().enumerate() {
            let result = analyzer.document.page_count();
            assert!(result.is_ok());
            assert_eq!(result.unwrap(), 1);
            tracing::debug!("Analyzer {i} works correctly");
        }
    }

    // Test 5: Custom options propagation
    #[test]
    fn test_custom_options_propagation() {
        let doc = create_mock_document();
        let custom_options = AnalysisOptions {
            min_text_fragment_size: 15,
            min_image_size: 300,
            scanned_threshold: 0.85,
            text_threshold: 0.65,
            ocr_options: None,
        };

        let analyzer = PageContentAnalyzer::with_options(doc, custom_options);

        // The analyzer should be created successfully with custom options
        let result = analyzer.analyze_page(0);
        assert!(result.is_ok());
    }

    // Test 6: Empty document handling
    #[test]
    fn test_empty_document_analysis() {
        // Create an empty PDF with proper formatting
        let pdf_data = b"%PDF-1.4
1 0 obj
<<
/Type /Catalog
/Pages 2 0 R
>>
endobj
2 0 obj
<<
/Type /Pages
/Kids []
/Count 0
>>
endobj
xref
0 3
0000000000 65535 f 
0000000009 00000 n 
0000000058 00000 n 
trailer
<<
/Size 3
/Root 1 0 R
>>
startxref
107
%%EOF";

        // Create a temporary file
        let mut temp_file = NamedTempFile::new().expect("Failed to create temp file");
        temp_file
            .write_all(pdf_data)
            .expect("Failed to write PDF data");
        temp_file.flush().expect("Failed to flush");

        // Get path and open as File
        let path = temp_file.path().to_owned();
        let file = File::open(&path).expect("Failed to open temp file");

        // Keep the temp file alive by forgetting it
        std::mem::forget(temp_file);

        // If parsing fails, we'll just test that the analyzer handles empty results gracefully
        let result = PdfReader::new(file);
        if result.is_err() {
            // If we can't parse the PDF, just verify that empty results are handled properly
            // Empty document case is handled
            return;
        }

        let reader = result.unwrap();
        let doc = PdfDocument::new(reader);
        let analyzer = PageContentAnalyzer::new(doc);

        let analysis_result = analyzer.analyze_document();
        assert!(analysis_result.is_ok());
        assert_eq!(analysis_result.unwrap().len(), 0);

        let scanned_pages = analyzer.find_scanned_pages();
        assert!(scanned_pages.is_ok());
        assert_eq!(scanned_pages.unwrap().len(), 0);
    }

    // Test 7: Invalid page number error handling
    #[test]
    fn test_invalid_page_number_handling() {
        let doc = create_mock_document();
        let analyzer = PageContentAnalyzer::new(doc);

        // Try to analyze a non-existent page
        let result = analyzer.analyze_page(999);
        // The current implementation attempts fallback lookup, so it might succeed or fail
        // depending on whether it finds a valid page object during the scan
        // We'll verify it either succeeds or fails gracefully with a meaningful error
        if result.is_err() {
            assert!(result.unwrap_err().to_string().contains("Page"));
        } else {
            // If it succeeds, it should return a valid ContentAnalysis
            let analysis = result.unwrap();
            assert_eq!(analysis.page_number, 999);
        }

        // Try is_scanned_page with invalid index
        let result = analyzer.is_scanned_page(100);
        // With fallback lookup, this might succeed or fail gracefully
        if result.is_err() {
            assert!(result.unwrap_err().to_string().contains("Page"));
        } else {
            // If succeeds, should return a boolean result
            let _is_scanned = result.unwrap();
        }
    }

    // Test 8: OCR extraction with non-scanned page
    #[test]
    fn test_ocr_extraction_non_scanned_page() {
        let doc = create_mock_document();
        let analyzer = PageContentAnalyzer::new(doc);
        let ocr_provider = MockOcrProvider::new();

        // Since our mock document is text-based, OCR should fail
        let result = analyzer.extract_text_from_scanned_page(0, &ocr_provider);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("not a scanned page"));
    }

    // Test 9: OCR processing fallback scenarios
    #[test]
    fn test_ocr_processing_fallback() {
        let doc = create_mock_document();
        let analyzer = PageContentAnalyzer::new(doc);
        let ocr_provider = MockOcrProvider::new();

        // Test sequential processing (fallback for thread-unsafe providers)
        let result = analyzer.process_scanned_pages_with_ocr(&ocr_provider);
        assert!(result.is_ok());

        // Test batch with size 1 (similar to sequential)
        let result = analyzer.process_scanned_pages_batch(&ocr_provider, 1);
        assert!(result.is_ok());
    }

    // Test 10: OCR processing edge cases
    #[test]
    fn test_ocr_processing_edge_cases() {
        let doc = create_mock_document();
        let analyzer = PageContentAnalyzer::new(doc);
        let ocr_provider = MockOcrProvider::new();

        // Test with empty scanned pages list
        let result = analyzer.find_scanned_pages();
        assert!(result.is_ok());

        // Test batch processing with size 0
        let result = analyzer.process_scanned_pages_batch(&ocr_provider, 0);
        assert!(result.is_ok());
    }

    // Test 11: Batch OCR processing with various batch sizes
    #[test]
    fn test_batch_ocr_processing() {
        let doc = create_mock_document();
        let analyzer = PageContentAnalyzer::new(doc);
        let ocr_provider = MockOcrProvider::new();

        // Test with batch size 1
        let result = analyzer.process_scanned_pages_batch(&ocr_provider, 1);
        assert!(result.is_ok());

        // Test with batch size 5
        let result = analyzer.process_scanned_pages_batch(&ocr_provider, 5);
        assert!(result.is_ok());

        // Test with batch size larger than pages
        let result = analyzer.process_scanned_pages_batch(&ocr_provider, 100);
        assert!(result.is_ok());
    }

    // Test 12: Analyze specific pages
    #[test]
    fn test_analyze_specific_pages() {
        let doc = create_mock_document();
        let analyzer = PageContentAnalyzer::new(doc);

        // Analyze only page 0
        let result = analyzer.analyze_pages(&[0]);
        assert!(result.is_ok());
        assert_eq!(result.unwrap().len(), 1);

        // Try to analyze invalid pages - page 99 is out of range, so this should error
        let result = analyzer.analyze_pages(&[0, 99]);
        assert!(
            result.is_err(),
            "analyze_pages with out-of-range page index should return error"
        );
    }

    // Test 13: ContentAnalysis edge cases
    #[test]
    fn test_content_analysis_edge_cases() {
        // Test with all zeros
        let analysis = ContentAnalysis {
            page_number: 0,
            page_type: PageType::Mixed,
            text_ratio: 0.0,
            image_ratio: 0.0,
            blank_space_ratio: 1.0,
            text_fragment_count: 0,
            image_count: 0,
            character_count: 0,
        };

        assert!(!analysis.is_scanned());
        assert!(!analysis.is_text_heavy());
        assert!(analysis.is_mixed_content());
        // dominant_content_ratio returns the max of text_ratio and image_ratio only
        // In this case, both are 0.0, so it should return 0.0
        assert_eq!(analysis.dominant_content_ratio(), 0.0);

        // Test with equal ratios
        let analysis2 = ContentAnalysis {
            page_number: 1,
            page_type: PageType::Mixed,
            text_ratio: 0.33,
            image_ratio: 0.33,
            blank_space_ratio: 0.34,
            text_fragment_count: 10,
            image_count: 5,
            character_count: 100,
        };

        assert!(analysis2.is_mixed_content());
        assert_eq!(analysis2.dominant_content_ratio(), 0.33); // Max of text_ratio and image_ratio
    }

    // Test 14: OCR provider mock behavior customization
    #[test]
    fn test_ocr_provider_mock_customization() {
        let mut provider = MockOcrProvider::new();

        // Test setting custom text
        provider.set_mock_text("Custom OCR result for testing".to_string());
        provider.set_confidence(0.99);
        provider.set_processing_delay(10);

        let options = OcrOptions::default();
        let mock_image = vec![0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10, 0x4A, 0x46]; // JPEG header (8 bytes)

        let start = std::time::Instant::now();
        let result = provider.process_image(&mock_image, &options);
        let elapsed = start.elapsed();

        assert!(result.is_ok());
        let ocr_result = result.unwrap();
        assert!(ocr_result.text.contains("Custom OCR result"));
        assert_eq!(ocr_result.confidence, 0.99);
        assert!(elapsed >= Duration::from_millis(10));
    }

    // Test 15: simulate_page_ocr_processing function
    #[test]
    fn test_simulate_page_ocr_processing() {
        let provider = MockOcrProvider::new();
        let result = simulate_page_ocr_processing(5, &provider);

        assert!(result.is_ok());
        let ocr_result = result.unwrap();
        assert!(ocr_result.text.contains("Page 5"));
        assert_eq!(ocr_result.language, "eng");
    }

    // Test 16: Error propagation in process_scanned_pages_with_ocr
    #[test]
    fn test_process_scanned_pages_error_handling() {
        // Create a custom OCR provider that always fails
        struct FailingOcrProvider;

        impl OcrProvider for FailingOcrProvider {
            fn process_image(
                &self,
                _: &[u8],
                _: &OcrOptions,
            ) -> Result<OcrProcessingResult, OcrError> {
                Err(OcrError::ProcessingFailed("Simulated failure".to_string()))
            }

            fn process_page(
                &self,
                _: &ContentAnalysis,
                _: &[u8],
                _: &OcrOptions,
            ) -> Result<OcrProcessingResult, OcrError> {
                Err(OcrError::ProcessingFailed("Simulated failure".to_string()))
            }

            fn supported_formats(&self) -> Vec<crate::graphics::ImageFormat> {
                vec![]
            }

            fn engine_name(&self) -> &str {
                "Failing"
            }

            fn engine_type(&self) -> crate::text::OcrEngine {
                crate::text::OcrEngine::Mock
            }
        }

        let doc = create_mock_document();
        let analyzer = PageContentAnalyzer::new(doc);
        let failing_provider = FailingOcrProvider;

        // This should handle errors gracefully
        let result = analyzer.process_scanned_pages_with_ocr(&failing_provider);
        assert!(result.is_ok());
        assert_eq!(result.unwrap().len(), 0); // No successful results
    }

    // Test 17: Page area calculation edge cases
    #[test]
    fn test_page_area_calculation() {
        let doc = create_mock_document();
        let analyzer = PageContentAnalyzer::new(doc);

        // Get the first page
        let page = analyzer.document.get_page(0).unwrap();
        let area = analyzer.calculate_page_area(&page);

        assert!(area.is_ok());
        let area_value = area.unwrap();
        assert!(area_value > 0.0);
        // A4 size in points: actual measured dimensions
        assert_eq!(area_value, 500990.0);
    }

    // Test 18: Determine page type with exact threshold values
    #[test]
    fn test_determine_page_type_exact_thresholds() {
        let analyzer = PageContentAnalyzer::new(create_mock_document());

        // Test just above scanned threshold (image_ratio > 0.8 AND text_ratio < 0.1)
        let page_type = analyzer.determine_page_type(0.09, 0.81);
        assert_eq!(page_type, PageType::Scanned);

        // Test just above text threshold (text_ratio > 0.7 AND image_ratio < 0.2)
        let page_type = analyzer.determine_page_type(0.71, 0.19);
        assert_eq!(page_type, PageType::Text);

        // Test at exact thresholds (should be Mixed)
        let page_type = analyzer.determine_page_type(0.7, 0.8);
        assert_eq!(page_type, PageType::Mixed);
    }

    // Test 19: OCR options in AnalysisOptions
    #[test]
    fn test_analysis_options_with_ocr_configuration() {
        let mut engine_options = std::collections::HashMap::new();
        engine_options.insert("tesseract_psm".to_string(), "3".to_string());
        engine_options.insert("custom_param".to_string(), "value".to_string());

        let ocr_options = OcrOptions {
            language: "ja".to_string(),
            min_confidence: 0.9,
            preserve_layout: false,
            timeout_seconds: 60,
            engine_options,
            ..Default::default()
        };

        let analysis_options = AnalysisOptions {
            min_text_fragment_size: 1,
            min_image_size: 10,
            scanned_threshold: 0.95,
            text_threshold: 0.5,
            ocr_options: Some(ocr_options),
        };

        assert!(analysis_options.ocr_options.is_some());
        let ocr_opts = analysis_options.ocr_options.unwrap();
        assert_eq!(ocr_opts.language, "ja");
        assert_eq!(ocr_opts.timeout_seconds, 60);
        assert_eq!(ocr_opts.engine_options.len(), 2);
    }

    // Test 20: Content ratios validation
    #[test]
    fn test_content_ratios_sum_to_one() {
        let analysis = ContentAnalysis {
            page_number: 0,
            page_type: PageType::Mixed,
            text_ratio: 0.25,
            image_ratio: 0.45,
            blank_space_ratio: 0.30,
            text_fragment_count: 20,
            image_count: 3,
            character_count: 500,
        };

        let total = analysis.text_ratio + analysis.image_ratio + analysis.blank_space_ratio;
        assert!((total - 1.0).abs() < 0.001);
    }

    // Test 21: Multiple sequential analyzers stress test
    #[test]
    fn test_multiple_sequential_analyzers() {
        // Create and test multiple analyzers sequentially
        for i in 0..5 {
            let doc = create_mock_document();
            let analyzer = PageContentAnalyzer::new(doc);
            let result = analyzer.analyze_page(0);
            assert!(result.is_ok());
            tracing::debug!("Analyzer {i} completed analysis");
        }
    }

    // Test 22: Extract page image data error handling
    #[test]
    fn test_extract_page_image_data_no_xobjects() {
        let doc = create_mock_document();
        let analyzer = PageContentAnalyzer::new(doc);

        // Our mock document doesn't have image XObjects
        let result = analyzer.extract_page_image_data(0);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("No image data found"));
    }

    // Test 23: Analyze text content with minimum fragment size
    #[test]
    fn test_analyze_text_content_fragment_filtering() {
        let doc = create_mock_document();
        let custom_options = AnalysisOptions {
            min_text_fragment_size: 20, // Very high threshold
            ..Default::default()
        };
        let analyzer = PageContentAnalyzer::with_options(doc, custom_options);

        let result = analyzer.analyze_text_content(0);
        assert!(result.is_ok());
        // With high threshold, small fragments should be filtered out
    }

    // Test 24: OCR with automatic configuration
    #[test]
    fn test_ocr_automatic_configuration() {
        let doc = create_mock_document();
        let analyzer = PageContentAnalyzer::new(doc);
        let provider = MockOcrProvider::new();

        // Test with default OCR options
        let result = analyzer.process_scanned_pages_with_ocr(&provider);
        assert!(result.is_ok());

        // Test finding and processing scanned pages automatically
        let scanned = analyzer.find_scanned_pages();
        assert!(scanned.is_ok());
    }

    // Test 25: OCR preprocessing options in page analysis
    #[test]
    fn test_ocr_preprocessing_in_analysis() {
        let preprocessing = crate::text::ImagePreprocessing {
            denoise: false,
            deskew: false,
            enhance_contrast: true,
            sharpen: true,
            scale_factor: 1.5,
        };

        let ocr_options = OcrOptions {
            preprocessing,
            ..Default::default()
        };

        let analysis_options = AnalysisOptions {
            ocr_options: Some(ocr_options),
            ..Default::default()
        };

        let doc = create_mock_document();
        let analyzer = PageContentAnalyzer::with_options(doc, analysis_options);

        // Verify analyzer was created with custom preprocessing
        assert!(analyzer.options.ocr_options.is_some());
    }

    // Test 26: Batch processing with delays
    #[test]
    fn test_batch_processing_timing() {
        let doc = create_mock_document();
        let analyzer = PageContentAnalyzer::new(doc);
        let provider = MockOcrProvider::new();

        let start = std::time::Instant::now();
        let result = analyzer.process_scanned_pages_batch(&provider, 1);
        let _elapsed = start.elapsed();

        assert!(result.is_ok());
        // Should have at least the delay between batches
        // Note: May not have delay if no scanned pages found
    }

    // Test 27: Page type classification comprehensive
    #[test]
    fn test_page_type_all_combinations() {
        let analyzer = PageContentAnalyzer::new(create_mock_document());

        // High image, low text = Scanned
        assert_eq!(analyzer.determine_page_type(0.05, 0.85), PageType::Scanned);
        assert_eq!(analyzer.determine_page_type(0.0, 0.95), PageType::Scanned);

        // High text, low image = Text
        assert_eq!(analyzer.determine_page_type(0.75, 0.15), PageType::Text);
        assert_eq!(analyzer.determine_page_type(0.85, 0.0), PageType::Text);

        // Balanced = Mixed
        assert_eq!(analyzer.determine_page_type(0.4, 0.4), PageType::Mixed);
        assert_eq!(analyzer.determine_page_type(0.3, 0.3), PageType::Mixed);

        // Edge cases
        assert_eq!(analyzer.determine_page_type(0.5, 0.5), PageType::Mixed);
        assert_eq!(analyzer.determine_page_type(0.15, 0.75), PageType::Mixed);
    }

    // Test 28: Multiple analyzers with shared results
    #[test]
    fn test_multiple_analyzers_shared_results() {
        let mut all_results = Vec::new();

        // Create multiple analyzers and collect results
        for i in 0..3 {
            let doc = create_mock_document();
            let analyzer = PageContentAnalyzer::new(doc);

            if let Ok(analysis) = analyzer.analyze_page(0) {
                all_results.push((i, analysis.page_type));
            }
        }

        assert_eq!(all_results.len(), 3);

        // Verify all analyzers produced consistent results
        for (i, page_type) in &all_results {
            tracing::debug!("Analyzer {i} detected page type: {page_type:?}");
        }
    }

    // Test 29: Error recovery in batch processing
    #[test]
    fn test_batch_processing_error_recovery() {
        // Create analyzer that will encounter errors
        let doc = create_mock_document();
        let analyzer = PageContentAnalyzer::new(doc);

        // Use a provider that fails intermittently
        struct IntermittentOcrProvider {
            fail_count: Mutex<usize>,
        }

        impl OcrProvider for IntermittentOcrProvider {
            fn process_image(
                &self,
                data: &[u8],
                opts: &OcrOptions,
            ) -> Result<OcrProcessingResult, OcrError> {
                let mut count = self.fail_count.lock().unwrap();
                *count += 1;

                if *count % 2 == 0 {
                    Err(OcrError::ProcessingFailed(
                        "Intermittent failure".to_string(),
                    ))
                } else {
                    MockOcrProvider::new().process_image(data, opts)
                }
            }

            fn process_page(
                &self,
                _analysis: &ContentAnalysis,
                data: &[u8],
                opts: &OcrOptions,
            ) -> Result<OcrProcessingResult, OcrError> {
                self.process_image(data, opts)
            }

            fn supported_formats(&self) -> Vec<crate::graphics::ImageFormat> {
                MockOcrProvider::new().supported_formats()
            }

            fn engine_name(&self) -> &str {
                "Intermittent"
            }

            fn engine_type(&self) -> crate::text::OcrEngine {
                crate::text::OcrEngine::Mock
            }
        }

        let provider = IntermittentOcrProvider {
            fail_count: Mutex::new(0),
        };

        let result = analyzer.process_scanned_pages_batch(&provider, 2);
        assert!(result.is_ok());
        // Some pages may fail, but the batch should continue
    }

    // Test 30: Memory stress test with large analysis
    #[test]
    fn test_memory_stress_multiple_analyses() {
        let doc = create_mock_document();
        let analyzer = PageContentAnalyzer::new(doc);

        // Perform many analyses to test memory handling
        for _ in 0..100 {
            let result = analyzer.analyze_page(0);
            assert!(result.is_ok());
        }

        // Analyze document multiple times
        for _ in 0..10 {
            let result = analyzer.analyze_document();
            assert!(result.is_ok());
        }
    }

    // Test 31: OCR language fallback
    #[test]
    fn test_ocr_language_fallback() {
        let ocr_options = OcrOptions {
            language: "unknown_lang".to_string(),
            ..Default::default()
        };

        let analysis_options = AnalysisOptions {
            ocr_options: Some(ocr_options),
            ..Default::default()
        };

        let doc = create_mock_document();
        let analyzer = PageContentAnalyzer::with_options(doc, analysis_options);
        let provider = MockOcrProvider::new();

        // Should handle unknown language gracefully
        let result = analyzer.process_scanned_pages_with_ocr(&provider);
        assert!(result.is_ok());
    }

    // Test 32: Timeout handling simulation
    #[test]
    fn test_ocr_timeout_simulation() {
        let mut provider = MockOcrProvider::new();
        provider.set_processing_delay(100); // 100ms delay

        let ocr_options = OcrOptions {
            timeout_seconds: 1, // Very short timeout for testing
            ..Default::default()
        };

        let analysis_options = AnalysisOptions {
            ocr_options: Some(ocr_options),
            ..Default::default()
        };

        let doc = create_mock_document();
        let analyzer = PageContentAnalyzer::with_options(doc, analysis_options);

        // Process should complete within timeout
        let result = analyzer.process_scanned_pages_with_ocr(&provider);
        assert!(result.is_ok());
    }

    // Test 33: Zero-sized images filtering
    #[test]
    fn test_zero_sized_image_filtering() {
        let doc = create_mock_document();
        let analyzer = PageContentAnalyzer::new(doc);

        // analyze_image_content should filter out zero-sized images
        let result = analyzer.analyze_image_content(0);
        assert!(result.is_ok());
        let image_analysis = result.unwrap();
        assert_eq!(image_analysis.image_count, 0);
        assert_eq!(image_analysis.total_area, 0.0);
    }

    // Test 34: Page numbers wraparound
    #[test]
    fn test_page_numbers_boundary() {
        let doc = create_mock_document();
        let analyzer = PageContentAnalyzer::new(doc);

        // Test with maximum safe page numbers
        let page_numbers = vec![0, usize::MAX];
        let result = analyzer.analyze_pages(&page_numbers);
        // With fallback lookup, this might succeed or fail depending on what objects are found
        // We verify it handles boundary values gracefully
        if result.is_ok() {
            let analyses = result.unwrap();
            // Should include at least the valid page 0
            assert!(analyses.len() >= 1);
            assert_eq!(analyses[0].page_number, 0);
        } else {
            // If it fails, should be due to invalid page access
            assert!(result.unwrap_err().to_string().contains("Page"));
        }
    }

    // Test 35: OCR confidence edge cases
    #[test]
    fn test_ocr_confidence_boundaries() {
        let mut provider = MockOcrProvider::new();

        // Create a valid minimal JPEG header
        let jpeg_data = [
            0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10, 0x4A, 0x46, 0x49, 0x46, 0x00, 0x01,
        ];

        // Test with 0% confidence
        provider.set_confidence(0.0);
        let result = provider.process_image(&jpeg_data, &OcrOptions::default());
        assert!(result.is_ok());

        // Test with 100% confidence
        provider.set_confidence(1.0);
        let result = provider.process_image(&jpeg_data, &OcrOptions::default());
        assert!(result.is_ok());

        // Test with confidence below threshold
        let options = OcrOptions {
            min_confidence: 0.9,
            ..Default::default()
        };
        provider.set_confidence(0.5);
        let result = provider.process_image(&jpeg_data, &options);
        // Note: MockOcrProvider doesn't check min_confidence, so this will succeed
        assert!(result.is_ok());
    }

    // Test 36: OCR processing with different configurations
    #[test]
    fn test_ocr_processing_configurations() {
        let doc = create_mock_document();
        let analyzer = PageContentAnalyzer::new(doc);
        let provider = MockOcrProvider::new();

        // Test sequential processing
        let result = analyzer.process_scanned_pages_with_ocr(&provider);
        assert!(result.is_ok());

        // Test batch processing with different sizes
        for batch_size in [1, 3, 5, 10] {
            let result = analyzer.process_scanned_pages_batch(&provider, batch_size);
            assert!(result.is_ok());
        }
    }

    // Test 37: Custom image size filtering
    #[test]
    fn test_custom_min_image_size() {
        let doc = create_mock_document();
        let custom_options = AnalysisOptions {
            min_image_size: 1000, // Very large minimum
            ..Default::default()
        };
        let analyzer = PageContentAnalyzer::with_options(doc, custom_options);

        let result = analyzer.analyze_image_content(0);
        assert!(result.is_ok());
        // With high threshold, small images should be filtered
    }

    // Test 38: Page analysis with all content types
    #[test]
    fn test_comprehensive_page_analysis() {
        let doc = create_mock_document();
        let analyzer = PageContentAnalyzer::new(doc);

        let analysis = analyzer.analyze_page(0);
        assert!(analysis.is_ok());

        let analysis = analysis.unwrap();

        // Verify all fields are populated
        assert!(analysis.page_number == 0);
        assert!(analysis.text_ratio >= 0.0 && analysis.text_ratio <= 1.0);
        assert!(analysis.image_ratio >= 0.0 && analysis.image_ratio <= 1.0);
        assert!(analysis.blank_space_ratio >= 0.0 && analysis.blank_space_ratio <= 1.0);

        // Ratios should sum to approximately 1.0
        let total = analysis.text_ratio + analysis.image_ratio + analysis.blank_space_ratio;
        assert!((total - 1.0).abs() < 0.01);
    }

    // Test 39: Error message formatting
    #[test]
    fn test_error_message_formatting() {
        let doc = create_mock_document();
        let analyzer = PageContentAnalyzer::new(doc);
        let provider = MockOcrProvider::new();

        // Test non-scanned page error message
        let result = analyzer.extract_text_from_scanned_page(0, &provider);
        assert!(result.is_err());
        let error_msg = result.unwrap_err().to_string();
        assert!(error_msg.contains("not a scanned page"));
        assert!(error_msg.contains("image ratio"));
        assert!(error_msg.contains("text ratio"));
    }

    // Test 40: Batch size edge cases
    #[test]
    fn test_batch_size_edge_cases() {
        let doc = create_mock_document();
        let analyzer = PageContentAnalyzer::new(doc);
        let provider = MockOcrProvider::new();

        // Test with batch size 0 (should handle gracefully)
        let result = analyzer.process_scanned_pages_batch(&provider, 0);
        assert!(result.is_ok());

        // Test with very large batch size
        let result = analyzer.process_scanned_pages_batch(&provider, usize::MAX);
        assert!(result.is_ok());
    }

    // Test 41: OCR provider robustness
    #[test]
    fn test_ocr_provider_robustness() {
        // Create a provider that might fail
        struct UnreliableOcrProvider {
            call_count: Mutex<usize>,
        }

        impl UnreliableOcrProvider {
            fn new() -> Self {
                UnreliableOcrProvider {
                    call_count: Mutex::new(0),
                }
            }
        }

        impl Clone for UnreliableOcrProvider {
            fn clone(&self) -> Self {
                UnreliableOcrProvider {
                    call_count: Mutex::new(0),
                }
            }
        }

        impl OcrProvider for UnreliableOcrProvider {
            fn process_image(
                &self,
                _: &[u8],
                _: &OcrOptions,
            ) -> Result<OcrProcessingResult, OcrError> {
                let mut count = self.call_count.lock().unwrap();
                *count += 1;

                // Fail on first call, succeed on subsequent calls
                if *count == 1 {
                    Err(OcrError::ProcessingFailed("Temporary failure".to_string()))
                } else {
                    MockOcrProvider::new().process_image(&[0xFF, 0xD8], &OcrOptions::default())
                }
            }

            fn process_page(
                &self,
                _: &ContentAnalysis,
                data: &[u8],
                opts: &OcrOptions,
            ) -> Result<OcrProcessingResult, OcrError> {
                self.process_image(data, opts)
            }

            fn supported_formats(&self) -> Vec<crate::graphics::ImageFormat> {
                MockOcrProvider::new().supported_formats()
            }

            fn engine_name(&self) -> &str {
                "Unreliable"
            }

            fn engine_type(&self) -> crate::text::OcrEngine {
                crate::text::OcrEngine::Mock
            }
        }

        let doc = create_mock_document();
        let analyzer = PageContentAnalyzer::new(doc);
        let provider = UnreliableOcrProvider::new();

        // Test sequential processing with unreliable provider
        let result = analyzer.process_scanned_pages_with_ocr(&provider);
        assert!(result.is_ok());

        // Test batch processing with unreliable provider
        let result = analyzer.process_scanned_pages_batch(&provider, 2);
        assert!(result.is_ok());
    }

    // Test 42: Analysis options validation
    #[test]
    fn test_analysis_options_validation() {
        // Test with negative values (logically invalid but should handle)
        let options = AnalysisOptions {
            min_text_fragment_size: 0,
            min_image_size: 0,
            scanned_threshold: 1.5, // Above 1.0
            text_threshold: -0.5,   // Below 0.0
            ocr_options: None,
        };

        let doc = create_mock_document();
        let analyzer = PageContentAnalyzer::with_options(doc, options);

        // Should still work despite invalid thresholds
        let result = analyzer.analyze_page(0);
        assert!(result.is_ok());
    }

    // Test 43: OCR result aggregation
    #[test]
    fn test_ocr_result_aggregation() {
        let doc = create_mock_document();
        let analyzer = PageContentAnalyzer::new(doc);
        let mut provider = MockOcrProvider::new();

        // Set up provider with specific results
        provider.set_mock_text("Page content from OCR".to_string());
        provider.set_confidence(0.85);

        let results = analyzer.process_scanned_pages_with_ocr(&provider);
        assert!(results.is_ok());

        let ocr_results = results.unwrap();

        // Verify results can be aggregated
        let total_chars: usize = ocr_results
            .iter()
            .map(|(_, result)| result.text.len())
            .sum();
        let avg_confidence: f64 = if !ocr_results.is_empty() {
            ocr_results
                .iter()
                .map(|(_, result)| result.confidence)
                .sum::<f64>()
                / ocr_results.len() as f64
        } else {
            0.0
        };

        // total_chars is usize, always >= 0
        assert!(total_chars == total_chars); // Just to use the variable
        assert!((0.0..=1.0).contains(&avg_confidence));
    }

    // Test 44: Resource cleanup verification
    #[test]
    fn test_resource_cleanup() {
        // Test that resources are properly cleaned up
        for _ in 0..10 {
            let doc = create_mock_document();
            let analyzer = PageContentAnalyzer::new(doc);
            let _result = analyzer.analyze_document();
            // Resources should be automatically cleaned up when analyzer goes out of scope
        }

        // If this test completes without issues, resource cleanup is working
        // Test passes if no panic occurs
    }

    // Test 45: Complete workflow integration test
    #[test]
    fn test_complete_analysis_workflow() {
        // Create analyzer
        let doc = create_mock_document();
        let analyzer = PageContentAnalyzer::new(doc);

        // 1. Analyze document
        let analyses = analyzer.analyze_document().unwrap();
        assert!(!analyses.is_empty());

        // 2. Find scanned pages
        let _scanned_pages = analyzer.find_scanned_pages().unwrap();

        // 3. Check specific page
        let _is_scanned = analyzer.is_scanned_page(0).unwrap();

        // 4. Process with OCR (if applicable)
        let provider = MockOcrProvider::new();
        let ocr_results = analyzer.process_scanned_pages_with_ocr(&provider).unwrap();

        // 5. Sequential processing (since parallel requires Send + Sync)
        let sequential_results = analyzer.process_scanned_pages_with_ocr(&provider).unwrap();

        // 6. Batch processing
        let batch_results = analyzer.process_scanned_pages_batch(&provider, 5).unwrap();

        // Verify consistency across methods
        assert_eq!(ocr_results.len(), sequential_results.len());
        assert_eq!(ocr_results.len(), batch_results.len());

        tracing::debug!(
            "Complete workflow test passed with {} pages analyzed",
            analyses.len()
        );
    }
}