edgevec 0.9.0

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

use crate::persistence::PersistenceError;
use bitvec::prelude::*;
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
use std::collections::BinaryHeap;
use thiserror::Error;

// ============================================================================
// ERROR TYPES
// ============================================================================

/// Errors that can occur during FlatIndex operations.
#[derive(Debug, Clone, PartialEq, Error)]
pub enum FlatIndexError {
    /// Vector dimension does not match index configuration.
    #[error("dimension mismatch: expected {expected}, got {actual}")]
    DimensionMismatch {
        /// Expected dimension from index configuration.
        expected: usize,
        /// Actual dimension of the provided vector.
        actual: usize,
    },

    /// Invalid search parameter k (must be > 0).
    #[error("invalid k: must be greater than 0")]
    InvalidK,

    /// Quantization is not enabled but quantized search was requested.
    #[error("quantization not enabled: call enable_quantization() first")]
    QuantizationNotEnabled,

    /// Index is empty (no vectors to search).
    #[error("index is empty")]
    EmptyIndex,

    /// ID counter overflow (u64::MAX reached).
    #[error("ID counter overflow: cannot assign more IDs (u64::MAX reached)")]
    IdOverflow,
}

// ============================================================================
// DISTANCE METRIC
// ============================================================================

/// Distance metric for vector comparison.
///
/// Determines how distances/similarities are computed during search.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum DistanceMetric {
    /// Cosine similarity: dot(a, b) / (||a|| * ||b||).
    /// Result is in [-1, 1], where 1 = identical, -1 = opposite.
    /// Search returns highest similarity first.
    #[default]
    Cosine,

    /// Dot product: sum(a[i] * b[i]).
    /// Result is unbounded. Higher = more similar.
    /// Search returns highest dot product first.
    DotProduct,

    /// L2 (Euclidean) distance: sqrt(sum((a[i] - b[i])^2)).
    /// Result is in [0, inf). Lower = more similar.
    /// Search returns lowest distance first.
    L2,

    /// Hamming distance: count of positions where a[i] != b[i].
    /// For use with binary-like vectors (0.0 vs non-zero).
    /// Result is in [0, dim]. Lower = more similar.
    /// Search returns lowest distance first.
    Hamming,
}

impl DistanceMetric {
    /// Returns true if this metric measures similarity (higher = better).
    /// Returns false if this metric measures distance (lower = better).
    #[must_use]
    pub const fn is_similarity(&self) -> bool {
        matches!(self, Self::Cosine | Self::DotProduct)
    }
}

// ============================================================================
// SEARCH RESULT
// ============================================================================

/// Search result from FlatIndex.
///
/// Contains the vector ID and its distance/similarity score to the query.
#[derive(Debug, Clone)]
pub struct FlatSearchResult {
    /// Vector ID in the index.
    pub id: u64,

    /// Distance or similarity score.
    /// - For distance metrics (L2, Hamming): lower is better
    /// - For similarity metrics (Cosine, Dot): higher is better
    pub score: f32,
}

impl PartialEq for FlatSearchResult {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id && (self.score - other.score).abs() < f32::EPSILON
    }
}

impl Eq for FlatSearchResult {}

/// Internal wrapper for max-heap behavior during search.
/// The heap keeps the k-worst results at the top for easy removal.
#[derive(Debug, Clone)]
struct HeapEntry {
    id: u64,
    score: f32,
}

impl PartialEq for HeapEntry {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id && (self.score - other.score).abs() < f32::EPSILON
    }
}

impl Eq for HeapEntry {}

impl PartialOrd for HeapEntry {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for HeapEntry {
    fn cmp(&self, other: &Self) -> Ordering {
        // Max-heap: higher score = higher priority (for removal)
        self.score
            .partial_cmp(&other.score)
            .unwrap_or(Ordering::Equal)
    }
}

// ============================================================================
// CONFIGURATION
// ============================================================================

/// Configuration for FlatIndex.
///
/// # Example
///
/// ```rust
/// use edgevec::index::{FlatIndexConfig, DistanceMetric};
///
/// let config = FlatIndexConfig::new(128)
///     .with_metric(DistanceMetric::Cosine)
///     .with_capacity(5000)
///     .with_cleanup_threshold(0.3);
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FlatIndexConfig {
    /// Vector dimension (must match all inserted vectors).
    pub dimensions: u32,

    /// Distance metric for search operations.
    pub metric: DistanceMetric,

    /// Initial capacity hint for pre-allocation.
    /// The index will grow automatically if this is exceeded.
    pub initial_capacity: usize,

    /// Cleanup threshold: fraction of deleted vectors that triggers compaction.
    /// Range: [0.0, 1.0]. Default: 0.5 (compact when 50% deleted).
    pub cleanup_threshold: f32,
}

impl FlatIndexConfig {
    /// Create a new configuration with the given vector dimension.
    ///
    /// Uses default values:
    /// - Metric: Cosine
    /// - Initial capacity: 1000
    /// - Cleanup threshold: 0.5
    #[must_use]
    pub fn new(dimensions: u32) -> Self {
        Self {
            dimensions,
            metric: DistanceMetric::Cosine,
            initial_capacity: 1000,
            cleanup_threshold: 0.5,
        }
    }

    /// Set the distance metric.
    #[must_use]
    pub fn with_metric(mut self, metric: DistanceMetric) -> Self {
        self.metric = metric;
        self
    }

    /// Set the initial capacity (number of vectors to pre-allocate).
    #[must_use]
    pub fn with_capacity(mut self, capacity: usize) -> Self {
        self.initial_capacity = capacity;
        self
    }

    /// Set the cleanup threshold (0.0 to 1.0).
    ///
    /// When the fraction of deleted vectors exceeds this threshold,
    /// compaction is triggered to reclaim memory.
    #[must_use]
    pub fn with_cleanup_threshold(mut self, threshold: f32) -> Self {
        self.cleanup_threshold = threshold.clamp(0.0, 1.0);
        self
    }
}

// ============================================================================
// FLAT INDEX
// ============================================================================

/// Flat (brute-force) index for exact nearest neighbor search.
///
/// Stores vectors in row-major layout for simple slicing.
/// Provides O(1) insertion and O(n·d) search with 100% recall guarantee.
///
/// # ID Convention
///
/// `FlatIndex` uses **0-based direct indexing**. The first inserted vector
/// receives ID `0`, the second receives `1`, and so on. IDs are assigned
/// by a monotonically increasing `next_id` counter and are never reused
/// after deletion. This means `vector_id == slot_index` in the contiguous
/// storage, allowing O(1) retrieval via `&vectors[id * dim..(id+1) * dim]`.
///
/// # Memory Layout
///
/// - Vectors: `Vec<f32>` in row-major order (n × d elements)
/// - Deletion bitmap: `BitVec` (1 bit per vector)
/// - For 10k vectors @ 768 dimensions:
///   - F32: ~30 MB (10,000 × 768 × 4 bytes)
///   - Bitmap: ~1.25 KB
///
/// # Thread Safety
///
/// `FlatIndex` is not thread-safe by default. Wrap in `Arc<RwLock<_>>` for
/// concurrent access.
pub struct FlatIndex {
    /// Configuration (immutable after creation).
    config: FlatIndexConfig,

    /// Dense vectors in row-major layout.
    /// Layout: [v0_d0, v0_d1, ..., v0_dn, v1_d0, v1_d1, ..., v1_dn, ...]
    vectors: Vec<f32>,

    /// Number of vectors stored (including deleted slots).
    count: u64,

    /// Bitmap tracking deleted vectors.
    /// `deleted[i] == true` means vector i is deleted.
    deleted: BitVec,

    /// Number of deleted vectors (for cleanup threshold calculation).
    delete_count: usize,

    /// Next ID to assign (monotonically increasing).
    next_id: u64,

    /// Optional binary quantized vectors for BQ mode.
    /// Layout: [v0_byte0, v0_byte1, ..., v1_byte0, ...] where each byte packs 8 dimensions.
    quantized: Option<Vec<u8>>,
}

impl FlatIndex {
    /// Create a new FlatIndex with the given configuration.
    ///
    /// # Example
    ///
    /// ```rust
    /// use edgevec::index::{FlatIndex, FlatIndexConfig};
    ///
    /// let config = FlatIndexConfig::new(128);
    /// let index = FlatIndex::new(config);
    ///
    /// assert_eq!(index.dimensions(), 128);
    /// assert!(index.is_empty());
    /// ```
    #[must_use]
    pub fn new(config: FlatIndexConfig) -> Self {
        let capacity = config.initial_capacity;
        let dim = config.dimensions as usize;

        Self {
            config,
            vectors: Vec::with_capacity(capacity * dim),
            count: 0,
            deleted: BitVec::with_capacity(capacity),
            delete_count: 0,
            next_id: 0,
            quantized: None,
        }
    }

    /// Returns the vector dimension.
    #[must_use]
    pub fn dimensions(&self) -> u32 {
        self.config.dimensions
    }

    /// Returns the distance metric.
    #[must_use]
    pub fn metric(&self) -> DistanceMetric {
        self.config.metric
    }

    /// Returns the number of vectors (excluding deleted).
    #[must_use]
    #[allow(clippy::cast_possible_truncation)] // FlatIndex targets <10k vectors; u64→usize safe
    pub fn len(&self) -> usize {
        (self.count as usize).saturating_sub(self.delete_count)
    }

    /// Returns true if the index is empty (no non-deleted vectors).
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Returns the total number of slots (including deleted).
    #[must_use]
    #[allow(clippy::cast_possible_truncation)] // FlatIndex targets <10k vectors; u64→usize safe
    pub fn capacity(&self) -> usize {
        self.count as usize
    }

    /// Returns a reference to the configuration.
    #[must_use]
    pub fn config(&self) -> &FlatIndexConfig {
        &self.config
    }

    // ========================================================================
    // INSERT
    // ========================================================================

    /// Insert a vector into the index.
    ///
    /// Returns the assigned vector ID.
    ///
    /// # Errors
    ///
    /// Returns `FlatIndexError::DimensionMismatch` if the vector dimension
    /// doesn't match the index configuration.
    ///
    /// # Example
    ///
    /// ```rust
    /// use edgevec::index::{FlatIndex, FlatIndexConfig};
    ///
    /// let mut index = FlatIndex::new(FlatIndexConfig::new(3));
    ///
    /// let id = index.insert(&[1.0, 2.0, 3.0]).unwrap();
    /// assert_eq!(id, 0);
    ///
    /// let id2 = index.insert(&[4.0, 5.0, 6.0]).unwrap();
    /// assert_eq!(id2, 1);
    /// ```
    pub fn insert(&mut self, vector: &[f32]) -> Result<u64, FlatIndexError> {
        // Validate dimension
        let expected_dim = self.config.dimensions as usize;
        if vector.len() != expected_dim {
            return Err(FlatIndexError::DimensionMismatch {
                expected: expected_dim,
                actual: vector.len(),
            });
        }

        // Allocate ID (monotonically increasing, with overflow protection)
        let id = self.next_id;
        self.next_id = self
            .next_id
            .checked_add(1)
            .ok_or(FlatIndexError::IdOverflow)?;

        // Store vector (append to contiguous storage)
        self.vectors.extend_from_slice(vector);

        // Update count and deleted bitmap
        self.count += 1;
        self.deleted.push(false);

        // Invalidate quantized cache if it exists
        if self.quantized.is_some() {
            self.quantized = None;
        }

        Ok(id)
    }

    /// Insert multiple vectors in batch.
    ///
    /// Returns the IDs assigned to each vector.
    ///
    /// # Errors
    ///
    /// Returns error if any vector has wrong dimension.
    /// On error, vectors inserted before the error remain in the index.
    ///
    /// # Example
    ///
    /// ```rust
    /// use edgevec::index::{FlatIndex, FlatIndexConfig};
    ///
    /// let mut index = FlatIndex::new(FlatIndexConfig::new(3));
    ///
    /// let vectors: Vec<&[f32]> = vec![
    ///     &[1.0, 2.0, 3.0],
    ///     &[4.0, 5.0, 6.0],
    /// ];
    /// let ids = index.insert_batch(&vectors).unwrap();
    ///
    /// assert_eq!(ids, vec![0, 1]);
    /// ```
    pub fn insert_batch(&mut self, vectors: &[&[f32]]) -> Result<Vec<u64>, FlatIndexError> {
        let mut ids = Vec::with_capacity(vectors.len());

        for vector in vectors {
            let id = self.insert(vector)?;
            ids.push(id);
        }

        Ok(ids)
    }

