wow-mpq 0.6.4

High-performance parser for World of Warcraft MPQ archives with parallel processing support
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
//! MPQ archive handling
//!
//! This module provides the main Archive type for reading MPQ files.
//! It supports:
//! - All MPQ versions (v1-v4)
//! - File extraction with decompression
//! - Sector CRC validation
//! - Encryption/decryption
//! - Multi-sector and single-unit files

use crate::{
    Error, Result,
    builder::ArchiveBuilder,
    compression,
    crypto::{decrypt_block, decrypt_dword, hash_string, hash_type},
    header::{self, MpqHeader, UserDataHeader},
    special_files,
    tables::{BetTable, BlockTable, HashTable, HetTable, HiBlockTable},
};
use byteorder::{LittleEndian, ReadBytesExt};
use std::fs::File;
use std::io::{BufReader, Read, Seek, SeekFrom};
use std::path::{Path, PathBuf};

/// Detailed information about an MPQ archive
#[derive(Debug, Clone)]
pub struct ArchiveInfo {
    /// Path to the archive file
    pub path: PathBuf,
    /// Total file size in bytes
    pub file_size: u64,
    /// Archive offset (if MPQ data starts after user data)
    pub archive_offset: u64,
    /// MPQ format version
    pub format_version: header::FormatVersion,
    /// Number of files in the archive
    pub file_count: usize,
    /// Maximum file capacity (hash table size)
    pub max_file_count: u32,
    /// Sector size in bytes
    pub sector_size: usize,
    /// Archive is encrypted
    pub is_encrypted: bool,
    /// Archive has digital signature
    pub has_signature: bool,
    /// Signature status (if applicable)
    pub signature_status: SignatureStatus,
    /// Hash table information
    pub hash_table_info: TableInfo,
    /// Block table information
    pub block_table_info: TableInfo,
    /// HET table information (v3+)
    pub het_table_info: Option<TableInfo>,
    /// BET table information (v3+)
    pub bet_table_info: Option<TableInfo>,
    /// Hi-block table information (v2+)
    pub hi_block_table_info: Option<TableInfo>,
    /// Has (attributes) file
    pub has_attributes: bool,
    /// Has (listfile) file
    pub has_listfile: bool,
    /// User data information
    pub user_data_info: Option<UserDataInfo>,
    /// MD5 checksums status (v4)
    pub md5_status: Option<Md5Status>,
}

/// Information about a table in the archive
#[derive(Debug, Clone)]
pub struct TableInfo {
    /// Table size in entries (None if table failed to load)
    pub size: Option<u32>,
    /// Table offset in archive
    pub offset: u64,
    /// Compressed size (if applicable)
    pub compressed_size: Option<u64>,
    /// Whether the table failed to load
    pub failed_to_load: bool,
}

/// User data information
#[derive(Debug, Clone)]
pub struct UserDataInfo {
    /// User data header size
    pub header_size: u32,
    /// User data size
    pub data_size: u32,
}

/// Digital signature status
#[derive(Debug, Clone, PartialEq)]
pub enum SignatureStatus {
    /// No signature present
    None,
    /// Weak signature present and valid
    WeakValid,
    /// Weak signature present but invalid
    WeakInvalid,
    /// Strong signature present and valid
    StrongValid,
    /// Strong signature present but invalid
    StrongInvalid,
    /// Strong signature present but no public key available
    StrongNoKey,
}

/// MD5 checksum verification status for v4 archives
#[derive(Debug, Clone)]
pub struct Md5Status {
    /// Hash table MD5 valid
    pub hash_table_valid: bool,
    /// Block table MD5 valid
    pub block_table_valid: bool,
    /// Hi-block table MD5 valid
    pub hi_block_table_valid: bool,
    /// HET table MD5 valid
    pub het_table_valid: bool,
    /// BET table MD5 valid
    pub bet_table_valid: bool,
    /// MPQ header MD5 valid
    pub header_valid: bool,
}

/// Options for opening MPQ archives
///
/// This struct provides configuration options for how MPQ archives are opened
/// and initialized. It follows the builder pattern for easy configuration.
///
/// # Examples
///
/// ```no_run
/// use wow_mpq::{Archive, OpenOptions};
///
/// // Open with default options
/// let archive = Archive::open("data.mpq")?;
///
/// // Open with custom options
/// let archive = OpenOptions::new()
///     .load_tables(false)  // Defer table loading for faster startup
///     .open("data.mpq")?;
/// # Ok::<(), wow_mpq::Error>(())
/// ```
#[derive(Debug, Clone)]
pub struct OpenOptions {
    /// Whether to load and parse all tables immediately when opening the archive.
    ///
    /// When `true` (default), all tables (hash, block, HET/BET) are loaded and
    /// validated during archive opening. This provides immediate error detection
    /// but slower startup for large archives.
    ///
    /// When `false`, tables are loaded on-demand when first accessed. This
    /// provides faster startup but may defer error detection.
    pub load_tables: bool,

    /// MPQ format version to use when creating new archives.
    ///
    /// This field is only used when creating new archives via `create()`.
    /// If `None`, defaults to MPQ version 1 for maximum compatibility.
    version: Option<crate::header::FormatVersion>,
}

impl OpenOptions {
    /// Create new default options
    ///
    /// Returns an `OpenOptions` instance with default settings:
    /// - `load_tables = true` (immediate table loading)
    /// - `version = None` (defaults to MPQ v1 for new archives)
    pub fn new() -> Self {
        Self {
            load_tables: true,
            version: None,
        }
    }

    /// Set whether to load tables immediately when opening
    ///
    /// # Parameters
    /// - `load`: If `true`, tables are loaded immediately during open.
    ///   If `false`, tables are loaded on first access.
    ///
    /// # Returns
    /// Self for method chaining
    pub fn load_tables(mut self, load: bool) -> Self {
        self.load_tables = load;
        self
    }

    /// Set the MPQ version for new archives
    ///
    /// This setting only affects archives created with `create()`, not
    /// archives opened with `open()`.
    ///
    /// # Parameters
    /// - `version`: The MPQ format version to use (V1, V2, V3, or V4)
    ///
    /// # Returns
    /// Self for method chaining
    pub fn version(mut self, version: crate::header::FormatVersion) -> Self {
        self.version = Some(version);
        self
    }

    /// Open an existing MPQ archive with these options
    ///
    /// # Parameters
    /// - `path`: Path to the MPQ archive file
    ///
    /// # Returns
    /// `Ok(Archive)` on success, `Err(Error)` on failure
    ///
    /// # Errors
    /// - `Error::Io` if the file cannot be opened
    /// - `Error::InvalidFormat` if the file is not a valid MPQ archive
    /// - `Error::Corruption` if table validation fails (when `load_tables = true`)
    pub fn open<P: AsRef<Path>>(self, path: P) -> Result<Archive> {
        Archive::open_with_options(path, self)
    }

    /// Create a new empty MPQ archive with these options
    ///
    /// Creates a new MPQ archive file with the specified format version.
    /// The archive will be empty but properly formatted.
    ///
    /// # Parameters
    /// - `path`: Path where the new archive should be created
    ///
    /// # Returns
    /// `Ok(Archive)` on success, `Err(Error)` on failure
    ///
    /// # Errors
    /// - `Error::Io` if the file cannot be created
    /// - `Error::InvalidFormat` if archive creation fails
    pub fn create<P: AsRef<Path>>(self, path: P) -> Result<Archive> {
        let path = path.as_ref();

        // Create an empty archive with the specified version
        let builder =
            ArchiveBuilder::new().version(self.version.unwrap_or(crate::header::FormatVersion::V1));

        // Build the empty archive
        builder.build(path)?;

        // Open the newly created archive
        Self::new().load_tables(self.load_tables).open(path)
    }
}

impl Default for OpenOptions {
    fn default() -> Self {
        Self::new()
    }
}

/// An MPQ archive
#[derive(Debug)]
pub struct Archive {
    /// Path to the archive file
    path: PathBuf,
    /// Archive file reader
    reader: BufReader<File>,
    /// Offset where the MPQ data starts in the file
    archive_offset: u64,
    /// Optional user data header
    user_data: Option<UserDataHeader>,
    /// MPQ header
    header: MpqHeader,
    /// Hash table (optional, loaded on demand)
    hash_table: Option<HashTable>,
    /// Block table (optional, loaded on demand)
    block_table: Option<BlockTable>,
    /// Hi-block table for v2+ archives (optional)
    hi_block_table: Option<HiBlockTable>,
    /// HET table for v3+ archives
    het_table: Option<HetTable>,
    /// BET table for v3+ archives
    bet_table: Option<BetTable>,
    /// File attributes from (attributes) file
    attributes: Option<special_files::Attributes>,
}

impl Archive {
    /// Open an existing MPQ archive
    pub fn open<P: AsRef<Path>>(path: P) -> Result<Self> {
        Self::open_with_options(path, OpenOptions::default())
    }

    /// Open an archive with specific options
    pub fn open_with_options<P: AsRef<Path>>(path: P, options: OpenOptions) -> Result<Self> {
        let path = path.as_ref().to_path_buf();
        let file = File::open(&path)?;
        let mut reader = BufReader::new(file);

        // Find and read the MPQ header
        let (archive_offset, user_data, header) = header::find_header(&mut reader)?;

        let mut archive = Archive {
            path,
            reader,
            archive_offset,
            user_data,
            header,
            hash_table: None,
            block_table: None,
            hi_block_table: None,
            bet_table: None,
            het_table: None,
            attributes: None,
        };

        // Load tables if requested
        if options.load_tables {
            archive.load_tables()?;
        }

        Ok(archive)
    }

