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
//! Archive builder for creating MPQ archives

use crate::{
    Error, Result,
    compression::{compress, flags as compression_flags},
    crypto::{encrypt_block, hash_string, hash_type, het_hash, jenkins_hash},
    header::{FormatVersion, MpqHeaderV4Data},
    special_files::{AttributeFlags, Attributes, FileAttributes},
    tables::{BetHeader, BlockEntry, BlockTable, HashEntry, HashTable, HetHeader, HiBlockTable},
};
use md5::{Digest, Md5};
use std::fs::{self};
use std::io::{Read, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};
use tempfile::NamedTempFile;

/// Helper trait for writing little-endian integers
trait WriteLittleEndian: Write {
    fn write_u16_le(&mut self, value: u16) -> Result<()> {
        self.write_all(&value.to_le_bytes())?;
        Ok(())
    }

    fn write_u32_le(&mut self, value: u32) -> Result<()> {
        self.write_all(&value.to_le_bytes())?;
        Ok(())
    }

    fn write_u64_le(&mut self, value: u64) -> Result<()> {
        self.write_all(&value.to_le_bytes())?;
        Ok(())
    }
}

impl<W: Write> WriteLittleEndian for W {}

/// File to be added to the archive
#[derive(Debug)]
struct PendingFile {
    /// Source path or data
    source: FileSource,
    /// Target filename in archive
    archive_name: String,
    /// Compression method to use
    compression: u8,
    /// Whether to encrypt the file
    encrypt: bool,
    /// Whether to use FIX_KEY encryption (adjusts key by block position)
    use_fix_key: bool,
    /// Locale code
    locale: u16,
}

#[derive(Debug)]
enum FileSource {
    Path(PathBuf),
    Data(Vec<u8>),
}

/// Parameters for writing a file to the archive
struct FileWriteParams<'a> {
    /// File data to write
    file_data: &'a [u8],
    /// Archive name for the file
    archive_name: &'a str,
    /// Compression method
    compression: u8,
    /// Whether to encrypt
    encrypt: bool,
    /// Whether to use FIX_KEY encryption
    use_fix_key: bool,
    /// Sector size
    sector_size: usize,
    /// File position in archive (64-bit for large archives)
    file_pos: u64,
}

/// Parameters for writing the MPQ header
struct HeaderWriteParams {
    archive_size: u64,
    hash_table_pos: u64,
    block_table_pos: u64,
    hash_table_size: u32,
    block_table_size: u32,
    hi_block_table_pos: Option<u64>,
    het_table_pos: Option<u64>,
    bet_table_pos: Option<u64>,
    _het_table_size: Option<u64>,
    _bet_table_size: Option<u64>,
    // V4 specific fields
    v4_data: Option<MpqHeaderV4Data>,
}

/// Options for listfile generation
#[derive(Debug, Clone)]
pub enum ListfileOption {
    /// Automatically generate listfile from added files
    Generate,
    /// Use external listfile
    External(PathBuf),
    /// Don't include a listfile
    None,
}

/// Options for attributes file generation
#[derive(Debug, Clone)]
pub enum AttributesOption {
    /// Generate attributes with CRC32 checksums
    GenerateCrc32,
    /// Generate attributes with CRC32 and MD5
    GenerateFull,
    /// Use external attributes file
    External(PathBuf),
    /// Don't include attributes file
    None,
}

/// Builder for creating new MPQ archives
///
/// `ArchiveBuilder` provides a fluent interface for creating MPQ archives with
/// complete control over format version, compression, encryption, and file organization.
///
/// # Examples
///
/// ## Basic archive creation
///
/// ```no_run
/// use wow_mpq::{ArchiveBuilder, FormatVersion};
///
/// // Create a simple archive with default settings
/// ArchiveBuilder::new()
///     .add_file("readme.txt", "README.txt")
///     .add_file_data(b"Hello world".to_vec(), "hello.txt")
///     .build("my_archive.mpq")?;
/// # Ok::<(), wow_mpq::Error>(())
/// ```
///
/// ## Advanced archive creation
///
/// ```no_run
/// use wow_mpq::{ArchiveBuilder, FormatVersion, compression, ListfileOption};
///
/// ArchiveBuilder::new()
///     .version(FormatVersion::V2)
///     .block_size(7)  // 64KB sectors for better performance
///     .default_compression(compression::flags::BZIP2)
///     .listfile_option(ListfileOption::Generate)
///     .generate_crcs(true)
///     .add_file_data_with_options(
///         b"secret data".to_vec(),
///         "encrypted.dat",
///         compression::flags::ZLIB,
///         true,  // encrypt
///         0,     // locale
///     )
///     .build("advanced.mpq")?;
/// # Ok::<(), wow_mpq::Error>(())
/// ```
#[derive(Debug)]
pub struct ArchiveBuilder {
    /// Target MPQ version
    version: FormatVersion,
    /// Block size (sector size = 512 * 2^block_size)
    block_size: u16,
    /// Files to be added
    pending_files: Vec<PendingFile>,
    /// Listfile option
    listfile_option: ListfileOption,
    /// Attributes option
    attributes_option: AttributesOption,
    /// Default compression method
    default_compression: u8,
    /// Whether to generate sector CRCs for files
    generate_crcs: bool,
    /// Whether to compress HET/BET tables (v3+ only)
    compress_tables: bool,
    /// Compression method for tables
    table_compression: u8,
}

impl ArchiveBuilder {
    /// Create a new archive builder
    pub fn new() -> Self {
        Self {
            version: FormatVersion::V1,
            block_size: 5, // Default 16KB sectors for StormLib compatibility
            pending_files: Vec::new(),
            listfile_option: ListfileOption::Generate,
            attributes_option: AttributesOption::None,
            default_compression: compression_flags::ZLIB,
            generate_crcs: false,
            compress_tables: false, // Default to uncompressed for compatibility
            table_compression: compression_flags::ZLIB,
        }
    }

    /// Set the MPQ format version
    pub fn version(mut self, version: FormatVersion) -> Self {
        self.version = version;
        self
    }

    /// Set the block size (sector size = 512 * 2^block_size)
    ///
    /// The block size determines the sector size used for file storage.
    /// Larger block sizes can improve compression efficiency for large files
    /// but increase overhead for small files.
    ///
    /// # Parameters
    /// - `block_size`: Power of 2 exponent (0-31). Final sector size = 512 * 2^block_size
    ///   - Common values: 3 (4KB sectors), 4 (8KB), 5 (16KB), 6 (32KB), 7 (64KB)
    ///
    /// # Examples
    /// ```no_run
    /// use wow_mpq::ArchiveBuilder;
    ///
    /// // Create archive with 64KB sectors (good for large files)
    /// let builder = ArchiveBuilder::new().block_size(7);
    ///
    /// // Create archive with 4KB sectors (good for small files)
    /// let builder = ArchiveBuilder::new().block_size(3);
    /// # Ok::<(), wow_mpq::Error>(())
    /// ```
    pub fn block_size(mut self, block_size: u16) -> Self {
        self.block_size = block_size;
        self
    }

    /// Set the default compression method
    pub fn default_compression(mut self, compression: u8) -> Self {
        self.default_compression = compression;
        self
    }

    /// Set the listfile option
    pub fn listfile_option(mut self, option: ListfileOption) -> Self {
        self.listfile_option = option;
        self
    }

    /// Enable or disable sector CRC generation
    ///
    /// When enabled, CRC32 checksums are generated for each sector of each file,
    /// providing integrity verification during file extraction. This adds security
    /// but increases archive size and creation time.
    ///
    /// # Parameters
    /// - `generate`: If `true`, sector CRCs are generated. If `false`, no CRCs.
    ///
    /// # Examples
    /// ```no_run
    /// use wow_mpq::ArchiveBuilder;
    ///
    /// // Enable CRC generation for data integrity
    /// let builder = ArchiveBuilder::new().generate_crcs(true);
    /// # Ok::<(), wow_mpq::Error>(())
    /// ```
    ///
    /// # Notes
    /// CRC generation is recommended for archives containing critical data
    /// where integrity verification is important.
    pub fn generate_crcs(mut self, generate: bool) -> Self {
        self.generate_crcs = generate;
        // Also enable attributes if CRCs are requested
        if generate && matches!(self.attributes_option, AttributesOption::None) {
            self.attributes_option = AttributesOption::GenerateCrc32;
        }
        self
    }

    /// Set the attributes file option
    ///
    /// Controls how the (attributes) special file is generated, which stores
    /// metadata like CRC32 checksums, MD5 hashes, and file timestamps.
    ///
    /// # Examples
    /// ```no_run
    /// use wow_mpq::{ArchiveBuilder, AttributesOption};
    ///
    /// // Generate attributes with CRC32 checksums
    /// let builder = ArchiveBuilder::new()
    ///     .attributes_option(AttributesOption::GenerateCrc32);
    ///
    /// // Generate full attributes with CRC32 and MD5
    /// let builder = ArchiveBuilder::new()
    ///     .attributes_option(AttributesOption::GenerateFull);
    /// # Ok::<(), wow_mpq::Error>(())
    /// ```
    pub fn attributes_option(mut self, option: AttributesOption) -> Self {
        // Enable CRC generation if attributes are requested
        if matches!(
            option,
            AttributesOption::GenerateCrc32 | AttributesOption::GenerateFull
        ) {
            self.generate_crcs = true;
        }
        self.attributes_option = option;
        self
    }