    // ========================================================================
    // GET
    // ========================================================================

    /// Get a vector by ID.
    ///
    /// Returns `None` if the ID doesn't exist or was deleted.
    ///
    /// # Example
    ///
    /// ```rust
    /// use edgevec::index::{FlatIndex, FlatIndexConfig};
    ///
    /// let mut index = FlatIndex::new(FlatIndexConfig::new(3));
    /// let id = index.insert(&[1.0, 2.0, 3.0]).unwrap();
    ///
    /// let v = index.get(id).unwrap();
    /// assert_eq!(v, &[1.0, 2.0, 3.0]);
    ///
    /// // Non-existent ID returns None
    /// assert!(index.get(999).is_none());
    /// ```
    #[must_use]
    #[allow(clippy::cast_possible_truncation)] // FlatIndex targets <10k vectors; u64→usize safe
    pub fn get(&self, id: u64) -> Option<&[f32]> {
        let idx = id as usize;
        let count = self.count as usize;

        // Check bounds
        if idx >= count {
            return None;
        }

        // Check if deleted
        if self.deleted.get(idx).map_or(true, |b| *b) {
            return None;
        }

        // Return vector slice
        let dim = self.config.dimensions as usize;
        let start = idx * dim;
        let end = start + dim;

        Some(&self.vectors[start..end])
    }

    /// Check if a vector ID exists and is not deleted.
    ///
    /// # Example
    ///
    /// ```rust
    /// use edgevec::index::{FlatIndex, FlatIndexConfig};
    ///
    /// let mut index = FlatIndex::new(FlatIndexConfig::new(3));
    /// let id = index.insert(&[1.0, 2.0, 3.0]).unwrap();
    ///
    /// assert!(index.contains(id));
    /// assert!(!index.contains(999));
    /// ```
    #[must_use]
    #[allow(clippy::cast_possible_truncation)] // FlatIndex targets <10k vectors; u64→usize safe
    pub fn contains(&self, id: u64) -> bool {
        let idx = id as usize;
        let count = self.count as usize;
        idx < count && !self.deleted.get(idx).map_or(true, |b| *b)
    }

    // ========================================================================
    // STATS
    // ========================================================================

    /// Returns the number of deleted vectors.
    #[must_use]
    pub fn deleted_count(&self) -> usize {
        self.delete_count
    }

    /// Returns the deletion ratio (deleted / total).
    #[must_use]
    #[allow(clippy::cast_precision_loss)] // Precision loss acceptable for ratio calculation
    pub fn deletion_ratio(&self) -> f32 {
        if self.count == 0 {
            0.0
        } else {
            self.delete_count as f32 / self.count as f32
        }
    }

    /// Returns true if quantization is enabled.
    #[must_use]
    pub fn is_quantized(&self) -> bool {
        self.quantized.is_some()
    }

    /// Returns memory usage in bytes (approximate).
    ///
    /// Includes:
    /// - Vector storage (n × dim × 4 bytes for f32)
    /// - Deleted bitmap (n / 8 bytes)
    /// - Quantized storage if enabled (n × ceil(dim/8) bytes)
    #[must_use]
    pub fn memory_usage(&self) -> usize {
        let vector_bytes = self.vectors.len() * std::mem::size_of::<f32>();
        let bitmap_bytes = (self.deleted.len() + 7) / 8;
        let quantized_bytes = self.quantized.as_ref().map_or(0, Vec::len);

        vector_bytes + bitmap_bytes + quantized_bytes
    }

    /// Get deletion statistics.
    ///
    /// Returns a tuple of (total_vectors, deleted_count, deletion_ratio).
    ///
    /// # Example
    ///
    /// ```rust
    /// use edgevec::index::{FlatIndex, FlatIndexConfig};
    ///
    /// let mut index = FlatIndex::new(FlatIndexConfig::new(3));
    /// index.insert(&[1.0, 2.0, 3.0]).unwrap();
    /// index.insert(&[4.0, 5.0, 6.0]).unwrap();
    /// index.delete(0);
    ///
    /// let (total, deleted, ratio) = index.deletion_stats();
    /// assert_eq!(total, 2);
    /// assert_eq!(deleted, 1);
    /// assert!((ratio - 0.5).abs() < 0.01);
    /// ```
    #[must_use]
    #[allow(clippy::cast_precision_loss)] // Precision loss acceptable for ratio calculation
    #[allow(clippy::cast_possible_truncation)] // FlatIndex targets <10k vectors
    pub fn deletion_stats(&self) -> (usize, usize, f32) {
        let total = self.count as usize;
        let deleted = self.delete_count;
        let ratio = if total > 0 {
            deleted as f32 / total as f32
        } else {
            0.0
        };
        (total, deleted, ratio)
    }

    // ========================================================================
    // DELETE
    // ========================================================================

    /// Mark a vector as deleted.
    ///
    /// The vector is not immediately removed; its slot is marked in the
    /// deletion bitmap and skipped during search. Call `compact()` to
    /// reclaim space when deletion rate exceeds the threshold.
    ///
    /// # Returns
    ///
    /// Returns `true` if the vector was deleted, `false` if it didn't exist
    /// or was already deleted.
    ///
    /// # Example
    ///
    /// ```rust
    /// use edgevec::index::{FlatIndex, FlatIndexConfig};
    ///
    /// let mut index = FlatIndex::new(FlatIndexConfig::new(3));
    /// let id = index.insert(&[1.0, 2.0, 3.0]).unwrap();
    ///
    /// assert!(index.delete(id)); // Success
    /// assert!(!index.delete(id)); // Already deleted
    /// assert!(!index.delete(999)); // Doesn't exist
    ///
    /// assert!(!index.contains(id)); // No longer accessible
    /// ```
    #[allow(clippy::cast_possible_truncation)] // FlatIndex targets <10k vectors
    pub fn delete(&mut self, id: u64) -> bool {
        let idx = id as usize;
        let count = self.count as usize;

        // Check bounds
        if idx >= count {
            return false;
        }

        // Check if already deleted
        if self.deleted.get(idx).map_or(true, |b| *b) {
            return false;
        }

        // Mark as deleted
        self.deleted.set(idx, true);
        self.delete_count += 1;

        // Invalidate quantized cache
        if self.quantized.is_some() {
            self.quantized = None;
        }

        // Auto-compact if threshold exceeded
        if self.should_compact() {
            self.compact();
        }

        true
    }

    /// Check if compaction is needed.
    #[allow(clippy::cast_precision_loss)] // Precision loss acceptable for ratio
    fn should_compact(&self) -> bool {
        if self.count == 0 {
            return false;
        }
        (self.delete_count as f32 / self.count as f32) > self.config.cleanup_threshold
    }

    /// Compact the index by removing deleted vectors.
    ///
    /// This rebuilds the internal storage, removing deleted slots.
    ///
    /// # Warning
    ///
    /// This operation changes vector IDs! After compaction, vectors are
    /// renumbered to be contiguous. Use with caution if external systems
    /// reference vector IDs.
    ///
    /// # Example
    ///
    /// ```rust
    /// use edgevec::index::{FlatIndex, FlatIndexConfig};
    ///
    /// let mut index = FlatIndex::new(
    ///     FlatIndexConfig::new(3).with_cleanup_threshold(1.0) // Disable auto-compact
    /// );
    ///
    /// index.insert(&[1.0, 2.0, 3.0]).unwrap();
    /// index.insert(&[4.0, 5.0, 6.0]).unwrap();
    /// index.insert(&[7.0, 8.0, 9.0]).unwrap();
    ///
    /// // Delete middle vector
    /// index.delete(1);
    /// assert_eq!(index.len(), 2);
    /// assert_eq!(index.capacity(), 3); // Still has 3 slots
    ///
    /// // Compact
    /// index.compact();
    /// assert_eq!(index.capacity(), 2); // Now only 2 slots
    /// ```
    #[allow(clippy::cast_possible_truncation)] // FlatIndex targets <10k vectors
    pub fn compact(&mut self) {
        if self.delete_count == 0 {
            return;
        }

        let dim = self.config.dimensions as usize;
        let count = self.count as usize;
        let new_count = count - self.delete_count;

        let mut new_vectors = Vec::with_capacity(new_count * dim);
        let mut new_deleted = BitVec::with_capacity(new_count);

        for idx in 0..count {
            if !self.deleted.get(idx).map_or(true, |b| *b) {
                // Copy live vector
                let start = idx * dim;
                new_vectors.extend_from_slice(&self.vectors[start..start + dim]);
                new_deleted.push(false);
            }
        }

        self.vectors = new_vectors;
        self.deleted = new_deleted;
        self.count = new_count as u64;
        self.delete_count = 0;
        // Note: next_id stays the same to avoid ID reuse confusion
        // (although after compact, old IDs are invalid anyway)

        // Quantized cache already invalidated in delete()
    }

    // ========================================================================
    // QUANTIZATION
    // ========================================================================

    /// Enable binary quantization for memory reduction.
    ///
    /// Converts stored vectors to binary format (32x compression).
    /// After enabling, use `search_quantized()` for fast Hamming distance search.
    ///
    /// # Memory Reduction
    ///
    /// - Original: 768D × 4 bytes = 3072 bytes per vector
    /// - Quantized: 768D / 8 = 96 bytes per vector (32x reduction)
    ///
    /// # Warning
    ///
    /// This is a lossy operation. Recall will decrease from 100%
    /// but memory usage drops significantly. The original F32 vectors
    /// are preserved for `search()` to maintain 100% recall option.
    ///
    /// # Example
    ///
    /// ```rust
    /// use edgevec::index::{FlatIndex, FlatIndexConfig};
    ///
    /// let mut index = FlatIndex::new(FlatIndexConfig::new(8));
    /// index.insert(&[1.0, -1.0, 1.0, -1.0, 1.0, -1.0, 1.0, -1.0]).unwrap();
    ///
    /// index.enable_quantization().unwrap();
    /// assert!(index.is_quantized());
    ///
    /// // Can still use exact search
    /// let exact = index.search(&[1.0, -1.0, 1.0, -1.0, 1.0, -1.0, 1.0, -1.0], 1).unwrap();
    ///
    /// // Or use faster quantized search
    /// let approx = index.search_quantized(&[1.0, -1.0, 1.0, -1.0, 1.0, -1.0, 1.0, -1.0], 1).unwrap();
    /// ```
    ///
    /// # Errors
    ///
    /// Currently infallible, but returns `Result` for API consistency
    /// and future error conditions.
    #[allow(clippy::cast_possible_truncation)] // FlatIndex targets <10k vectors
    pub fn enable_quantization(&mut self) -> Result<(), FlatIndexError> {
        if self.quantized.is_some() {
            return Ok(()); // Already enabled
        }

        if self.count == 0 {
            // Nothing to quantize, but set up empty storage
            self.quantized = Some(Vec::new());
            return Ok(());
        }

        let dim = self.config.dimensions as usize;
        let packed_dim = (dim + 7) / 8; // Bytes needed for dim bits
        let count = self.count as usize;

        let mut quantized = Vec::with_capacity(count * packed_dim);

        for idx in 0..count {
            if self.deleted.get(idx).map_or(true, |b| *b) {
                // Placeholder for deleted vectors (zeros)
                quantized.extend(std::iter::repeat(0u8).take(packed_dim));
                continue;
            }

            let start = idx * dim;
            let vector = &self.vectors[start..start + dim];

            // Binarize: value > 0 = 1, else 0
            let packed = Self::binarize_vector(vector);
            quantized.extend_from_slice(&packed);
        }

        self.quantized = Some(quantized);
        Ok(())
    }