    /// Load hash and block tables
    pub fn load_tables(&mut self) -> Result<()> {
        log::debug!(
            "Loading tables for archive version {:?}",
            self.header.format_version
        );

        // For v3+ archives, check for HET/BET tables first
        if self.header.format_version >= header::FormatVersion::V3 {
            // Try to load HET table
            if let Some(het_pos) = self.header.het_table_pos
                && het_pos != 0
            {
                let mut het_size = self
                    .header
                    .v4_data
                    .as_ref()
                    .map(|v4| v4.het_table_size_64)
                    .unwrap_or(0);

                // For V3 without V4 data, we need to determine the size
                if het_size == 0 && self.header.format_version == header::FormatVersion::V3 {
                    log::debug!("V3 archive without V4 data, reading HET table size from header");
                    // Try to read the table size from the HET header
                    match self.read_het_table_size(het_pos) {
                        Ok(size) => {
                            log::debug!("Determined HET table size: 0x{size:X}");
                            het_size = size;
                        }
                        Err(e) => {
                            log::warn!("Failed to determine HET table size: {e}");
                        }
                    }
                }

                if het_size > 0 {
                    log::debug!("Loading HET table from offset 0x{het_pos:X}, size 0x{het_size:X}");

                    // HET table key is based on table name
                    let key = hash_string("(hash table)", hash_type::FILE_KEY);

                    match HetTable::read(
                        &mut self.reader,
                        self.archive_offset + het_pos,
                        het_size,
                        key,
                    ) {
                        Ok(het) => {
                            let file_count = het.header.max_file_count;
                            log::info!("Loaded HET table with {file_count} max files");
                            self.het_table = Some(het);
                        }
                        Err(e) => {
                            log::warn!("Failed to load HET table: {e}");
                        }
                    }
                }
            }

            // Try to load BET table
            if let Some(bet_pos) = self.header.bet_table_pos
                && bet_pos != 0
            {
                let mut bet_size = self
                    .header
                    .v4_data
                    .as_ref()
                    .map(|v4| v4.bet_table_size_64)
                    .unwrap_or(0);

                // For V3 without V4 data, we need to determine the size
                if bet_size == 0 && self.header.format_version == header::FormatVersion::V3 {
                    log::debug!("V3 archive without V4 data, reading BET table size from header");
                    // Try to read the table size from the BET header
                    match self.read_bet_table_size(bet_pos) {
                        Ok(size) => {
                            log::debug!("Determined BET table size: 0x{size:X}");
                            bet_size = size;
                        }
                        Err(e) => {
                            log::warn!("Failed to determine BET table size: {e}");
                        }
                    }
                }

                if bet_size > 0 {
                    log::debug!("Loading BET table from offset 0x{bet_pos:X}, size 0x{bet_size:X}");

                    // First, check if the BET offset actually points to a HET table
                    // This is a known issue in some MoP update archives
                    self.reader
                        .seek(SeekFrom::Start(self.archive_offset + bet_pos))?;
                    let mut sig_buf = [0u8; 4];
                    self.reader.read_exact(&mut sig_buf)?;

                    if &sig_buf == b"HET\x1A" {
                        log::error!(
                            "BET offset points to HET table! This archive has swapped table offsets."
                        );
                        log::warn!(
                            "Skipping BET table loading for this archive due to invalid offset."
                        );
                    } else {
                        // Reset position and proceed with normal BET loading
                        self.reader
                            .seek(SeekFrom::Start(self.archive_offset + bet_pos))?;

                        // BET table key is based on table name
                        let key = hash_string("(block table)", hash_type::FILE_KEY);

                        match BetTable::read(
                            &mut self.reader,
                            self.archive_offset + bet_pos,
                            bet_size,
                            key,
                        ) {
                            Ok(bet) => {
                                let file_count = bet.header.file_count;
                                log::info!("Loaded BET table with {file_count} files");
                                self.bet_table = Some(bet);
                            }
                            Err(e) => {
                                log::warn!("Failed to load BET table: {e}");
                            }
                        }
                    }
                }
            }
        }

        // Check if we have valid HET/BET tables with actual entries
        let _has_valid_het_bet = match (&self.het_table, &self.bet_table) {
            (Some(het), Some(bet)) => {
                // Tables are valid if they have entries
                het.header.max_file_count > 0 && bet.header.file_count > 0
            }
            _ => false,
        };

        // Always try to load classic tables if they exist (for compatibility)
        // Only skip them if the archive appears to be truncated/corrupted
        if self.header.hash_table_size > 0 {
            // Load hash table
            let hash_table_offset = self.archive_offset + self.header.get_hash_table_pos();
            let uncompressed_size = self.header.hash_table_size as usize * 16; // Each hash entry is 16 bytes

            // For V4 archives, we have explicit compressed size info
            if let Some(v4_data) = &self.header.v4_data {
                // Validate V4 sizes are reasonable (not corrupted)
                let file_size = self.reader.get_ref().metadata()?.len();
                let v4_size_valid = v4_data.hash_table_size_64 > 0
                    && v4_data.hash_table_size_64 < file_size
                    && v4_data.hash_table_size_64 < (uncompressed_size as u64 * 2); // Compressed shouldn't be much larger

                if v4_size_valid {
                    // Use compressed size for V4
                    let compressed_size = v4_data.hash_table_size_64;

                    log::debug!(
                        "Loading hash table from 0x{hash_table_offset:X}, compressed size: {compressed_size} bytes, uncompressed size: {uncompressed_size} bytes"
                    );

                    // Check if it would extend beyond file
                    let file_size = self.reader.get_ref().metadata()?.len();
                    if hash_table_offset + compressed_size > file_size {
                        log::warn!("Hash table extends beyond file, skipping");
                    } else {
                        // Read potentially compressed table
                        match self.read_compressed_table(
                            hash_table_offset,
                            compressed_size,
                            uncompressed_size,
                        ) {
                            Ok(table_data) => {
                                // Parse the hash table from the decompressed data
                                match HashTable::from_bytes(
                                    &table_data,
                                    self.header.hash_table_size,
                                ) {
                                    Ok(hash_table) => {
                                        self.hash_table = Some(hash_table);
                                    }
                                    Err(e) => {
                                        log::warn!("Failed to parse hash table: {e}");
                                    }
                                }
                            }
                            Err(e) => {
                                log::warn!("Failed to decompress hash table: {e}");
                            }
                        }
                    }
                } else {
                    // V4 sizes are invalid, fall back to V3-style detection
                    log::warn!(
                        "V4 archive has invalid compressed size ({}), using heuristic detection",
                        v4_data.hash_table_size_64
                    );
                    // Fall through to V3-style detection below
                }
            }

            // If we don't have valid V4 data or V4 size was invalid, use heuristic
            if self.hash_table.is_none() {
                // For V3 and earlier, or V4 with invalid sizes, we need to detect if tables are compressed
                // by checking the available space between tables
                let block_table_offset = self.archive_offset + self.header.get_block_table_pos();
                let available_space = if block_table_offset > hash_table_offset {
                    (block_table_offset - hash_table_offset) as usize
                } else {
                    // If block table comes before hash table, calculate differently
                    let file_size = self.reader.get_ref().metadata()?.len();
                    (file_size - hash_table_offset) as usize
                };

                if available_space < uncompressed_size {
                    // Table appears to be compressed
                    log::debug!(
                        "V3 hash table appears compressed: available space {available_space} < expected size {uncompressed_size}"
                    );

                    // Try to read as compressed
                    match self.read_compressed_table(
                        hash_table_offset,
                        available_space as u64,
                        uncompressed_size,
                    ) {
                        Ok(table_data) => {
                            match HashTable::from_bytes(&table_data, self.header.hash_table_size) {
                                Ok(hash_table) => {
                                    self.hash_table = Some(hash_table);
                                }
                                Err(e) => {
                                    log::warn!("Failed to parse hash table: {e}");
                                }
                            }
                        }
                        Err(e) => {
                            log::warn!("Failed to decompress hash table: {e}");
                            // Try to read as truncated uncompressed table
                            // Calculate how many entries we can fit in available space
                            let entries_that_fit = available_space / 16; // 16 bytes per entry
                            // Round down to nearest power of 2 for hash table
                            let mut pow2_entries = 1u32;
                            while pow2_entries * 2 <= entries_that_fit as u32 {
                                pow2_entries *= 2;
                            }
                            if pow2_entries > 0 {
                                log::warn!(
                                    "Trying to read truncated hash table with {} entries (originally {})",
                                    pow2_entries,
                                    self.header.hash_table_size
                                );
                                match HashTable::read(
                                    &mut self.reader,
                                    hash_table_offset,
                                    pow2_entries,
                                ) {
                                    Ok(hash_table) => {
                                        self.hash_table = Some(hash_table);
                                        log::info!("Successfully loaded truncated hash table");
                                    }
                                    Err(e2) => {
                                        log::warn!("Failed to read truncated hash table: {e2}");
                                    }
                                }
                            }
                        }
                    }
                } else {
                    // Normal uncompressed reading
                    match HashTable::read(
                        &mut self.reader,
                        hash_table_offset,
                        self.header.hash_table_size,
                    ) {
                        Ok(hash_table) => {
                            self.hash_table = Some(hash_table);
                        }
                        Err(e) => {
                            log::warn!("Failed to read hash table: {e}");
                        }
                    }
                }
            }
        }

        if self.header.block_table_size > 0 {
            // Load block table
            let block_table_offset = self.archive_offset + self.header.get_block_table_pos();
            let uncompressed_size = self.header.block_table_size as usize * 16; // Each block entry is 16 bytes

            // For V4 archives, we have explicit compressed size info
            if let Some(v4_data) = &self.header.v4_data {
                // Validate V4 sizes are reasonable (not corrupted)
                let file_size = self.reader.get_ref().metadata()?.len();
                let v4_size_valid = v4_data.block_table_size_64 > 0
                    && v4_data.block_table_size_64 < file_size
                    && v4_data.block_table_size_64 < (uncompressed_size as u64 * 2); // Compressed shouldn't be much larger

                if v4_size_valid {
                    // Use compressed size for V4
                    let compressed_size = v4_data.block_table_size_64;

                    log::debug!(
                        "Loading block table from 0x{block_table_offset:X}, compressed size: {compressed_size} bytes, uncompressed size: {uncompressed_size} bytes"
                    );

                    // Check if it would extend beyond file
                    let file_size = self.reader.get_ref().metadata()?.len();
                    if block_table_offset + compressed_size > file_size {
                        log::warn!("Block table extends beyond file, skipping");
                    } else {
                        // Read potentially compressed table
                        match self.read_compressed_table(
                            block_table_offset,
                            compressed_size,
                            uncompressed_size,
                        ) {
                            Ok(table_data) => {
                                // Parse the block table from the decompressed data
                                match BlockTable::from_bytes(
                                    &table_data,
                                    self.header.block_table_size,
                                ) {
                                    Ok(block_table) => {
                                        self.block_table = Some(block_table);
                                    }
                                    Err(e) => {
                                        log::warn!("Failed to parse block table: {e}");
                                    }
                                }
                            }
                            Err(e) => {
                                log::warn!("Failed to decompress block table: {e}");
                            }
                        }
                    }
                } else {
                    // V4 sizes are invalid, fall back to V3-style detection
                    log::warn!(
                        "V4 archive has invalid compressed size ({}), using heuristic detection",
                        v4_data.block_table_size_64
                    );
                    // Fall through to V3-style detection below
                }
            }

            // If we don't have valid V4 data or V4 size was invalid, use heuristic
            if self.block_table.is_none() {
                // For V3 and earlier, or V4 with invalid sizes, we need to detect if tables are compressed
                // Calculate available space for block table
                let file_size = self.reader.get_ref().metadata()?.len();
                let next_section = if let Some(hi_block_pos) = self.header.hi_block_table_pos {
                    if hi_block_pos != 0 {
                        self.archive_offset + hi_block_pos
                    } else {
                        file_size
                    }
                } else {
                    file_size
                };

                let available_space = (next_section.saturating_sub(block_table_offset)) as usize;

                if available_space < uncompressed_size {
                    // Table appears to be compressed
                    log::debug!(
                        "V3 block table appears compressed: available space {available_space} < expected size {uncompressed_size}"
                    );

                    // Try to read as compressed
                    match self.read_compressed_table(
                        block_table_offset,
                        available_space as u64,
                        uncompressed_size,
                    ) {
                        Ok(table_data) => {
                            match BlockTable::from_bytes(&table_data, self.header.block_table_size)
                            {
                                Ok(block_table) => {
                                    self.block_table = Some(block_table);
                                }
                                Err(e) => {
                                    log::warn!("Failed to parse block table: {e}");
                                }
                            }
                        }
                        Err(e) => {
                            log::warn!("Failed to decompress block table: {e}");
                            // Try to read as truncated uncompressed table
                            // Calculate how many entries we can fit in available space
                            let entries_that_fit = available_space / 16; // 16 bytes per entry
                            if entries_that_fit > 0 {
                                log::warn!(
                                    "Trying to read truncated block table with {} entries (originally {})",
                                    entries_that_fit,
                                    self.header.block_table_size
                                );
                                match BlockTable::read(
                                    &mut self.reader,
                                    block_table_offset,
                                    entries_that_fit as u32,
                                ) {
                                    Ok(block_table) => {
                                        self.block_table = Some(block_table);
                                        log::info!("Successfully loaded truncated block table");
                                    }
                                    Err(e2) => {
                                        log::warn!("Failed to read truncated block table: {e2}");
                                    }
                                }
                            }
                        }
                    }
                } else {
                    // Normal uncompressed reading
                    match BlockTable::read(
                        &mut self.reader,
                        block_table_offset,
                        self.header.block_table_size,
                    ) {
                        Ok(block_table) => {
                            self.block_table = Some(block_table);
                        }
                        Err(e) => {
                            log::warn!("Failed to read block table: {e}");
                        }
                    }
                }
            }
        }

        // Load hi-block table if present (v2+)
        if let Some(hi_block_pos) = self.header.hi_block_table_pos
            && hi_block_pos != 0
        {
            let hi_block_offset = self.archive_offset + hi_block_pos;
            let hi_block_end = hi_block_offset + (self.header.block_table_size as u64 * 8);

            let file_size = self.reader.get_ref().metadata()?.len();
            if hi_block_end > file_size {
                log::warn!(
                    "Hi-block table extends beyond file (ends at 0x{hi_block_end:X}, file size 0x{file_size:X}). Skipping."
                );
            } else {
                self.hi_block_table = Some(HiBlockTable::read(
                    &mut self.reader,
                    hi_block_offset,
                    self.header.block_table_size,
                )?);
            }
        }

        // Load attributes if present
        match self.load_attributes() {
            Ok(()) => {}
            Err(e) => {
                log::warn!("Failed to load attributes: {e:?}");
                // Continue without attributes
            }
        }

        Ok(())
    }