    /// Enable or disable HET/BET table compression (v3+ only)
    ///
    /// For MPQ format version 3 and 4, the HET (Hash Extended Table) and BET
    /// (Block Extended Table) can be compressed to reduce archive size. This
    /// only applies to v3+ archives; v1/v2 archives ignore this setting.
    ///
    /// # Parameters
    /// - `compress`: If `true`, HET/BET tables are compressed. If `false`, stored uncompressed.
    ///
    /// # Examples
    /// ```no_run
    /// use wow_mpq::{ArchiveBuilder, FormatVersion};
    ///
    /// // Enable table compression for v3 archive
    /// let builder = ArchiveBuilder::new()
    ///     .version(FormatVersion::V3)
    ///     .compress_tables(true);
    /// # Ok::<(), wow_mpq::Error>(())
    /// ```
    ///
    /// # Notes
    /// Table compression can significantly reduce archive size for large archives
    /// with many files, but may slightly increase archive opening time.
    pub fn compress_tables(mut self, compress: bool) -> Self {
        self.compress_tables = compress;
        self
    }

    /// Set compression method for tables (default: zlib)
    ///
    /// Specifies which compression algorithm to use when compressing HET/BET tables
    /// in v3+ archives. Only used when `compress_tables` is enabled.
    ///
    /// # Parameters
    /// - `compression`: Compression method flag from `compression::flags`
    ///   - `compression::flags::ZLIB` (default): Fast and widely compatible
    ///   - `compression::flags::BZIP2`: Better compression ratio but slower
    ///   - `compression::flags::LZMA`: Best compression but slowest
    ///
    /// # Examples
    /// ```no_run
    /// use wow_mpq::{ArchiveBuilder, FormatVersion, compression};
    ///
    /// // Use BZIP2 for table compression
    /// let builder = ArchiveBuilder::new()
    ///     .version(FormatVersion::V3)
    ///     .compress_tables(true)
    ///     .table_compression(compression::flags::BZIP2);
    /// # Ok::<(), wow_mpq::Error>(())
    /// ```
    pub fn table_compression(mut self, compression: u8) -> Self {
        self.table_compression = compression;
        self
    }

    /// Add a file from disk to the archive
    ///
    /// Reads a file from the filesystem and adds it to the archive with default
    /// compression and no encryption. The file will use the builder's default
    /// compression method and neutral locale.
    ///
    /// # Parameters
    /// - `path`: Path to the source file on disk
    /// - `archive_name`: Name the file will have inside the archive
    ///
    /// # Examples
    /// ```no_run
    /// use wow_mpq::ArchiveBuilder;
    ///
    /// let builder = ArchiveBuilder::new()
    ///     .add_file("data/config.txt", "config.txt")
    ///     .add_file("assets/image.jpg", "images/image.jpg");
    /// # Ok::<(), wow_mpq::Error>(())
    /// ```
    ///
    /// # Notes
    /// - The source file is read when `build()` is called, not when `add_file()` is called
    /// - Archive names are automatically normalized to use backslashes as path separators
    /// - Use `add_file_with_options()` for custom compression or encryption settings
    pub fn add_file<P: AsRef<Path>>(mut self, path: P, archive_name: &str) -> Self {
        self.pending_files.push(PendingFile {
            source: FileSource::Path(path.as_ref().to_path_buf()),
            archive_name: crate::path::normalize_mpq_path(archive_name),
            compression: self.default_compression,
            encrypt: false,
            use_fix_key: false,
            locale: 0, // Neutral locale
        });
        self
    }

    /// Add a file from disk with custom compression and encryption options
    ///
    /// Provides full control over how the file is stored in the archive,
    /// including compression method, encryption, and locale settings.
    ///
    /// # Parameters
    /// - `path`: Path to the source file on disk
    /// - `archive_name`: Name the file will have inside the archive
    /// - `compression`: Compression method from `compression::flags` (0 = no compression)
    /// - `encrypt`: Whether to encrypt the file
    /// - `locale`: Locale code for the file (0 = neutral locale)
    ///
    /// # Examples
    /// ```no_run
    /// use wow_mpq::{ArchiveBuilder, compression};
    ///
    /// let builder = ArchiveBuilder::new()
    ///     .add_file_with_options(
    ///         "secret.txt",
    ///         "hidden/secret.txt",
    ///         compression::flags::BZIP2,
    ///         true,  // encrypt
    ///         0      // neutral locale
    ///     );
    /// # Ok::<(), wow_mpq::Error>(())
    /// ```
    pub fn add_file_with_options<P: AsRef<Path>>(
        mut self,
        path: P,
        archive_name: &str,
        compression: u8,
        encrypt: bool,
        locale: u16,
    ) -> Self {
        self.pending_files.push(PendingFile {
            source: FileSource::Path(path.as_ref().to_path_buf()),
            archive_name: crate::path::normalize_mpq_path(archive_name),
            compression,
            encrypt,
            use_fix_key: false,
            locale,
        });
        self
    }

    /// Add a file from in-memory data
    ///
    /// Creates a file in the archive from data already loaded in memory.
    /// Useful for dynamically generated content or when you already have
    /// the file data loaded.
    ///
    /// # Parameters
    /// - `data`: Raw file data to store in the archive
    /// - `archive_name`: Name the file will have inside the archive
    ///
    /// # Examples
    /// ```no_run
    /// use wow_mpq::ArchiveBuilder;
    ///
    /// let config_data = b"version=1.0\ndebug=false".to_vec();
    /// let builder = ArchiveBuilder::new()
    ///     .add_file_data(config_data, "config.ini")
    ///     .add_file_data(b"Hello, World!".to_vec(), "readme.txt");
    /// # Ok::<(), wow_mpq::Error>(())
    /// ```
    ///
    /// # Notes
    /// - Uses the builder's default compression method and neutral locale
    /// - More memory efficient than `add_file()` when data is already in memory
    /// - Use `add_file_data_with_options()` for custom compression or encryption
    pub fn add_file_data(mut self, data: Vec<u8>, archive_name: &str) -> Self {
        self.pending_files.push(PendingFile {
            source: FileSource::Data(data),
            archive_name: crate::path::normalize_mpq_path(archive_name),
            compression: self.default_compression,
            encrypt: false,
            use_fix_key: false,
            locale: 0,
        });
        self
    }

    /// Add a file from memory with custom compression and encryption options
    ///
    /// Creates a file in the archive from in-memory data with full control
    /// over compression, encryption, and locale settings.
    ///
    /// # Parameters
    /// - `data`: Raw file data to store in the archive
    /// - `archive_name`: Name the file will have inside the archive
    /// - `compression`: Compression method from `compression::flags` (0 = no compression)
    /// - `encrypt`: Whether to encrypt the file
    /// - `locale`: Locale code for the file (0 = neutral locale)
    ///
    /// # Examples
    /// ```no_run
    /// use wow_mpq::{ArchiveBuilder, compression};
    ///
    /// let secret_data = b"TOP SECRET INFORMATION".to_vec();
    /// let builder = ArchiveBuilder::new()
    ///     .add_file_data_with_options(
    ///         secret_data,
    ///         "classified.txt",
    ///         compression::flags::LZMA,
    ///         true,  // encrypt
    ///         0      // neutral locale
    ///     );
    /// # Ok::<(), wow_mpq::Error>(())
    /// ```
    pub fn add_file_data_with_options(
        mut self,
        data: Vec<u8>,
        archive_name: &str,
        compression: u8,
        encrypt: bool,
        locale: u16,
    ) -> Self {
        self.pending_files.push(PendingFile {
            source: FileSource::Data(data),
            archive_name: crate::path::normalize_mpq_path(archive_name),
            compression,
            encrypt,
            use_fix_key: false,
            locale,
        });
        self
    }

    /// Add a file with full encryption options including FIX_KEY support
    pub fn add_file_with_encryption<P: AsRef<Path>>(
        mut self,
        path: P,
        archive_name: &str,
        compression: u8,
        use_fix_key: bool,
        locale: u16,
    ) -> Self {
        self.pending_files.push(PendingFile {
            source: FileSource::Path(path.as_ref().to_path_buf()),
            archive_name: crate::path::normalize_mpq_path(archive_name),
            compression,
            encrypt: true,
            use_fix_key,
            locale,
        });
        self
    }

    /// Add file data with full encryption options including FIX_KEY support
    pub fn add_file_data_with_encryption(
        mut self,
        data: Vec<u8>,
        archive_name: &str,
        compression: u8,
        use_fix_key: bool,
        locale: u16,
    ) -> Self {
        self.pending_files.push(PendingFile {
            source: FileSource::Data(data),
            archive_name: crate::path::normalize_mpq_path(archive_name),
            compression,
            encrypt: true,
            use_fix_key,
            locale,
        });
        self
    }

    /// Calculate optimal hash table size based on file count
    fn calculate_hash_table_size(&self) -> u32 {
        let file_count = self.pending_files.len()
            + match &self.listfile_option {
                ListfileOption::Generate | ListfileOption::External(_) => 1,
                ListfileOption::None => 0,
            }
            + match &self.attributes_option {
                AttributesOption::GenerateCrc32
                | AttributesOption::GenerateFull
                | AttributesOption::External(_) => 1,
                AttributesOption::None => 0,
            };

        // Use 2x the file count for good performance, minimum 16
        let optimal_size = (file_count * 2).max(16) as u32;

        // Round up to next power of 2
        optimal_size.next_power_of_two()
    }