    /// Disable quantization, freeing the quantized storage.
    ///
    /// Search will use original F32 vectors only.
    pub fn disable_quantization(&mut self) {
        self.quantized = None;
    }

    /// Binarize a vector to packed bytes.
    ///
    /// Each bit represents whether the corresponding dimension value is > 0.
    /// Bits are packed MSB-first within each byte.
    fn binarize_vector(vector: &[f32]) -> Vec<u8> {
        let dim = vector.len();
        let packed_dim = (dim + 7) / 8;
        let mut packed = vec![0u8; packed_dim];

        for (i, &val) in vector.iter().enumerate() {
            if val > 0.0 {
                packed[i / 8] |= 1 << (7 - (i % 8));
            }
        }

        packed
    }

    /// Compute Hamming distance between two packed binary vectors.
    ///
    /// Uses popcount for efficient bit counting.
    #[inline]
    fn hamming_distance_binary(a: &[u8], b: &[u8]) -> u32 {
        a.iter()
            .zip(b.iter())
            .map(|(x, y)| (x ^ y).count_ones())
            .sum()
    }

    /// Search using quantized vectors.
    ///
    /// Uses Hamming distance on binary-quantized vectors for fast approximate search.
    /// Results are sorted by ascending Hamming distance (lower = more similar).
    ///
    /// # Errors
    ///
    /// - `FlatIndexError::QuantizationNotEnabled` if quantization is not enabled
    /// - `FlatIndexError::DimensionMismatch` if query dimension is wrong
    /// - `FlatIndexError::InvalidK` if k is 0
    ///
    /// # Example
    ///
    /// ```rust
    /// use edgevec::index::{FlatIndex, FlatIndexConfig};
    ///
    /// let mut index = FlatIndex::new(FlatIndexConfig::new(8));
    /// index.insert(&[1.0, -1.0, 1.0, -1.0, 1.0, -1.0, 1.0, -1.0]).unwrap();
    /// index.insert(&[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]).unwrap();
    ///
    /// index.enable_quantization().unwrap();
    ///
    /// let results = index.search_quantized(&[1.0, -1.0, 1.0, -1.0, 1.0, -1.0, 1.0, -1.0], 2).unwrap();
    /// assert_eq!(results[0].id, 0); // Exact match has Hamming distance 0
    /// assert!((results[0].score - 0.0).abs() < f32::EPSILON);
    /// ```
    #[allow(clippy::cast_possible_truncation)] // FlatIndex targets <10k vectors
    #[allow(clippy::cast_precision_loss)] // Hamming distance fits in f32
    pub fn search_quantized(
        &self,
        query: &[f32],
        k: usize,
    ) -> Result<Vec<FlatSearchResult>, FlatIndexError> {
        let quantized = self
            .quantized
            .as_ref()
            .ok_or(FlatIndexError::QuantizationNotEnabled)?;

        // Validate
        let expected_dim = self.config.dimensions as usize;
        if query.len() != expected_dim {
            return Err(FlatIndexError::DimensionMismatch {
                expected: expected_dim,
                actual: query.len(),
            });
        }

        if k == 0 {
            return Err(FlatIndexError::InvalidK);
        }

        // Empty index returns empty results
        if self.count == 0 {
            return Ok(Vec::new());
        }

        // Binarize query
        let query_packed = Self::binarize_vector(query);
        let packed_dim = query_packed.len();

        let mut heap: BinaryHeap<HeapEntry> = BinaryHeap::with_capacity(k + 1);
        let count = self.count as usize;

        for idx in 0..count {
            if self.deleted.get(idx).map_or(true, |b| *b) {
                continue;
            }

            let start = idx * packed_dim;
            let vector_packed = &quantized[start..start + packed_dim];

            let distance = Self::hamming_distance_binary(&query_packed, vector_packed);

            // For Hamming distance, lower is better, so use max-heap directly
            // (heap pops highest, which is worst for distance)
            if heap.len() < k {
                heap.push(HeapEntry {
                    id: idx as u64,
                    score: distance as f32,
                });
            } else if let Some(top) = heap.peek() {
                if (distance as f32) < top.score {
                    heap.pop();
                    heap.push(HeapEntry {
                        id: idx as u64,
                        score: distance as f32,
                    });
                }
            }
        }

        // Extract and sort (ascending distance)
        let mut results: Vec<FlatSearchResult> = heap
            .into_iter()
            .map(|entry| FlatSearchResult {
                id: entry.id,
                score: entry.score,
            })
            .collect();

        // Sort by ascending Hamming distance (lower = better)
        results.sort_by(|a, b| a.score.partial_cmp(&b.score).unwrap_or(Ordering::Equal));

        Ok(results)
    }

    // ========================================================================
    // SEARCH
    // ========================================================================

    /// Search for the k nearest neighbors.
    ///
    /// Returns results sorted by relevance (best first):
    /// - For distance metrics (L2, Hamming): lowest distance first
    /// - For similarity metrics (Cosine, Dot): highest similarity first
    ///
    /// # Arguments
    ///
    /// * `query` - Query vector (must match index dimensions)
    /// * `k` - Number of results to return
    ///
    /// # Errors
    ///
    /// - `FlatIndexError::DimensionMismatch` if query dimension is wrong
    /// - `FlatIndexError::InvalidK` if k is 0
    ///
    /// # Example
    ///
    /// ```rust
    /// use edgevec::index::{FlatIndex, FlatIndexConfig, DistanceMetric};
    ///
    /// let config = FlatIndexConfig::new(3).with_metric(DistanceMetric::Cosine);
    /// let mut index = FlatIndex::new(config);
    ///
    /// index.insert(&[1.0, 0.0, 0.0]).unwrap();
    /// index.insert(&[0.0, 1.0, 0.0]).unwrap();
    ///
    /// let results = index.search(&[0.9, 0.1, 0.0], 2).unwrap();
    /// assert_eq!(results.len(), 2);
    /// assert_eq!(results[0].id, 0); // First vector is most similar
    /// ```
    #[allow(clippy::cast_possible_truncation)] // FlatIndex targets <10k vectors
    pub fn search(&self, query: &[f32], k: usize) -> Result<Vec<FlatSearchResult>, FlatIndexError> {
        // Validate inputs
        let expected_dim = self.config.dimensions as usize;
        if query.len() != expected_dim {
            return Err(FlatIndexError::DimensionMismatch {
                expected: expected_dim,
                actual: query.len(),
            });
        }

        if k == 0 {
            return Err(FlatIndexError::InvalidK);
        }

        // Empty index returns empty results
        if self.count == 0 {
            return Ok(Vec::new());
        }

        let dim = self.config.dimensions as usize;
        let is_similarity = self.config.metric.is_similarity();

        // Use max-heap to track top-k
        // For similarity: negate scores so max-heap gives us lowest negated (highest original)
        // For distance: use scores directly so max-heap gives us highest (worst) for removal
        let mut heap: BinaryHeap<HeapEntry> = BinaryHeap::with_capacity(k + 1);

        // Iterate all vectors
        let count = self.count as usize;
        for idx in 0..count {
            // Skip deleted
            if self.deleted.get(idx).map_or(true, |b| *b) {
                continue;
            }

            // Get vector
            let start = idx * dim;
            let end = start + dim;
            let vector = &self.vectors[start..end];

            // Compute distance/similarity
            let raw_score = self.compute_distance(query, vector);

            // Transform score for heap ordering:
            // - Similarity: negate (max-heap pops highest = lowest similarity = worst)
            // - Distance: use as-is (max-heap pops highest = highest distance = worst)
            let heap_score = if is_similarity { -raw_score } else { raw_score };

            if heap.len() < k {
                heap.push(HeapEntry {
                    id: idx as u64,
                    score: heap_score,
                });
            } else if let Some(top) = heap.peek() {
                if heap_score < top.score {
                    heap.pop();
                    heap.push(HeapEntry {
                        id: idx as u64,
                        score: heap_score,
                    });
                }
            }
        }

        // Extract results and restore original scores
        let mut results: Vec<FlatSearchResult> = heap
            .into_iter()
            .map(|entry| FlatSearchResult {
                id: entry.id,
                score: if is_similarity {
                    -entry.score
                } else {
                    entry.score
                },
            })
            .collect();

        // Sort by score (best first)
        if is_similarity {
            // Descending for similarity (higher = better)
            results.sort_by(|a, b| b.score.partial_cmp(&a.score).unwrap_or(Ordering::Equal));
        } else {
            // Ascending for distance (lower = better)
            results.sort_by(|a, b| a.score.partial_cmp(&b.score).unwrap_or(Ordering::Equal));
        }

        Ok(results)
    }

    // ========================================================================
    // DISTANCE COMPUTATION
    // ========================================================================

    /// Compute distance/similarity between two vectors.
    #[allow(clippy::unused_self)] // Instance method for API consistency and future SIMD
    fn compute_distance(&self, a: &[f32], b: &[f32]) -> f32 {
        match self.config.metric {
            DistanceMetric::Cosine => Self::cosine_similarity(a, b),
            DistanceMetric::DotProduct => Self::dot_product(a, b),
            DistanceMetric::L2 => Self::euclidean_distance(a, b),
            DistanceMetric::Hamming => Self::hamming_distance(a, b),
        }
    }

    /// Dot product: sum(a[i] * b[i]).
    #[inline]
    fn dot_product(a: &[f32], b: &[f32]) -> f32 {
        a.iter().zip(b.iter()).map(|(x, y)| x * y).sum()
    }

    /// Cosine similarity: dot(a, b) / (||a|| * ||b||).
    #[inline]
    fn cosine_similarity(a: &[f32], b: &[f32]) -> f32 {
        let dot: f32 = a.iter().zip(b.iter()).map(|(x, y)| x * y).sum();
        let norm_a: f32 = a.iter().map(|x| x * x).sum::<f32>().sqrt();
        let norm_b: f32 = b.iter().map(|x| x * x).sum::<f32>().sqrt();

        if norm_a == 0.0 || norm_b == 0.0 {
            0.0
        } else {
            dot / (norm_a * norm_b)
        }
    }

    /// Euclidean distance: sqrt(sum((a[i] - b[i])^2)).
    #[inline]
    fn euclidean_distance(a: &[f32], b: &[f32]) -> f32 {
        a.iter()
            .zip(b.iter())
            .map(|(x, y)| (x - y) * (x - y))
            .sum::<f32>()
            .sqrt()
    }

    /// Hamming distance for f32 vectors.
    ///
    /// Treats values as binary: 0.0 = 0, non-zero = 1.
    /// Returns count of positions where binary values differ.
    ///
    /// Note: For actual binary vectors, use BQ quantization (Day 3)
    /// which uses packed u8 bytes and popcount-based Hamming distance.
    #[inline]
    #[allow(clippy::float_cmp)] // Intentional exact comparison for binary detection
    #[allow(clippy::cast_precision_loss)] // Precision loss acceptable for count→f32
    fn hamming_distance(a: &[f32], b: &[f32]) -> f32 {
        a.iter()
            .zip(b.iter())
            .filter(|(x, y)| (**x != 0.0) != (**y != 0.0))
            .count() as f32
    }
}

// ============================================================================
// PERSISTENCE
// ============================================================================

/// Snapshot format version for FlatIndex.
pub const FLAT_INDEX_VERSION: u32 = 1;

/// Magic number for FlatIndex snapshots: "EVFI" (EdgeVec Flat Index).
pub const FLAT_INDEX_MAGIC: [u8; 4] = [b'E', b'V', b'F', b'I'];

/// FlatIndex snapshot header.
///
/// Contains all metadata needed to reconstruct a FlatIndex from a snapshot.
/// Serialized using postcard for WASM compatibility.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FlatIndexHeader {
    /// Magic number (must be `FLAT_INDEX_MAGIC`).
    pub magic: [u8; 4],

    /// Format version (must be <= `FLAT_INDEX_VERSION`).
    pub version: u32,

    /// Vector dimension.
    pub dimensions: u32,

    /// Distance metric.
    pub metric: DistanceMetric,