    /// Get the archive header
    pub fn header(&self) -> &MpqHeader {
        &self.header
    }

    /// Get the user data header if present
    pub fn user_data(&self) -> Option<&UserDataHeader> {
        self.user_data.as_ref()
    }

    /// Get the archive offset in the file
    pub fn archive_offset(&self) -> u64 {
        self.archive_offset
    }

    /// Get the path to the archive
    pub fn path(&self) -> &Path {
        &self.path
    }

    /// Get the hi-block table if present (v2+ archives)
    pub fn hi_block_table(&self) -> Option<&HiBlockTable> {
        self.hi_block_table.as_ref()
    }

    /// Validate MD5 checksums for v4 archives
    fn validate_v4_md5_checksums(&mut self) -> Result<Option<Md5Status>> {
        use md5::{Digest, Md5};

        let v4_data = match &self.header.v4_data {
            Some(data) => data,
            None => return Ok(None),
        };

        // Helper function to calculate MD5 of raw table data
        let mut validate_table_md5 = |expected: &[u8; 16],
                                      offset: u64,
                                      size: u64|
         -> Result<bool> {
            if size == 0 {
                return Ok(true); // Empty table is valid
            }

            // Read raw table data
            self.reader
                .seek(SeekFrom::Start(self.archive_offset + offset))?;
            let mut table_data = vec![0u8; size as usize];
            match self.reader.read_exact(&mut table_data) {
                Ok(_) => {
                    // Calculate MD5
                    let mut hasher = Md5::new();
                    hasher.update(&table_data);
                    let actual_md5: [u8; 16] = hasher.finalize().into();

                    Ok(actual_md5 == *expected)
                }
                Err(e) => {
                    log::warn!(
                        "Failed to read table data for MD5 validation at offset 0x{:X}, size {}: {}",
                        self.archive_offset + offset,
                        size,
                        e
                    );
                    Ok(false)
                }
            }
        };

        // Validate hash table MD5
        let hash_table_valid = if self.header.hash_table_size > 0 {
            let hash_offset = self.header.get_hash_table_pos();
            let hash_size = v4_data.hash_table_size_64;
            validate_table_md5(&v4_data.md5_hash_table, hash_offset, hash_size)?
        } else {
            true // No hash table to validate
        };

        // Validate block table MD5
        let block_table_valid = if self.header.block_table_size > 0 {
            let block_offset = self.header.get_block_table_pos();
            let block_size = v4_data.block_table_size_64;
            validate_table_md5(&v4_data.md5_block_table, block_offset, block_size)?
        } else {
            true // No block table to validate
        };

        // Validate hi-block table MD5 (if present)
        let hi_block_table_valid = if let Some(hi_pos) = self.header.hi_block_table_pos {
            if hi_pos != 0 {
                let hi_size = v4_data.hi_block_table_size_64;
                validate_table_md5(&v4_data.md5_hi_block_table, hi_pos, hi_size)?
            } else {
                true
            }
        } else {
            true // No hi-block table
        };

        // Validate HET table MD5 (if present)
        let het_table_valid = if let Some(het_pos) = self.header.het_table_pos {
            if het_pos != 0 {
                let het_size = v4_data.het_table_size_64;
                validate_table_md5(&v4_data.md5_het_table, het_pos, het_size)?
            } else {
                true
            }
        } else {
            true // No HET table
        };

        // Validate BET table MD5 (if present)
        let bet_table_valid = if let Some(bet_pos) = self.header.bet_table_pos {
            if bet_pos != 0 {
                let bet_size = v4_data.bet_table_size_64;
                validate_table_md5(&v4_data.md5_bet_table, bet_pos, bet_size)?
            } else {
                true
            }
        } else {
            true // No BET table
        };

        // Validate header MD5 (first 192 bytes of header, excluding the MD5 field itself)
        let header_valid = {
            self.reader.seek(SeekFrom::Start(self.archive_offset))?;
            let mut header_data = vec![0u8; 192];
            match self.reader.read_exact(&mut header_data) {
                Ok(_) => {
                    let mut hasher = Md5::new();
                    hasher.update(&header_data);
                    let actual_md5: [u8; 16] = hasher.finalize().into();

                    actual_md5 == v4_data.md5_mpq_header
                }
                Err(e) => {
                    log::warn!("Failed to read header for MD5 validation: {e}");
                    false
                }
            }
        };

        Ok(Some(Md5Status {
            hash_table_valid,
            block_table_valid,
            hi_block_table_valid,
            het_table_valid,
            bet_table_valid,
            header_valid,
        }))
    }

    /// Get detailed information about the archive
    pub fn get_info(&mut self) -> Result<ArchiveInfo> {
        log::debug!("Getting archive info");

        // Ensure tables are loaded
        if self.hash_table.is_none() && self.het_table.is_none() {
            log::debug!("Loading tables for info");
            self.load_tables()?;
        }

        // Get file size
        log::debug!("Getting file size");
        let file_size = self.reader.get_ref().metadata()?.len();

        // Count files
        let file_count = if let Some(bet) = &self.bet_table {
            bet.header.file_count as usize
        } else if let Some(block_table) = &self.block_table {
            // Count non-empty entries in block table
            block_table
                .entries()
                .iter()
                .filter(|entry| entry.file_size != 0)
                .count()
        } else {
            0
        };

        // Get max file count
        let max_file_count = if let Some(het) = &self.het_table {
            het.header.max_file_count
        } else {
            self.header.hash_table_size
        };

        // Check for special files
        let has_listfile = self.find_file("(listfile)")?.is_some();
        let has_signature = self.find_file("(signature)")?.is_some();
        let has_attributes = self.attributes.is_some() || self.find_file("(attributes)")?.is_some();

        // Determine encryption status
        let is_encrypted = if let Some(block_table) = &self.block_table {
            use crate::tables::BlockEntry;
            block_table
                .entries()
                .iter()
                .any(|entry| (entry.flags & BlockEntry::FLAG_ENCRYPTED) != 0)
        } else {
            false
        };

        // Verify signature if present
        let signature_status = if has_signature {
            match self.verify_signature() {
                Ok(status) => status,
                Err(e) => {
                    log::warn!("Failed to verify signature: {e}");
                    SignatureStatus::WeakInvalid
                }
            }
        } else {
            SignatureStatus::None
        };

        // Build table info
        let hash_table_info = TableInfo {
            size: Some(self.header.hash_table_size),
            offset: self.header.get_hash_table_pos(),
            compressed_size: self.header.v4_data.as_ref().map(|v4| v4.hash_table_size_64),
            failed_to_load: self.hash_table.is_none() && self.header.hash_table_size > 0,
        };

        let block_table_info = TableInfo {
            size: Some(self.header.block_table_size),
            offset: self.header.get_block_table_pos(),
            compressed_size: self
                .header
                .v4_data
                .as_ref()
                .map(|v4| v4.block_table_size_64),
            failed_to_load: self.block_table.is_none() && self.header.block_table_size > 0,
        };

        let het_table_info = self.header.het_table_pos.and_then(|pos| {
            if pos == 0 {
                return None;
            }

            // For v4, use the size from v4 data
            let mut compressed_size = self.header.v4_data.as_ref().map(|v4| v4.het_table_size_64);

            // For v3 without v4 data, try to determine the size
            if compressed_size.is_none() && self.header.format_version == header::FormatVersion::V3
            {
                // Make a copy of the reader to avoid interfering with the main archive
                if let Ok(temp_reader) =
                    std::fs::File::open(&self.path).map(std::io::BufReader::new)
                {
                    let mut temp_archive = Self {
                        path: self.path.clone(),
                        reader: temp_reader,
                        archive_offset: self.archive_offset,
                        user_data: self.user_data.clone(),
                        header: self.header.clone(),
                        hash_table: None,
                        block_table: None,
                        hi_block_table: None,
                        het_table: None,
                        bet_table: None,
                        attributes: None,
                    };

                    if let Ok(size) = temp_archive.read_het_table_size(pos) {
                        compressed_size = Some(size);
                    }
                }
            }

            Some(TableInfo {
                size: self.het_table.as_ref().map(|het| het.header.max_file_count),
                offset: pos,
                compressed_size,
                failed_to_load: self.het_table.is_none(),
            })
        });

        let bet_table_info = self.header.bet_table_pos.and_then(|pos| {
            if pos == 0 {
                return None;
            }

            // For v4, use the size from v4 data
            let mut compressed_size = self.header.v4_data.as_ref().map(|v4| v4.bet_table_size_64);

            // For v3 without v4 data, try to determine the size
            if compressed_size.is_none() && self.header.format_version == header::FormatVersion::V3
            {
                // Make a copy of the reader to avoid interfering with the main archive
                if let Ok(temp_reader) =
                    std::fs::File::open(&self.path).map(std::io::BufReader::new)
                {
                    let mut temp_archive = Self {
                        path: self.path.clone(),
                        reader: temp_reader,
                        archive_offset: self.archive_offset,
                        user_data: self.user_data.clone(),
                        header: self.header.clone(),
                        hash_table: None,
                        block_table: None,
                        hi_block_table: None,
                        het_table: None,
                        bet_table: None,
                        attributes: None,
                    };

                    if let Ok(size) = temp_archive.read_bet_table_size(pos) {
                        compressed_size = Some(size);
                    }
                }
            }

            Some(TableInfo {
                size: self.bet_table.as_ref().map(|bet| bet.header.file_count),
                offset: pos,
                compressed_size,
                failed_to_load: self.bet_table.is_none(),
            })
        });

        let hi_block_table_info = self.header.hi_block_table_pos.and_then(|pos| {
            if pos == 0 {
                return None;
            }

            Some(TableInfo {
                size: if self.hi_block_table.is_some() {
                    Some(self.header.block_table_size)
                } else {
                    None
                },
                offset: pos,
                compressed_size: self
                    .header
                    .v4_data
                    .as_ref()
                    .map(|v4| v4.hi_block_table_size_64),
                failed_to_load: self.hi_block_table.is_none(),
            })
        });

        let user_data_info = self.user_data.as_ref().map(|ud| UserDataInfo {
            header_size: ud.user_data_header_size,
            data_size: ud.user_data_size,
        });

        // MD5 verification for v4 archives
        let md5_status = if self.header.v4_data.is_some() {
            self.validate_v4_md5_checksums()?
        } else {
            None
        };

        Ok(ArchiveInfo {
            path: self.path.clone(),
            file_size,
            archive_offset: self.archive_offset,
            format_version: self.header.format_version,
            file_count,
            max_file_count,
            sector_size: self.header.sector_size(),
            is_encrypted,
            has_signature,
            signature_status,
            hash_table_info,
            block_table_info,
            het_table_info,
            bet_table_info,
            hi_block_table_info,
            has_attributes,
            has_listfile,
            user_data_info,
            md5_status,
        })
    }

    /// Get the hash table
    pub fn hash_table(&self) -> Option<&HashTable> {
        self.hash_table.as_ref()
    }

    /// Get the block table
    pub fn block_table(&self) -> Option<&BlockTable> {
        self.block_table.as_ref()
    }

    /// Get HET table reference
    pub fn het_table(&self) -> Option<&HetTable> {
        self.het_table.as_ref()
    }

    /// Get BET table reference
    pub fn bet_table(&self) -> Option<&BetTable> {
        self.bet_table.as_ref()
    }