    /// Build the archive and write to the specified path
    pub fn build<P: AsRef<Path>>(mut self, path: P) -> Result<()> {
        let path = path.as_ref();

        // Create a temporary file in the same directory
        let mut temp_file = NamedTempFile::new_in(path.parent().unwrap_or_else(|| Path::new(".")))?;

        // Add listfile if needed
        self.prepare_listfile()?;

        // Add attributes file if needed
        self.prepare_attributes()?;

        // Write the archive directly to the temp file
        {
            let file = temp_file.as_file_mut();
            use std::io::{Seek as _, Write as _};

            // For v3+ archives that need read-back support, we need to write everything
            // to a buffer first, then copy to file
            if self.version >= FormatVersion::V3 {
                // For v3+, we need to write everything to a buffer first
                // Estimate total size needed to avoid buffer reallocation issues
                let header_size = self.version.header_size() as usize;

                // Estimate file data size (assuming 10:1 compression ratio average)
                let estimated_file_data_size: usize = self
                    .pending_files
                    .iter()
                    .map(|f| match &f.source {
                        FileSource::Data(data) => data.len() / 10 + 1000, // Assume 10:1 compression + overhead
                        FileSource::Path(_) => 100_000,                   // Conservative estimate
                    })
                    .sum();

                // Add table sizes (conservative estimates)
                let estimated_table_size = self.pending_files.len() * 1000; // Conservative per-file overhead

                let total_estimated_size =
                    header_size + estimated_file_data_size + estimated_table_size;

                log::debug!(
                    "Pre-allocating buffer of {total_estimated_size} bytes for v3+ archive (header: {header_size}, estimated data: {estimated_file_data_size}, tables: {estimated_table_size})"
                );

                let mut vec = Vec::with_capacity(total_estimated_size);
                vec.resize(header_size, 0u8);
                let mut buffer = std::io::Cursor::new(vec);
                buffer.seek(SeekFrom::Start(header_size as u64))?;

                self.write_archive(&mut buffer)?;

                // Write the buffer to file
                file.write_all(buffer.get_ref())?;
                file.flush()?;
            } else {
                // For v1/v2, we can write directly
                self.write_archive(file)?;
                file.flush()?;
            }
        }

        // Atomically rename temp file to final destination
        temp_file.persist(path).map_err(|e| Error::Io(e.error))?;

        Ok(())
    }

    /// Prepare the listfile based on the option
    fn prepare_listfile(&mut self) -> Result<()> {
        match &self.listfile_option {
            ListfileOption::Generate => {
                // Generate listfile content from pending files
                let mut content = String::new();
                for file in &self.pending_files {
                    content.push_str(&file.archive_name);
                    content.push('\r');
                    content.push('\n');
                }

                // Add special files
                content.push_str("(listfile)\r\n");

                // Add attributes file if it will be generated
                if matches!(
                    self.attributes_option,
                    AttributesOption::GenerateCrc32 | AttributesOption::GenerateFull
                ) {
                    content.push_str("(attributes)\r\n");
                }

                self.pending_files.push(PendingFile {
                    source: FileSource::Data(content.into_bytes()),
                    archive_name: "(listfile)".to_string(),
                    compression: self.default_compression,
                    encrypt: false,
                    use_fix_key: false,
                    locale: 0,
                });
            }
            ListfileOption::External(path) => {
                // Read external listfile
                let data = fs::read(path)?;

                self.pending_files.push(PendingFile {
                    source: FileSource::Data(data),
                    archive_name: "(listfile)".to_string(),
                    compression: self.default_compression,
                    encrypt: false,
                    use_fix_key: false,
                    locale: 0,
                });
            }
            ListfileOption::None => {}
        }

        Ok(())
    }

    /// Prepare the attributes file based on the option
    fn prepare_attributes(&mut self) -> Result<()> {
        // Import required types (will be used when we implement generation)
        // use crate::special_files::{Attributes, FileAttributes, AttributeFlags};

        match &self.attributes_option {
            AttributesOption::GenerateCrc32 | AttributesOption::GenerateFull => {
                // We'll generate attributes after all files are written
                // For now, just mark that we need to generate them
                // The actual generation happens in write_archive methods
            }
            AttributesOption::External(path) => {
                // Read external attributes file
                let content = fs::read(path)?;
                self.pending_files.push(PendingFile {
                    source: FileSource::Data(content),
                    archive_name: "(attributes)".to_string(),
                    compression: 0, // Attributes are not compressed
                    encrypt: false,
                    use_fix_key: false,
                    locale: 0,
                });
            }
            AttributesOption::None => {}
        }

        Ok(())
    }

    /// Write the complete archive
    fn write_archive<W: Write + Seek + Read>(&self, writer: &mut W) -> Result<()> {
        // For v3+, we should create HET/BET tables instead of/in addition to hash/block
        let use_het_bet = self.version >= FormatVersion::V3;

        if use_het_bet {
            return self.write_archive_with_het_bet(writer);
        }

        let hash_table_size = self.calculate_hash_table_size();
        let mut block_table_size = self.pending_files.len() as u32;

        // Account for attributes file if it will be generated
        if matches!(
            self.attributes_option,
            AttributesOption::GenerateCrc32 | AttributesOption::GenerateFull
        ) {
            block_table_size += 1;
        }

        // Calculate sector size
        let sector_size = crate::calculate_sector_size(self.block_size);

        // Reserve space for header (we'll write it at the end)
        let header_size = self.version.header_size();
        writer.seek(SeekFrom::Start(header_size as u64))?;

        // Build tables and write files
        let mut hash_table = HashTable::new(hash_table_size as usize)?;
        let mut block_table = BlockTable::new(block_table_size as usize)?;
        let mut hi_block_table = if self.version >= FormatVersion::V2 {
            Some(HiBlockTable::new(block_table_size as usize))
        } else {
            None
        };

        // Prepare to collect attributes if needed
        let collect_attributes = matches!(
            self.attributes_option,
            AttributesOption::GenerateCrc32 | AttributesOption::GenerateFull
        );
        let mut collected_attributes = if collect_attributes {
            Some(Vec::new())
        } else {
            None
        };

        // Write all files and populate tables
        let mut actual_block_index = 0;
        for pending_file in self.pending_files.iter() {
            // Skip (attributes) file if it's being generated - we'll write it later
            if pending_file.archive_name == "(attributes)" && collect_attributes {
                continue;
            }

            let file_pos = writer.stream_position()?;

            // Read file data
            let file_data = match &pending_file.source {
                FileSource::Path(path) => fs::read(path)?,
                FileSource::Data(data) => data.clone(),
            };

            // Write file and get sizes
            let params = FileWriteParams {
                file_data: &file_data,
                archive_name: &pending_file.archive_name,
                compression: pending_file.compression,
                encrypt: pending_file.encrypt,
                use_fix_key: pending_file.use_fix_key,
                sector_size,
                file_pos,
            };

            let (compressed_size, flags, file_attr) = if collect_attributes {
                self.write_file_with_attributes(writer, &params)?
            } else {
                let (size, flags) = self.write_file(writer, &params)?;
                (size, flags, FileAttributes::new())
            };

            // Collect attributes if needed
            if let Some(ref mut attrs) = collected_attributes {
                attrs.push(file_attr);
            }

            // Add to hash table
            self.add_to_hash_table(
                &mut hash_table,
                &pending_file.archive_name,
                actual_block_index as u32,
                pending_file.locale,
            )?;

            // Add to block table and hi-block table if needed
            let block_entry = BlockEntry {
                file_pos: file_pos as u32, // Low 32 bits
                compressed_size: compressed_size as u32,
                file_size: file_data.len() as u32,
                flags: flags | BlockEntry::FLAG_EXISTS,
            };

            // Store high 16 bits in hi-block table if needed
            if let Some(ref mut hi_table) = hi_block_table {
                let high_bits = (file_pos >> 32) as u16;
                hi_table.set(actual_block_index, high_bits);
            }

            // Get mutable reference and update
            if let Some(entry) = block_table.get_mut(actual_block_index) {
                *entry = block_entry;
            } else {
                return Err(Error::invalid_format("Block index out of bounds"));
            }

            actual_block_index += 1;
        }

        // Generate and write attributes file if needed
        if let Some(attrs) = collected_attributes {
            log::debug!("Writing attributes for {} files", attrs.len());
            self.write_attributes_file(
                writer,
                &mut hash_table,
                &mut block_table,
                attrs,
                actual_block_index,
            )?;
        }

        // Write hash table
        let hash_table_pos = writer.stream_position()?;
        self.write_hash_table(writer, &hash_table)?;

        // Write block table
        let block_table_pos = writer.stream_position()?;
        self.write_block_table(writer, &block_table)?;

        // Write hi-block table if needed
        let hi_block_table_pos = if let Some(ref hi_table) = hi_block_table {
            if hi_table.is_needed() {
                let pos = writer.stream_position()?;
                self.write_hi_block_table(writer, hi_table)?;
                Some(pos)
            } else {
                None
            }
        } else {
            None
        };

        // Calculate archive size
        let archive_size = writer.stream_position()?;

        // Write header at the beginning
        writer.seek(SeekFrom::Start(0))?;
        let header_params = HeaderWriteParams {
            archive_size,
            hash_table_pos,
            block_table_pos,
            hash_table_size,
            block_table_size,
            hi_block_table_pos,
            het_table_pos: None,
            bet_table_pos: None,
            _het_table_size: None,
            _bet_table_size: None,
            v4_data: None, // V1/V2 don't use v4_data
        };
        self.write_header(writer, &header_params)?;

        // TODO: For V4, implement proper MD5 calculation

        Ok(())
    }