    /// Number of vectors stored (including deleted slots).
    pub count: u64,

    /// Number of deleted vectors.
    pub delete_count: u64,

    /// Next ID to assign.
    pub next_id: u64,

    /// Whether quantization is enabled.
    pub is_quantized: bool,

    /// Cleanup threshold for auto-compaction.
    pub cleanup_threshold: f32,

    /// CRC32 checksum of the data section.
    pub checksum: u32,
}

impl FlatIndexHeader {
    /// Create a header from a FlatIndex and computed checksum.
    #[must_use]
    pub fn from_index(index: &FlatIndex, checksum: u32) -> Self {
        Self {
            magic: FLAT_INDEX_MAGIC,
            version: FLAT_INDEX_VERSION,
            dimensions: index.config.dimensions,
            metric: index.config.metric,
            count: index.count,
            delete_count: index.delete_count as u64,
            next_id: index.next_id,
            is_quantized: index.quantized.is_some(),
            cleanup_threshold: index.config.cleanup_threshold,
            checksum,
        }
    }

    /// Validate the header.
    ///
    /// # Errors
    ///
    /// Returns `PersistenceError::InvalidMagic` if magic doesn't match.
    /// Returns `PersistenceError::UnsupportedVersion` if version is too new.
    #[allow(clippy::cast_possible_truncation)] // Intentional truncation for version bytes
    pub fn validate(&self) -> Result<(), PersistenceError> {
        if self.magic != FLAT_INDEX_MAGIC {
            return Err(PersistenceError::InvalidMagic {
                expected: FLAT_INDEX_MAGIC,
                actual: self.magic,
            });
        }
        if self.version > FLAT_INDEX_VERSION {
            return Err(PersistenceError::UnsupportedVersion(
                (self.version >> 8) as u8,
                (self.version & 0xFF) as u8,
            ));
        }
        Ok(())
    }
}

impl FlatIndex {
    // ========================================================================
    // PERSISTENCE
    // ========================================================================

    /// Serialize the index to a snapshot.
    ///
    /// The snapshot format is:
    /// 1. Header length (u32, 4 bytes)
    /// 2. Header (postcard serialized `FlatIndexHeader`)
    /// 3. Deleted bitmap length (u32, 4 bytes)
    /// 4. Deleted bitmap (variable bytes)
    /// 5. Vectors length (u64, 8 bytes)
    /// 6. Vectors (n × dim × 4 bytes, little-endian f32)
    /// 7. Quantized length (u64, 8 bytes, 0 if not enabled)
    /// 8. Quantized vectors (optional, n × ceil(dim/8) bytes)
    ///
    /// # Returns
    ///
    /// Bytes that can be written to IndexedDB or file system.
    ///
    /// # Errors
    ///
    /// Returns `PersistenceError::SerializationError` if serialization fails.
    #[allow(clippy::cast_possible_truncation)] // Header and bitmap sizes are small (<4GB)
    pub fn to_snapshot(&self) -> Result<Vec<u8>, PersistenceError> {
        let mut buffer = Vec::new();

        // Serialize data sections first to compute checksum
        let deleted_bytes = self.serialize_deleted_bitmap();
        let vectors_bytes = self.serialize_vectors();
        let quantized_bytes = self.serialize_quantized();

        // Compute checksum of data
        let checksum = Self::compute_checksum(&deleted_bytes, &vectors_bytes, &quantized_bytes);

        // Create and serialize header
        let header = FlatIndexHeader::from_index(self, checksum);
        let header_bytes = postcard::to_allocvec(&header)
            .map_err(|e| PersistenceError::SerializationError(e.to_string()))?;

        // Write header length (u32) + header
        buffer.extend_from_slice(&(header_bytes.len() as u32).to_le_bytes());
        buffer.extend_from_slice(&header_bytes);

        // Write deleted bitmap length + data
        buffer.extend_from_slice(&(deleted_bytes.len() as u32).to_le_bytes());
        buffer.extend_from_slice(&deleted_bytes);

        // Write vectors length + data
        buffer.extend_from_slice(&(vectors_bytes.len() as u64).to_le_bytes());
        buffer.extend_from_slice(&vectors_bytes);

        // Write quantized length + data (0 if not enabled)
        buffer.extend_from_slice(&(quantized_bytes.len() as u64).to_le_bytes());
        if !quantized_bytes.is_empty() {
            buffer.extend_from_slice(&quantized_bytes);
        }

        Ok(buffer)
    }

    /// Restore index from a snapshot.
    ///
    /// # Arguments
    ///
    /// * `data` - Bytes from `to_snapshot()` or loaded from storage
    ///
    /// # Errors
    ///
    /// Returns error if:
    /// - Magic number doesn't match
    /// - Version is unsupported
    /// - Checksum doesn't match
    /// - Data is corrupted or truncated
    ///
    /// # Panics
    ///
    /// This function does not panic. The internal `expect` is guarded by
    /// `chunks_exact(4)` which guarantees the slice length.
    #[allow(clippy::cast_possible_truncation)] // FlatIndex targets <10k vectors
    #[allow(clippy::missing_panics_doc)] // Panic is unreachable by design
    pub fn from_snapshot(data: &[u8]) -> Result<Self, PersistenceError> {
        let mut cursor = 0;

        // Read header length
        if data.len() < 4 {
            return Err(PersistenceError::TruncatedData);
        }
        let header_len = u32::from_le_bytes(
            data[0..4]
                .try_into()
                .map_err(|_| PersistenceError::TruncatedData)?,
        ) as usize;
        cursor += 4;

        // Read and parse header
        if data.len() < cursor + header_len {
            return Err(PersistenceError::TruncatedData);
        }
        let header: FlatIndexHeader = postcard::from_bytes(&data[cursor..cursor + header_len])
            .map_err(|e| PersistenceError::DeserializationError(e.to_string()))?;
        cursor += header_len;

        // Validate header
        header.validate()?;

        // Read deleted bitmap length
        if data.len() < cursor + 4 {
            return Err(PersistenceError::TruncatedData);
        }
        let deleted_len = u32::from_le_bytes(
            data[cursor..cursor + 4]
                .try_into()
                .map_err(|_| PersistenceError::TruncatedData)?,
        ) as usize;
        cursor += 4;

        // Read deleted bitmap
        if data.len() < cursor + deleted_len {
            return Err(PersistenceError::TruncatedData);
        }
        let deleted_bytes = &data[cursor..cursor + deleted_len];
        cursor += deleted_len;

        // Read vectors length
        if data.len() < cursor + 8 {
            return Err(PersistenceError::TruncatedData);
        }
        let vectors_len = u64::from_le_bytes(
            data[cursor..cursor + 8]
                .try_into()
                .map_err(|_| PersistenceError::TruncatedData)?,
        ) as usize;
        cursor += 8;

        // Read vectors
        if data.len() < cursor + vectors_len {
            return Err(PersistenceError::TruncatedData);
        }
        let vectors_bytes = &data[cursor..cursor + vectors_len];
        cursor += vectors_len;

        // Read quantized length
        if data.len() < cursor + 8 {
            return Err(PersistenceError::TruncatedData);
        }
        let quantized_len = u64::from_le_bytes(
            data[cursor..cursor + 8]
                .try_into()
                .map_err(|_| PersistenceError::TruncatedData)?,
        ) as usize;
        cursor += 8;

        // Read quantized data
        let quantized_bytes = if quantized_len > 0 {
            if data.len() < cursor + quantized_len {
                return Err(PersistenceError::TruncatedData);
            }
            Some(data[cursor..cursor + quantized_len].to_vec())
        } else {
            None
        };

        // Verify checksum
        let computed_checksum = Self::compute_checksum(
            deleted_bytes,
            vectors_bytes,
            quantized_bytes.as_deref().unwrap_or(&[]),
        );

        if computed_checksum != header.checksum {
            return Err(PersistenceError::ChecksumMismatch {
                expected: header.checksum,
                actual: computed_checksum,
            });
        }

        // Reconstruct config
        let config = FlatIndexConfig {
            dimensions: header.dimensions,
            metric: header.metric,
            initial_capacity: header.count as usize,
            cleanup_threshold: header.cleanup_threshold,
        };

        // Deserialize vectors
        let vectors: Vec<f32> = vectors_bytes
            .chunks_exact(4)
            .map(|chunk| {
                // SAFETY: chunks_exact(4) guarantees each chunk is exactly 4 bytes,
                // so try_into() to [u8; 4] is infallible.
                f32::from_le_bytes(chunk.try_into().expect("chunks_exact guarantees 4 bytes"))
            })
            .collect();

        // Deserialize deleted bitmap
        let deleted = Self::deserialize_deleted_bitmap(deleted_bytes, header.count as usize);

        Ok(Self {
            config,
            vectors,
            count: header.count,
            deleted,
            delete_count: header.delete_count as usize,
            next_id: header.next_id,
            quantized: quantized_bytes,
        })
    }

    /// Serialize the deleted bitmap to bytes.
    fn serialize_deleted_bitmap(&self) -> Vec<u8> {
        // BitVec stores bits in usize chunks; convert to raw bytes
        let raw_slice = self.deleted.as_raw_slice();
        let mut bytes = Vec::with_capacity(std::mem::size_of_val(raw_slice));
        for &word in raw_slice {
            bytes.extend_from_slice(&word.to_le_bytes());
        }
        bytes
    }

    /// Deserialize the deleted bitmap from bytes.
    fn deserialize_deleted_bitmap(bytes: &[u8], count: usize) -> BitVec {
        // Reconstruct from raw usize words
        let word_size = std::mem::size_of::<usize>();
        let mut words: Vec<usize> = bytes
            .chunks(word_size)
            .map(|chunk| {
                let mut arr = [0u8; std::mem::size_of::<usize>()];
                let len = chunk.len().min(word_size);
                arr[..len].copy_from_slice(&chunk[..len]);
                usize::from_le_bytes(arr)
            })
            .collect();

        // Ensure we have enough capacity
        let needed_words = (count + usize::BITS as usize - 1) / usize::BITS as usize;
        words.resize(needed_words, 0);

        let mut bv = BitVec::from_vec(words);
        bv.truncate(count);
        bv
    }

    /// Serialize vectors to bytes.
    fn serialize_vectors(&self) -> Vec<u8> {
        self.vectors.iter().flat_map(|f| f.to_le_bytes()).collect()
    }

    /// Serialize quantized vectors to bytes.
    fn serialize_quantized(&self) -> Vec<u8> {
        self.quantized.clone().unwrap_or_default()
    }

    /// Compute CRC32 checksum of data sections.
    fn compute_checksum(deleted: &[u8], vectors: &[u8], quantized: &[u8]) -> u32 {
        let mut hasher = crc32fast::Hasher::new();
        hasher.update(deleted);
        hasher.update(vectors);
        hasher.update(quantized);
        hasher.finalize()
    }
}