    /// Find a file in the archive
    pub fn find_file(&self, filename: &str) -> Result<Option<FileInfo>> {
        // Check if this is a special file that should be searched in both table types
        let is_special_file = matches!(
            filename,
            "(listfile)" | "(attributes)" | "(signature)" | "(patch_metadata)"
        );

        // For v3+ archives, prioritize HET/BET tables if they exist and are valid
        if let (Some(het), Some(bet)) = (&self.het_table, &self.bet_table) {
            // Check if tables have actual entries
            if het.header.max_file_count > 0 && bet.header.file_count > 0 {
                let (_file_index_opt, collision_candidates) =
                    het.find_file_with_collision_info(filename);

                // HET uses 8-bit hashes which naturally have many collisions.
                // We must verify each candidate against the full BET hash to find the correct file.
                if !collision_candidates.is_empty() {
                    if collision_candidates.len() > 1 {
                        log::debug!(
                            "HET: '{}' has {} collision candidates, verifying against BET hashes",
                            filename,
                            collision_candidates.len()
                        );
                    }

                    // Check each collision candidate against BET hash
                    for &candidate_index in &collision_candidates {
                        // Verify this candidate has the correct BET hash
                        if bet.verify_file_hash(candidate_index, filename) {
                            // Found the correct file - return its info
                            if let Some(bet_info) = bet.get_file_info(candidate_index) {
                                log::debug!(
                                    "HET/BET: Found '{}' at file_index={} (verified by BET hash)",
                                    filename,
                                    candidate_index
                                );
                                return Ok(Some(FileInfo {
                                    filename: filename.to_string(),
                                    hash_index: 0, // Not applicable for HET/BET
                                    block_index: candidate_index as usize,
                                    file_pos: self.archive_offset + bet_info.file_pos,
                                    compressed_size: bet_info.compressed_size,
                                    file_size: bet_info.file_size,
                                    flags: bet_info.flags,
                                    locale: 0, // HET/BET don't store locale separately
                                }));
                            }
                        }
                    }

                    // No candidate matched - file not found in HET/BET
                    log::debug!(
                        "HET/BET: '{}' not found - {} candidates checked, none matched BET hash",
                        filename,
                        collision_candidates.len()
                    );
                }

                // For special files, always check hash/block tables as fallback
                // For regular files, only fall back if hash tables exist
                if !is_special_file && (self.hash_table.is_none() || self.block_table.is_none()) {
                    return Ok(None);
                }
            }
        }

        // Fall back to traditional hash/block tables if:
        // 1. HET/BET tables don't exist
        // 2. HET/BET tables are empty/invalid
        // 3. File wasn't found in HET/BET but we're looking for a special file
        // 4. File wasn't found in HET/BET but hash/block tables exist
        self.find_file_classic(filename)
    }

    /// Classic file lookup using hash/block tables
    fn find_file_classic(&self, filename: &str) -> Result<Option<FileInfo>> {
        // If tables aren't loaded, return None instead of error
        // This is common for V3+ archives that only have HET/BET tables
        let hash_table = match self.hash_table.as_ref() {
            Some(table) => table,
            None => return Ok(None),
        };
        let block_table = match self.block_table.as_ref() {
            Some(table) => table,
            None => return Ok(None),
        };

        // Try to find the file with default locale
        if let Some((hash_index, hash_entry)) = hash_table.find_file(filename, 0) {
            let block_entry = block_table
                .get(hash_entry.block_index as usize)
                .ok_or_else(|| Error::block_table("Invalid block index"))?;

            // Calculate full file position for v2+ archives
            let file_pos = if let Some(hi_block) = &self.hi_block_table {
                let high_bits = hi_block.get_file_pos_high(hash_entry.block_index as usize);
                (high_bits << 32) | (block_entry.file_pos as u64)
            } else {
                block_entry.file_pos as u64
            };

            Ok(Some(FileInfo {
                filename: filename.to_string(),
                hash_index,
                block_index: hash_entry.block_index as usize,
                file_pos: self.archive_offset + file_pos,
                compressed_size: block_entry.compressed_size as u64,
                file_size: block_entry.file_size as u64,
                flags: block_entry.flags,
                locale: hash_entry.locale,
            }))
        } else {
            Ok(None)
        }
    }

    /// List files in the archive
    pub fn list(&mut self) -> Result<Vec<FileEntry>> {
        // Try to find and read (listfile)
        if let Some(_listfile_info) = self.find_file("(listfile)")? {
            // Try to read the listfile
            match self.read_file("(listfile)") {
                Ok(listfile_data) => {
                    // Parse the listfile
                    match special_files::parse_listfile(&listfile_data) {
                        Ok(filenames) => {
                            let mut entries = Vec::new();

                            // Look up each file
                            for filename in filenames {
                                if let Some(file_info) = self.find_file(&filename)? {
                                    entries.push(FileEntry {
                                        name: filename,
                                        size: file_info.file_size,
                                        compressed_size: file_info.compressed_size,
                                        flags: file_info.flags,
                                        hashes: None,
                                        table_indices: Some((
                                            file_info.hash_index,
                                            Some(file_info.block_index),
                                        )),
                                    });
                                } else {
                                    // File is in listfile but not found in archive
                                    log::warn!(
                                        "File '{filename}' listed in (listfile) but not found in archive"
                                    );
                                }
                            }

                            return Ok(entries);
                        }
                        Err(e) => {
                            log::warn!(
                                "Failed to parse (listfile): {e}. Falling back to anonymous enumeration."
                            );
                        }
                    }
                }
                Err(e) => {
                    log::warn!(
                        "Failed to read (listfile): {e}. Falling back to anonymous enumeration."
                    );
                }
            }
        }

        // No listfile or failed to read/parse it, we'll need to enumerate entries without names
        log::info!("Enumerating anonymous entries");

        let mut entries = Vec::new();

        // For v3+ archives, prioritize HET/BET tables if they exist and are valid
        if let (Some(het), Some(bet)) = (&self.het_table, &self.bet_table)
            && het.header.max_file_count > 0
            && bet.header.file_count > 0
        {
            log::info!("Enumerating files using HET/BET tables");

            // Enumerate using BET table
            for i in 0..bet.header.file_count {
                if let Some(bet_info) = bet.get_file_info(i) {
                    // Only include files that actually exist
                    if bet_info.flags & crate::tables::BlockEntry::FLAG_EXISTS != 0 {
                        entries.push(FileEntry {
                            name: format!("file_{i:08}.dat"), // Unknown name with file index
                            size: bet_info.file_size,
                            compressed_size: bet_info.compressed_size,
                            flags: bet_info.flags,
                            hashes: None,
                            table_indices: Some((i as usize, None)), // file_index for HET/BET tables
                        });
                    }
                }
            }

            // If we enumerated from HET/BET successfully, return early
            if !entries.is_empty() {
                return Ok(entries);
            }
        }

        // Fall back to classic hash/block tables
        let hash_table = self
            .hash_table
            .as_ref()
            .ok_or_else(|| Error::invalid_format("No tables loaded for enumeration"))?;
        let block_table = self
            .block_table
            .as_ref()
            .ok_or_else(|| Error::invalid_format("No block table loaded"))?;

        log::info!("Enumerating files using hash/block tables");

        // Scan hash table for valid entries
        for (i, hash_entry) in hash_table.entries().iter().enumerate() {
            if hash_entry.is_valid()
                && let Some(block_entry) = block_table.get(hash_entry.block_index as usize)
                && block_entry.exists()
            {
                entries.push(FileEntry {
                    name: format!("file_{i:08}.dat"), // Unknown name with hash index
                    size: block_entry.file_size as u64,
                    compressed_size: block_entry.compressed_size as u64,
                    flags: block_entry.flags,
                    hashes: None,
                    table_indices: Some((i, Some(hash_entry.block_index as usize))), // hash_index, block_index
                });
            }
        }

        Ok(entries)
    }

    /// List all files in the archive by enumerating tables
    /// This shows all entries, using generic names for files not in listfile
    pub fn list_all(&mut self) -> Result<Vec<FileEntry>> {
        let mut entries = Vec::new();

        // For v3+ archives, prioritize HET/BET tables if they exist and are valid
        if let (Some(het), Some(bet)) = (&self.het_table, &self.bet_table)
            && het.header.max_file_count > 0
            && bet.header.file_count > 0
        {
            log::info!("Enumerating all files using HET/BET tables");

            // Enumerate using BET table
            for i in 0..bet.header.file_count {
                if let Some(bet_info) = bet.get_file_info(i) {
                    // Only include files that actually exist
                    if bet_info.flags & crate::tables::BlockEntry::FLAG_EXISTS != 0 {
                        entries.push(FileEntry {
                            name: format!("file_{i:08}.dat"), // Unknown name with file index
                            size: bet_info.file_size,
                            compressed_size: bet_info.compressed_size,
                            flags: bet_info.flags,
                            hashes: None,
                            table_indices: Some((i as usize, None)), // file_index for HET/BET tables
                        });
                    }
                }
            }

            // If we enumerated from HET/BET successfully, return early
            if !entries.is_empty() {
                return Ok(entries);
            }
        }

        // Fall back to classic hash/block tables
        let hash_table = self
            .hash_table
            .as_ref()
            .ok_or_else(|| Error::invalid_format("No tables loaded for enumeration"))?;
        let block_table = self
            .block_table
            .as_ref()
            .ok_or_else(|| Error::invalid_format("No block table loaded"))?;

        log::info!("Enumerating all files using hash/block tables");

        // Enumerate all hash table entries
        let mut block_indices_seen = std::collections::HashSet::new();

        for hash_entry in hash_table.entries().iter() {
            if hash_entry.is_valid() {
                let block_index = hash_entry.block_index as usize;

                // Skip if we've already seen this block index (collision chain)
                if !block_indices_seen.insert(block_index) {
                    continue;
                }

                if let Some(block_entry) = block_table.get(block_index)
                    && block_entry.exists()
                {
                    entries.push(FileEntry {
                        name: format!("file_{block_index:08}.dat"),
                        size: block_entry.file_size as u64,
                        compressed_size: block_entry.compressed_size as u64,
                        flags: block_entry.flags,
                        hashes: None,
                        table_indices: Some((0, Some(block_index))), // Use 0 for hash_index since we don't track it here
                    });
                }
            }
        }

        // Sort by block index (which is embedded in the generated names)
        entries.sort_by(|a, b| a.name.cmp(&b.name));

        Ok(entries)
    }

    /// List files in the archive with hash information
    pub fn list_with_hashes(&mut self) -> Result<Vec<FileEntry>> {
        let mut entries = self.list()?;

        // Calculate hashes for each entry
        for entry in &mut entries {
            let hash1 = crate::crypto::hash_string(&entry.name, crate::crypto::hash_type::NAME_A);
            let hash2 = crate::crypto::hash_string(&entry.name, crate::crypto::hash_type::NAME_B);
            entry.hashes = Some((hash1, hash2));
        }

        Ok(entries)
    }

    /// List all files in the archive by enumerating tables with hash information
    pub fn list_all_with_hashes(&mut self) -> Result<Vec<FileEntry>> {
        let mut entries = Vec::new();

        // For v3+ archives, use HET/BET tables
        if let (Some(het), Some(bet)) = (&self.het_table, &self.bet_table)
            && het.header.max_file_count > 0
            && bet.header.file_count > 0
        {
            log::info!("Enumerating all files using HET/BET tables with hashes");

            // Enumerate using BET table
            for i in 0..bet.header.file_count {
                if let Some(bet_info) = bet.get_file_info(i)
                    && bet_info.flags & crate::tables::BlockEntry::FLAG_EXISTS != 0
                {
                    entries.push(FileEntry {
                        name: format!("file_{i:08}.dat"),
                        size: bet_info.file_size,
                        compressed_size: bet_info.compressed_size,
                        flags: bet_info.flags,
                        hashes: None, // HET/BET doesn't expose name hashes directly
                        table_indices: Some((i as usize, None)), // file_index for HET/BET tables
                    });
                }
            }

            if !entries.is_empty() {
                return Ok(entries);
            }
        }

        // Fall back to classic hash/block tables
        let hash_table = self
            .hash_table
            .as_ref()
            .ok_or_else(|| Error::invalid_format("No tables loaded for enumeration"))?;
        let block_table = self
            .block_table
            .as_ref()
            .ok_or_else(|| Error::invalid_format("No block table loaded"))?;

        log::info!("Enumerating all files using hash/block tables with hashes");

        // Enumerate all hash table entries - here we can get the actual hashes!
        let mut block_indices_seen = std::collections::HashSet::new();

        for hash_entry in hash_table.entries().iter() {
            if hash_entry.is_valid() {
                let block_index = hash_entry.block_index as usize;

                if !block_indices_seen.insert(block_index) {
                    continue;
                }

                if let Some(block_entry) = block_table.get(block_index)
                    && block_entry.exists()
                {
                    entries.push(FileEntry {
                        name: format!("file_{block_index:08}.dat"),
                        size: block_entry.file_size as u64,
                        compressed_size: block_entry.compressed_size as u64,
                        flags: block_entry.flags,
                        hashes: Some((hash_entry.name_1, hash_entry.name_2)),
                        table_indices: Some((0, Some(block_index))), // Use 0 for hash_index since we don't track it here
                    });
                }
            }
        }

        // Sort by block index
        entries.sort_by(|a, b| a.name.cmp(&b.name));

        Ok(entries)
    }