    /// Write archive with HET/BET tables (v3+)
    fn write_archive_with_het_bet<W: Write + Seek + Read>(&self, writer: &mut W) -> Result<()> {
        let mut block_table_size = self.pending_files.len() as u32;

        // Account for attributes file if it will be generated
        if matches!(
            self.attributes_option,
            AttributesOption::GenerateCrc32 | AttributesOption::GenerateFull
        ) {
            block_table_size += 1;
        }

        // Calculate sector size
        let sector_size = crate::calculate_sector_size(self.block_size);

        // Reserve space for header by seeking past it (we'll write it at the end)
        let header_size = self.version.header_size();
        writer.seek(SeekFrom::Start(header_size as u64))?;

        // We'll still need block table data for file information
        let mut block_table = BlockTable::new(block_table_size as usize)?;
        let mut hi_block_table = Some(HiBlockTable::new(block_table_size as usize));

        // Prepare to collect attributes if needed
        let collect_attributes = matches!(
            self.attributes_option,
            AttributesOption::GenerateCrc32 | AttributesOption::GenerateFull
        );
        let mut collected_attributes = if collect_attributes {
            Some(Vec::new())
        } else {
            None
        };

        // Write all files and populate block table
        let mut actual_block_index = 0;
        for pending_file in self.pending_files.iter() {
            // Skip (attributes) file if it's being generated - we'll write it later
            if pending_file.archive_name == "(attributes)" && collect_attributes {
                continue;
            }

            let file_pos = writer.stream_position()?;

            // Read file data
            let file_data = match &pending_file.source {
                FileSource::Path(path) => fs::read(path)?,
                FileSource::Data(data) => data.clone(),
            };

            // Write file and get sizes
            let params = FileWriteParams {
                file_data: &file_data,
                archive_name: &pending_file.archive_name,
                compression: pending_file.compression,
                encrypt: pending_file.encrypt,
                use_fix_key: pending_file.use_fix_key,
                sector_size,
                file_pos,
            };

            let (compressed_size, flags, file_attr) = if collect_attributes {
                self.write_file_with_attributes(writer, &params)?
            } else {
                let (size, flags) = self.write_file(writer, &params)?;
                (size, flags, FileAttributes::new())
            };

            // Collect attributes if needed
            if let Some(ref mut attrs) = collected_attributes {
                attrs.push(file_attr);
            }

            // Add to block table
            let block_entry = BlockEntry {
                file_pos: file_pos as u32, // Low 32 bits
                compressed_size: compressed_size as u32,
                file_size: file_data.len() as u32,
                flags: flags | BlockEntry::FLAG_EXISTS,
            };

            // Store high 16 bits in hi-block table
            if let Some(ref mut hi_table) = hi_block_table {
                let high_bits = (file_pos >> 32) as u16;
                hi_table.set(actual_block_index, high_bits);
            }

            // Update block table entry
            if let Some(entry) = block_table.get_mut(actual_block_index) {
                *entry = block_entry;
            } else {
                return Err(Error::invalid_format("Block index out of bounds"));
            }

            actual_block_index += 1;
        }

        // Track if we have collected attributes
        let has_collected_attributes = collected_attributes.is_some();

        // For compatibility, also write classic tables
        let hash_table_size = self.calculate_hash_table_size();
        let mut hash_table = HashTable::new(hash_table_size as usize)?;

        // Populate hash table
        let mut hash_block_index = 0;
        for pending_file in self.pending_files.iter() {
            // Skip (attributes) file if it's being generated - already processed
            if pending_file.archive_name == "(attributes)" && has_collected_attributes {
                continue;
            }

            self.add_to_hash_table(
                &mut hash_table,
                &pending_file.archive_name,
                hash_block_index as u32,
                pending_file.locale,
            )?;

            hash_block_index += 1;
        }

        // Write attributes file if we collected them
        if let Some(attrs) = collected_attributes {
            self.write_attributes_file(
                writer,
                &mut hash_table,
                &mut block_table,
                attrs,
                actual_block_index,
            )?;
        }

        // Create HET table (now includes proper attributes file info)
        let het_table_pos = writer.stream_position()?;
        let (het_data, _het_header) = self.create_het_table_with_hash_table(&hash_table)?;
        let (het_table_size, het_table_md5) = self.write_het_table(writer, &het_data, true)?;

        // Create BET table (now includes proper attributes file info)
        let bet_table_pos = writer.stream_position()?;
        let (bet_data, _bet_header) = self.create_bet_table(&block_table)?;
        let (bet_table_size, bet_table_md5) = self.write_bet_table(writer, &bet_data, true)?;

        // Write hash table
        let hash_table_pos = writer.stream_position()?;
        let hash_table_md5 = self.write_hash_table(writer, &hash_table)?;

        // Write block table
        let block_table_pos = writer.stream_position()?;
        let block_table_md5 = self.write_block_table(writer, &block_table)?;

        // Write hi-block table if needed
        let (hi_block_table_pos, hi_block_table_md5) = if let Some(ref hi_table) = hi_block_table {
            if hi_table.is_needed() {
                let pos = writer.stream_position()?;
                let md5 = self.write_hi_block_table(writer, hi_table)?;
                (Some(pos), md5)
            } else {
                (None, [0u8; 16])
            }
        } else {
            (None, [0u8; 16])
        };

        // Calculate archive size
        let archive_size = writer.stream_position()?;

        // Save the current position (end of archive)
        let _archive_end_pos = writer.stream_position()?;

        // Write header at the beginning
        writer.seek(SeekFrom::Start(0))?;

        // For V4, we need to use the MD5 checksums calculated during table writes
        let actual_file_count = block_table_size; // This includes attributes file
        let v4_data = if self.version == FormatVersion::V4 {
            Some(MpqHeaderV4Data {
                hash_table_size_64: hash_table_size as u64 * 16, // 16 bytes per hash entry
                block_table_size_64: actual_file_count as u64 * 16, // 16 bytes per block entry (includes attributes)
                hi_block_table_size_64: if let Some(ref hi_table) = hi_block_table {
                    if hi_table.is_needed() {
                        actual_file_count as u64 * 2 // 2 bytes per hi-block entry (includes attributes)
                    } else {
                        0
                    }
                } else {
                    0
                },
                het_table_size_64: het_table_size,
                bet_table_size_64: bet_table_size,
                raw_chunk_size: 0x4000, // 16KB default as per StormLib
                md5_block_table: block_table_md5,
                md5_hash_table: hash_table_md5,
                md5_hi_block_table: hi_block_table_md5,
                md5_bet_table: bet_table_md5,
                md5_het_table: het_table_md5,
                md5_mpq_header: [0u8; 16], // Will be calculated after header write
            })
        } else {
            None
        };

        let header_params = HeaderWriteParams {
            archive_size,
            hash_table_pos,
            block_table_pos,
            hash_table_size,
            block_table_size: actual_file_count,
            hi_block_table_pos,
            het_table_pos: Some(het_table_pos),
            bet_table_pos: Some(bet_table_pos),
            _het_table_size: Some(het_table_size),
            _bet_table_size: Some(bet_table_size),
            v4_data,
        };

        // Write header
        self.write_header(writer, &header_params)?;

        // For V4, calculate and write the header MD5
        if self.version == FormatVersion::V4 {
            self.finalize_v4_header_md5(writer)?;
        }

        Ok(())
    }

    /// Write a single file to the archive and collect attributes
    fn write_file_with_attributes<W: Write>(
        &self,
        writer: &mut W,
        params: &FileWriteParams<'_>,
    ) -> Result<(usize, u32, FileAttributes)> {
        let (size, flags) = self.write_file(writer, params)?;

        // Create file attributes based on what we calculated
        let mut file_attr = FileAttributes::new();

        // CRC32 is calculated from uncompressed data
        if matches!(
            self.attributes_option,
            AttributesOption::GenerateCrc32 | AttributesOption::GenerateFull
        ) {
            let crc32 = crc32fast::hash(params.file_data);
            file_attr.crc32 = Some(crc32);
        }

        // MD5 if requested
        if matches!(self.attributes_option, AttributesOption::GenerateFull) {
            let mut hasher = Md5::new();
            hasher.update(params.file_data);
            let md5_result = hasher.finalize();
            let mut md5_bytes = [0u8; 16];
            md5_bytes.copy_from_slice(&md5_result);
            file_attr.md5 = Some(md5_bytes);
        }

        // File time (use current time for now)
        if matches!(self.attributes_option, AttributesOption::GenerateFull) {
            // Convert current time to Windows FILETIME (100-nanosecond intervals since 1601-01-01)
            use std::time::{SystemTime, UNIX_EPOCH};
            let duration = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
            let unix_seconds = duration.as_secs();
            // Windows epoch is 11644473600 seconds before Unix epoch
            let windows_seconds = unix_seconds + 11644473600;
            let filetime = windows_seconds * 10_000_000; // Convert to 100-nanosecond intervals
            file_attr.filetime = Some(filetime);
        }

        Ok((size, flags, file_attr))
    }