// ============================================================================
// TESTS
// ============================================================================

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

    // ------------------------------------------------------------------------
    // Configuration Tests
    // ------------------------------------------------------------------------

    #[test]
    fn test_config_new() {
        let config = FlatIndexConfig::new(128);

        assert_eq!(config.dimensions, 128);
        assert_eq!(config.metric, DistanceMetric::Cosine);
        assert_eq!(config.initial_capacity, 1000);
        assert!((config.cleanup_threshold - 0.5).abs() < f32::EPSILON);
    }

    #[test]
    fn test_config_builder() {
        let config = FlatIndexConfig::new(64)
            .with_metric(DistanceMetric::DotProduct)
            .with_capacity(5000)
            .with_cleanup_threshold(0.3);

        assert_eq!(config.dimensions, 64);
        assert_eq!(config.metric, DistanceMetric::DotProduct);
        assert_eq!(config.initial_capacity, 5000);
        assert!((config.cleanup_threshold - 0.3).abs() < f32::EPSILON);
    }

    #[test]
    fn test_config_cleanup_threshold_clamping() {
        let config_low = FlatIndexConfig::new(64).with_cleanup_threshold(-0.5);
        assert!((config_low.cleanup_threshold - 0.0).abs() < f32::EPSILON);

        let config_high = FlatIndexConfig::new(64).with_cleanup_threshold(1.5);
        assert!((config_high.cleanup_threshold - 1.0).abs() < f32::EPSILON);
    }

    // ------------------------------------------------------------------------
    // Distance Metric Tests
    // ------------------------------------------------------------------------

    #[test]
    fn test_distance_metric_is_similarity() {
        assert!(DistanceMetric::Cosine.is_similarity());
        assert!(DistanceMetric::DotProduct.is_similarity());
        assert!(!DistanceMetric::L2.is_similarity());
        assert!(!DistanceMetric::Hamming.is_similarity());
    }

    #[test]
    fn test_distance_metric_default() {
        let metric: DistanceMetric = DistanceMetric::default();
        assert_eq!(metric, DistanceMetric::Cosine);
    }

    // ------------------------------------------------------------------------
    // FlatIndex Creation Tests
    // ------------------------------------------------------------------------

    #[test]
    fn test_new_flat_index() {
        let config = FlatIndexConfig::new(128);
        let index = FlatIndex::new(config);

        assert_eq!(index.dimensions(), 128);
        assert_eq!(index.metric(), DistanceMetric::Cosine);
        assert_eq!(index.len(), 0);
        assert!(index.is_empty());
        assert_eq!(index.capacity(), 0);
        assert_eq!(index.deleted_count(), 0);
        assert!(!index.is_quantized());
    }

    #[test]
    fn test_new_with_different_metrics() {
        for metric in [
            DistanceMetric::Cosine,
            DistanceMetric::DotProduct,
            DistanceMetric::L2,
            DistanceMetric::Hamming,
        ] {
            let config = FlatIndexConfig::new(64).with_metric(metric);
            let index = FlatIndex::new(config);
            assert_eq!(index.metric(), metric);
        }
    }

    // ------------------------------------------------------------------------
    // Insert Tests
    // ------------------------------------------------------------------------

    #[test]
    fn test_insert_single() {
        let mut index = FlatIndex::new(FlatIndexConfig::new(3));

        let id = index.insert(&[1.0, 2.0, 3.0]).unwrap();

        assert_eq!(id, 0);
        assert_eq!(index.len(), 1);
        assert!(!index.is_empty());
        assert_eq!(index.capacity(), 1);
    }

    #[test]
    fn test_insert_multiple_sequential_ids() {
        let mut index = FlatIndex::new(FlatIndexConfig::new(3));

        let id1 = index.insert(&[1.0, 2.0, 3.0]).unwrap();
        let id2 = index.insert(&[4.0, 5.0, 6.0]).unwrap();
        let id3 = index.insert(&[7.0, 8.0, 9.0]).unwrap();

        assert_eq!(id1, 0);
        assert_eq!(id2, 1);
        assert_eq!(id3, 2);
        assert_eq!(index.len(), 3);
    }

    #[test]
    fn test_insert_dimension_mismatch() {
        let mut index = FlatIndex::new(FlatIndexConfig::new(3));

        // Too few dimensions
        let result = index.insert(&[1.0, 2.0]);
        assert!(matches!(
            result,
            Err(FlatIndexError::DimensionMismatch {
                expected: 3,
                actual: 2
            })
        ));

        // Too many dimensions
        let result = index.insert(&[1.0, 2.0, 3.0, 4.0]);
        assert!(matches!(
            result,
            Err(FlatIndexError::DimensionMismatch {
                expected: 3,
                actual: 4
            })
        ));

        // Index should be unchanged
        assert!(index.is_empty());
    }

    #[test]
    fn test_insert_batch() {
        let mut index = FlatIndex::new(FlatIndexConfig::new(3));

        let vectors: Vec<&[f32]> = vec![&[1.0, 2.0, 3.0], &[4.0, 5.0, 6.0], &[7.0, 8.0, 9.0]];

        let ids = index.insert_batch(&vectors).unwrap();

        assert_eq!(ids, vec![0, 1, 2]);
        assert_eq!(index.len(), 3);
    }

    #[test]
    fn test_insert_batch_dimension_mismatch() {
        let mut index = FlatIndex::new(FlatIndexConfig::new(3));

        let vectors: Vec<&[f32]> = vec![
            &[1.0, 2.0, 3.0], // OK
            &[4.0, 5.0],      // Wrong dimension
            &[7.0, 8.0, 9.0], // Would be OK but not reached
        ];

        let result = index.insert_batch(&vectors);
        assert!(result.is_err());

        // First vector should have been inserted
        assert_eq!(index.len(), 1);
        assert!(index.contains(0));
    }

    #[test]
    fn test_insert_capacity_growth() {
        let config = FlatIndexConfig::new(3).with_capacity(2);
        let mut index = FlatIndex::new(config);

        // Insert more than initial capacity
        for i in 0..10 {
            let id = index.insert(&[i as f32, i as f32, i as f32]).unwrap();
            assert_eq!(id, i);
        }

        assert_eq!(index.len(), 10);

        // All vectors should be retrievable
        for i in 0..10 {
            assert!(index.contains(i));
        }
    }

    // ------------------------------------------------------------------------
    // Get Tests
    // ------------------------------------------------------------------------

    #[test]
    fn test_get_vector() {
        let mut index = FlatIndex::new(FlatIndexConfig::new(3));

        index.insert(&[1.0, 2.0, 3.0]).unwrap();
        index.insert(&[4.0, 5.0, 6.0]).unwrap();

        let v0 = index.get(0).unwrap();
        let v1 = index.get(1).unwrap();

        assert_eq!(v0, &[1.0, 2.0, 3.0]);
        assert_eq!(v1, &[4.0, 5.0, 6.0]);
    }

    #[test]
    fn test_get_nonexistent() {
        let mut index = FlatIndex::new(FlatIndexConfig::new(3));

        index.insert(&[1.0, 2.0, 3.0]).unwrap();

        assert!(index.get(1).is_none()); // Not inserted yet
        assert!(index.get(99).is_none()); // Way out of bounds
        assert!(index.get(u64::MAX).is_none()); // Max value
    }

    #[test]
    fn test_get_empty_index() {
        let index = FlatIndex::new(FlatIndexConfig::new(3));
        assert!(index.get(0).is_none());
    }

    // ------------------------------------------------------------------------
    // Contains Tests
    // ------------------------------------------------------------------------

    #[test]
    fn test_contains() {
        let mut index = FlatIndex::new(FlatIndexConfig::new(3));

        index.insert(&[1.0, 2.0, 3.0]).unwrap();
        index.insert(&[4.0, 5.0, 6.0]).unwrap();

        assert!(index.contains(0));
        assert!(index.contains(1));
        assert!(!index.contains(2));
        assert!(!index.contains(99));
    }

    // ------------------------------------------------------------------------
    // Stats Tests
    // ------------------------------------------------------------------------

    #[test]
    fn test_memory_usage() {
        let mut index = FlatIndex::new(FlatIndexConfig::new(3));

        // Empty index
        let empty_usage = index.memory_usage();
        assert_eq!(empty_usage, 0);

        // After insert
        index.insert(&[1.0, 2.0, 3.0]).unwrap();
        let usage = index.memory_usage();

        // 3 floats * 4 bytes = 12 bytes for vectors
        // 1 bit (rounded up) for bitmap = 1 byte (actually 0 in bitvec)
        // But bitvec internal representation varies
        assert!(usage >= 12);
    }

    #[test]
    fn test_deletion_ratio_empty() {
        let index = FlatIndex::new(FlatIndexConfig::new(3));
        assert!((index.deletion_ratio() - 0.0).abs() < f32::EPSILON);
    }

    // ------------------------------------------------------------------------
    // Edge Cases
    // ------------------------------------------------------------------------

    #[test]
    fn test_high_dimension_vectors() {
        let dim = 768; // Typical embedding dimension
        let mut index = FlatIndex::new(FlatIndexConfig::new(dim));

        let vector: Vec<f32> = (0..dim).map(|i| i as f32 / dim as f32).collect();
        let id = index.insert(&vector).unwrap();

        assert_eq!(id, 0);
        let retrieved = index.get(0).unwrap();
        assert_eq!(retrieved.len(), dim as usize);
        assert_eq!(retrieved, vector.as_slice());
    }

    #[test]
    fn test_zero_vector() {
        let mut index = FlatIndex::new(FlatIndexConfig::new(3));

        let id = index.insert(&[0.0, 0.0, 0.0]).unwrap();
        let v = index.get(id).unwrap();

        assert_eq!(v, &[0.0, 0.0, 0.0]);
    }

    #[test]
    fn test_negative_values() {
        let mut index = FlatIndex::new(FlatIndexConfig::new(3));

        let id = index.insert(&[-1.0, -2.0, -3.0]).unwrap();
        let v = index.get(id).unwrap();

        assert_eq!(v, &[-1.0, -2.0, -3.0]);
    }

    #[test]
    fn test_special_float_values() {
        let mut index = FlatIndex::new(FlatIndexConfig::new(3));

        // Insert vector with special values (NaN, Inf allowed at storage level)
        // Note: Distance calculations may behave unexpectedly with these values
        let id = index
            .insert(&[f32::INFINITY, f32::NEG_INFINITY, 0.0])
            .unwrap();
        let v = index.get(id).unwrap();

        assert!(v[0].is_infinite());
        assert!(v[1].is_infinite());
    }

    // ------------------------------------------------------------------------
    // Search Tests
    // ------------------------------------------------------------------------

    #[test]
    fn test_search_basic_cosine() {
        let config = FlatIndexConfig::new(3).with_metric(DistanceMetric::Cosine);
        let mut index = FlatIndex::new(config);

        index.insert(&[1.0, 0.0, 0.0]).unwrap(); // ID 0
        index.insert(&[0.0, 1.0, 0.0]).unwrap(); // ID 1
        index.insert(&[0.0, 0.0, 1.0]).unwrap(); // ID 2

        // Query closest to first vector
        let results = index.search(&[0.9, 0.1, 0.0], 2).unwrap();

        assert_eq!(results.len(), 2);
        assert_eq!(results[0].id, 0); // First vector is closest
    }

    #[test]
    fn test_search_all_metrics() {
        for metric in [
            DistanceMetric::Cosine,
            DistanceMetric::DotProduct,
            DistanceMetric::L2,
            DistanceMetric::Hamming,
        ] {
            let config = FlatIndexConfig::new(3).with_metric(metric);
            let mut index = FlatIndex::new(config);

            index.insert(&[1.0, 0.0, 0.0]).unwrap();
            index.insert(&[0.0, 1.0, 0.0]).unwrap();

            let results = index.search(&[1.0, 0.0, 0.0], 2).unwrap();

            assert_eq!(results.len(), 2, "Failed for metric {:?}", metric);
            // First vector should be exact match (best score)
            assert_eq!(results[0].id, 0, "Failed for metric {:?}", metric);
        }
    }

    #[test]
    fn test_search_dimension_mismatch() {
        let mut index = FlatIndex::new(FlatIndexConfig::new(3));
        index.insert(&[1.0, 0.0, 0.0]).unwrap();

        let result = index.search(&[1.0, 0.0], 1); // Wrong dimension

        assert!(matches!(
            result,
            Err(FlatIndexError::DimensionMismatch {
                expected: 3,
                actual: 2
            })
        ));
    }

    #[test]
    fn test_search_k_zero() {
        let mut index = FlatIndex::new(FlatIndexConfig::new(3));
        index.insert(&[1.0, 0.0, 0.0]).unwrap();

        let result = index.search(&[1.0, 0.0, 0.0], 0);

        assert!(matches!(result, Err(FlatIndexError::InvalidK)));
    }

    #[test]
    fn test_search_empty_index() {
        let index = FlatIndex::new(FlatIndexConfig::new(3));

        let results = index.search(&[1.0, 0.0, 0.0], 5).unwrap();

        assert!(results.is_empty());
    }

    #[test]
    fn test_search_k_larger_than_count() {
        let mut index = FlatIndex::new(FlatIndexConfig::new(3));
        index.insert(&[1.0, 0.0, 0.0]).unwrap();
        index.insert(&[0.0, 1.0, 0.0]).unwrap();

        let results = index.search(&[1.0, 0.0, 0.0], 10).unwrap();

        assert_eq!(results.len(), 2); // Only 2 vectors available
    }

    #[test]
    fn test_search_results_sorted_cosine() {
        let config = FlatIndexConfig::new(3).with_metric(DistanceMetric::Cosine);
        let mut index = FlatIndex::new(config);

        // Insert vectors with known similarities to [1, 0, 0]
        index.insert(&[1.0, 0.0, 0.0]).unwrap(); // ID 0: cos = 1.0 (exact match)
        index.insert(&[0.707, 0.707, 0.0]).unwrap(); // ID 1: cos ≈ 0.707
        index.insert(&[0.0, 1.0, 0.0]).unwrap(); // ID 2: cos = 0.0

        let query = [1.0, 0.0, 0.0];
        let results = index.search(&query, 3).unwrap();

        // Results should be sorted by descending similarity
        assert_eq!(results[0].id, 0); // Highest similarity
        assert_eq!(results[2].id, 2); // Lowest similarity

        for i in 1..results.len() {
            assert!(
                results[i - 1].score >= results[i].score,
                "Results not sorted at index {}: {} < {}",
                i,
                results[i - 1].score,
                results[i].score
            );
        }
    }

    #[test]
    fn test_search_l2_metric() {
        let config = FlatIndexConfig::new(3).with_metric(DistanceMetric::L2);
        let mut index = FlatIndex::new(config);

        index.insert(&[0.0, 0.0, 0.0]).unwrap(); // ID 0: at origin
        index.insert(&[1.0, 0.0, 0.0]).unwrap(); // ID 1: distance 1
        index.insert(&[2.0, 0.0, 0.0]).unwrap(); // ID 2: distance 2

        let results = index.search(&[0.0, 0.0, 0.0], 3).unwrap();

        // Closest first (ascending distance)
        assert_eq!(results[0].id, 0);
        assert!((results[0].score - 0.0).abs() < 1e-6);
        assert_eq!(results[1].id, 1);
        assert!((results[1].score - 1.0).abs() < 1e-6);
        assert_eq!(results[2].id, 2);
        assert!((results[2].score - 2.0).abs() < 1e-6);
    }

    #[test]
    fn test_search_dot_product_metric() {
        let config = FlatIndexConfig::new(3).with_metric(DistanceMetric::DotProduct);
        let mut index = FlatIndex::new(config);

        index.insert(&[1.0, 0.0, 0.0]).unwrap(); // ID 0: dot = 1.0
        index.insert(&[0.5, 0.0, 0.0]).unwrap(); // ID 1: dot = 0.5
        index.insert(&[0.0, 1.0, 0.0]).unwrap(); // ID 2: dot = 0.0

        let results = index.search(&[1.0, 0.0, 0.0], 3).unwrap();

        // Highest dot product first (descending)
        assert_eq!(results[0].id, 0);
        assert!((results[0].score - 1.0).abs() < 1e-6);
        assert_eq!(results[1].id, 1);
        assert!((results[1].score - 0.5).abs() < 1e-6);
    }

    #[test]
    fn test_search_hamming_metric() {
        let config = FlatIndexConfig::new(4).with_metric(DistanceMetric::Hamming);
        let mut index = FlatIndex::new(config);

        // Binary-like vectors (0.0 = 0, non-zero = 1)
        index.insert(&[1.0, 1.0, 0.0, 0.0]).unwrap(); // ID 0: [1,1,0,0]
        index.insert(&[1.0, 0.0, 0.0, 0.0]).unwrap(); // ID 1: [1,0,0,0]
        index.insert(&[0.0, 0.0, 1.0, 1.0]).unwrap(); // ID 2: [0,0,1,1]

        // Query: [1,1,0,0]
        let results = index.search(&[1.0, 1.0, 0.0, 0.0], 3).unwrap();

        // ID 0 has Hamming distance 0 (exact match)
        assert_eq!(results[0].id, 0);
        assert!((results[0].score - 0.0).abs() < 1e-6);

        // ID 1 has Hamming distance 1
        assert_eq!(results[1].id, 1);
        assert!((results[1].score - 1.0).abs() < 1e-6);

        // ID 2 has Hamming distance 4
        assert_eq!(results[2].id, 2);
        assert!((results[2].score - 4.0).abs() < 1e-6);
    }

    #[test]
    fn test_search_100_recall_validation() {
        // Verify brute-force achieves 100% recall
        let mut index = FlatIndex::new(FlatIndexConfig::new(64));

        // Insert 100 deterministic vectors using LCG
        let mut seed: u64 = 42;
        let lcg = |s: &mut u64| -> f32 {
            *s = s.wrapping_mul(6_364_136_223_846_793_005).wrapping_add(1);
            ((*s >> 33) as f32) / (u32::MAX as f32)
        };

        for _ in 0..100 {
            let v: Vec<f32> = (0..64).map(|_| lcg(&mut seed)).collect();
            index.insert(&v).unwrap();
        }

        // Search should return exactly k results
        let query: Vec<f32> = (0..64).map(|_| lcg(&mut seed)).collect();
        let results = index.search(&query, 10).unwrap();

        assert_eq!(results.len(), 10);

        // Results should be sorted by similarity (descending for cosine)
        for i in 1..results.len() {
            assert!(results[i - 1].score >= results[i].score);
        }
    }

    #[test]
    fn test_search_high_dimension() {
        let dim = 768;
        let config = FlatIndexConfig::new(dim).with_metric(DistanceMetric::Cosine);
        let mut index = FlatIndex::new(config);

        // Insert 100 high-dimensional vectors
        for i in 0..100 {
            let v: Vec<f32> = (0..dim as usize).map(|j| (i * j) as f32 / 1000.0).collect();
            index.insert(&v).unwrap();
        }

        let query: Vec<f32> = (0..dim as usize).map(|j| j as f32 / 1000.0).collect();
        let results = index.search(&query, 5).unwrap();

        assert_eq!(results.len(), 5);
    }

    // ------------------------------------------------------------------------
    // Deletion Tests (Day 3)
    // ------------------------------------------------------------------------

    #[test]
    fn test_delete_basic() {
        let config = FlatIndexConfig::new(3).with_cleanup_threshold(1.0); // Disable auto-compact
        let mut index = FlatIndex::new(config);

        let id = index.insert(&[1.0, 2.0, 3.0]).unwrap();

        assert!(index.contains(id));
        assert!(index.delete(id)); // Should return true
        assert!(!index.contains(id)); // Should no longer be accessible
        assert!(index.get(id).is_none());
    }

    #[test]
    fn test_delete_already_deleted() {
        let config = FlatIndexConfig::new(3).with_cleanup_threshold(1.0);
        let mut index = FlatIndex::new(config);

        let id = index.insert(&[1.0, 2.0, 3.0]).unwrap();

        assert!(index.delete(id)); // First delete succeeds
        assert!(!index.delete(id)); // Second delete returns false
    }

    #[test]
    fn test_delete_nonexistent() {
        let mut index = FlatIndex::new(FlatIndexConfig::new(3));

        assert!(!index.delete(0)); // Empty index
        assert!(!index.delete(999)); // Out of bounds
    }

    #[test]
    fn test_delete_updates_len() {
        let config = FlatIndexConfig::new(3).with_cleanup_threshold(1.0);
        let mut index = FlatIndex::new(config);

        index.insert(&[1.0, 2.0, 3.0]).unwrap();
        index.insert(&[4.0, 5.0, 6.0]).unwrap();

        assert_eq!(index.len(), 2);

        index.delete(0);
        assert_eq!(index.len(), 1);

        index.delete(1);
        assert_eq!(index.len(), 0);
        assert!(index.is_empty());
    }

    #[test]
    fn test_delete_updates_deleted_count() {
        let config = FlatIndexConfig::new(3).with_cleanup_threshold(1.0);
        let mut index = FlatIndex::new(config);

        index.insert(&[1.0, 2.0, 3.0]).unwrap();
        index.insert(&[4.0, 5.0, 6.0]).unwrap();

        assert_eq!(index.deleted_count(), 0);

        index.delete(0);
        assert_eq!(index.deleted_count(), 1);

        index.delete(1);
        assert_eq!(index.deleted_count(), 2);
    }

    #[test]
    fn test_deletion_stats() {
        let config = FlatIndexConfig::new(3).with_cleanup_threshold(1.0);
        let mut index = FlatIndex::new(config);

        index.insert(&[1.0, 2.0, 3.0]).unwrap();
        index.insert(&[4.0, 5.0, 6.0]).unwrap();

        let (total, deleted, ratio) = index.deletion_stats();
        assert_eq!(total, 2);
        assert_eq!(deleted, 0);
        assert!((ratio - 0.0).abs() < f32::EPSILON);

        index.delete(0);

        let (total, deleted, ratio) = index.deletion_stats();
        assert_eq!(total, 2);
        assert_eq!(deleted, 1);
        assert!((ratio - 0.5).abs() < 0.01);
    }

    #[test]
    fn test_search_skips_deleted() {
        let config = FlatIndexConfig::new(3)
            .with_metric(DistanceMetric::Cosine)
            .with_cleanup_threshold(1.0);
        let mut index = FlatIndex::new(config);

        index.insert(&[1.0, 0.0, 0.0]).unwrap(); // ID 0 - exact match
        index.insert(&[0.0, 1.0, 0.0]).unwrap(); // ID 1

        // Before delete: ID 0 should be best match
        let results = index.search(&[1.0, 0.0, 0.0], 2).unwrap();
        assert_eq!(results[0].id, 0);

        // Delete the best match
        index.delete(0);

        // After delete: ID 1 should be the only result
        let results = index.search(&[1.0, 0.0, 0.0], 2).unwrap();
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].id, 1);
    }

    // ------------------------------------------------------------------------
    // Compaction Tests (Day 3)
    // ------------------------------------------------------------------------

    #[test]
    fn test_compact_basic() {
        let config = FlatIndexConfig::new(3).with_cleanup_threshold(1.0); // Disable auto
        let mut index = FlatIndex::new(config);

        index.insert(&[1.0, 2.0, 3.0]).unwrap();
        index.insert(&[4.0, 5.0, 6.0]).unwrap();
        index.insert(&[7.0, 8.0, 9.0]).unwrap();

        // Delete middle
        index.delete(1);
        assert_eq!(index.capacity(), 3);
        assert_eq!(index.len(), 2);

        // Compact
        index.compact();

        assert_eq!(index.capacity(), 2);
        assert_eq!(index.len(), 2);
        assert_eq!(index.deleted_count(), 0);
    }

    #[test]
    fn test_compact_preserves_data() {
        let config = FlatIndexConfig::new(3).with_cleanup_threshold(1.0);
        let mut index = FlatIndex::new(config);

        index.insert(&[1.0, 2.0, 3.0]).unwrap(); // ID 0
        index.insert(&[4.0, 5.0, 6.0]).unwrap(); // ID 1
        index.insert(&[7.0, 8.0, 9.0]).unwrap(); // ID 2

        // Delete first and last
        index.delete(0);
        index.delete(2);

        // Compact
        index.compact();

        // Only middle vector should remain (now at ID 0 after compaction)
        assert_eq!(index.len(), 1);
        let v = index.get(0).unwrap(); // After compact, renumbered to 0
        assert_eq!(v, &[4.0, 5.0, 6.0]);
    }

    #[test]
    fn test_compact_empty() {
        let mut index = FlatIndex::new(FlatIndexConfig::new(3));

        // Compact on empty should be a no-op
        index.compact();
        assert!(index.is_empty());
    }

    #[test]
    fn test_compact_nothing_to_do() {
        let mut index = FlatIndex::new(FlatIndexConfig::new(3));

        index.insert(&[1.0, 2.0, 3.0]).unwrap();

        // No deletions, compact should be a no-op
        let capacity_before = index.capacity();
        index.compact();
        assert_eq!(index.capacity(), capacity_before);
    }

    #[test]
    fn test_auto_compact_on_threshold() {
        // Set threshold to 0.3 (30%)
        let config = FlatIndexConfig::new(3).with_cleanup_threshold(0.3);
        let mut index = FlatIndex::new(config);

        // Insert 10 vectors
        for i in 0..10 {
            index.insert(&[i as f32, 0.0, 0.0]).unwrap();
        }

        assert_eq!(index.capacity(), 10);

        // Delete 4 vectors (40% > 30% threshold)
        index.delete(0);
        index.delete(1);
        index.delete(2);
        // 30% not yet exceeded (3/10 = 30%, not > 30%)
        assert_eq!(index.capacity(), 10);

        index.delete(3); // 4/10 = 40% > 30% => auto compact

        // Should have auto-compacted
        assert_eq!(index.capacity(), 6);
        assert_eq!(index.deleted_count(), 0);
    }

    // ------------------------------------------------------------------------
    // Quantization Tests (Day 3)
    // ------------------------------------------------------------------------

    #[test]
    fn test_enable_quantization() {
        let mut index = FlatIndex::new(FlatIndexConfig::new(8));

        index
            .insert(&[1.0, -1.0, 1.0, -1.0, 1.0, -1.0, 1.0, -1.0])
            .unwrap();

        assert!(!index.is_quantized());

        index.enable_quantization().unwrap();

        assert!(index.is_quantized());
    }

    #[test]
    fn test_enable_quantization_idempotent() {
        let mut index = FlatIndex::new(FlatIndexConfig::new(8));

        index
            .insert(&[1.0, -1.0, 1.0, -1.0, 1.0, -1.0, 1.0, -1.0])
            .unwrap();

        index.enable_quantization().unwrap();
        index.enable_quantization().unwrap(); // Should be a no-op

        assert!(index.is_quantized());
    }

    #[test]
    fn test_enable_quantization_empty_index() {
        let mut index = FlatIndex::new(FlatIndexConfig::new(8));

        // Should succeed on empty index
        index.enable_quantization().unwrap();
        assert!(index.is_quantized());
    }

    #[test]
    fn test_disable_quantization() {
        let mut index = FlatIndex::new(FlatIndexConfig::new(8));

        index
            .insert(&[1.0, -1.0, 1.0, -1.0, 1.0, -1.0, 1.0, -1.0])
            .unwrap();
        index.enable_quantization().unwrap();
        assert!(index.is_quantized());

        index.disable_quantization();
        assert!(!index.is_quantized());
    }

    #[test]
    fn test_search_quantized_basic() {
        let mut index = FlatIndex::new(FlatIndexConfig::new(8));

        // Vector 0: all positive (binary: 11111111)
        index
            .insert(&[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0])
            .unwrap();
        // Vector 1: alternating (binary: 10101010)
        index
            .insert(&[1.0, -1.0, 1.0, -1.0, 1.0, -1.0, 1.0, -1.0])
            .unwrap();
        // Vector 2: all negative (binary: 00000000)
        index
            .insert(&[-1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0])
            .unwrap();

        index.enable_quantization().unwrap();

        // Query for alternating pattern
        let query = [1.0, -1.0, 1.0, -1.0, 1.0, -1.0, 1.0, -1.0];
        let results = index.search_quantized(&query, 3).unwrap();

        assert_eq!(results.len(), 3);
        assert_eq!(results[0].id, 1); // Exact match
        assert!((results[0].score - 0.0).abs() < f32::EPSILON); // Hamming distance 0
    }

    #[test]
    fn test_search_quantized_hamming_distances() {
        let mut index = FlatIndex::new(FlatIndexConfig::new(8));

        // Binary patterns:
        // 11111111 (all positive)
        index
            .insert(&[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0])
            .unwrap();
        // 11111110 (7 positive, 1 negative)
        index
            .insert(&[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0])
            .unwrap();
        // 00000000 (all negative)
        index
            .insert(&[-1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0])
            .unwrap();

        index.enable_quantization().unwrap();

        // Query: 11111111
        let results = index.search_quantized(&[1.0; 8], 3).unwrap();

        // ID 0: Hamming distance 0
        assert_eq!(results[0].id, 0);
        assert!((results[0].score - 0.0).abs() < f32::EPSILON);

        // ID 1: Hamming distance 1
        assert_eq!(results[1].id, 1);
        assert!((results[1].score - 1.0).abs() < f32::EPSILON);

        // ID 2: Hamming distance 8
        assert_eq!(results[2].id, 2);
        assert!((results[2].score - 8.0).abs() < f32::EPSILON);
    }

    #[test]
    fn test_search_quantized_not_enabled() {
        let mut index = FlatIndex::new(FlatIndexConfig::new(8));

        index.insert(&[1.0; 8]).unwrap();

        let result = index.search_quantized(&[1.0; 8], 1);
        assert!(matches!(
            result,
            Err(FlatIndexError::QuantizationNotEnabled)
        ));
    }

    #[test]
    fn test_search_quantized_dimension_mismatch() {
        let mut index = FlatIndex::new(FlatIndexConfig::new(8));

        index.insert(&[1.0; 8]).unwrap();
        index.enable_quantization().unwrap();

        let result = index.search_quantized(&[1.0; 4], 1); // Wrong dimension
        assert!(matches!(
            result,
            Err(FlatIndexError::DimensionMismatch {
                expected: 8,
                actual: 4
            })
        ));
    }

    #[test]
    fn test_search_quantized_k_zero() {
        let mut index = FlatIndex::new(FlatIndexConfig::new(8));

        index.insert(&[1.0; 8]).unwrap();
        index.enable_quantization().unwrap();

        let result = index.search_quantized(&[1.0; 8], 0);
        assert!(matches!(result, Err(FlatIndexError::InvalidK)));
    }

    #[test]
    fn test_search_quantized_skips_deleted() {
        let config = FlatIndexConfig::new(8).with_cleanup_threshold(1.0);
        let mut index = FlatIndex::new(config);

        index.insert(&[1.0; 8]).unwrap(); // ID 0: exact match
        index.insert(&[-1.0; 8]).unwrap(); // ID 1: max distance

        index.enable_quantization().unwrap();

        // Before delete
        let results = index.search_quantized(&[1.0; 8], 2).unwrap();
        assert_eq!(results[0].id, 0);

        // Delete and re-enable quantization (delete invalidates it)
        index.delete(0);
        index.enable_quantization().unwrap();

        // After delete: only ID 1 should be found
        let results = index.search_quantized(&[1.0; 8], 2).unwrap();
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].id, 1);
    }

    #[test]
    fn test_insert_invalidates_quantization() {
        let mut index = FlatIndex::new(FlatIndexConfig::new(8));

        index.insert(&[1.0; 8]).unwrap();
        index.enable_quantization().unwrap();
        assert!(index.is_quantized());

        // Insert invalidates quantization cache
        index.insert(&[-1.0; 8]).unwrap();
        assert!(!index.is_quantized());
    }

    #[test]
    fn test_delete_invalidates_quantization() {
        let config = FlatIndexConfig::new(8).with_cleanup_threshold(1.0);
        let mut index = FlatIndex::new(config);

        index.insert(&[1.0; 8]).unwrap();
        index.enable_quantization().unwrap();
        assert!(index.is_quantized());

        // Delete invalidates quantization cache
        index.delete(0);
        assert!(!index.is_quantized());
    }

    #[test]
    fn test_quantization_memory_reduction() {
        let dim = 768;
        let mut index = FlatIndex::new(FlatIndexConfig::new(dim));

        // Insert 100 vectors
        for i in 0..100 {
            let v: Vec<f32> = (0..dim as usize)
                .map(|j| if (i + j) % 2 == 0 { 1.0 } else { -1.0 })
                .collect();
            index.insert(&v).unwrap();
        }

        let memory_before = index.memory_usage();

        index.enable_quantization().unwrap();

        let memory_after = index.memory_usage();

        // Quantized storage should add ~100 * 96 bytes = 9600 bytes
        // F32 storage is 100 * 768 * 4 = 307200 bytes
        // Total should be ~316800 bytes
        // Without quantization: ~307200 bytes
        assert!(memory_after > memory_before); // Quantized adds to memory (doesn't replace)

        // But quantized-only search uses much less memory per vector
        // 768 / 8 = 96 bytes (quantized) vs 768 * 4 = 3072 bytes (f32)
        // Ratio: 3072 / 96 = 32x reduction
        let f32_per_vector = dim as usize * 4;
        let bq_per_vector = (dim as usize + 7) / 8;
        assert_eq!(f32_per_vector / bq_per_vector, 32);
    }

    #[test]
    fn test_binarize_vector() {
        // Test the internal binarization logic
        let packed = FlatIndex::binarize_vector(&[1.0, -1.0, 1.0, -1.0, 1.0, -1.0, 1.0, -1.0]);

        // Expected: 10101010 = 170 in decimal (MSB first)
        assert_eq!(packed.len(), 1);
        assert_eq!(packed[0], 0b1010_1010);
    }

    #[test]
    fn test_binarize_vector_16_dim() {
        let packed = FlatIndex::binarize_vector(&[
            1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, // 11111111
            -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, // 00000000
        ]);

        assert_eq!(packed.len(), 2);
        assert_eq!(packed[0], 0b1111_1111);
        assert_eq!(packed[1], 0b0000_0000);
    }

    #[test]
    fn test_hamming_distance_binary() {
        let a = [0b1111_1111u8];
        let b = [0b1111_1110u8];

        let distance = FlatIndex::hamming_distance_binary(&a, &b);
        assert_eq!(distance, 1); // 1 bit different

        let c = [0b0000_0000u8];
        let distance2 = FlatIndex::hamming_distance_binary(&a, &c);
        assert_eq!(distance2, 8); // All 8 bits different
    }

    #[test]
    fn test_search_quantized_high_dimension() {
        let dim = 768;
        let mut index = FlatIndex::new(FlatIndexConfig::new(dim));

        // Insert 50 vectors with different patterns
        for i in 0..50 {
            let v: Vec<f32> = (0..dim as usize)
                .map(|j| if (i + j) % 2 == 0 { 1.0 } else { -1.0 })
                .collect();
            index.insert(&v).unwrap();
        }

        index.enable_quantization().unwrap();

        // Query that matches pattern at i=0
        let query: Vec<f32> = (0..dim as usize)
            .map(|j| if j % 2 == 0 { 1.0 } else { -1.0 })
            .collect();

        let results = index.search_quantized(&query, 10).unwrap();

        assert_eq!(results.len(), 10);
        // All even IDs should have better (lower) Hamming distance
        assert!(results[0].id % 2 == 0);
    }

    // ------------------------------------------------------------------------
    // Persistence Tests (Day 4)
    // ------------------------------------------------------------------------

    #[test]
    fn test_snapshot_round_trip_basic() {
        let mut index = FlatIndex::new(FlatIndexConfig::new(64));

        // Insert vectors
        for i in 0..100 {
            let v: Vec<f32> = (0..64).map(|j| (i * 64 + j) as f32 / 1000.0).collect();
            index.insert(&v).unwrap();
        }

        // Save snapshot
        let snapshot = index.to_snapshot().unwrap();

        // Restore from snapshot
        let restored = FlatIndex::from_snapshot(&snapshot).unwrap();

        // Verify state
        assert_eq!(restored.dimensions(), index.dimensions());
        assert_eq!(restored.len(), index.len());
        assert_eq!(restored.metric(), index.metric());

        // Verify vectors
        for i in 0..100 {
            let original = index.get(i).unwrap();
            let restored_vec = restored.get(i).unwrap();
            assert_eq!(original, restored_vec);
        }
    }

    #[test]
    #[allow(clippy::useless_vec)] // Variable value requires vec!
    fn test_snapshot_with_deletions() {
        let mut index = FlatIndex::new(FlatIndexConfig::new(16));

        // Insert vectors
        for i in 0..50 {
            index.insert(&vec![i as f32; 16]).unwrap();
        }

        // Delete some
        assert!(index.delete(10));
        assert!(index.delete(20));
        assert!(index.delete(30));

        // Save and restore
        let snapshot = index.to_snapshot().unwrap();
        let restored = FlatIndex::from_snapshot(&snapshot).unwrap();

        // Verify deletions preserved
        assert!(restored.get(10).is_none());
        assert!(restored.get(20).is_none());
        assert!(restored.get(30).is_none());
        assert!(restored.get(0).is_some());
        assert!(restored.get(49).is_some());

        // Verify deletion count
        let (_, delete_count, _) = restored.deletion_stats();
        assert_eq!(delete_count, 3);
    }

    #[test]
    fn test_snapshot_with_quantization() {
        let mut index = FlatIndex::new(FlatIndexConfig::new(128));

        // Insert vectors
        for i in 0..100 {
            let v: Vec<f32> = (0..128)
                .map(|j| if (i + j) % 2 == 0 { 1.0 } else { -1.0 })
                .collect();
            index.insert(&v).unwrap();
        }

        // Enable quantization
        index.enable_quantization().unwrap();
        assert!(index.is_quantized());

        // Save and restore
        let snapshot = index.to_snapshot().unwrap();
        let restored = FlatIndex::from_snapshot(&snapshot).unwrap();

        // Verify quantization state
        assert!(restored.is_quantized());

        // Search should work
        let query: Vec<f32> = (0..128)
            .map(|j| if j % 2 == 0 { 1.0 } else { -1.0 })
            .collect();
        let results = restored.search_quantized(&query, 5).unwrap();
        assert_eq!(results.len(), 5);
    }

    #[test]
    fn test_snapshot_different_metrics() {
        for metric in [
            DistanceMetric::Cosine,
            DistanceMetric::DotProduct,
            DistanceMetric::L2,
            DistanceMetric::Hamming,
        ] {
            let config = FlatIndexConfig::new(32).with_metric(metric);
            let mut index = FlatIndex::new(config);

            index.insert(&[0.5; 32]).unwrap();

            let snapshot = index.to_snapshot().unwrap();
            let restored = FlatIndex::from_snapshot(&snapshot).unwrap();

            assert_eq!(restored.metric(), metric);
        }
    }

    #[test]
    fn test_snapshot_invalid_magic() {
        // Create minimal valid header, then corrupt magic
        let mut index = FlatIndex::new(FlatIndexConfig::new(4));
        index.insert(&[1.0; 4]).unwrap();

        let mut snapshot = index.to_snapshot().unwrap();

        // Corrupt magic in header (after the 4-byte length)
        if snapshot.len() > 8 {
            snapshot[4] = b'X';
            snapshot[5] = b'X';
            snapshot[6] = b'X';
            snapshot[7] = b'X';
        }

        let result = FlatIndex::from_snapshot(&snapshot);
        assert!(result.is_err());
    }

    #[test]
    fn test_snapshot_truncated() {
        let mut index = FlatIndex::new(FlatIndexConfig::new(16));
        index.insert(&[1.0; 16]).unwrap();

        let snapshot = index.to_snapshot().unwrap();

        // Truncate snapshot
        let truncated = &snapshot[..snapshot.len() / 2];

        let result = FlatIndex::from_snapshot(truncated);
        assert!(result.is_err());
    }

    #[test]
    fn test_snapshot_corrupted_checksum() {
        let mut index = FlatIndex::new(FlatIndexConfig::new(16));
        index.insert(&[1.0; 16]).unwrap();

        let mut snapshot = index.to_snapshot().unwrap();

        // Corrupt data section (flip bits in vector data)
        if snapshot.len() > 100 {
            snapshot[100] ^= 0xFF;
        }

        let result = FlatIndex::from_snapshot(&snapshot);
        assert!(result.is_err());
    }

    #[test]
    fn test_search_after_restore() {
        let config = FlatIndexConfig::new(64).with_metric(DistanceMetric::Cosine);
        let mut index = FlatIndex::new(config);

        // Insert known vectors
        index.insert(&[1.0; 64]).unwrap(); // ID 0
        index.insert(&[0.5; 64]).unwrap(); // ID 1
        index.insert(&[0.0; 64]).unwrap(); // ID 2

        // Save and restore
        let snapshot = index.to_snapshot().unwrap();
        let restored = FlatIndex::from_snapshot(&snapshot).unwrap();

        // Search should return same results
        let query = [1.0; 64];
        let original_results = index.search(&query, 3).unwrap();
        let restored_results = restored.search(&query, 3).unwrap();

        assert_eq!(original_results.len(), restored_results.len());
        for (orig, rest) in original_results.iter().zip(restored_results.iter()) {
            assert_eq!(orig.id, rest.id);
            assert!((orig.score - rest.score).abs() < 1e-6);
        }
    }

    #[test]
    fn test_snapshot_empty_index() {
        let index = FlatIndex::new(FlatIndexConfig::new(128));

        let snapshot = index.to_snapshot().unwrap();
        let restored = FlatIndex::from_snapshot(&snapshot).unwrap();

        assert_eq!(restored.dimensions(), 128);
        assert!(restored.is_empty());
        assert_eq!(restored.len(), 0);
    }

    #[test]
    fn test_snapshot_preserves_next_id() {
        let mut index = FlatIndex::new(FlatIndexConfig::new(8));

        // Insert, delete, insert pattern to increase next_id
        let id1 = index.insert(&[1.0; 8]).unwrap();
        let id2 = index.insert(&[2.0; 8]).unwrap();
        index.delete(id1);
        let id3 = index.insert(&[3.0; 8]).unwrap();

        assert_eq!(id1, 0);
        assert_eq!(id2, 1);
        assert_eq!(id3, 2);

        // Save and restore
        let snapshot = index.to_snapshot().unwrap();
        let mut restored = FlatIndex::from_snapshot(&snapshot).unwrap();

        // Next insert should get ID 3
        let id4 = restored.insert(&[4.0; 8]).unwrap();
        assert_eq!(id4, 3);
    }

    #[test]
    fn test_snapshot_cleanup_threshold() {
        let config = FlatIndexConfig::new(8).with_cleanup_threshold(0.25);
        let index = FlatIndex::new(config);

        let snapshot = index.to_snapshot().unwrap();
        let restored = FlatIndex::from_snapshot(&snapshot).unwrap();

        // Can't directly access cleanup_threshold, but we can verify behavior
        // by inserting and deleting (would need additional getter)
        assert_eq!(restored.dimensions(), 8);
    }

    #[test]
    fn test_snapshot_header_validation() {
        use crate::persistence::PersistenceError;

        let header = FlatIndexHeader {
            magic: FLAT_INDEX_MAGIC,
            version: FLAT_INDEX_VERSION,
            dimensions: 64,
            metric: DistanceMetric::Cosine,
            count: 10,
            delete_count: 0,
            next_id: 10,
            is_quantized: false,
            cleanup_threshold: 0.5,
            checksum: 0,
        };

        assert!(header.validate().is_ok());

        // Wrong magic
        let bad_magic = FlatIndexHeader {
            magic: [b'X', b'X', b'X', b'X'],
            ..header.clone()
        };
        assert!(matches!(
            bad_magic.validate(),
            Err(PersistenceError::InvalidMagic { .. })
        ));

        // Future version
        let future_version = FlatIndexHeader {
            version: FLAT_INDEX_VERSION + 1,
            ..header.clone()
        };
        assert!(matches!(
            future_version.validate(),
            Err(PersistenceError::UnsupportedVersion(_, _))
        ));
    }

    // ------------------------------------------------------------------------
    // BQ Recall Comparison Test (HOSTILE_REVIEWER m2)
    // ------------------------------------------------------------------------

    /// Test that measures recall degradation of BQ vs F32 search.
    ///
    /// BQ uses Hamming distance on binary-packed vectors, which is an
    /// approximation of angular distance. This test validates:
    /// 1. BQ recall is documented and measured
    /// 2. BQ provides meaningful (non-random) results
    ///
    /// # Important Notes on BQ Recall
    ///
    /// Binary quantization works best when:
    /// - Data has clear sign patterns (e.g., embedding models)
    /// - Using angular/cosine similarity metrics
    /// - Vectors are normalized
    ///
    /// For uniformly random data, BQ recall is lower (~30-50%) because
    /// random vectors don't have meaningful sign structure. Real embedding
    /// data (e.g., from BERT, OpenAI) typically achieves 70-90% recall.
    ///
    /// The 32x memory reduction makes BQ valuable even at lower recall
    /// when used as a first-pass filter (reranking with F32).
    #[test]
    fn test_bq_vs_f32_recall_comparison() {
        use std::collections::HashSet;

        let dim = 128;
        let count = 500;
        let k = 10;
        let num_queries = 50;

        // Use a consistent seed for reproducibility
        let seed = 12345u64;

        // Create index with cosine metric
        let config = FlatIndexConfig::new(dim).with_metric(DistanceMetric::Cosine);
        let mut index = FlatIndex::new(config);

        // Generate reproducible vectors using LCG
        let mut state = seed;
        let next_f32 = |s: &mut u64| -> f32 {
            // LCG: state = state * a + c mod m
            *s = s.wrapping_mul(6_364_136_223_846_793_005).wrapping_add(1);
            // Map to [-1, 1]
            ((*s >> 33) as f32 / (u32::MAX >> 1) as f32) * 2.0 - 1.0
        };

        // Insert vectors
        for _ in 0..count {
            let v: Vec<f32> = (0..dim).map(|_| next_f32(&mut state)).collect();
            index.insert(&v).unwrap();
        }

        // Enable quantization
        index.enable_quantization().unwrap();

        // Measure recall across multiple queries
        let mut total_recall = 0.0;

        for q in 0..num_queries {
            // Use existing vectors as queries (ensures they exist in the index)
            let query_id = q * (count / num_queries);
            let query = index
                .get(u64::try_from(query_id).unwrap())
                .unwrap()
                .to_vec();

            // Get ground truth (F32 exact search)
            let f32_results = index.search(&query, k).unwrap();
            let f32_ids: HashSet<u64> = f32_results.iter().map(|r| r.id).collect();

            // Get BQ approximate results
            let bq_results = index.search_quantized(&query, k).unwrap();
            let bq_ids: HashSet<u64> = bq_results.iter().map(|r| r.id).collect();

            // Calculate recall: |F32 ∩ BQ| / |F32|
            let intersection = f32_ids.intersection(&bq_ids).count();
            let recall = intersection as f32 / k as f32;
            total_recall += recall;
        }

        let avg_recall = total_recall / num_queries as f32;

        // Document the measured recall
        println!(
            "BQ vs F32 Recall Comparison:\n\
             - Dataset: {} vectors @ {}D (random uniform)\n\
             - Queries: {}\n\
             - k: {}\n\
             - Average Recall@{}: {:.1}%\n\
             - Note: Random data has lower recall; real embeddings achieve 70-90%",
            count,
            dim,
            num_queries,
            k,
            k,
            avg_recall * 100.0
        );

        // For random data, BQ recall is ~30-50% (expected).
        // Random chance for 10/500 = 2%, so BQ is significantly better.
        // We assert > 20% to ensure BQ provides non-random results.
        // Real embedding data would use a higher threshold (e.g., 70%).
        assert!(
            avg_recall >= 0.20,
            "BQ recall too low: {:.1}% (minimum: 20% for random data)",
            avg_recall * 100.0
        );

        // Verify BQ is significantly better than random chance (2%)
        let random_chance = k as f32 / count as f32;
        assert!(
            avg_recall > random_chance * 5.0,
            "BQ recall ({:.1}%) not significantly better than random ({:.1}%)",
            avg_recall * 100.0,
            random_chance * 100.0
        );
    }

    // ============= Phase 3 Remediation: IdOverflow =============

    #[test]
    fn test_id_overflow_protection() {
        let mut index = FlatIndex::new(FlatIndexConfig::new(3));
        // Set next_id to u64::MAX to trigger overflow on next insert
        index.next_id = u64::MAX;

        let result = index.insert(&[1.0, 2.0, 3.0]);
        assert!(matches!(result, Err(FlatIndexError::IdOverflow)));
    }
}