    /// Read a file from the archive
    pub fn read_file(&mut self, name: &str) -> Result<Vec<u8>> {
        let file_info = self
            .find_file(name)?
            .ok_or_else(|| Error::FileNotFound(name.to_string()))?;

        // Check if this is a patch file - patch files cannot be read directly
        if file_info.is_patch_file() {
            return Err(Error::OperationNotSupported {
                version: self.header.format_version as u16,
                operation: format!(
                    "Reading patch file '{name}' directly. Patch files contain binary patches that must be applied to base files."
                ),
            });
        }

        // For v3+ archives with HET/BET tables, we already have all the info we need in FileInfo
        // For classic archives, we need to get additional info from the block table
        let (file_size_for_key, actual_file_size) =
            if self.het_table.is_some() && self.bet_table.is_some() {
                // Using HET/BET tables - FileInfo already has all the data
                (file_info.file_size as u32, file_info.file_size)
            } else {
                // Using classic tables - need block entry for accurate sizes
                let block_table = self
                    .block_table
                    .as_ref()
                    .ok_or_else(|| Error::invalid_format("Block table not loaded"))?;
                let block_entry = block_table
                    .get(file_info.block_index)
                    .ok_or_else(|| Error::block_table("Invalid block index"))?;
                (block_entry.file_size, block_entry.file_size as u64)
            };

        // Calculate encryption key if needed
        let key = if file_info.is_encrypted() {
            let base_key = hash_string(name, hash_type::FILE_KEY);
            if file_info.has_fix_key() {
                // Apply FIX_KEY modification
                let file_pos = (file_info.file_pos - self.archive_offset) as u32;
                (base_key.wrapping_add(file_pos)) ^ file_size_for_key
            } else {
                base_key
            }
        } else {
            0
        };

        // Read the file data
        self.reader.seek(SeekFrom::Start(file_info.file_pos))?;

        if file_info.is_single_unit() || !file_info.is_compressed() {
            // Single unit or uncompressed file - read directly
            let mut data = vec![0u8; file_info.compressed_size as usize];
            self.reader.read_exact(&mut data)?;

            // Decrypt if needed
            if file_info.is_encrypted() {
                log::debug!(
                    "Decrypting file data: key=0x{:08X}, size={}",
                    key,
                    data.len()
                );
                if data.len() <= 64 {
                    log::debug!("Before decrypt: {:02X?}", &data);
                }
                decrypt_file_data(&mut data, key);
                if data.len() <= 64 {
                    log::debug!("After decrypt: {:02X?}", &data);
                }
            }

            // Validate CRC if present for single unit files
            if file_info.has_sector_crc() && file_info.is_single_unit() {
                // For single unit files, there's one CRC after the data
                let mut crc_bytes = [0u8; 4];
                self.reader.read_exact(&mut crc_bytes)?;
                let expected_crc = u32::from_le_bytes(crc_bytes);

                // CRC is calculated on the decompressed data
                let data_to_check = if file_info.is_compressed() {
                    // We need to decompress first to check CRC
                    let compression_type = data[0];
                    let compressed_data = &data[1..];
                    compression::decompress(
                        compressed_data,
                        compression_type,
                        actual_file_size as usize,
                    )?
                } else {
                    data.clone()
                };

                // MPQ uses ADLER32 for sector checksums, not CRC32 despite the name
                let actual_crc = adler2::adler32_slice(&data_to_check);
                if actual_crc != expected_crc {
                    return Err(Error::ChecksumMismatch {
                        file: name.to_string(),
                        expected: expected_crc,
                        actual: actual_crc,
                    });
                }

                log::debug!("Single unit file CRC validated: 0x{actual_crc:08X}");
            }

            // Decompress if needed
            if file_info.is_compressed() {
                if file_info.is_single_unit() {
                    // SINGLE_UNIT files: Get compression method from block table flags
                    // NO compression type byte prefix in the data

                    // Special case: If compressed_size == file_size, the file might be stored uncompressed
                    // despite having the COMPRESS flag set
                    if data.len() == actual_file_size as usize {
                        log::debug!(
                            "SINGLE_UNIT file has equal compressed/uncompressed size ({} bytes), trying uncompressed first",
                            data.len()
                        );

                        // Try treating as uncompressed data first
                        // This handles cases where the COMPRESS flag is set but data is actually uncompressed
                        Ok(data)
                    } else if let Some(compression_method) = file_info.get_compression_method() {
                        // SINGLE_UNIT files DO have compression method byte prefix!
                        // This was our bug - we thought they didn't
                        if !data.is_empty() {
                            let actual_compression_method = data[0];
                            let compressed_data = &data[1..];

                            log::debug!(
                                "Decompressing SINGLE_UNIT file: method_from_flags=0x{:02X}, actual_method_byte=0x{:02X}, compressed_size={}, expected_size={}",
                                compression_method,
                                actual_compression_method,
                                compressed_data.len(),
                                actual_file_size
                            );

                            // Use the actual compression method from the data, not from flags
                            // This ensures we handle multi-compression correctly
                            compression::decompress(
                                compressed_data,
                                actual_compression_method,
                                actual_file_size as usize,
                            )
                        } else {
                            Err(Error::compression("Empty compressed data"))
                        }
                    } else {
                        Err(Error::compression(
                            "Could not determine compression method from flags",
                        ))
                    }
                } else {
                    // SECTORED files: Should not reach here for single-unit code path
                    // This will be handled in read_sectored_file()
                    log::warn!("Non-single-unit compressed file in single-unit code path");
                    Ok(data)
                }
            } else {
                // For encrypted files, trim to original file size to remove padding
                if file_info.is_encrypted() && data.len() > actual_file_size as usize {
                    data.truncate(actual_file_size as usize);
                }
                Ok(data)
            }
        } else {
            // Multi-sector compressed file
            self.read_sectored_file(&file_info, key)
        }
    }

    /// Read raw patch file data
    ///
    /// This method reads patch files (files with MPQ_FILE_PATCH_FILE flag) without
    /// rejecting them. It returns the raw PTCH format data that can be parsed and
    /// applied to base files.
    ///
    /// This is used internally by PatchChain to read patch files for application.
    pub(crate) fn read_patch_file_raw(&mut self, name: &str) -> Result<Vec<u8>> {
        let file_info = self
            .find_file(name)?
            .ok_or_else(|| Error::FileNotFound(name.to_string()))?;

        // Verify this is actually a patch file
        if !file_info.is_patch_file() {
            return Err(Error::invalid_format(format!(
                "File '{name}' is not a patch file"
            )));
        }

        // For v3+ archives with HET/BET tables, we already have all the info we need in FileInfo
        // For classic archives, we need to get additional info from the block table
        let (file_size_for_key, _actual_file_size) =
            if self.het_table.is_some() && self.bet_table.is_some() {
                // Using HET/BET tables - FileInfo already has all the data
                (file_info.file_size as u32, file_info.file_size)
            } else {
                // Using classic tables - need block entry for accurate sizes
                let block_table = self
                    .block_table
                    .as_ref()
                    .ok_or_else(|| Error::invalid_format("Block table not loaded"))?;
                let block_entry = block_table
                    .get(file_info.block_index)
                    .ok_or_else(|| Error::block_table("Invalid block index"))?;
                (block_entry.file_size, block_entry.file_size as u64)
            };

        // Calculate encryption key if needed
        let key = if file_info.is_encrypted() {
            let base_key = hash_string(name, hash_type::FILE_KEY);
            if file_info.has_fix_key() {
                // Apply FIX_KEY modification
                let file_pos = (file_info.file_pos - self.archive_offset) as u32;
                (base_key.wrapping_add(file_pos)) ^ file_size_for_key
            } else {
                base_key
            }
        } else {
            0
        };

        // Read the file data
        // Patch files start with TPatchInfo structure (uncompressed metadata)
        self.reader.seek(SeekFrom::Start(file_info.file_pos))?;

        // Read TPatchInfo header (28 bytes minimum)
        let mut patch_info_buf = [0u8; 28];
        self.reader.read_exact(&mut patch_info_buf)?;

        let patch_info_length = u32::from_le_bytes([
            patch_info_buf[0],
            patch_info_buf[1],
            patch_info_buf[2],
            patch_info_buf[3],
        ]);
        let patch_info_flags = u32::from_le_bytes([
            patch_info_buf[4],
            patch_info_buf[5],
            patch_info_buf[6],
            patch_info_buf[7],
        ]);
        let patch_data_size = u32::from_le_bytes([
            patch_info_buf[8],
            patch_info_buf[9],
            patch_info_buf[10],
            patch_info_buf[11],
        ]);

        log::debug!(
            "TPatchInfo: length={}, flags=0x{:08X}, data_size={} bytes",
            patch_info_length,
            patch_info_flags,
            patch_data_size
        );

        // The actual decompressed size is patch_data_size, not file_info.file_size!
        let actual_patch_size = patch_data_size as usize;

        // After TPatchInfo, check if file is sectored or single-unit
        // Patch files can be sectored despite lacking the SINGLE_UNIT flag
        let is_single_unit = file_info.is_single_unit();

        if is_single_unit {
            log::debug!("Patch file is stored as single unit");
            let compressed_data_size =
                file_info.compressed_size as usize - patch_info_length as usize;

            let mut data = vec![0u8; compressed_data_size];
            self.reader.read_exact(&mut data)?;

            log::debug!(
                "Read {} bytes of compressed patch data (single unit)",
                data.len()
            );
            log::debug!("First 32 bytes: {:02X?}", &data[..32.min(data.len())]);

            // Decrypt if needed (though patch files are typically not encrypted)
            if file_info.is_encrypted() {
                log::debug!(
                    "Decrypting patch file data: key=0x{:08X}, size={}",
                    key,
                    data.len()
                );
                decrypt_file_data(&mut data, key);
            }

            // Decompress if needed
            if file_info.is_compressed() {
                let compression_type = data[0];
                let compressed_data = &data[1..];

                log::debug!(
                    "Decompressing patch file (single unit): method=0x{:02X}, compressed={} bytes → {} bytes",
                    compression_type,
                    compressed_data.len(),
                    actual_patch_size
                );

                compression::decompress(compressed_data, compression_type, actual_patch_size)
            } else {
                Ok(data)
            }
        } else {
            // Sectored patch file - read using sector table
            log::debug!("Patch file is sectored, reading with modified sector handling");

            // Calculate sector count based on patch_data_size, not file_size
            let sector_size = self.header.sector_size();
            let sector_count = (patch_data_size as usize).div_ceil(sector_size);

            log::debug!(
                "Patch sectors: data_size={}, sector_size={}, sector_count={}",
                patch_data_size,
                sector_size,
                sector_count
            );

            // Read sector offset table
            let offset_table_size = (sector_count + 1) * 4;
            let mut offset_data = vec![0u8; offset_table_size];
            self.reader.read_exact(&mut offset_data)?;

            log::debug!(
                "Read sector offset table: {} bytes for {} sectors",
                offset_table_size,
                sector_count
            );

            // Parse sector offsets
            let mut sector_offsets = Vec::with_capacity(sector_count + 1);
            let mut cursor = std::io::Cursor::new(&offset_data);
            for _ in 0..=sector_count {
                sector_offsets.push(cursor.read_u32::<LittleEndian>()?);
            }

            log::debug!("Sector offsets: {:?}", &sector_offsets);

            // Read and decompress each sector
            let mut decompressed_data = Vec::with_capacity(patch_data_size as usize);

            for i in 0..sector_count {
                let sector_start = sector_offsets[i] as usize;
                let sector_end = sector_offsets[i + 1] as usize;
                let sector_compressed_size = sector_end - sector_start;

                log::debug!(
                    "Reading sector {}: offset={}, size={} bytes",
                    i,
                    sector_start,
                    sector_compressed_size
                );

                // Sector offsets are relative to the START of the offset table, NOT after it
                // So we need to seek to: file_pos + TPatchInfo + sector_offset
                let sector_file_pos =
                    file_info.file_pos + patch_info_length as u64 + sector_start as u64;

                self.reader.seek(SeekFrom::Start(sector_file_pos))?;

                let mut sector_data = vec![0u8; sector_compressed_size];
                self.reader.read_exact(&mut sector_data)?;

                log::debug!(
                    "Sector {} data first 16 bytes: {:02X?}",
                    i,
                    &sector_data[..16.min(sector_data.len())]
                );

                // Patch file sectors use standard MPQ compression (Zlib/BZip2/etc)
                // First byte indicates compression method, remaining bytes are compressed PTCH data
                let compression_method = sector_data[0];
                log::debug!(
                    "Decompressing sector {} with method 0x{:02X} ({} bytes compressed)",
                    i,
                    compression_method,
                    sector_data.len() - 1
                );

                // Decompress using standard MPQ decompression
                let expected_size =
                    sector_size.min(patch_data_size as usize - decompressed_data.len());
                let sector_decompressed = compression::decompress(
                    &sector_data[1..], // Skip compression method byte
                    compression_method,
                    expected_size,
                )?;

                log::debug!(
                    "Sector {} decompressed to {} bytes",
                    i,
                    sector_decompressed.len()
                );

                decompressed_data.extend_from_slice(&sector_decompressed);
            }

            log::debug!(
                "Successfully decompressed {} bytes from {} sectors",
                decompressed_data.len(),
                sector_count
            );

            Ok(decompressed_data)
        }
    }