    /// Write a single file to the archive
    fn write_file<W: Write>(
        &self,
        writer: &mut W,
        params: &FileWriteParams<'_>,
    ) -> Result<(usize, u32)> {
        let FileWriteParams {
            file_data,
            archive_name,
            compression,
            encrypt,
            use_fix_key,
            sector_size,
            file_pos,
        } = params;
        let mut flags = 0u32;

        // For small files or if single unit is requested, write as single unit
        let is_single_unit = file_data.len() <= *sector_size;

        if is_single_unit {
            flags |= BlockEntry::FLAG_SINGLE_UNIT;

            // Set CRC flag early if enabled (needed for encryption key calculation)
            if self.generate_crcs {
                flags |= BlockEntry::FLAG_SECTOR_CRC;
            }

            // Compress if needed
            let compressed_data = if *compression != 0 && !file_data.is_empty() {
                log::debug!("Compressing {archive_name} with method 0x{compression:02X}");
                let compressed = compress(file_data, *compression)?;

                // The compress function now handles the compression byte prefix
                // and only returns compressed data if it's beneficial
                if compressed != *file_data {
                    // Compression was beneficial and the data now includes the method byte
                    log::debug!(
                        "Compression successful: {} -> {} bytes (including method byte)",
                        file_data.len(),
                        compressed.len()
                    );
                    flags |= BlockEntry::FLAG_COMPRESS;
                    compressed
                } else {
                    // Compression not beneficial, returned original data
                    log::debug!("Compression not beneficial, storing uncompressed");
                    file_data.to_vec()
                }
            } else {
                file_data.to_vec()
            };

            // Encrypt if needed
            let final_data = if *encrypt {
                flags |= BlockEntry::FLAG_ENCRYPTED;
                if *use_fix_key {
                    flags |= BlockEntry::FLAG_FIX_KEY;
                }
                let key =
                    self.calculate_file_key(archive_name, *file_pos, file_data.len() as u32, flags);
                let mut encrypted = compressed_data;
                self.encrypt_data(&mut encrypted, key);
                encrypted
            } else {
                compressed_data
            };

            // Write the data
            writer.write_all(&final_data)?;

            // Write CRC if enabled
            if self.generate_crcs {
                // MPQ uses ADLER32 for sector checksums
                let crc = adler2::adler32_slice(file_data);
                writer.write_u32_le(crc)?;
                log::debug!("Generated CRC for single unit file {archive_name}: 0x{crc:08X}");
            }

            // Return compressed size (NOT including CRC)
            Ok((final_data.len(), flags))
        } else {
            // Multi-sector file
            let sector_count = file_data.len().div_ceil(*sector_size);

            // Set CRC flag early if enabled (needed for encryption key calculation)
            if self.generate_crcs {
                flags |= BlockEntry::FLAG_SECTOR_CRC;
            }

            // Reserve space for sector offset table and CRC table if enabled
            let offset_table_size = (sector_count + 1) * 4;
            let crc_table_size = if self.generate_crcs {
                sector_count * 4
            } else {
                0
            };
            let data_start = offset_table_size + crc_table_size;

            let mut sector_offsets = vec![0u32; sector_count + 1];
            let mut sector_data = Vec::new();
            let mut sector_crcs = if self.generate_crcs {
                Vec::with_capacity(sector_count)
            } else {
                Vec::new()
            };

            // Process each sector
            for (i, offset) in sector_offsets.iter_mut().enumerate().take(sector_count) {
                let sector_start = i * *sector_size;
                let sector_end = ((i + 1) * *sector_size).min(file_data.len());
                let sector_bytes = &file_data[sector_start..sector_end];

                *offset = (data_start + sector_data.len()) as u32;

                // Calculate CRC for uncompressed sector if enabled
                if self.generate_crcs {
                    // MPQ uses ADLER32 for sector checksums
                    let crc = adler2::adler32_slice(sector_bytes);
                    sector_crcs.push(crc);
                }

                // Compress sector if needed
                let compressed_sector = if *compression != 0 && !sector_bytes.is_empty() {
                    // The compress function now handles the compression byte prefix
                    // and only returns compressed data if it's beneficial
                    let compressed = compress(sector_bytes, *compression)?;
                    if compressed != *sector_bytes {
                        // Compression was beneficial and the data now includes the method byte
                        flags |= BlockEntry::FLAG_COMPRESS;
                        compressed
                    } else {
                        // Compression not beneficial, returned original data
                        sector_bytes.to_vec()
                    }
                } else {
                    sector_bytes.to_vec()
                };

                sector_data.extend_from_slice(&compressed_sector);
            }

            // Set last offset
            sector_offsets[sector_count] = (data_start + sector_data.len()) as u32;

            // Log CRC generation if enabled
            if self.generate_crcs {
                log::debug!(
                    "Generated {} sector CRCs for file {}, first few: {:?}",
                    sector_count,
                    archive_name,
                    &sector_crcs[..5.min(sector_crcs.len())]
                );
            }

            // Encrypt if needed
            if *encrypt {
                flags |= BlockEntry::FLAG_ENCRYPTED;
                if *use_fix_key {
                    flags |= BlockEntry::FLAG_FIX_KEY;
                }
                let key =
                    self.calculate_file_key(archive_name, *file_pos, file_data.len() as u32, flags);

                // Save original offsets for sector encryption
                let original_offsets = sector_offsets.clone();

                // Encrypt sector offset table
                let offset_key = key.wrapping_sub(1);
                self.encrypt_data_u32(&mut sector_offsets, offset_key);

                // Encrypt each sector using the original (unencrypted) offsets
                let mut encrypted_sectors = Vec::new();
                for (i, offset_pair) in original_offsets.windows(2).enumerate() {
                    let start = (offset_pair[0] - data_start as u32) as usize;
                    let end = (offset_pair[1] - data_start as u32) as usize;

                    let mut sector = sector_data[start..end].to_vec();
                    let sector_key = key.wrapping_add(i as u32);
                    self.encrypt_data(&mut sector, sector_key);
                    encrypted_sectors.extend_from_slice(&sector);
                }

                sector_data = encrypted_sectors;
            }

            // Write sector offset table
            for offset in &sector_offsets {
                writer.write_u32_le(*offset)?;
            }

            // Write CRC table if enabled
            if self.generate_crcs {
                for crc in &sector_crcs {
                    writer.write_u32_le(*crc)?;
                }
            }

            // Write sector data
            writer.write_all(&sector_data)?;

            // Return size NOT including CRC table (offset table + sector data only)
            let total_size = offset_table_size + sector_data.len();
            Ok((total_size, flags))
        }
    }

    /// Write the attributes file to the archive
    fn write_attributes_file<W: Write + Seek>(
        &self,
        writer: &mut W,
        hash_table: &mut HashTable,
        block_table: &mut BlockTable,
        file_attributes: Vec<FileAttributes>,
        block_index: usize,
    ) -> Result<()> {
        // Check if we're actually generating attributes
        let flags = match self.attributes_option {
            AttributesOption::GenerateCrc32 => AttributeFlags::CRC32,
            AttributesOption::GenerateFull => {
                AttributeFlags::CRC32 | AttributeFlags::MD5 | AttributeFlags::FILETIME
            }
            _ => return Ok(()), // Should not happen due to earlier checks
        };

        // Create attributes structure
        let attributes = Attributes {
            version: Attributes::EXPECTED_VERSION,
            flags: AttributeFlags::new(flags),
            file_attributes,
            crc32: None,    // Phase 1 stub
            md5: None,      // Phase 1 stub
            filetime: None, // Phase 1 stub
        };

        // Convert to bytes
        let attributes_data = attributes.to_bytes()?;

        // Write the attributes file
        let file_pos = writer.stream_position()?;
        writer.write_all(&attributes_data)?;

        // Use the provided block index

        let block_entry = BlockEntry {
            file_pos: file_pos as u32,
            compressed_size: attributes_data.len() as u32,
            file_size: attributes_data.len() as u32,
            flags: BlockEntry::FLAG_EXISTS,
        };

        // Update the block table entry
        if let Some(entry) = block_table.get_mut(block_index) {
            *entry = block_entry;
        } else {
            return Err(Error::invalid_format(
                "Invalid block index for attributes file",
            ));
        }

        // Add to hash table
        self.add_to_hash_table(hash_table, "(attributes)", block_index as u32, 0)?;

        Ok(())
    }

    /// Add a file to the hash table
    fn add_to_hash_table(
        &self,
        hash_table: &mut HashTable,
        filename: &str,
        block_index: u32,
        locale: u16,
    ) -> Result<()> {
        let table_offset = hash_string(filename, hash_type::TABLE_OFFSET);
        let name_a = hash_string(filename, hash_type::NAME_A);
        let name_b = hash_string(filename, hash_type::NAME_B);

        let table_size = hash_table.size() as u32;
        let mut index = table_offset & (table_size - 1);

        // Linear probing to find empty slot
        loop {
            let entry = hash_table
                .get_mut(index as usize)
                .ok_or_else(|| Error::invalid_format("Hash table index out of bounds"))?;

            if entry.is_empty() {
                // Found empty slot
                *entry = HashEntry {
                    name_1: name_a,
                    name_2: name_b,
                    locale,
                    platform: 0, // Always 0 - platform codes are vestigial
                    block_index,
                };
                break;
            }

            // Check for duplicate
            if entry.name_1 == name_a && entry.name_2 == name_b && entry.locale == locale {
                return Err(Error::invalid_format(format!(
                    "Duplicate file in archive: {filename}"
                )));
            }

            // Move to next slot
            index = (index + 1) & (table_size - 1);
        }

        Ok(())
    }