    /// Read a file by table indices (for files with generic names)
    pub fn read_file_by_indices(
        &mut self,
        hash_index: usize,
        block_index: Option<usize>,
    ) -> Result<Vec<u8>> {
        let file_info = if let Some(block_idx) = block_index {
            // Classic hash/block table access
            let hash_table = self
                .hash_table
                .as_ref()
                .ok_or_else(|| Error::invalid_format("Hash table not loaded"))?;
            let block_table = self
                .block_table
                .as_ref()
                .ok_or_else(|| Error::invalid_format("Block table not loaded"))?;

            let hash_entry = hash_table
                .entries()
                .get(hash_index)
                .ok_or_else(|| Error::hash_table("Invalid hash index"))?;
            let block_entry = block_table
                .get(block_idx)
                .ok_or_else(|| Error::block_table("Invalid block index"))?;

            // Calculate full file position for v2+ archives
            let file_pos = if let Some(hi_block) = &self.hi_block_table {
                let high_bits = hi_block.get_file_pos_high(block_idx);
                (high_bits << 32) | (block_entry.file_pos as u64)
            } else {
                block_entry.file_pos as u64
            };

            FileInfo {
                filename: format!("file_{hash_index:08}.dat"),
                hash_index,
                block_index: block_idx,
                file_pos: self.archive_offset + file_pos,
                compressed_size: block_entry.compressed_size as u64,
                file_size: block_entry.file_size as u64,
                flags: block_entry.flags,
                locale: hash_entry.locale,
            }
        } else {
            // HET/BET table access (file_index is in hash_index parameter)
            let bet = self
                .bet_table
                .as_ref()
                .ok_or_else(|| Error::invalid_format("BET table not loaded"))?;

            let bet_info = bet
                .get_file_info(hash_index as u32)
                .ok_or_else(|| Error::invalid_format("Invalid file index"))?;

            // For HET/BET files, the file position is calculated differently
            let file_pos = self.archive_offset + bet_info.file_pos;

            FileInfo {
                filename: format!("file_{hash_index:08}.dat"),
                hash_index: 0,  // Not meaningful for HET/BET
                block_index: 0, // Not meaningful for HET/BET
                file_pos,
                compressed_size: bet_info.compressed_size,
                file_size: bet_info.file_size,
                flags: bet_info.flags,
                locale: 0, // Not applicable for HET/BET
            }
        };

        // Check if this is a patch file - patch files cannot be read directly
        if file_info.is_patch_file() {
            return Err(Error::OperationNotSupported {
                version: self.header.format_version as u16,
                operation: format!(
                    "Reading patch file '{}' directly. Patch files contain binary patches that must be applied to base files.",
                    file_info.filename
                ),
            });
        }

        // Now use the existing file reading logic
        // For encrypted files, we need a key. Since we don't have the real filename,
        // we'll use a default key based on the table index
        let key = if file_info.is_encrypted() {
            // Use a generic key calculation for anonymous files
            hash_string(&file_info.filename, hash_type::FILE_KEY)
        } else {
            0
        };

        // Continue with normal file reading logic based on whether it's sectored
        let (file_size_for_key, actual_file_size) =
            if self.het_table.is_some() && self.bet_table.is_some() {
                // Using HET/BET tables - FileInfo already has all the data
                (file_info.file_size as u32, file_info.file_size)
            } else {
                // Using classic tables - need block entry for accurate sizes
                let block_table = self
                    .block_table
                    .as_ref()
                    .ok_or_else(|| Error::invalid_format("Block table not loaded"))?;
                let block_entry = block_table
                    .get(file_info.block_index)
                    .ok_or_else(|| Error::block_table("Invalid block index"))?;
                (block_entry.file_size, block_entry.file_size as u64)
            };

        // Adjust key for file size if needed
        let key = if file_info.is_encrypted() && file_info.has_fix_key() {
            key.wrapping_add(file_size_for_key)
        } else {
            key
        };

        // Read the file data
        self.reader.seek(SeekFrom::Start(file_info.file_pos))?;

        if file_info.is_single_unit() || !file_info.is_compressed() {
            // Single unit or uncompressed file - read directly
            let mut data = vec![0u8; file_info.compressed_size as usize];
            self.reader.read_exact(&mut data)?;

            // Decrypt if needed
            if file_info.is_encrypted() {
                log::debug!(
                    "Decrypting file data: key=0x{:08X}, size={}",
                    key,
                    data.len()
                );
                decrypt_file_data(&mut data, key);
            }

            // Handle compression for single unit files
            if file_info.is_compressed() {
                if data.is_empty() {
                    return Err(Error::compression("File data is empty"));
                }

                // Check if this is IMPLODE compression (no compression type prefix)
                if file_info.is_implode() {
                    log::debug!(
                        "Decompressing single unit IMPLODE file: input_size={}, target_size={}",
                        data.len(),
                        actual_file_size
                    );
                    compression::decompress(&data, 0x08, actual_file_size as usize)
                } else {
                    // COMPRESS flag - has compression type byte prefix
                    let compression_type = data[0];
                    let compressed_data = &data[1..];

                    log::debug!(
                        "Decompressing single unit file: method=0x{:02X}, input_size={}, target_size={}, first bytes: {:02X?}",
                        compression_type,
                        compressed_data.len(),
                        actual_file_size,
                        &compressed_data[..compressed_data.len().min(16)]
                    );

                    compression::decompress(
                        compressed_data,
                        compression_type,
                        actual_file_size as usize,
                    )
                }
            } else {
                Ok(data)
            }
        } else {
            // Multi-sector compressed file
            self.read_sectored_file(&file_info, key)
        }
    }

    /// Read a file that is split into sectors
    fn read_sectored_file(&mut self, file_info: &FileInfo, key: u32) -> Result<Vec<u8>> {
        let sector_size = self.header.sector_size();
        let sector_count = (file_info.file_size as usize).div_ceil(sector_size);

        log::debug!("Reading sectored file:");
        log::debug!("  file_size: {} bytes", file_info.file_size);
        log::debug!("  compressed_size: {} bytes", file_info.compressed_size);
        log::debug!("  sector_size: {} bytes", sector_size);
        log::debug!("  sector_count: {}", sector_count);
        log::debug!("  is_patch_file: {}", file_info.is_patch_file());

        // Read sector offset table
        self.reader.seek(SeekFrom::Start(file_info.file_pos))?;
        let offset_table_size = (sector_count + 1) * 4;
        log::debug!("  offset_table_size: {} bytes", offset_table_size);
        log::debug!(
            "  Attempting to read offset table at position 0x{:X}",
            file_info.file_pos
        );

        let mut offset_data = vec![0u8; offset_table_size];
        self.reader.read_exact(&mut offset_data).map_err(|e| {
            log::error!("Failed to read offset table: {}", e);
            log::error!(
                "  Tried to read {} bytes at position 0x{:X}",
                offset_table_size,
                file_info.file_pos
            );
            e
        })?;

        // Decrypt sector offset table if needed
        if file_info.is_encrypted() {
            let offset_key = key.wrapping_sub(1);
            decrypt_file_data(&mut offset_data, offset_key);
        }

        // Parse sector offsets
        let mut sector_offsets = Vec::with_capacity(sector_count + 1);
        let mut cursor = std::io::Cursor::new(&offset_data);
        for _ in 0..=sector_count {
            sector_offsets.push(cursor.read_u32::<LittleEndian>()?);
        }

        log::debug!(
            "Sector offsets: first={}, last={}",
            sector_offsets.first().copied().unwrap_or(0),
            sector_offsets.last().copied().unwrap_or(0)
        );

        // Check if we have sector CRCs
        let mut sector_crcs = None;
        if file_info.has_sector_crc() {
            // The first sector offset tells us where the data starts
            // If it's large enough to accommodate a CRC table, then CRCs are present
            let first_data_offset = sector_offsets[0] as usize;
            let expected_crc_table_start = offset_table_size;
            let expected_crc_table_size = sector_count * 4;

            if first_data_offset >= expected_crc_table_start + expected_crc_table_size {
                // CRC table follows the offset table
                let mut crc_data = vec![0u8; expected_crc_table_size];
                self.reader.read_exact(&mut crc_data)?;

                // CRC table may be encrypted if the file is encrypted
                // According to MPQ format, CRC table uses the same key as the offset table but offset by sector count
                if file_info.is_encrypted() {
                    let crc_key = key.wrapping_sub(1).wrapping_add(sector_count as u32);
                    decrypt_file_data(&mut crc_data, crc_key);
                }

                let mut crcs = Vec::with_capacity(sector_count);
                let mut cursor = std::io::Cursor::new(&crc_data);
                for _ in 0..sector_count {
                    crcs.push(cursor.read_u32::<LittleEndian>()?);
                }

                // Log before moving
                log::debug!(
                    "Read {} sector CRCs, first few: {:?}",
                    sector_count,
                    &crcs[..5.min(crcs.len())]
                );

                sector_crcs = Some(crcs);
            } else {
                log::debug!(
                    "File has SECTOR_CRC flag but insufficient space for CRC table (offset_table_size={}, first_data_offset={}, needed={}). This is common in some MPQ implementations.",
                    offset_table_size,
                    first_data_offset,
                    expected_crc_table_start + expected_crc_table_size
                );
            }
        }

        // Read and decompress each sector
        let mut decompressed_data = Vec::with_capacity(file_info.file_size as usize);

        // Pre-allocate a reusable buffer for sector reading
        // Add some overhead for compression headers
        let max_sector_size = sector_size + 1024;
        let mut sector_buffer = vec![0u8; max_sector_size];

        for i in 0..sector_count {
            let sector_start = sector_offsets[i] as u64;
            let sector_end = sector_offsets[i + 1] as u64;

            if sector_end < sector_start {
                // This can happen with corrupted or malformed archives
                // Try to recover by using the expected sector size
                log::warn!(
                    "Invalid sector offsets detected: start={sector_start}, end={sector_end} for sector {i}. Attempting recovery."
                );

                // Skip this sector and continue with zeros
                let remaining = file_info.file_size as usize - decompressed_data.len();
                let expected_size = remaining.min(sector_size);
                decompressed_data.extend(vec![0u8; expected_size]);
                continue;
            }

            let sector_size_compressed = (sector_end - sector_start) as usize;

            // Calculate expected decompressed size for this sector
            let remaining = file_info.file_size as usize - decompressed_data.len();
            let expected_size = remaining.min(sector_size);

            // Seek to sector data - offsets are absolute from file position
            self.reader
                .seek(SeekFrom::Start(file_info.file_pos + sector_start))?;

            // Ensure our buffer is large enough
            if sector_size_compressed > sector_buffer.len() {
                sector_buffer.resize(sector_size_compressed, 0);
            }

            // Read sector data into the reusable buffer
            let sector_data = &mut sector_buffer[..sector_size_compressed];
            self.reader.read_exact(sector_data)?;

            if i == 0 {
                log::debug!(
                    "First sector: offset={}, size={}, first 16 bytes: {:02X?}",
                    sector_start,
                    sector_size_compressed,
                    &sector_data[..16.min(sector_data.len())]
                );
            }

            // Decrypt sector if needed
            if file_info.is_encrypted() {
                let sector_key = key.wrapping_add(i as u32);
                decrypt_file_data(sector_data, sector_key);
            }

            // Validate CRC if present - MUST be done AFTER decryption but BEFORE decompression
            // Skip CRC validation for now due to decryption key issues in some archives
            if let Some(ref _crcs) = sector_crcs {
                // Temporarily disabled CRC validation
                // TODO: Fix CRC decryption key calculation for proper validation
                log::trace!("Skipping CRC validation for sector {i}");
            }

            // Decompress sector
            let decompressed_sector = if file_info.is_compressed()
                && sector_size_compressed < expected_size
            {
                if !sector_data.is_empty() {
                    // Check if this is IMPLODE compression (no compression type prefix)
                    if file_info.is_implode() {
                        // IMPLODE compression - no compression type byte prefix
                        match compression::decompress(sector_data, 0x08, expected_size) {
                            Ok(decompressed) => decompressed,
                            Err(e) => {
                                log::warn!(
                                    "Failed to decompress IMPLODE sector {i}: {e}. Using zeros."
                                );
                                vec![0u8; expected_size]
                            }
                        }
                    } else {
                        // COMPRESS flag - has compression type byte prefix
                        let compression_type = sector_data[0];
                        let compressed_data = &sector_data[1..];
                        match compression::decompress(
                            compressed_data,
                            compression_type,
                            expected_size,
                        ) {
                            Ok(decompressed) => decompressed,
                            Err(e) => {
                                log::warn!("Failed to decompress sector {i}: {e}. Using zeros.");
                                vec![0u8; expected_size]
                            }
                        }
                    }
                } else {
                    log::warn!("Empty compressed sector data for sector {i}. Using zeros.");
                    vec![0u8; expected_size]
                }
            } else {
                // Sector is not compressed
                sector_data[..expected_size.min(sector_data.len())].to_vec()
            };

            decompressed_data.extend_from_slice(&decompressed_sector);
        }

        Ok(decompressed_data)
    }

    /// Load attributes from the (attributes) file if present
    pub fn load_attributes(&mut self) -> Result<()> {
        // Check if attributes are already loaded
        if self.attributes.is_some() {
            return Ok(());
        }

        // Try to read the (attributes) file
        match self.read_file("(attributes)") {
            Ok(mut data) => {
                // Get block count for parsing
                // The attributes file should contain entries for all files in the archive,
                // including potentially itself (varies by MPQ implementation)
                let total_files = if let Some(ref block_table) = self.block_table {
                    block_table.entries().len()
                } else if let Some(ref bet_table) = self.bet_table {
                    bet_table.header.file_count as usize
                } else {
                    return Err(Error::invalid_format(
                        "No block/BET table available for attributes",
                    ));
                };

                // Determine the actual block count by checking the attributes file structure
                // We'll try the full count first, then fall back to count-1 if that fails
                let block_count = {
                    // Calculate expected size with full file count
                    let flags_from_data = if data.len() >= 8 {
                        u32::from_le_bytes([data[4], data[5], data[6], data[7]])
                    } else {
                        0
                    };

                    let mut expected_size_full = 8; // header
                    if flags_from_data & 0x01 != 0 {
                        expected_size_full += total_files * 4;
                    } // CRC32
                    if flags_from_data & 0x02 != 0 {
                        expected_size_full += total_files * 8;
                    } // FILETIME  
                    if flags_from_data & 0x04 != 0 {
                        expected_size_full += total_files * 16;
                    } // MD5
                    if flags_from_data & 0x08 != 0 {
                        expected_size_full += total_files.div_ceil(8);
                    } // PATCH_BIT

                    if data.len() == expected_size_full {
                        // Perfect match with full file count - attributes includes itself
                        log::debug!(
                            "Attributes file contains entries for all {total_files} files (including itself)"
                        );
                        total_files
                    } else {
                        // Try with count-1 (traditional behavior)
                        let count_minus_1 = total_files.saturating_sub(1);
                        let mut expected_size_minus1 = 8; // header
                        if flags_from_data & 0x01 != 0 {
                            expected_size_minus1 += count_minus_1 * 4;
                        }
                        if flags_from_data & 0x02 != 0 {
                            expected_size_minus1 += count_minus_1 * 8;
                        }
                        if flags_from_data & 0x04 != 0 {
                            expected_size_minus1 += count_minus_1 * 16;
                        }
                        if flags_from_data & 0x08 != 0 {
                            expected_size_minus1 += count_minus_1.div_ceil(8);
                        }

                        if data.len() == expected_size_minus1 {
                            log::debug!(
                                "Attributes file contains entries for {count_minus_1} files (excluding itself)"
                            );
                            count_minus_1
                        } else {
                            // Neither exact match - use full count and let the parser handle the discrepancy
                            log::debug!(
                                "Attributes file size doesn't match expected patterns, using full count {total_files} (actual: {}, expected_full: {expected_size_full}, expected_minus1: {expected_size_minus1})",
                                data.len()
                            );
                            total_files
                        }
                    }
                };

                // Check if attributes data needs additional decompression
                // Some MPQ files have doubly-compressed attributes
                if data.len() >= 4 {
                    let first_dword = u32::from_le_bytes([data[0], data[1], data[2], data[3]]);

                    // Check if this looks like compressed data instead of version 100
                    if first_dword != 100 && data[0] != 0x64 {
                        log::debug!(
                            "Attributes file may be compressed, first dword: 0x{:08X} ({}), first byte: 0x{:02X}",
                            first_dword,
                            first_dword,
                            data[0]
                        );

                        // Try to decompress if it looks like compression flags
                        if data[0] & 0x0F != 0 || data[0] == 0x02 {
                            log::info!(
                                "Attempting to decompress attributes file with method 0x{:02X}",
                                data[0]
                            );
                            match compression::decompress(&data[1..], data[0], block_count * 100) {
                                Ok(decompressed) => {
                                    log::info!("Successfully decompressed attributes file");
                                    data = decompressed;
                                }
                                Err(e) => {
                                    log::warn!("Failed to decompress attributes file: {e}");
                                    // Continue with original data
                                }
                            }
                        }
                    }
                }

                // Parse attributes
                let attributes = special_files::Attributes::parse(&data.into(), block_count)?;
                self.attributes = Some(attributes);

                log::info!("Loaded (attributes) file with {block_count} entries");
                Ok(())
            }
            Err(Error::FileNotFound(_)) => {
                log::debug!("No (attributes) file found in archive");
                Ok(())
            }
            Err(e) => Err(e),
        }
    }

    /// Get attributes for a specific file by block index
    pub fn get_file_attributes(
        &self,
        block_index: usize,
    ) -> Option<&special_files::FileAttributes> {
        self.attributes.as_ref()?.get_file_attributes(block_index)
    }

    /// Get all loaded attributes
    pub fn attributes(&self) -> Option<&special_files::Attributes> {
        self.attributes.as_ref()
    }

    /// Add a file to the archive
    pub fn add_file(&mut self, _name: &str, _data: &[u8]) -> Result<()> {
        Err(Error::invalid_format(
            "In-place file addition not yet implemented. Use ArchiveBuilder to create new archives.",
        ))
    }

    /// Read HET table size from the table header for V3 archives
    fn read_het_table_size(&mut self, het_pos: u64) -> Result<u64> {
        // For compressed tables, calculate the actual size based on the next table position
        log::debug!("Determining HET table size from file structure");

        // Calculate the actual size based on what comes after HET table
        let actual_size = if let Some(bet_pos) = self.header.bet_table_pos {
            if bet_pos > het_pos {
                // BET table comes after HET
                bet_pos - het_pos
            } else {
                // Calculate from hash table position
                self.header.get_hash_table_pos() - het_pos
            }
        } else {
            // Calculate from hash table position
            self.header.get_hash_table_pos() - het_pos
        };

        log::debug!("HET table position: 0x{het_pos:X}, calculated size: {actual_size} bytes");

        Ok(actual_size)
    }

    /// Read BET table size from the table header for V3 archives
    fn read_bet_table_size(&mut self, bet_pos: u64) -> Result<u64> {
        // For compressed tables, calculate the actual size based on the next table position
        log::debug!("Determining BET table size from file structure");

        // Calculate the actual size based on what comes after BET table (usually hash table)
        let actual_size = self.header.get_hash_table_pos() - bet_pos;

        log::debug!("BET table position: 0x{bet_pos:X}, calculated size: {actual_size} bytes");

        Ok(actual_size)
    }

    /// Verify the digital signature of the archive
    pub fn verify_signature(&mut self) -> Result<SignatureStatus> {
        // First check for strong signature (external to archive)
        if let Ok(strong_status) = self.verify_strong_signature()
            && strong_status != SignatureStatus::None
        {
            return Ok(strong_status);
        }

        // Then check for weak signature (inside archive)
        self.verify_weak_signature()
    }

    /// Verify weak signature from (signature) file inside the archive
    fn verify_weak_signature(&mut self) -> Result<SignatureStatus> {
        // Check if (signature) file exists
        let signature_info = match self.find_file("(signature)")? {
            Some(info) => info,
            None => return Ok(SignatureStatus::None),
        };

        // Read the signature file
        let signature_data = self.read_file("(signature)")?;

        // Try to parse as weak signature
        match crate::crypto::parse_weak_signature(&signature_data) {
            Ok(weak_sig) => {
                // Create signature info for StormLib-compatible hash calculation
                let archive_size = self.header.archive_size as u64;
                let sig_info = crate::crypto::SignatureInfo::new_weak(
                    self.archive_offset,
                    archive_size,
                    signature_info.file_pos,
                    signature_info.compressed_size,
                    weak_sig.clone(),
                );

                // Seek to beginning of archive
                self.reader.seek(SeekFrom::Start(self.archive_offset))?;

                // Verify the weak signature using StormLib-compatible approach
                match crate::crypto::verify_weak_signature_stormlib(
                    &mut self.reader,
                    &weak_sig,
                    &sig_info,
                ) {
                    Ok(true) => Ok(SignatureStatus::WeakValid),
                    Ok(false) => Ok(SignatureStatus::WeakInvalid),
                    Err(e) => {
                        log::warn!("Failed to verify weak signature: {e}");
                        Ok(SignatureStatus::WeakInvalid)
                    }
                }
            }
            Err(_) => {
                // Not a weak signature
                log::debug!("Signature file found but not a valid weak signature format");
                Ok(SignatureStatus::None)
            }
        }
    }

    /// Read a potentially compressed table from the archive
    ///
    /// This handles V4 archives where hash/block tables can be compressed.
    /// The compressed data format is:
    /// - Compression type byte (e.g., 0x02 for ZLIB)
    /// - Compressed data
    fn read_compressed_table(
        &mut self,
        offset: u64,
        compressed_size: u64,
        uncompressed_size: usize,
    ) -> Result<Vec<u8>> {
        // Seek to the table position
        self.reader.seek(SeekFrom::Start(offset))?;

        // Read the compressed data
        let mut compressed_data = vec![0u8; compressed_size as usize];
        self.reader.read_exact(&mut compressed_data)?;

        // Check if the table is actually compressed
        // In V4 archives, if compressed_size < expected uncompressed size, it's compressed
        let expected_uncompressed_size = uncompressed_size;

        if (compressed_size as usize) < expected_uncompressed_size {
            // Table is compressed
            log::debug!(
                "Table is compressed: compressed_size={compressed_size}, uncompressed_size={expected_uncompressed_size}"
            );

            // First byte is the compression type
            if compressed_data.is_empty() {
                return Err(Error::invalid_format("Empty compressed table data"));
            }

            let compression_type = compressed_data[0];
            let compressed_content = &compressed_data[1..];

            log::debug!("Decompressing table with method 0x{compression_type:02X}");

            // Decompress the data
            compression::decompress(
                compressed_content,
                compression_type,
                expected_uncompressed_size,
            )
        } else {
            // Table is not compressed, return as-is
            log::debug!("Table is not compressed, using as-is");
            Ok(compressed_data[..expected_uncompressed_size].to_vec())
        }
    }