    /// Write the hash table
    fn write_hash_table<W: Write>(
        &self,
        writer: &mut W,
        hash_table: &HashTable,
    ) -> Result<[u8; 16]> {
        // Convert to bytes for encryption
        let mut table_data = Vec::new();
        for entry in hash_table.entries() {
            table_data.write_u32_le(entry.name_1)?;
            table_data.write_u32_le(entry.name_2)?;
            table_data.write_u16_le(entry.locale)?;
            table_data.write_u16_le(entry.platform)?;
            table_data.write_u32_le(entry.block_index)?;
        }

        // Encrypt the table
        let key = hash_string("(hash table)", hash_type::FILE_KEY);
        self.encrypt_data(&mut table_data, key);

        // Calculate MD5 of encrypted data (for v4)
        let md5 = self.calculate_md5(&table_data);

        // Write encrypted table
        writer.write_all(&table_data)?;

        Ok(md5)
    }

    /// Write the block table
    fn write_block_table<W: Write>(
        &self,
        writer: &mut W,
        block_table: &BlockTable,
    ) -> Result<[u8; 16]> {
        // Convert to bytes for encryption
        let mut table_data = Vec::new();
        for entry in block_table.entries() {
            table_data.write_u32_le(entry.file_pos)?;
            table_data.write_u32_le(entry.compressed_size)?;
            table_data.write_u32_le(entry.file_size)?;
            table_data.write_u32_le(entry.flags)?;
        }

        // Encrypt the table
        let key = hash_string("(block table)", hash_type::FILE_KEY);
        self.encrypt_data(&mut table_data, key);

        // Calculate MD5 of encrypted data (for v4)
        let md5 = self.calculate_md5(&table_data);

        // Write encrypted table
        writer.write_all(&table_data)?;

        Ok(md5)
    }

    /// Write the hi-block table
    fn write_hi_block_table<W: Write>(
        &self,
        writer: &mut W,
        hi_block_table: &HiBlockTable,
    ) -> Result<[u8; 16]> {
        // Hi-block table is not encrypted
        let mut table_data = Vec::new();
        for &entry in hi_block_table.entries() {
            table_data.write_u16_le(entry)?;
        }

        // Calculate MD5 (for v4)
        let md5 = self.calculate_md5(&table_data);

        // Write table
        writer.write_all(&table_data)?;

        Ok(md5)
    }

    /// Write the MPQ header
    fn write_header<W: Write + Seek>(
        &self,
        writer: &mut W,
        params: &HeaderWriteParams,
    ) -> Result<()> {
        // Write signature
        writer.write_u32_le(crate::signatures::MPQ_ARCHIVE)?;

        // Write header size
        writer.write_u32_le(self.version.header_size())?;

        // Write archive size (32-bit for v1, deprecated in v2+)
        writer.write_u32_le(params.archive_size.min(u32::MAX as u64) as u32)?;

        // Write format version
        writer.write_u16_le(self.version as u16)?;

        // Write block size
        writer.write_u16_le(self.block_size)?;

        // Write table positions and sizes (low 32 bits)
        writer.write_u32_le(params.hash_table_pos as u32)?;
        writer.write_u32_le(params.block_table_pos as u32)?;
        writer.write_u32_le(params.hash_table_size)?;
        writer.write_u32_le(params.block_table_size)?;

        // Write version-specific fields
        match self.version {
            FormatVersion::V1 => {
                // No additional fields
            }
            FormatVersion::V2 => {
                // Hi-block table position
                writer.write_u64_le(params.hi_block_table_pos.unwrap_or(0))?;

                // High 16 bits of positions
                writer.write_u16_le((params.hash_table_pos >> 32) as u16)?; // hash_table_pos_hi
                writer.write_u16_le((params.block_table_pos >> 32) as u16)?; // block_table_pos_hi
            }
            FormatVersion::V3 => {
                // V2 fields
                writer.write_u64_le(params.hi_block_table_pos.unwrap_or(0))?; // hi_block_table_pos
                writer.write_u16_le((params.hash_table_pos >> 32) as u16)?; // hash_table_pos_hi
                writer.write_u16_le((params.block_table_pos >> 32) as u16)?; // block_table_pos_hi

                // V3 fields
                writer.write_u64_le(params.archive_size)?; // archive_size_64
                writer.write_u64_le(params.het_table_pos.unwrap_or(0))?; // het_table_pos
                writer.write_u64_le(params.bet_table_pos.unwrap_or(0))?; // bet_table_pos
            }
            FormatVersion::V4 => {
                // V2 fields
                writer.write_u64_le(params.hi_block_table_pos.unwrap_or(0))?; // hi_block_table_pos
                writer.write_u16_le((params.hash_table_pos >> 32) as u16)?; // hash_table_pos_hi
                writer.write_u16_le((params.block_table_pos >> 32) as u16)?; // block_table_pos_hi

                // V3 fields
                writer.write_u64_le(params.archive_size)?; // archive_size_64
                writer.write_u64_le(params.het_table_pos.unwrap_or(0))?; // het_table_pos
                writer.write_u64_le(params.bet_table_pos.unwrap_or(0))?; // bet_table_pos

                // V4 fields
                if let Some(v4_data) = &params.v4_data {
                    writer.write_u64_le(v4_data.hash_table_size_64)?;
                    writer.write_u64_le(v4_data.block_table_size_64)?;
                    writer.write_u64_le(v4_data.hi_block_table_size_64)?;
                    writer.write_u64_le(v4_data.het_table_size_64)?;
                    writer.write_u64_le(v4_data.bet_table_size_64)?;
                    writer.write_u32_le(v4_data.raw_chunk_size)?;

                    // Write MD5 hashes (all except header MD5 which is calculated later)
                    writer.write_all(&v4_data.md5_block_table)?;
                    writer.write_all(&v4_data.md5_hash_table)?;
                    writer.write_all(&v4_data.md5_hi_block_table)?;
                    writer.write_all(&v4_data.md5_bet_table)?;
                    writer.write_all(&v4_data.md5_het_table)?;
                    writer.write_all(&v4_data.md5_mpq_header)?;
                } else {
                    return Err(Error::invalid_format("V4 format requires v4_data"));
                }
            }
        }

        Ok(())
    }

    /// Calculate file encryption key
    fn calculate_file_key(&self, filename: &str, file_pos: u64, file_size: u32, flags: u32) -> u32 {
        let base_key = hash_string(filename, hash_type::FILE_KEY);

        if flags & BlockEntry::FLAG_FIX_KEY != 0 {
            // For FIX_KEY, use only the low 32 bits of the file position
            (base_key.wrapping_add(file_pos as u32)) ^ file_size
        } else {
            base_key
        }
    }