    /// Verify strong signature appended after the archive
    fn verify_strong_signature(&mut self) -> Result<SignatureStatus> {
        use crate::crypto::{
            STRONG_SIGNATURE_SIZE, parse_strong_signature, verify_strong_signature,
        };

        // Get total file size
        let file_size = self.reader.get_ref().metadata()?.len();

        // Calculate expected archive end position
        let archive_end = self.archive_offset + self.header.get_archive_size();

        // Check if there's enough space for a strong signature after the archive
        if file_size < archive_end + STRONG_SIGNATURE_SIZE as u64 {
            log::debug!("File too small for strong signature");
            return Ok(SignatureStatus::None);
        }

        // Seek to where the strong signature should be
        let signature_pos = archive_end;
        self.reader.seek(SeekFrom::Start(signature_pos))?;

        // Read potential strong signature data
        let mut signature_data = vec![0u8; STRONG_SIGNATURE_SIZE];
        match self.reader.read_exact(&mut signature_data) {
            Ok(()) => {
                // Try to parse as strong signature
                match parse_strong_signature(&signature_data) {
                    Ok(strong_sig) => {
                        log::debug!("Found strong signature at offset 0x{signature_pos:X}");

                        // Seek to beginning of archive for verification
                        self.reader.seek(SeekFrom::Start(self.archive_offset))?;

                        // Verify the strong signature
                        match verify_strong_signature(
                            &mut self.reader,
                            &strong_sig,
                            archive_end - self.archive_offset,
                        ) {
                            Ok(true) => {
                                log::info!("Strong signature verification successful");
                                Ok(SignatureStatus::StrongValid)
                            }
                            Ok(false) => {
                                log::warn!("Strong signature verification failed");
                                Ok(SignatureStatus::StrongInvalid)
                            }
                            Err(e) => {
                                log::warn!("Failed to verify strong signature: {e}");
                                Ok(SignatureStatus::StrongInvalid)
                            }
                        }
                    }
                    Err(_) => {
                        // Not a strong signature
                        log::debug!("No valid strong signature found");
                        Ok(SignatureStatus::None)
                    }
                }
            }
            Err(e) => {
                log::debug!("Failed to read potential strong signature: {e}");
                Ok(SignatureStatus::None)
            }
        }
    }
}

/// Decrypt file data in-place
pub fn decrypt_file_data(data: &mut [u8], key: u32) {
    if data.is_empty() || key == 0 {
        return;
    }

    // Process full u32 chunks
    let chunks = data.len() / 4;
    if chunks > 0 {
        // Create a properly aligned u32 slice
        let mut u32_data = Vec::with_capacity(chunks);

        // Copy data as u32 values (little-endian)
        for i in 0..chunks {
            let offset = i * 4;
            let value = u32::from_le_bytes([
                data[offset],
                data[offset + 1],
                data[offset + 2],
                data[offset + 3],
            ]);
            u32_data.push(value);
        }

        // Decrypt the u32 data
        decrypt_block(&mut u32_data, key);

        // Copy back to byte array
        for (i, &value) in u32_data.iter().enumerate() {
            let offset = i * 4;
            let bytes = value.to_le_bytes();
            data[offset] = bytes[0];
            data[offset + 1] = bytes[1];
            data[offset + 2] = bytes[2];
            data[offset + 3] = bytes[3];
        }
    }

    // Handle remaining bytes if not aligned to 4
    let remainder = data.len() % 4;
    if remainder > 0 {
        let offset = chunks * 4;

        // Read remaining bytes into a u32 (padding with zeros)
        let mut last_bytes = [0u8; 4];
        last_bytes[..remainder].copy_from_slice(&data[offset..(remainder + offset)]);
        let last_dword = u32::from_le_bytes(last_bytes);

        // Decrypt with adjusted key
        let decrypted = decrypt_dword(last_dword, key.wrapping_add(chunks as u32));

        // Write back only the remainder bytes
        let decrypted_bytes = decrypted.to_le_bytes();
        data[offset..(remainder + offset)].copy_from_slice(&decrypted_bytes[..remainder]);
    }
}

/// Information about a file in the archive
#[derive(Debug)]
pub struct FileInfo {
    /// File name
    pub filename: String,
    /// Index in hash table
    pub hash_index: usize,
    /// Index in block table
    pub block_index: usize,
    /// Absolute file position in archive file
    pub file_pos: u64,
    /// Compressed size
    pub compressed_size: u64,
    /// Uncompressed size
    pub file_size: u64,
    /// File flags
    pub flags: u32,
    /// File locale
    pub locale: u16,
}

impl FileInfo {
    /// Check if the file is compressed
    pub fn is_compressed(&self) -> bool {
        use crate::tables::BlockEntry;
        (self.flags & (BlockEntry::FLAG_IMPLODE | BlockEntry::FLAG_COMPRESS)) != 0
    }

    /// Check if the file is encrypted
    pub fn is_encrypted(&self) -> bool {
        use crate::tables::BlockEntry;
        (self.flags & BlockEntry::FLAG_ENCRYPTED) != 0
    }

    /// Check if the file has fixed key encryption
    pub fn has_fix_key(&self) -> bool {
        use crate::tables::BlockEntry;
        (self.flags & BlockEntry::FLAG_FIX_KEY) != 0
    }

    /// Check if the file is stored as a single unit
    pub fn is_single_unit(&self) -> bool {
        use crate::tables::BlockEntry;
        (self.flags & BlockEntry::FLAG_SINGLE_UNIT) != 0
    }

    /// Check if the file has sector CRCs
    pub fn has_sector_crc(&self) -> bool {
        use crate::tables::BlockEntry;
        (self.flags & BlockEntry::FLAG_SECTOR_CRC) != 0
    }

    /// Check if the file is a patch file
    pub fn is_patch_file(&self) -> bool {
        use crate::tables::BlockEntry;
        (self.flags & BlockEntry::FLAG_PATCH_FILE) != 0
    }

    /// Check if the file uses IMPLODE compression specifically
    pub fn is_implode(&self) -> bool {
        use crate::tables::BlockEntry;
        (self.flags & BlockEntry::FLAG_IMPLODE) != 0
            && (self.flags & BlockEntry::FLAG_COMPRESS) == 0
    }

    /// Check if the file uses COMPRESS (multi-method compression)
    pub fn uses_compression_prefix(&self) -> bool {
        use crate::tables::BlockEntry;
        (self.flags & BlockEntry::FLAG_COMPRESS) != 0
    }

    /// Extract compression method from block table flags
    /// Returns the compression method byte that should be used for decompression
    pub fn get_compression_method(&self) -> Option<u8> {
        use crate::compression::flags;

        if !self.is_compressed() {
            return None;
        }

        // Extract compression method from flags (MPQ_FILE_COMPRESS_MASK = 0x0000FF00)
        let compression_mask = (self.flags & 0x0000FF00) >> 8;

        log::debug!(
            "Compression method extraction: flags=0x{:08X}, mask=0x{:02X}",
            self.flags,
            compression_mask
        );

        // Convert from StormLib block table flag format to compression method byte format
        match compression_mask {
            0x02 => Some(flags::ZLIB),         // ZLIB/DEFLATE
            0x01 => Some(flags::IMPLODE),      // IMPLODE
            0x08 => Some(flags::PKWARE),       // PKWARE
            0x10 => Some(flags::BZIP2),        // BZIP2
            0x20 => Some(flags::SPARSE),       // SPARSE
            0x40 => Some(flags::ADPCM_MONO),   // ADPCM_MONO
            0x80 => Some(flags::ADPCM_STEREO), // ADPCM_STEREO
            _ => {
                log::warn!("Unknown compression method in flags: 0x{compression_mask:02X}");
                None
            }
        }
    }
}

/// Information about a file in the archive (for listing)
#[derive(Debug)]
pub struct FileEntry {
    /// File name
    pub name: String,
    /// Uncompressed size
    pub size: u64,
    /// Compressed size
    pub compressed_size: u64,
    /// File flags
    pub flags: u32,
    /// Hash values (name_1, name_2) - only populated when requested
    pub hashes: Option<(u32, u32)>,
    /// Table indices for direct file access (when name is generic)
    /// Contains (hash_index, block_index) for classic tables or (file_index, None) for HET/BET
    pub table_indices: Option<(usize, Option<usize>)>,
}

impl FileEntry {
    /// Check if the file is compressed
    pub fn is_compressed(&self) -> bool {
        use crate::tables::BlockEntry;
        (self.flags & (BlockEntry::FLAG_IMPLODE | BlockEntry::FLAG_COMPRESS)) != 0
    }

    /// Check if the file is encrypted
    pub fn is_encrypted(&self) -> bool {
        use crate::tables::BlockEntry;
        (self.flags & BlockEntry::FLAG_ENCRYPTED) != 0
    }

    /// Check if the file uses fixed key encryption
    pub fn has_fix_key(&self) -> bool {
        use crate::tables::BlockEntry;
        (self.flags & BlockEntry::FLAG_FIX_KEY) != 0
    }

    /// Check if the file is stored as a single unit
    pub fn is_single_unit(&self) -> bool {
        use crate::tables::BlockEntry;
        (self.flags & BlockEntry::FLAG_SINGLE_UNIT) != 0
    }

    /// Check if the file has sector CRCs
    pub fn has_sector_crc(&self) -> bool {
        use crate::tables::BlockEntry;
        (self.flags & BlockEntry::FLAG_SECTOR_CRC) != 0
    }

    /// Check if the file exists
    pub fn exists(&self) -> bool {
        use crate::tables::BlockEntry;
        (self.flags & BlockEntry::FLAG_EXISTS) != 0
    }

    /// Check if the file is a patch file (Cataclysm+ PTCH format)
    pub fn is_patch_file(&self) -> bool {
        use crate::tables::BlockEntry;
        (self.flags & BlockEntry::FLAG_PATCH_FILE) != 0
    }
}

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

    #[test]
    fn test_open_options() {
        let opts = OpenOptions::new().load_tables(false);

        assert!(!opts.load_tables);
    }

    #[test]
    fn test_file_info_flags() {
        use crate::tables::BlockEntry;

        let info = FileInfo {
            filename: "test.txt".to_string(),
            hash_index: 0,
            block_index: 0,
            file_pos: 0,
            compressed_size: 100,
            file_size: 200,
            flags: BlockEntry::FLAG_COMPRESS | BlockEntry::FLAG_ENCRYPTED,
            locale: 0,
        };

        assert!(info.is_compressed());
        assert!(info.is_encrypted());
        assert!(!info.has_fix_key());
    }

    #[test]
    fn test_decrypt_file_data() {
        let mut data = vec![0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0];
        let original = data.clone();

        // For testing, we need an encrypt function
        fn encrypt_test_data(data: &mut [u8], key: u32) {
            if data.is_empty() || key == 0 {
                return;
            }

            // Convert to u32 for encryption
            let chunks = data.len() / 4;
            if chunks > 0 {
                let mut u32_data = Vec::with_capacity(chunks);
                for i in 0..chunks {
                    let offset = i * 4;
                    let value = u32::from_le_bytes([
                        data[offset],
                        data[offset + 1],
                        data[offset + 2],
                        data[offset + 3],
                    ]);
                    u32_data.push(value);
                }

                encrypt_block(&mut u32_data, key);

                for (i, &value) in u32_data.iter().enumerate() {
                    let offset = i * 4;
                    let bytes = value.to_le_bytes();
                    data[offset] = bytes[0];
                    data[offset + 1] = bytes[1];
                    data[offset + 2] = bytes[2];
                    data[offset + 3] = bytes[3];
                }
            }
        }

        // Encrypt
        encrypt_test_data(&mut data, 0xDEADBEEF);
        assert_ne!(data, original, "Data should be changed after encryption");

        // Decrypt
        decrypt_file_data(&mut data, 0xDEADBEEF);
        assert_eq!(data, original, "Data should be restored after decryption");
    }

    #[test]
    fn test_crc_calculation() {
        // Test that we're using the correct checksum algorithm (ADLER32)
        // MPQ uses ADLER32 for sector checksums, not CRC32 despite the name "SECTOR_CRC"
        let test_data = b"Hello, World!";
        let crc = adler2::adler32_slice(test_data);

        // This is the expected ADLER32 value for "Hello, World!"
        assert_eq!(crc, 0x1F9E046A);
    }
}