    /// Encrypt data in place
    pub fn encrypt_data(&self, data: &mut [u8], key: u32) {
        if data.is_empty() || key == 0 {
            return;
        }

        // Process full u32 chunks
        let (chunks, remainder) = data.split_at_mut((data.len() / 4) * 4);

        // Convert chunks to u32 values, encrypt, and write back
        let mut u32_buffer = Vec::with_capacity(chunks.len() / 4);
        for chunk in chunks.chunks_exact(4) {
            u32_buffer.push(u32::from_le_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]));
        }

        encrypt_block(&mut u32_buffer, key);

        // Write encrypted u32s back to bytes
        for (i, &encrypted) in u32_buffer.iter().enumerate() {
            let bytes = encrypted.to_le_bytes();
            chunks[i * 4..(i + 1) * 4].copy_from_slice(&bytes);
        }

        // Handle remaining bytes
        if !remainder.is_empty() {
            let mut last_dword = [0u8; 4];
            last_dword[..remainder.len()].copy_from_slice(remainder);

            let mut last_u32 = u32::from_le_bytes(last_dword);
            encrypt_block(
                std::slice::from_mut(&mut last_u32),
                key.wrapping_add((chunks.len() / 4) as u32),
            );

            let encrypted_bytes = last_u32.to_le_bytes();
            remainder.copy_from_slice(&encrypted_bytes[..remainder.len()]);
        }
    }
    /// Encrypt u32 data in place
    fn encrypt_data_u32(&self, data: &mut [u32], key: u32) {
        encrypt_block(data, key);
    }

    /// Calculate MD5 hash of data
    fn calculate_md5(&self, data: &[u8]) -> [u8; 16] {
        let mut hasher = Md5::new();
        hasher.update(data);
        hasher.finalize().into()
    }

    /// Finalize V4 header by calculating and writing the header MD5
    fn finalize_v4_header_md5<W: Write + Seek + Read>(&self, writer: &mut W) -> Result<()> {
        // Read the header data (excluding the MD5 field itself)
        writer.seek(SeekFrom::Start(0))?;
        let header_size = self.version.header_size() as usize;
        let md5_size = 16;
        let header_data_size = header_size - md5_size; // 208 - 16 = 192 bytes

        let mut header_data = vec![0u8; header_data_size];
        writer.read_exact(&mut header_data)?;

        // Calculate MD5 of header data
        let header_md5 = self.calculate_md5(&header_data);

        // Write the MD5 at the end of the header (offset 0xC0 = 192)
        writer.seek(SeekFrom::Start(192))?;
        writer.write_all(&header_md5)?;

        Ok(())
    }

    /// Create HET table data using the final hash table (includes attributes file)
    fn create_het_table_with_hash_table(
        &self,
        hash_table: &HashTable,
    ) -> Result<(Vec<u8>, HetHeader)> {
        // Count actual files from the hash table
        let mut file_count = 0u32;

        // Extract filenames from hash table entries
        for entry in hash_table.entries() {
            if !entry.is_empty() {
                file_count += 1;
            }
        }

        // For simplicity, let's use the same logic as create_het_table but with proper file count
        let hash_table_entries = (file_count * 2).next_power_of_two();

        log::debug!(
            "Creating HET table from hash_table: {file_count} files, {hash_table_entries} hash entries"
        );

        // Create header
        let header = HetHeader {
            table_size: 0, // Will be calculated later
            max_file_count: file_count,
            hash_table_size: hash_table_entries, // Number of hash entries (in bytes)
            hash_entry_size: 8,                  // Always 8 bits for the name hash
            total_index_size: hash_table_entries * Self::calculate_bits_needed(file_count as u64),
            index_size_extra: 0,
            index_size: Self::calculate_bits_needed(file_count as u64),
            block_table_size: 0, // Not used
        };

        // Copy values from packed struct to avoid alignment issues
        let index_size = header.index_size;

        // Create hash table (8-bit name hashes)
        let mut het_hash_table = vec![0xFFu8; hash_table_entries as usize]; // Initialize with 0xFF (empty)

        // Create file indices array
        let file_indices_size = (header.total_index_size as usize).div_ceil(8);
        let mut file_indices = vec![0u8; file_indices_size]; // Initialize with 0

        // Pre-fill with invalid indices (all bits set)
        let invalid_index = (1u64 << index_size) - 1; // e.g., 0b111 for 3 bits = 7
        for i in 0..hash_table_entries {
            self.write_bit_entry(&mut file_indices, i as usize, invalid_index, index_size)?;
        }

        // Process files from the original pending_files plus attributes if present
        let mut file_index = 0;

        // Process pending files (excluding attributes to match write order)
        let collect_attributes = matches!(
            self.attributes_option,
            AttributesOption::GenerateCrc32 | AttributesOption::GenerateFull
        );

        for pending_file in self.pending_files.iter() {
            // Skip (attributes) file if it's being generated - we'll add it later
            if pending_file.archive_name == "(attributes)" && collect_attributes {
                continue;
            }

            let hash_bits = 8;
            let (hash, name_hash1) = het_hash(&pending_file.archive_name, hash_bits);

            // Calculate starting position for linear probing
            let start_index = (hash % hash_table_entries as u64) as usize;

            // Linear probing for collision resolution
            let mut current_index = start_index;
            loop {
                // Check if slot is empty (0xFF)
                if het_hash_table[current_index] == 0xFF {
                    // Store the 8-bit name hash
                    het_hash_table[current_index] = name_hash1;

                    // Store the file index in the bit-packed array
                    self.write_bit_entry(
                        &mut file_indices,
                        current_index,
                        file_index as u64,
                        index_size,
                    )?;

                    break;
                }

                current_index = (current_index + 1) % hash_table_entries as usize;
                if current_index == start_index {
                    return Err(Error::invalid_format("HET table full"));
                }
            }
            file_index += 1;
        }

        // Add attributes file if it was generated (use the same file_index that write_attributes_file used)
        if collect_attributes {
            let hash_bits = 8;
            let (hash, name_hash1) = het_hash("(attributes)", hash_bits);
            let start_index = (hash % hash_table_entries as u64) as usize;

            let mut current_index = start_index;
            loop {
                if het_hash_table[current_index] == 0xFF {
                    het_hash_table[current_index] = name_hash1;
                    self.write_bit_entry(
                        &mut file_indices,
                        current_index,
                        file_index as u64,
                        index_size,
                    )?;
                    break;
                }

                current_index = (current_index + 1) % hash_table_entries as usize;
                if current_index == start_index {
                    return Err(Error::invalid_format("HET table full"));
                }
            }
        }

        // Calculate sizes
        let het_header_size = std::mem::size_of::<HetHeader>();
        let data_size = het_header_size as u32 + hash_table_entries + file_indices_size as u32;
        let table_size = 12 + data_size; // Extended header (12 bytes) + data

        // Update header with final size
        let mut final_header = header;
        final_header.table_size = table_size;

        // Build the final result
        let mut result = Vec::with_capacity((12 + data_size) as usize);

        // Write extended header
        result.write_u32_le(0x1A544548)?; // "HET\x1A"
        result.write_u32_le(1)?; // version
        result.write_u32_le(data_size)?; // data_size

        // Write HET header
        result.write_u32_le(final_header.table_size)?;
        result.write_u32_le(final_header.max_file_count)?;
        result.write_u32_le(final_header.hash_table_size)?;
        result.write_u32_le(final_header.hash_entry_size)?;
        result.write_u32_le(final_header.total_index_size)?;
        result.write_u32_le(final_header.index_size_extra)?;
        result.write_u32_le(final_header.index_size)?;
        result.write_u32_le(final_header.block_table_size)?;

        // Write hash table and file indices
        result.extend_from_slice(&het_hash_table);
        result.extend_from_slice(&file_indices);

        log::debug!("HET table created with {file_count} files (including attributes)");

        Ok((result, final_header))
    }

    /// Write a bit-packed entry to a byte array
    fn write_bit_entry(
        &self,
        data: &mut [u8],
        index: usize,
        value: u64,
        bit_size: u32,
    ) -> Result<()> {
        let bit_offset = index * bit_size as usize;
        let byte_offset = bit_offset / 8;
        let bit_shift = bit_offset % 8;

        // Calculate how many bytes we actually need
        let bits_needed = bit_shift + bit_size as usize;
        let bytes_needed = bits_needed.div_ceil(8);

        if byte_offset + bytes_needed > data.len() {
            log::error!(
                "Bit entry out of bounds: index={}, bit_size={}, bit_offset={}, byte_offset={}, bytes_needed={}, data.len()={}",
                index,
                bit_size,
                bit_offset,
                byte_offset,
                bytes_needed,
                data.len()
            );
            return Err(Error::invalid_format("Bit entry out of bounds"));
        }

        // Read existing bits (limit to 8 bytes for u64)
        let mut existing = 0u64;
        let max_bytes = bytes_needed.min(8);
        for i in 0..max_bytes {
            if byte_offset + i < data.len() && i * 8 < 64 {
                existing |= (data[byte_offset + i] as u64) << (i * 8);
            }
        }

        // Clear the bits we're about to write
        let value_mask = if bit_size >= 64 {
            u64::MAX
        } else {
            (1u64 << bit_size) - 1
        };
        let mask = value_mask << bit_shift;
        existing &= !mask;

        // Write the new value
        existing |= (value & value_mask) << bit_shift;

        // Write back (limit to 8 bytes for u64)
        for i in 0..max_bytes {
            if byte_offset + i < data.len() && i * 8 < 64 {
                data[byte_offset + i] = (existing >> (i * 8)) as u8;
            }
        }

        Ok(())
    }

    /// Calculate the number of bits needed to represent a value
    fn calculate_bits_needed(max_value: u64) -> u32 {
        if max_value == 0 {
            1
        } else {
            (64 - max_value.leading_zeros()).max(1)
        }
    }

    /// Write HET table to the archive, returns the written size and MD5
    fn write_het_table<W: Write>(
        &self,
        writer: &mut W,
        data: &[u8],
        encrypt: bool,
    ) -> Result<(u64, [u8; 16])> {
        // HET table structure:
        // - Extended header (12 bytes) - NEVER encrypted
        // - Table data (rest) - can be compressed and/or encrypted

        if data.len() < 12 {
            return Err(Error::invalid_format("HET table data too small"));
        }

        // Split extended header and table data
        let (extended_header, table_data) = data.split_at(12);
        let mut processed_data = table_data.to_vec();

        log::debug!(
            "HET table before processing: extended_header len={}, table_data len={}",
            extended_header.len(),
            processed_data.len()
        );
        log::debug!(
            "HET table data (first 20 bytes): {:?}",
            &processed_data[..processed_data.len().min(20)]
        );

        // Compress if enabled and this is a v3+ archive
        if self.compress_tables && matches!(self.version, FormatVersion::V3 | FormatVersion::V4) {
            log::debug!("Compressing HET table data: {} -> ", processed_data.len());
            let compressed = compress(&processed_data, self.table_compression)?;
            log::debug!(
                "{} bytes ({}% reduction)",
                compressed.len(),
                (100 * (processed_data.len() - compressed.len()) / processed_data.len())
            );

            // Prepend compression type byte
            let mut compressed_with_type = Vec::with_capacity(1 + compressed.len());
            compressed_with_type.push(self.table_compression);
            compressed_with_type.extend_from_slice(&compressed);
            processed_data = compressed_with_type;
        }

        // Encrypt the data portion (after extended header)
        if encrypt {
            let key = hash_string("(hash table)", hash_type::FILE_KEY);
            log::debug!("Encrypting HET table with key: 0x{key:08X}");
            log::debug!(
                "Data size: {} bytes (multiple of 4: {})",
                processed_data.len(),
                processed_data.len() % 4 == 0
            );
            log::debug!(
                "Data before encryption (last 10 bytes): {:?}",
                &processed_data[processed_data.len().saturating_sub(10)..]
            );
            self.encrypt_data(&mut processed_data, key);
            log::debug!(
                "Data after encryption (last 10 bytes): {:?}",
                &processed_data[processed_data.len().saturating_sub(10)..]
            );
        }

        // Combine extended header with processed data
        let mut final_data = Vec::with_capacity(extended_header.len() + processed_data.len());
        final_data.extend_from_slice(extended_header);
        final_data.extend_from_slice(&processed_data);

        // Calculate MD5 of final data
        let md5 = self.calculate_md5(&final_data);

        let written_size = final_data.len() as u64;
        writer.write_all(&final_data)?;
        Ok((written_size, md5))
    }

    /// Create BET table data
    fn create_bet_table(&self, block_table: &BlockTable) -> Result<(Vec<u8>, BetHeader)> {
        // Get actual file count from block table entries (includes attributes if generated)
        let file_count = block_table.entries().len() as u32;

        // Analyze block table to determine optimal bit widths
        let mut max_file_pos = 0u64;
        let mut max_file_size = 0u64;
        let mut max_compressed_size = 0u64;
        let mut unique_flags = std::collections::HashSet::new();

        for i in 0..file_count as usize {
            if let Some(entry) = block_table.get(i) {
                max_file_pos = max_file_pos.max(entry.file_pos as u64);
                max_file_size = max_file_size.max(entry.file_size as u64);
                max_compressed_size = max_compressed_size.max(entry.compressed_size as u64);
                unique_flags.insert(entry.flags);
            }
        }

        // Calculate bit counts for each field
        let bit_count_file_pos = Self::calculate_bits_needed(max_file_pos);
        let bit_count_file_size = Self::calculate_bits_needed(max_file_size);
        let bit_count_cmp_size = Self::calculate_bits_needed(max_compressed_size);
        let bit_count_flag_index = if unique_flags.is_empty() {
            0
        } else {
            Self::calculate_bits_needed(unique_flags.len() as u64 - 1)
        };
        let bit_count_unknown = 0; // Not used

        // Calculate bit positions
        let bit_index_file_pos = 0;
        let bit_index_file_size = bit_index_file_pos + bit_count_file_pos;
        let bit_index_cmp_size = bit_index_file_size + bit_count_file_size;
        let bit_index_flag_index = bit_index_cmp_size + bit_count_cmp_size;
        let bit_index_unknown = bit_index_flag_index + bit_count_flag_index;

        // Calculate table entry size
        let table_entry_size = bit_index_unknown + bit_count_unknown;

        // Create flag array
        let mut flag_array: Vec<u32> = unique_flags.into_iter().collect();
        flag_array.sort();
        let flag_count = flag_array.len() as u32;

        // Create flag index map
        let mut flag_index_map = std::collections::HashMap::new();
        for (index, &flags) in flag_array.iter().enumerate() {
            flag_index_map.insert(flags, index as u32);
        }

        // Calculate table sizes
        let file_table_bits = file_count * table_entry_size;
        let file_table_size = file_table_bits.div_ceil(8); // Round up to bytes

        // BET hash information (simplified - we'll use 64-bit hashes)
        let bet_hash_size = 64;
        let total_bet_hash_size = file_count * bet_hash_size;
        let bet_hash_size_extra = 0;
        let bet_hash_array_size = total_bet_hash_size.div_ceil(8);

        // Create header (without extended header fields)
        let header = BetHeader {
            table_size: 0, // Will be calculated later
            file_count,
            unknown_08: 0x10,
            table_entry_size,
            bit_index_file_pos,
            bit_index_file_size,
            bit_index_cmp_size,
            bit_index_flag_index,
            bit_index_unknown,
            bit_count_file_pos,
            bit_count_file_size,
            bit_count_cmp_size,
            bit_count_flag_index,
            bit_count_unknown,
            total_bet_hash_size,
            bet_hash_size_extra,
            bet_hash_size,
            bet_hash_array_size,
            flag_count,
        };

        // Create file table
        let mut file_table = vec![0u8; file_table_size as usize];

        // Create BET hashes
        let mut bet_hashes = Vec::with_capacity(file_count as usize);

        // Fill tables
        for i in 0..file_count as usize {
            if let Some(entry) = block_table.get(i) {
                // Get flag index
                let flag_index = flag_index_map.get(&entry.flags).unwrap();

                // Pack entry data
                let mut entry_bits = 0u64;
                entry_bits |= (entry.file_pos as u64) << bit_index_file_pos;
                entry_bits |= (entry.file_size as u64) << bit_index_file_size;
                entry_bits |= (entry.compressed_size as u64) << bit_index_cmp_size;
                entry_bits |= (*flag_index as u64) << bit_index_flag_index;

                // Write to file table
                self.write_bit_entry(&mut file_table, i, entry_bits, table_entry_size)?;

                // Generate BET hash (Jenkins one-at-a-time hash of filename)
                // Note: BET uses Jenkins one-at-a-time, not hashlittle2 like HET
                let filename = if i < self.pending_files.len() {
                    &self.pending_files[i].archive_name
                } else {
                    // This must be the attributes file
                    "(attributes)"
                };
                let hash = jenkins_hash(filename);
                bet_hashes.push(hash);
            }
        }

        // Calculate final sizes
        let bet_header_size = std::mem::size_of::<BetHeader>();
        let flag_array_size = flag_count * 4;
        let data_size =
            bet_header_size as u32 + flag_array_size + file_table_size + bet_hash_array_size;
        let table_size = 12 + data_size; // Extended header (12 bytes) + data

        // Update header with final size
        let mut final_header = header;
        final_header.table_size = table_size;

        // Serialize everything
        let mut result = Vec::with_capacity((12 + data_size) as usize);

        // Write extended header first
        result.write_u32_le(0x1A544542)?; // "BET\x1A"
        result.write_u32_le(1)?; // version
        result.write_u32_le(data_size)?; // data_size

        // Then write the BET header
        result.write_u32_le(final_header.table_size)?;
        result.write_u32_le(final_header.file_count)?;
        result.write_u32_le(final_header.unknown_08)?;
        result.write_u32_le(final_header.table_entry_size)?;
        result.write_u32_le(final_header.bit_index_file_pos)?;
        result.write_u32_le(final_header.bit_index_file_size)?;
        result.write_u32_le(final_header.bit_index_cmp_size)?;
        result.write_u32_le(final_header.bit_index_flag_index)?;
        result.write_u32_le(final_header.bit_index_unknown)?;
        result.write_u32_le(final_header.bit_count_file_pos)?;
        result.write_u32_le(final_header.bit_count_file_size)?;
        result.write_u32_le(final_header.bit_count_cmp_size)?;
        result.write_u32_le(final_header.bit_count_flag_index)?;
        result.write_u32_le(final_header.bit_count_unknown)?;
        result.write_u32_le(final_header.total_bet_hash_size)?;
        result.write_u32_le(final_header.bet_hash_size_extra)?;
        result.write_u32_le(final_header.bet_hash_size)?;
        result.write_u32_le(final_header.bet_hash_array_size)?;
        result.write_u32_le(final_header.flag_count)?;

        // Write flag array
        for &flags in &flag_array {
            result.write_u32_le(flags)?;
        }

        // Write file table
        result.extend_from_slice(&file_table);

        // Write BET hashes (bit-packed)
        let mut hash_bytes = vec![0u8; bet_hash_array_size as usize];
        for (i, &hash) in bet_hashes.iter().enumerate() {
            self.write_bit_entry(&mut hash_bytes, i, hash, bet_hash_size)?;
        }
        result.extend_from_slice(&hash_bytes);

        Ok((result, final_header))
    }

    /// Write BET table to the archive, returns the written size and MD5
    fn write_bet_table<W: Write>(
        &self,
        writer: &mut W,
        data: &[u8],
        encrypt: bool,
    ) -> Result<(u64, [u8; 16])> {
        // BET table structure:
        // - Extended header (12 bytes) - NEVER encrypted
        // - Table data (rest) - can be compressed and/or encrypted

        if data.len() < 12 {
            return Err(Error::invalid_format("BET table data too small"));
        }

        // Split extended header and table data
        let (extended_header, table_data) = data.split_at(12);
        let mut processed_data = table_data.to_vec();

        // Compress if enabled and this is a v3+ archive
        if self.compress_tables && matches!(self.version, FormatVersion::V3 | FormatVersion::V4) {
            log::debug!("Compressing BET table data: {} -> ", processed_data.len());
            let compressed = compress(&processed_data, self.table_compression)?;
            log::debug!(
                "{} bytes ({}% reduction)",
                compressed.len(),
                (100 * (processed_data.len() - compressed.len()) / processed_data.len())
            );

            // Prepend compression type byte
            let mut compressed_with_type = Vec::with_capacity(1 + compressed.len());
            compressed_with_type.push(self.table_compression);
            compressed_with_type.extend_from_slice(&compressed);
            processed_data = compressed_with_type;
        }

        // Encrypt the data portion (after extended header)
        if encrypt {
            let key = hash_string("(block table)", hash_type::FILE_KEY);
            self.encrypt_data(&mut processed_data, key);
        }

        // Combine extended header with processed data
        let mut final_data = Vec::with_capacity(extended_header.len() + processed_data.len());
        final_data.extend_from_slice(extended_header);
        final_data.extend_from_slice(&processed_data);

        // Calculate MD5 of final data
        let md5 = self.calculate_md5(&final_data);

        let written_size = final_data.len() as u64;
        writer.write_all(&final_data)?;
        Ok((written_size, md5))
    }
}

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