kimun_core 0.2.20

Core library for the Kimün notes application
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
pub(crate) mod search_terms;

use std::collections::HashMap;
use std::path::Path;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::Duration;

use log::{debug, error};
use search_terms::{OrderBy, SearchTerms};
use sqlx::sqlite::{SqlitePool, SqlitePoolOptions};
use sqlx::{Row, Sqlite, Transaction};

use crate::note::{ContentChunk, LinkType, NoteContentData, NoteDetails};

fn row_to_note_entry(
    row: &sqlx::sqlite::SqliteRow,
) -> Result<(NoteEntryData, NoteContentData), DBError> {
    let path: String = row.try_get("path")?;
    let title: String = row.try_get("title")?;
    let size: i64 = row.try_get("size")?;
    let modified: i64 = row.try_get("modified")?;
    let hash: String = row.try_get("hash")?;

    let hash_val: u64 = hash.parse().unwrap_or_else(|e| {
        // A non-numeric hash means a corrupt row (or schema drift). Falling
        // back to 0 lets indexing continue but flags the issue loudly so the
        // operator can rebuild the index.
        log::warn!(
            "Non-numeric hash in DB for {}: {} ({}). Treating as 0.",
            path,
            hash,
            e
        );
        0
    });

    let note_path = VaultPath::new(&path);
    let entry = NoteEntryData {
        path: note_path,
        size: size as u64,
        modified_secs: modified as u64,
    };
    let content = NoteContentData::new(title, hash_val);
    Ok((entry, content))
}

use super::error::DBError;

/// All columns after `path` for `SELECT … FROM notes` queries. Used to build
/// qualified column lists without `.split_once` + `.unwrap()`.
const NOTE_COLUMNS_REST: &str = "title, size, modified, hash, noteName";

/// Column list shared by every `SELECT … FROM notes` query that maps rows
/// through `row_to_note_entry`. Order must match the `try_get` calls there.
const NOTE_COLUMNS: &str = "path, title, size, modified, hash, noteName";

/// Prefixes each comma-separated column name in `cols` with `prefix.`, useful
/// for join queries that disambiguate which table a column comes from.
fn qualify_columns(prefix: &str, cols: &str) -> String {
    cols.split(", ")
        .map(|c| format!("{}.{}", prefix, c))
        .collect::<Vec<_>>()
        .join(", ")
}

use super::{
    nfs::{with_note_extension, NoteEntryData, PATH_SEPARATOR},
    VaultPath,
};

// 0.10: Added `links(source)` and `notes(noteName)` indexes so the forward-link
//       filter `>`/`fwd:` and bare-name source resolution are index-served
//       instead of full scans. Bump forces a clean reindex.
// 0.8: Tightened hashtag word-boundary rule — `##tag`, `#tag#more`, and
//      similar adjacent-`#` patterns are no longer treated as labels. Bump
//      forces a clean reindex so the `labels` table drops the stale rows
//      that the old extractor produced.
// 0.7: Dropped the redundant `labels_by_name` index (the PK autoindex
//      sqlite_autoindex_labels_1 already covers WHERE name = ? lookups).
//      Bump forces a clean reindex so existing 0.6 installs drop the dead
//      index on next launch.
// 0.6: Added `labels` table populated from hashtags in note bodies. Bump
//      forces a clean reindex so the table is filled for existing vaults.
// 0.5: BREADCRUMB_SEP changed from `>` to `\x1f`. Bump forces a clean
//      reindex so stale rows with the old separator are rewritten.
// 0.9: Added `dest_name` column + index to `links` (bare lowercased filename
//      of each link destination) so the `>`/`lk:` link filter matches notes
//      by name with an indexed lookup instead of a leading-`%` scan. Bump
//      forces a clean reindex so the column is populated for existing vaults.
const VERSION: &str = "0.10";
pub(crate) const DB_FILE: &str = "kimun.sqlite";

/// The diff a vault sync walk produces and `NoteIndex::apply` consumes in
/// one atomic operation — the currency crossing the index's interface.
/// The order of `to_add` and `to_modify` is non-deterministic: they are
/// populated by parallel walker threads and entries land in the order each
/// thread completes its file read.
pub struct IndexDiff {
    /// Notes present in the vault but absent from the index, each paired with
    /// its full text content for FTS insertion.
    pub to_add: Vec<(NoteEntryData, String)>,
    /// Notes present in both the vault and the index whose content has
    /// changed, each paired with its current text content.
    pub to_modify: Vec<(NoteEntryData, String)>,
    /// Notes present in the index but no longer on disk, to be removed.
    pub to_delete: Vec<VaultPath>,
}

/// The searchable index of the vault — search, suggestions, backlinks, and
/// the index's own lifecycle. The interface speaks in notes, queries, and
/// note links; SQLite, sqlx, transactions, and schema versioning are
/// implementation and never cross it. Atomicity is carried by composite
/// operations ([`apply`](Self::apply), [`rename_note`](Self::rename_note))
/// rather than by exposing transactions.
#[derive(Debug, Clone)]
pub(crate) struct NoteIndex {
    pool: SqlitePool,
    /// `true` while the index is valid but possibly *empty*: set when
    /// [`open`](Self::open) recreated a missing/outdated/invalid schema
    /// (self-heal) or when [`recreate`](Self::recreate) dropped the
    /// tables, cleared by [`mark_synced`](Self::mark_synced) once a vault
    /// sync pass has filled the index. Shared across clones (like the pool)
    /// so every handle agrees on readiness.
    healed: Arc<AtomicBool>,
}

impl NoteIndex {
    /// Opens the index at `db_path`, self-healing the schema: when the stored
    /// index is missing, outdated, or invalid, the tables are silently
    /// recreated, leaving a valid but empty index that the next sync pass
    /// fills. [`ready`](Self::ready) reports whether a heal
    /// happened.
    pub(crate) async fn open<P: AsRef<Path>>(db_path: P) -> Result<Self, DBError> {
        let db_path = db_path.as_ref().to_owned();
        if let Some(parent) = db_path.parent() {
            crate::nfs::ensure_dir(parent).map_err(|e| DBError::Other(e.to_string()))?;
        }
        let connection_string = format!("sqlite:{}?mode=rwc", db_path.display());

        let pool = SqlitePoolOptions::new()
            .max_connections(5)
            .acquire_timeout(Duration::from_secs(30))
            .connect(&connection_string)
            .await?;

        // Only a *readable* schema that is missing or stale heals (the
        // "no such table" case is mapped to `Ok(false)` inside the probe).
        // A probe that errors — SQLITE_BUSY from a concurrent process,
        // transient I/O — propagates and fails the open: silently dropping
        // the tables of a healthy index on a transient error would destroy
        // a valid cache.
        let healed = if Self::schema_is_current(&pool).await? {
            false
        } else {
            debug!("Index schema missing/outdated/invalid — recreating");
            init_db(&pool).await?;
            true
        };

        Ok(Self {
            pool,
            healed: Arc::new(AtomicBool::new(healed)),
        })
    }

    /// `false` when the schema was healed ([`open`](Self::open)) or dropped
    /// ([`recreate`](Self::recreate)) and no sync pass has filled the index
    /// since. Fast paths use this to refuse to operate against an empty
    /// index without paying for a sync.
    pub(crate) fn ready(&self) -> bool {
        // Relaxed: the flag is advisory — it gates whether callers bother
        // with a sync, it does not publish index contents (the SQLite pool
        // provides the real synchronization). No happens-before is implied.
        !self.healed.load(Ordering::Relaxed)
    }

    /// Records that a vault sync pass completed: the index now mirrors the
    /// vault on disk, so [`ready`](Self::ready) reports `true` from here on.
    pub(crate) fn mark_synced(&self) {
        self.healed.store(false, Ordering::Relaxed);
    }

    /// `true` when the stored schema version matches [`VERSION`].
    async fn schema_is_current(pool: &SqlitePool) -> Result<bool, DBError> {
        let version: Option<String> =
            sqlx::query_scalar("SELECT value FROM appData WHERE name = 'version'")
                .fetch_optional(pool)
                .await
                .or_else(|e| {
                    // No appData table at all — fresh or foreign file.
                    if e.to_string().contains("no such table") {
                        return Ok(None);
                    }
                    Err(e)
                })?;
        match version {
            Some(v) => {
                debug!("DB Version: {}, current DB Version: {}", v, VERSION);
                Ok(v == VERSION)
            }
            None => Ok(false),
        }
    }

    /// Drops every table and recreates the schema, leaving the index valid
    /// but empty — [`ready`](Self::ready) reports `false` until the full
    /// sync pass that callers are expected to run afterwards
    /// [`mark_synced`](Self::mark_synced)s.
    pub(crate) async fn recreate(&self) -> Result<(), DBError> {
        init_db(&self.pool).await?;
        self.healed.store(true, Ordering::Relaxed);
        Ok(())
    }

    /// Applies a sync diff — adds, modifications, deletions — in one atomic
    /// operation.
    pub(crate) async fn apply(&self, diff: IndexDiff) -> Result<(), DBError> {
        let mut tx = self.pool.begin().await?;
        delete_notes(&mut tx, &diff.to_delete).await?;
        insert_notes(&mut tx, &diff.to_add).await?;
        update_notes(&mut tx, &diff.to_modify).await?;
        tx.commit().await?;
        Ok(())
    }

    /// Renames a note's index rows and updates the rewritten backlink
    /// victims' chunks/links, atomically.
    pub(crate) async fn rename_note(
        &self,
        from: &VaultPath,
        to: &VaultPath,
        rewritten: &[(NoteEntryData, String)],
    ) -> Result<(), DBError> {
        let mut tx = self.pool.begin().await?;
        rename_note(&mut tx, from, to).await?;
        update_notes(&mut tx, rewritten).await?;
        tx.commit().await?;
        Ok(())
    }

    pub(crate) async fn rename_directory(
        &self,
        from: &VaultPath,
        to: &VaultPath,
    ) -> Result<(), DBError> {
        let mut tx = self.pool.begin().await?;
        rename_directory(&mut tx, from, to).await?;
        tx.commit().await?;
        Ok(())
    }

    pub(crate) async fn delete_notes(&self, paths: &[VaultPath]) -> Result<(), DBError> {
        let mut tx = self.pool.begin().await?;
        delete_notes(&mut tx, paths).await?;
        tx.commit().await?;
        Ok(())
    }

    pub(crate) async fn delete_directories(
        &self,
        directories: &[VaultPath],
    ) -> Result<(), DBError> {
        let mut tx = self.pool.begin().await?;
        delete_directories(&mut tx, directories).await?;
        tx.commit().await?;
        Ok(())
    }

    /// Indexes one saved note and returns its computed content data (title
    /// + hash), so callers never parse the note a second time.
    pub(crate) async fn save_note(
        &self,
        entry_data: &NoteEntryData,
        note_details: &NoteDetails,
    ) -> Result<NoteContentData, DBError> {
        save_note(&self.pool, entry_data, note_details).await
    }

    pub(crate) async fn search<S: AsRef<str>>(
        &self,
        search_query: S,
    ) -> Result<Vec<(NoteEntryData, NoteContentData)>, DBError> {
        search_terms(&self.pool, search_query).await
    }

    pub(crate) async fn search_note_by_name<S: AsRef<str>>(
        &self,
        name: S,
    ) -> Result<Vec<(NoteEntryData, NoteContentData)>, DBError> {
        search_note_by_name(&self.pool, name).await
    }

    pub(crate) async fn search_note_by_path(
        &self,
        path: &VaultPath,
    ) -> Result<Vec<(NoteEntryData, NoteContentData)>, DBError> {
        search_note_by_path(&self.pool, path).await
    }

    pub(crate) async fn get_notes(
        &self,
        path: &VaultPath,
        recursive: bool,
    ) -> Result<Vec<(NoteEntryData, NoteContentData)>, DBError> {
        get_notes(&self.pool, path, recursive).await
    }

    pub(crate) async fn get_all_notes(
        &self,
    ) -> Result<Vec<(NoteEntryData, NoteContentData)>, DBError> {
        get_all_notes(&self.pool).await
    }

    pub(crate) async fn get_backlinks(
        &self,
        path: &VaultPath,
    ) -> Result<Vec<(NoteEntryData, NoteContentData)>, DBError> {
        get_backlinks(&self.pool, path).await
    }

    pub(crate) async fn get_notes_sections(
        &self,
        path: &VaultPath,
        recursive: bool,
    ) -> Result<HashMap<VaultPath, Vec<ContentChunk>>, DBError> {
        get_notes_sections(&self.pool, path, recursive).await
    }

    pub(crate) async fn list_labels(&self) -> Result<Vec<String>, DBError> {
        list_labels(&self.pool).await
    }

    pub(crate) async fn label_counts(&self) -> Result<Vec<(String, i64)>, DBError> {
        label_counts(&self.pool).await
    }

    pub(crate) async fn notes_with_label(&self, name: &str) -> Result<Vec<VaultPath>, DBError> {
        notes_with_label(&self.pool, name).await
    }

    pub(crate) async fn suggest_notes_by_prefix(
        &self,
        prefix: &str,
        limit: usize,
    ) -> Result<Vec<NoteSuggestion>, DBError> {
        suggest_notes_by_prefix(&self.pool, prefix, limit).await
    }

    pub(crate) async fn suggest_tags_by_prefix(
        &self,
        prefix: &str,
        limit: usize,
    ) -> Result<Vec<TagSuggestion>, DBError> {
        suggest_tags_by_prefix(&self.pool, prefix, limit).await
    }
}

#[cfg(test)]
impl NoteIndex {
    /// Test-only pool accessor — index-internal tests exercise SQL and the
    /// query builders directly through this internal seam.
    fn pool(&self) -> &SqlitePool {
        &self.pool
    }

    /// Test-only: release the pool's file handles promptly.
    async fn close(&self) {
        self.pool.close().await;
    }
}

/// Deletes all tables and recreates them
async fn init_db(pool: &SqlitePool) -> Result<(), DBError> {
    debug!("Deleting DB");
    delete_db(pool).await?;
    debug!("Creating Tables");
    create_tables(pool).await
}

async fn delete_db(pool: &SqlitePool) -> Result<(), DBError> {
    let rows = sqlx::query("SELECT name FROM sqlite_schema WHERE type = 'table'")
        .fetch_all(pool)
        .await?;

    let mut tables = vec![];
    for row in rows {
        let table_name: String = row.try_get("name")?;
        tables.push(table_name);
    }

    for table in tables {
        // Can't use params for tables or columns, so we use format!
        let drop_query = format!("DROP TABLE '{}'", table);
        match sqlx::query(&drop_query).execute(pool).await {
            Ok(_) => {}
            Err(e) => {
                if table.contains("_") {
                    // Some virtual tables are automatically deleted
                    debug!("Error for table {}: {}", table, e);
                } else {
                    return Err(DBError::DBError(e));
                }
            }
        }
    }

    sqlx::query("VACUUM").execute(pool).await?;
    Ok(())
}

async fn create_tables(pool: &SqlitePool) -> Result<(), DBError> {
    let mut tx = pool.begin().await?;

    sqlx::query(
        "CREATE TABLE appData (
            name TEXT PRIMARY KEY,
            value TEXT
        )",
    )
    .execute(&mut *tx)
    .await?;

    sqlx::query("INSERT INTO appData (name, value) VALUES (?, ?)")
        .bind("version")
        .bind(VERSION)
        .execute(&mut *tx)
        .await?;

    // Storing hash as a string, as SQLite doesn't like
    // unsigned 64bit integers, alternatively we could
    // have used signed numbers by subtracting the half
    // of the max value
    sqlx::query(
        "CREATE TABLE notes (
            path TEXT PRIMARY KEY,
            title TEXT,
            hash TEXT,
            size INTEGER,
            modified INTEGER,
            basePath TEXT,
            noteName TEXT
        )",
    )
    .execute(&mut *tx)
    .await?;

    sqlx::query(
        "CREATE TABLE links (
            source TEXT,
            destination TEXT,
            dest_name TEXT
        )",
    )
    .execute(&mut *tx)
    .await?;

    sqlx::query(
        "CREATE INDEX backlinks
            ON links(destination)",
    )
    .execute(&mut *tx)
    .await?;

    // Backs the `<`/`lk:` backlink filter's name-anywhere match (folder-independent
    // bare filename), so it never has to scan with a leading-`%` LIKE.
    sqlx::query(
        "CREATE INDEX links_by_dest_name
            ON links(dest_name)",
    )
    .execute(&mut *tx)
    .await?;

    // Backs the `>`/`fwd:` forward-link filter, which filters/joins on
    // `links.source`, so it never has to full-scan the links table.
    sqlx::query(
        "CREATE INDEX links_by_source
            ON links(source)",
    )
    .execute(&mut *tx)
    .await?;

    // Backs bare-name source resolution (the `>`/`fwd:` filter joins links
    // back to `notes.noteName`), so the join is index-served instead of a
    // full scan.
    sqlx::query(
        "CREATE INDEX notes_by_name
            ON notes(noteName)",
    )
    .execute(&mut *tx)
    .await?;

    sqlx::query(
        "CREATE VIRTUAL TABLE notesContent USING fts4(
            path,
            breadcrumb,
            text
        )",
    )
    .execute(&mut *tx)
    .await?;

    sqlx::query(
        "CREATE TABLE labels (
            name TEXT NOT NULL,
            path TEXT NOT NULL,
            PRIMARY KEY (name, path)
        )",
    )
    .execute(&mut *tx)
    .await?;

    sqlx::query(
        "CREATE INDEX labels_by_path
            ON labels(path)",
    )
    .execute(&mut *tx)
    .await?;

    tx.commit().await?;

    Ok(())
}

/// Joins the positive and negative conditions of one operator class. Positives
/// AND together (each same-type term must match, matching the documented
/// "all terms are ANDed" precedence and the `#`/`>`/`<` operators); negatives
/// already AND.
fn combine_conditions(positive: Vec<String>, negative: Vec<String>) -> Option<String> {
    match (positive.is_empty(), negative.is_empty()) {
        (true, true) => None,
        (false, true) => Some(positive.join(" AND ")),
        (true, false) => Some(negative.join(" AND ")),
        (false, false) => Some(format!(
            "{} AND {}",
            positive.join(" AND "),
            negative.join(" AND ")
        )),
    }
}

fn build_like_conditions(
    positive_terms: &[String],
    negative_terms: &[String],
    pos_condition_fn: impl Fn(usize) -> String,
    neg_condition_fn: impl Fn(usize) -> String,
    var_num: &mut usize,
    params: &mut Vec<String>,
    push_term_fn: impl Fn(&String) -> String,
) -> Option<String> {
    let mut positive_conditions = vec![];
    let mut negative_conditions = vec![];

    for term in positive_terms {
        if !term.is_empty() {
            positive_conditions.push(pos_condition_fn(*var_num));
            params.push(push_term_fn(term));
            *var_num += 1;
        }
    }

    for term in negative_terms {
        if !term.is_empty() {
            negative_conditions.push(neg_condition_fn(*var_num));
            params.push(push_term_fn(term));
            *var_num += 1;
        }
    }

    combine_conditions(positive_conditions, negative_conditions)
}

/// Base query for the search fan-out. Aliases `notes.path` to `path` so the
/// shared `row_to_note_entry` mapper finds all `NOTE_COLUMNS` keys. First
/// column is qualified to disambiguate the `notesContent`/`notes` join; the
/// rest are unique to `notes` and need no prefix.
static SEARCH_BASE_SQL: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| {
    format!(
        "SELECT DISTINCT notes.path as path, {} FROM notesContent JOIN notes ON notesContent.path = notes.path",
        NOTE_COLUMNS_REST
    )
});

fn search_base_sql() -> &'static str {
    &SEARCH_BASE_SQL
}

fn build_search_sql_query_inner(search_terms: &SearchTerms) -> (String, Vec<String>) {
    let mut var_num = 1;
    let mut params: Vec<String> = vec![];
    let mut queries: Vec<String> = vec![];

    add_fts_query(search_terms, &mut var_num, &mut params, &mut queries);
    add_filename_query(search_terms, &mut var_num, &mut params, &mut queries);
    add_path_query(search_terms, &mut var_num, &mut params, &mut queries);
    add_labels_query(search_terms, &mut var_num, &mut params, &mut queries);
    add_links_query(search_terms, &mut var_num, &mut params, &mut queries);
    add_forward_links_query(search_terms, &mut var_num, &mut params, &mut queries);

    if queries.is_empty() {
        debug!("No query provided");
        return (String::new(), vec![]);
    }
    (queries.join(" INTERSECT "), params)
}

/// Free-text + breadcrumb FTS branches. Content (whole-row) and breadcrumb
/// (heading-path column) are *separate* INTERSECT branches: FTS4 allows only
/// one `MATCH` per virtual table per SELECT, and its in-MATCH column filter
/// (`breadcrumb:"x"`) is unreliable across builds, so the two cannot be folded
/// into a single scan. Within each branch, the positive `MATCH` is ANDed with
/// `NOT IN` subqueries for that field's exclusions (FTS4 has no reliable
/// pure-negative / inline `-term`, so a subquery is used uniformly).
fn add_fts_query(
    s: &SearchTerms,
    var_num: &mut usize,
    params: &mut Vec<String>,
    queries: &mut Vec<String>,
) {
    add_fts_field_query(
        &s.terms,
        &s.excluded_terms,
        "notesContent",
        fts4_quote,
        var_num,
        params,
        queries,
    );
    add_fts_field_query(
        &s.breadcrumb,
        &s.excluded_breadcrumb,
        "notesContent.breadcrumb",
        fts4_quote,
        var_num,
        params,
        queries,
    );
}

/// Emits one FTS branch for a single field (`notesContent` for content,
/// `notesContent.breadcrumb` for headings): a positive `MATCH` (all positive
/// terms space-joined into one query) ANDed with one `NOT IN` subquery per
/// excluded term. Pure-exclusion (no positives) drops the leading `MATCH`.
fn add_fts_field_query(
    positives: &[String],
    excludeds: &[String],
    match_target: &str,
    quote: impl Fn(&str) -> String,
    var_num: &mut usize,
    params: &mut Vec<String>,
    queries: &mut Vec<String>,
) {
    if positives.is_empty() && excludeds.is_empty() {
        return;
    }

    let mut conditions: Vec<String> = vec![];

    if !positives.is_empty() {
        conditions.push(format!("{} MATCH ?{}", match_target, var_num));
        params.push(
            positives
                .iter()
                .map(|t| quote(t))
                .collect::<Vec<_>>()
                .join(" "),
        );
        *var_num += 1;
    }

    for term in excludeds {
        conditions.push(format!(
            "notes.path NOT IN (SELECT DISTINCT notesContent.path FROM notesContent WHERE {} MATCH ?{})",
            match_target, var_num
        ));
        params.push(quote(term));
        *var_num += 1;
    }

    queries.push(format!(
        "{} WHERE {}",
        search_base_sql(),
        conditions.join(" AND ")
    ));
}

fn add_filename_query(
    s: &SearchTerms,
    var_num: &mut usize,
    params: &mut Vec<String>,
    queries: &mut Vec<String>,
) {
    if s.filename.is_empty() && s.excluded_filename.is_empty() {
        return;
    }
    if let Some(final_where) = build_like_conditions(
        &s.filename,
        &s.excluded_filename,
        |n| format!("notes.noteName LIKE ?{} ESCAPE '\\'", n),
        |n| format!("notes.noteName NOT LIKE ?{} ESCAPE '\\'", n),
        var_num,
        params,
        |t: &String| {
            if t.contains('*') {
                // Explicit wildcard: extension-aware whole-name match, * → %.
                // Escape first (so literal % / _ stay escaped), then * → %.
                escape_like_pattern(&with_note_extension(t)).replace('*', "%")
            } else {
                // Substring match (unchanged behaviour).
                format!("%{}%", escape_like_pattern(t))
            }
        },
    ) {
        queries.push(format!("{} WHERE {}", search_base_sql(), final_where));
    }
}

fn add_path_query(
    s: &SearchTerms,
    var_num: &mut usize,
    params: &mut Vec<String>,
    queries: &mut Vec<String>,
) {
    if s.path.is_empty() && s.excluded_path.is_empty() {
        return;
    }
    let positive_conditions = path_term_conditions(&s.path, var_num, params, true);
    let negative_conditions = path_term_conditions(&s.excluded_path, var_num, params, false);
    if let Some(final_where) = combine_conditions(positive_conditions, negative_conditions) {
        queries.push(format!("{} WHERE {}", search_base_sql(), final_where));
    }
}

/// Notes-only base SELECT (no `notesContent` join) so membership-style filters
/// (labels, links) don't pay an FTS scan. No `DISTINCT`: `notes.path` is the
/// primary key, so every notes-only branch already yields unique paths (and
/// `INTERSECT` dedups across branches regardless). Same columns as
/// `SEARCH_BASE_SQL` so INTERSECT branches line up.
static NOTES_BASE_SQL: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| {
    format!(
        "SELECT notes.path as path, {} FROM notes",
        NOTE_COLUMNS_REST
    )
});

fn notes_base_sql() -> &'static str {
    &NOTES_BASE_SQL
}

/// Fan-out shared by membership-style operators (labels, links): each positive
/// term becomes its own INTERSECT branch (`notes.path IN (subquery)`); excluded
/// terms are bundled into one notes-only SELECT chaining `NOT IN (subquery)` so
/// the INTERSECT machinery still composes. `mk_subquery(term, var_num, params)`
/// returns the inner `SELECT <col> FROM …` for one term (pushing its bind
/// params and advancing `var_num`), or `None` to skip a degenerate term.
fn add_membership_query<F>(
    positives: &[String],
    excludeds: &[String],
    var_num: &mut usize,
    params: &mut Vec<String>,
    queries: &mut Vec<String>,
    mk_subquery: F,
) where
    F: Fn(&str, &mut usize, &mut Vec<String>) -> Option<String>,
{
    for term in positives {
        if let Some(sub) = mk_subquery(term, var_num, params) {
            queries.push(format!(
                "{} WHERE notes.path IN ({})",
                notes_base_sql(),
                sub
            ));
        }
    }

    if excludeds.is_empty() {
        return;
    }
    let mut clauses = Vec::with_capacity(excludeds.len());
    for term in excludeds {
        if let Some(sub) = mk_subquery(term, var_num, params) {
            clauses.push(format!("notes.path NOT IN ({})", sub));
        }
    }
    if !clauses.is_empty() {
        queries.push(format!(
            "{} WHERE {}",
            notes_base_sql(),
            clauses.join(" AND ")
        ));
    }
}

fn add_labels_query(
    s: &SearchTerms,
    var_num: &mut usize,
    params: &mut Vec<String>,
    queries: &mut Vec<String>,
) {
    // Each label is matched via the labels PK autoindex.
    add_membership_query(
        &s.labels,
        &s.excluded_labels,
        var_num,
        params,
        queries,
        |label, var_num, params| {
            let sub = format!("SELECT path FROM labels WHERE name = ?{}", var_num);
            params.push(label.to_string());
            *var_num += 1;
            Some(sub)
        },
    );
}

/// Builds the `SELECT source FROM links WHERE …` subquery for one link-filter
/// target (`>`/`lk:`). Matching is by note name, extension optional,
/// case-insensitive, with `*` wildcards.
///
/// A bare name matches a link in *any* folder via the indexed `dest_name`
/// column (the folder-independent basename) — no leading-`%` scan. A slash in
/// the target anchors it to a full path via indexed equality on `destination`
/// (covering both the relative and absolute stored forms). Wildcards fall back
/// to `LIKE`, but the pattern is prefix-anchored so the index can still help
/// (e.g. `proj*`).
///
/// This is the name-anywhere counterpart to [`get_backlinks`], which matches a
/// *specific* note by its exact full path or bare name. Both rely on the same
/// stored-form invariant: link destinations are lowercased, carry the note
/// extension, and are either a bare relative name or a relative/absolute path.
/// Returns `None` for an empty target.
/// Normalized pieces of a link-filter target, shared by [`link_subquery`]
/// (backlinks) and [`forward_link_subquery`] (forward links). Only the SQL
/// column names differ between the two; the normalization is identical.
struct LinkTarget {
    /// Lowercased, extension-applied note name/path used as the bound param.
    name: String,
    /// `true` when the target contains a path separator (anchor to full path).
    is_path_qualified: bool,
    /// `true` when the target contains a `*` wildcard (use `LIKE`).
    has_wildcard: bool,
}

/// Normalize a link-filter target: trim/lowercase, strip a leading separator,
/// detect path-qualified / wildcard, and apply the note extension. Returns
/// `None` for an empty target.
///
/// A leading separator only signals "absolute"; both stored forms are matched
/// anyway, so it is stripped before normalizing.
fn normalize_link_target(target: &str) -> Option<LinkTarget> {
    let lowered = target.trim().to_lowercase();
    let stripped = lowered.strip_prefix(PATH_SEPARATOR).unwrap_or(&lowered);
    if stripped.is_empty() {
        return None;
    }
    let is_path_qualified = stripped.contains(PATH_SEPARATOR);
    let has_wildcard = stripped.contains('*');
    let name = with_note_extension(stripped);
    Some(LinkTarget {
        name,
        is_path_qualified,
        has_wildcard,
    })
}

fn link_subquery(target: &str, var_num: &mut usize, params: &mut Vec<String>) -> Option<String> {
    let LinkTarget {
        name,
        is_path_qualified,
        has_wildcard,
    } = normalize_link_target(target)?;

    let body = if has_wildcard {
        // Escape LIKE metacharacters in the literal, then turn user `*` into
        // the SQL `%` wildcard. Destinations never contain `*`, so this is safe.
        let pattern = escape_like_pattern(&name).replace('*', "%");
        params.push(pattern);
        let body = if is_path_qualified {
            format!(
                "destination LIKE ?{n} ESCAPE '\\' OR destination LIKE ('/' || ?{n}) ESCAPE '\\'",
                n = var_num
            )
        } else {
            format!("dest_name LIKE ?{n} ESCAPE '\\'", n = var_num)
        };
        *var_num += 1;
        body
    } else if is_path_qualified {
        // Indexed equality on the full path (relative or absolute stored form).
        params.push(name);
        let body = format!(
            "destination = ?{n} OR destination = ('/' || ?{n})",
            n = var_num
        );
        *var_num += 1;
        body
    } else {
        // Indexed equality on the folder-independent basename.
        params.push(name);
        let body = format!("dest_name = ?{n}", n = var_num);
        *var_num += 1;
        body
    };
    Some(format!("SELECT source FROM links WHERE {body}"))
}

/// Backlinks filter (`<` / `lk:`). Each positive target is its own INTERSECT
/// branch (AND semantics, like labels); exclusions are bundled into a single
/// notes-only SELECT chaining `NOT IN`.
fn add_links_query(
    s: &SearchTerms,
    var_num: &mut usize,
    params: &mut Vec<String>,
    queries: &mut Vec<String>,
) {
    add_membership_query(
        &s.links,
        &s.excluded_links,
        var_num,
        params,
        queries,
        link_subquery,
    );
}

/// Builds the subquery of NOTE PATHS that are link *destinations* of the
/// note(s) named `target` — i.e. the forward-links of `target` (`>` / `fwd:`).
///
/// Where [`link_subquery`] selects the *sources* of links pointing at a name,
/// this selects the *destinations* of links emitted by the note(s) matching the
/// name. The destination column is heterogeneous (a bare relative name, or a
/// relative/absolute path), so resolving it back to a concrete note path is
/// done by joining `notes n2` and matching on all stored forms:
///   - `l.dest_name = n2.noteName` (folder-independent bare basename, both
///     carry the `.md` extension), or
///   - `l.destination = n2.path` (relative stored form), or
///   - `l.destination = '/' || n2.path` (absolute stored form).
///
/// The *source* note is matched by name exactly as [`link_subquery`] matches a
/// target: bare name → indexed equality on `src.noteName` (the lowercased
/// basename, `.md`-suffixed); path-qualified → equality on `src.path` (relative
/// or absolute); wildcards → `LIKE`. Returns `None` for an empty target.
fn forward_link_subquery(
    target: &str,
    var_num: &mut usize,
    params: &mut Vec<String>,
) -> Option<String> {
    let LinkTarget {
        name,
        is_path_qualified,
        has_wildcard,
    } = normalize_link_target(target)?;

    let src_match = if has_wildcard {
        let pattern = escape_like_pattern(&name).replace('*', "%");
        params.push(pattern);
        let body = if is_path_qualified {
            format!(
                "src.path LIKE ?{n} ESCAPE '\\' OR src.path LIKE ('/' || ?{n}) ESCAPE '\\'",
                n = var_num
            )
        } else {
            format!("src.noteName LIKE ?{n} ESCAPE '\\'", n = var_num)
        };
        *var_num += 1;
        body
    } else if is_path_qualified {
        params.push(name);
        let body = format!("src.path = ?{n} OR src.path = ('/' || ?{n})", n = var_num);
        *var_num += 1;
        body
    } else {
        params.push(name);
        let body = format!("src.noteName = ?{n}", n = var_num);
        *var_num += 1;
        body
    };

    Some(format!(
        "SELECT n2.path FROM notes n2 \
         JOIN links l ON (l.dest_name = n2.noteName \
                          OR l.destination = n2.path \
                          OR l.destination = ('/' || n2.path)) \
         JOIN notes src ON src.path = l.source \
         WHERE {src_match}"
    ))
}

/// Forward-links filter (`>` / `fwd:`). Mirrors [`add_links_query`] but over
/// `forward_links`/`excluded_forward_links` with [`forward_link_subquery`], so
/// forward-link branches INTERSECT/compose like every other membership filter.
fn add_forward_links_query(
    s: &SearchTerms,
    var_num: &mut usize,
    params: &mut Vec<String>,
    queries: &mut Vec<String>,
) {
    add_membership_query(
        &s.forward_links,
        &s.excluded_forward_links,
        var_num,
        params,
        queries,
        forward_link_subquery,
    );
}

/// Builds basePath conditions for path-style search terms. A trailing
/// `PATH_SEPARATOR` means an exact directory match; otherwise the term is a
/// prefix. `positive` selects the operator family (`=` / `LIKE` vs.
/// `!=` / `NOT LIKE`).
fn path_term_conditions(
    terms: &[String],
    var_num: &mut usize,
    params: &mut Vec<String>,
    positive: bool,
) -> Vec<String> {
    let mut out = vec![];
    for term in terms {
        if term.is_empty() {
            continue;
        }
        let (cond, value) = if term.contains('*') {
            // Explicit wildcard: anchor at the leading separator, translate the
            // user's `*` into the SQL `%` wildcard (escape first so any literal
            // `%`/`_` stays literal). No auto-appended `%` — the `*` placement
            // fully controls matching (e.g. `/work*` = prefix, `/wo*k` = infix).
            let op = if positive { "LIKE" } else { "NOT LIKE" };
            (
                format!("notes.basePath {} ('/' || ?{}) ESCAPE '\\'", op, var_num),
                escape_like_pattern(term).replace('*', "%"),
            )
        } else {
            match term.strip_suffix(PATH_SEPARATOR) {
                Some(absolute) => {
                    let op = if positive { "=" } else { "!=" };
                    (
                        format!("notes.basePath {} ('/' || ?{})", op, var_num),
                        absolute.to_string(),
                    )
                }
                None => {
                    let op = if positive { "LIKE" } else { "NOT LIKE" };
                    (
                        format!(
                            "notes.basePath {} ('/' || ?{} || '%') ESCAPE '\\'",
                            op, var_num
                        ),
                        escape_like_pattern(term),
                    )
                }
            }
        };
        out.push(cond);
        params.push(value);
        *var_num += 1;
    }
    out
}

#[cfg(test)]
fn build_search_sql_query<S: AsRef<str>>(query: S) -> (String, Vec<String>) {
    let search_terms = SearchTerms::from_query_string(query);
    build_search_sql_query_inner(&search_terms)
}

async fn get_all_notes(
    pool: &SqlitePool,
) -> Result<Vec<(NoteEntryData, NoteContentData)>, DBError> {
    let query = format!("SELECT DISTINCT {} FROM notes", NOTE_COLUMNS);
    let rows = sqlx::query(&query).fetch_all(pool).await?;
    rows.iter().map(row_to_note_entry).collect()
}

async fn list_labels(pool: &SqlitePool) -> Result<Vec<String>, DBError> {
    let rows: Vec<(String,)> = sqlx::query_as("SELECT DISTINCT name FROM labels")
        .fetch_all(pool)
        .await?;
    Ok(rows.into_iter().map(|(n,)| n).collect())
}

/// A note suggestion for the autocomplete popup.
///
/// `name` is the note's filename without extension — the string a wikilink
/// actually targets, since wikilinks are stored by name, not by full vault
/// path (see `get_backlinks` and the surrounding `noteName` column).
/// `path` is carried so the UI can disambiguate when multiple notes share a
/// name, but the wikilink target inserted on accept is `name`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NoteSuggestion {
    /// The note's filename without extension — the exact text a wikilink
    /// targets, since wikilinks are stored by name rather than by full path.
    pub name: String,
    /// The note's full vault path, so the UI can disambiguate when several
    /// notes share a `name`. The link inserted on accept is still `name`.
    pub path: VaultPath,
}

/// A tag suggestion for the autocomplete popup. `usage_count` is computed
/// per-query via `COUNT(*) GROUP BY name` over the `labels` table.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TagSuggestion {
    /// The label text (lowercased, as stored in the `labels` table).
    pub label: String,
    /// How many notes carry this label, computed per-query via
    /// `COUNT(*) GROUP BY name`, so the UI can rank common tags first.
    pub usage_count: u32,
}

/// Returns notes whose `noteName` starts with `prefix` (case-insensitive),
/// capped at `limit`. Empty prefix returns the top `limit` notes by name.
///
/// Results are ordered alphabetically by name. Notes that share a name are
/// both returned as separate rows; callers (the autocomplete UI) are
/// responsible for disambiguating them via `path`.
///
/// The returned `name` is the note's filename with the extension stripped
/// (via `VaultPath::get_clean_name`) — i.e. the exact text a wikilink
/// targets. Filenames in the index are already lowercased on insert
/// (see `VaultPathSlice::new`), so callers get lowercase names back.
async fn suggest_notes_by_prefix(
    pool: &SqlitePool,
    prefix: &str,
    limit: usize,
) -> Result<Vec<NoteSuggestion>, DBError> {
    let pattern = format!("{}%", escape_like_pattern(&prefix.to_lowercase()));
    // `noteName` is lowercased on insert, so `LIKE` against a lowercased
    // pattern is naturally case-insensitive; the explicit `LOWER()` is a
    // defensive belt-and-braces against any future code path that might
    // insert mixed case.
    let sql = "SELECT path \
               FROM notes \
               WHERE LOWER(noteName) LIKE ?1 ESCAPE '\\' \
               ORDER BY noteName ASC, path ASC \
               LIMIT ?2";
    let rows: Vec<(String,)> = sqlx::query_as(sql)
        .bind(&pattern)
        .bind(limit as i64)
        .fetch_all(pool)
        .await?;
    Ok(rows
        .into_iter()
        .map(|(path,)| {
            let vault_path = VaultPath::new(path);
            let name = vault_path.get_clean_name();
            NoteSuggestion {
                name,
                path: vault_path,
            }
        })
        .collect())
}

/// Returns tag labels whose name starts with `prefix` (case-insensitive),
/// each paired with how many notes carry the tag, capped at `limit`. Empty
/// prefix returns the top `limit` tags by usage.
///
/// The `labels` table is stored lowercased, so prefix matching is naturally
/// case-insensitive once we lowercase the input. Ranking is `usage_count
/// DESC, label ASC` so the most-used tags surface first.
async fn suggest_tags_by_prefix(
    pool: &SqlitePool,
    prefix: &str,
    limit: usize,
) -> Result<Vec<TagSuggestion>, DBError> {
    let pattern = format!("{}%", escape_like_pattern(&prefix.to_lowercase()));
    let sql = "SELECT name, COUNT(*) AS cnt \
               FROM labels \
               WHERE name LIKE ?1 ESCAPE '\\' \
               GROUP BY name \
               ORDER BY cnt DESC, name ASC \
               LIMIT ?2";
    let rows: Vec<(String, i64)> = sqlx::query_as(sql)
        .bind(&pattern)
        .bind(limit as i64)
        .fetch_all(pool)
        .await?;
    Ok(rows
        .into_iter()
        .map(|(label, cnt)| TagSuggestion {
            label,
            usage_count: cnt.max(0) as u32,
        })
        .collect())
}

async fn label_counts(pool: &SqlitePool) -> Result<Vec<(String, i64)>, DBError> {
    let rows: Vec<(String, i64)> =
        sqlx::query_as("SELECT name, COUNT(*) as cnt FROM labels GROUP BY name ORDER BY name")
            .fetch_all(pool)
            .await?;
    Ok(rows)
}

async fn notes_with_label(pool: &SqlitePool, name: &str) -> Result<Vec<VaultPath>, DBError> {
    let normalized = name.to_lowercase();
    let rows: Vec<(String,)> = sqlx::query_as("SELECT path FROM labels WHERE name = ?")
        .bind(&normalized)
        .fetch_all(pool)
        .await?;
    Ok(rows.into_iter().map(|(p,)| VaultPath::new(p)).collect())
}

async fn search_terms<S: AsRef<str>>(
    pool: &SqlitePool,
    search_query: S,
) -> Result<Vec<(NoteEntryData, NoteContentData)>, DBError> {
    let search_query = search_query.as_ref();
    let search_terms = SearchTerms::from_query_string(search_query);
    let (query, params) = build_search_sql_query_inner(&search_terms);
    let order_by = search_terms.order_by;

    if query.is_empty() {
        debug!("No query provided");
        return Ok(vec![]);
    }

    debug!("QUERY: {}", query);

    let mut sql_query = sqlx::query(&query);
    for param in params {
        sql_query = sql_query.bind(param);
    }

    let rows = sql_query.fetch_all(pool).await?;

    let mut result: Vec<(NoteEntryData, NoteContentData)> = rows
        .iter()
        .map(row_to_note_entry)
        .collect::<Result<_, _>>()?;

    if !order_by.is_empty() {
        result.sort_by(|(a_entry, a_content), (b_entry, b_content)| {
            for ob in &order_by {
                let ord = match ob {
                    OrderBy::Title { asc } => {
                        let cmp = a_content
                            .title
                            .to_lowercase()
                            .cmp(&b_content.title.to_lowercase());
                        if *asc {
                            cmp
                        } else {
                            cmp.reverse()
                        }
                    }
                    OrderBy::FileName { asc } => {
                        let cmp = a_entry.path.to_string().cmp(&b_entry.path.to_string());
                        if *asc {
                            cmp
                        } else {
                            cmp.reverse()
                        }
                    }
                };
                if ord != std::cmp::Ordering::Equal {
                    return ord;
                }
            }
            std::cmp::Ordering::Equal
        });
    }

    Ok(result)
}

async fn search_note_by_name<S: AsRef<str>>(
    pool: &SqlitePool,
    name: S,
) -> Result<Vec<(NoteEntryData, NoteContentData)>, DBError> {
    let name = name.as_ref().to_lowercase();
    let sql = format!("SELECT {} FROM notes where noteName = ?", NOTE_COLUMNS);
    let rows = sqlx::query(&sql).bind(&name).fetch_all(pool).await?;

    rows.iter().map(row_to_note_entry).collect()
}

async fn search_note_by_path(
    pool: &SqlitePool,
    path: &VaultPath,
) -> Result<Vec<(NoteEntryData, NoteContentData)>, DBError> {
    let sql = format!("SELECT {} FROM notes where path = ?", NOTE_COLUMNS);
    let path_string = path.to_string();
    let rows = sqlx::query(&sql).bind(&path_string).fetch_all(pool).await?;

    // Should always return one or zero
    rows.iter().map(row_to_note_entry).collect()
}

async fn get_notes(
    pool: &SqlitePool,
    path: &VaultPath,
    recursive: bool,
) -> Result<Vec<(NoteEntryData, NoteContentData)>, DBError> {
    let (where_clause, bind_value) = if recursive {
        (
            "basePath LIKE (? || '%') ESCAPE '\\'".to_string(),
            escape_like_pattern(&path.to_string()),
        )
    } else {
        ("basePath = ?".to_string(), path.to_string())
    };
    let sql = format!("SELECT {} FROM notes where {}", NOTE_COLUMNS, where_clause);
    let rows = sqlx::query(&sql).bind(bind_value).fetch_all(pool).await?;

    rows.iter().map(row_to_note_entry).collect()
}

/// Backlinks of a *specific* note: notes whose body links to exactly this note,
/// matched by its full path OR its bare filename (wikilinks stored without a
/// path). This is intentionally narrower than the `>`/`lk:` search filter
/// (see [`link_subquery`]), which matches a name in *any* folder; keep the two
/// in step on the stored-form invariant they share (lowercased, `.md`-suffixed
/// destinations, bare-relative or relative/absolute path).
async fn get_backlinks(
    pool: &SqlitePool,
    path: &VaultPath,
) -> Result<Vec<(NoteEntryData, NoteContentData)>, DBError> {
    // Match notes that link to the full path OR by filename only (wikilinks stored without path)
    let sql = format!(
        "SELECT DISTINCT {cols} \
         FROM notes n \
         JOIN links l ON n.path = l.source \
         WHERE l.destination = ? OR l.destination = ?",
        cols = qualify_columns("n", NOTE_COLUMNS),
    );
    let rows = sqlx::query(&sql)
        .bind(path.to_string())
        .bind(path.get_name())
        .fetch_all(pool)
        .await?;

    rows.iter().map(row_to_note_entry).collect()
}

async fn get_notes_sections(
    pool: &SqlitePool,
    path: &VaultPath,
    recursive: bool,
) -> Result<HashMap<VaultPath, Vec<ContentChunk>>, DBError> {
    let mut result = HashMap::new();
    let (sql, bind_value) = if path.is_note() {
        // Exact note path
        (
            "SELECT path, breadcrumb, text FROM notesContent WHERE path = ?".to_string(),
            path.to_string(),
        )
    } else if recursive {
        // All notes under this directory tree
        (
            "SELECT path, breadcrumb, text FROM notesContent WHERE path LIKE (? || '%') ESCAPE '\\'".to_string(),
            escape_like_pattern(&path.to_string()),
        )
    } else {
        // Only notes directly in this directory (basePath join)
        ("SELECT nc.path, nc.breadcrumb, nc.text FROM notesContent nc JOIN notes n ON nc.path = n.path WHERE n.basePath = ?".to_string(), path.to_string())
    };

    let rows = sqlx::query(&sql).bind(bind_value).fetch_all(pool).await?;

    for row in rows {
        let path: String = row.try_get("path")?;
        let breadcrumb: String = row.try_get("breadcrumb")?;
        let text: String = row.try_get("text")?;

        let path = VaultPath::new(path);
        let chunk = ContentChunk { breadcrumb, text };
        result.entry(path).or_insert_with(Vec::new).push(chunk);
    }

    Ok(result)
}

async fn insert_notes(
    tx: &mut Transaction<'_, Sqlite>,
    notes: &[(NoteEntryData, String)],
) -> Result<(), DBError> {
    if notes.is_empty() {
        return Ok(());
    }
    debug!("Inserting {} notes", notes.len());
    upsert_notes_batched(tx, notes).await
}

async fn update_notes(
    tx: &mut Transaction<'_, Sqlite>,
    notes: &[(NoteEntryData, String)],
) -> Result<(), DBError> {
    if notes.is_empty() {
        return Ok(());
    }
    debug!("Updating {} notes", notes.len());
    upsert_notes_batched(tx, notes).await
}

async fn delete_notes(
    tx: &mut Transaction<'_, Sqlite>,
    paths: &[VaultPath],
) -> Result<(), DBError> {
    if paths.is_empty() {
        return Ok(());
    }
    let path_strings: Vec<String> = paths.iter().map(|p| p.to_string()).collect();
    bulk_delete_in(tx, "notes", &["path"], &path_strings).await?;
    bulk_delete_in(tx, "notesContent", &["path"], &path_strings).await?;
    bulk_delete_in(tx, "links", &["source", "destination"], &path_strings).await?;
    bulk_delete_in(tx, "labels", &["path"], &path_strings).await?;
    Ok(())
}

async fn save_note(
    pool: &SqlitePool,
    entry_data: &NoteEntryData,
    note_details: &NoteDetails,
) -> Result<NoteContentData, DBError> {
    // Parse once and hand the computed content data back to the caller, so
    // the full-text hash + title extraction is never done twice per save.
    let data = note_details.get_content_data();
    let (chunks, links) = note_details.get_chunks_and_links();
    let label_count = links
        .iter()
        .filter(|l| matches!(l.ltype, LinkType::Hashtag))
        .count();
    let mut batch = NoteBatch::with_capacity(1, chunks.len(), links.len(), label_count);
    batch.push(entry_data, data.clone(), chunks, links);

    let mut tx = pool.begin().await?;
    batch.flush(&mut tx).await?;
    tx.commit().await?;
    Ok(data)
}

// SQLite default parameter limit is 999. Stay under for safety.
const SQLITE_PARAM_BUDGET: usize = 900;

struct NoteRow {
    path_idx: usize,
    title: String,
    size: i64,
    modified: i64,
    hash: String,
    base_path: String,
    name: String,
}

struct ChunkRow {
    path_idx: usize,
    breadcrumb: String,
    text: String,
}

struct LinkRow {
    path_idx: usize,
    destination: String,
    /// Bare lowercased filename of `destination` (folder-independent), indexed
    /// to back the link filter's name-anywhere match. See `link_subquery`.
    dest_name: String,
}

struct LabelRow {
    path_idx: usize,
    name: String,
}

/// Bulk-upserts a slice of notes plus their chunks and links inside the given
/// transaction. Each note's raw text is parsed once; chunks/links are bound by
/// `path_idx` into a shared `paths` table to avoid per-row clones. Inserts
/// chunk via `bulk_insert` so binds-per-statement stay under
/// `SQLITE_PARAM_BUDGET`.
async fn upsert_notes_batched(
    tx: &mut Transaction<'_, Sqlite>,
    notes: &[(NoteEntryData, String)],
) -> Result<(), DBError> {
    if notes.is_empty() {
        return Ok(());
    }
    let mut batch = NoteBatch::with_capacity(notes.len(), 0, 0, notes.len() * 4);
    for (entry_data, text) in notes {
        // Avoid `NoteDetails::new` — it would clone the raw text purely to be
        // re-borrowed for each parse pass below. The borrowed-text associated
        // functions take the text by `AsRef<str>` and keep it borrowed.
        let data = NoteDetails::content_data_of(text);
        let (chunks, links) = NoteDetails::chunks_and_links_of(&entry_data.path, text);
        batch.push(entry_data, data, chunks, links);
    }
    batch.flush(tx).await
}

/// Accumulates the per-note row sets for a multi-note write. `paths` holds
/// each note's path once; chunk and link rows reference paths by index, so
/// no path string is cloned per row.
struct NoteBatch {
    paths: Vec<String>,
    notes: Vec<NoteRow>,
    chunks: Vec<ChunkRow>,
    links: Vec<LinkRow>,
    labels: Vec<LabelRow>,
}

impl NoteBatch {
    fn with_capacity(notes: usize, chunks: usize, links: usize, labels: usize) -> Self {
        Self {
            paths: Vec::with_capacity(notes),
            notes: Vec::with_capacity(notes),
            chunks: Vec::with_capacity(chunks),
            links: Vec::with_capacity(links),
            labels: Vec::with_capacity(labels),
        }
    }

    fn push(
        &mut self,
        entry_data: &NoteEntryData,
        data: NoteContentData,
        chunks: Vec<ContentChunk>,
        links: Vec<crate::note::NoteLink>,
    ) {
        let idx = self.paths.len();
        let (parent_path, name) = entry_data.path.get_parent_path();
        self.paths.push(entry_data.path.to_string());
        self.notes.push(NoteRow {
            path_idx: idx,
            title: data.title,
            size: entry_data.size as i64,
            modified: entry_data.modified_secs as i64,
            hash: data.hash.to_string(),
            base_path: parent_path.to_string(),
            name,
        });
        for c in chunks {
            self.chunks.push(ChunkRow {
                path_idx: idx,
                breadcrumb: c.breadcrumb,
                text: c.text,
            });
        }
        for l in &links {
            match &l.ltype {
                LinkType::Note(p) => {
                    self.links.push(LinkRow {
                        path_idx: idx,
                        destination: p.to_string(),
                        // Already lowercased by VaultPathSlice; folder-independent.
                        dest_name: p.get_name(),
                    });
                }
                LinkType::Hashtag => {
                    let normalized = l.text.to_lowercase();
                    if !normalized.is_empty() {
                        self.labels.push(LabelRow {
                            path_idx: idx,
                            name: normalized,
                        });
                    }
                }
                _ => {}
            }
        }
    }

    async fn flush(self, tx: &mut Transaction<'_, Sqlite>) -> Result<(), DBError> {
        bulk_upsert_note_rows(tx, &self.notes, &self.paths).await?;
        bulk_delete_in(tx, "notesContent", &["path"], &self.paths).await?;
        bulk_delete_in(tx, "links", &["source"], &self.paths).await?;
        bulk_delete_in(tx, "labels", &["path"], &self.paths).await?;
        bulk_insert(tx, &self.chunks, &self.paths).await?;
        bulk_insert(tx, &self.links, &self.paths).await?;
        bulk_insert(tx, &self.labels, &self.paths).await?;
        Ok(())
    }
}

async fn bulk_upsert_note_rows(
    tx: &mut Transaction<'_, Sqlite>,
    rows: &[NoteRow],
    paths: &[String],
) -> Result<(), DBError> {
    bulk_insert(tx, rows, paths).await.map_err(|e| match e {
        DBError::DBError(inner) => {
            error!("Error upserting {} notes: {}", rows.len(), inner);
            DBError::DBError(inner)
        }
        other => other,
    })
}

fn placeholders(rows: usize, cols: usize) -> String {
    let one = format!("({})", vec!["?"; cols].join(", "));
    std::iter::repeat_n(one.as_str(), rows)
        .collect::<Vec<_>>()
        .join(", ")
}

/// `DELETE FROM <table> WHERE <col1> IN (?, ?, …) [OR <col2> IN (...) …]`,
/// chunked by parameter budget. With multiple columns each value is bound
/// once per column; budget halves accordingly.
///
/// `table` and `columns` are interpolated into the SQL — never accept
/// untrusted input here. The `&'static str` bound prevents passing
/// caller-derived strings.
async fn bulk_delete_in(
    tx: &mut Transaction<'_, Sqlite>,
    table: &'static str,
    columns: &[&'static str],
    values: &[String],
) -> Result<(), DBError> {
    if values.is_empty() || columns.is_empty() {
        return Ok(());
    }
    let max_per_chunk = SQLITE_PARAM_BUDGET / columns.len();
    for chunk in values.chunks(max_per_chunk) {
        let ph = vec!["?"; chunk.len()].join(", ");
        let where_clause = columns
            .iter()
            .map(|c| format!("{} IN ({})", c, ph))
            .collect::<Vec<_>>()
            .join(" OR ");
        let sql = format!("DELETE FROM {} WHERE {}", table, where_clause);
        let mut q = sqlx::query(&sql);
        for _ in columns {
            for v in chunk {
                q = q.bind(v);
            }
        }
        q.execute(&mut **tx).await?;
    }
    Ok(())
}

/// Trait for rows that can be batch-inserted via `bulk_insert`. Each impl
/// provides the SQL framing constants and a per-row `bind_to` method.
trait BulkInsertRow {
    /// Statement prefix ending in `VALUES `.
    const HEADER: &'static str;
    /// Optional clause appended after the placeholders (e.g. `ON CONFLICT …`).
    const FOOTER: &'static str;
    /// Number of `?` placeholders per row.
    const COLS: usize;

    fn bind_to<'q>(
        &'q self,
        q: sqlx::query::Query<'q, Sqlite, sqlx::sqlite::SqliteArguments<'q>>,
        paths: &'q [String],
    ) -> sqlx::query::Query<'q, Sqlite, sqlx::sqlite::SqliteArguments<'q>>;
}

impl BulkInsertRow for NoteRow {
    const HEADER: &'static str =
        "INSERT INTO notes (path, title, size, modified, hash, basePath, noteName) VALUES ";
    const FOOTER: &'static str = " ON CONFLICT(path) DO UPDATE SET \
                                   title = excluded.title, \
                                   size = excluded.size, \
                                   modified = excluded.modified, \
                                   hash = excluded.hash";
    const COLS: usize = 7;

    fn bind_to<'q>(
        &'q self,
        q: sqlx::query::Query<'q, Sqlite, sqlx::sqlite::SqliteArguments<'q>>,
        paths: &'q [String],
    ) -> sqlx::query::Query<'q, Sqlite, sqlx::sqlite::SqliteArguments<'q>> {
        q.bind(&paths[self.path_idx])
            .bind(&self.title)
            .bind(self.size)
            .bind(self.modified)
            .bind(&self.hash)
            .bind(&self.base_path)
            .bind(&self.name)
    }
}

impl BulkInsertRow for ChunkRow {
    const HEADER: &'static str = "INSERT INTO notesContent (path, breadcrumb, text) VALUES ";
    const FOOTER: &'static str = "";
    const COLS: usize = 3;

    fn bind_to<'q>(
        &'q self,
        q: sqlx::query::Query<'q, Sqlite, sqlx::sqlite::SqliteArguments<'q>>,
        paths: &'q [String],
    ) -> sqlx::query::Query<'q, Sqlite, sqlx::sqlite::SqliteArguments<'q>> {
        q.bind(&paths[self.path_idx])
            .bind(&self.breadcrumb)
            .bind(&self.text)
    }
}

impl BulkInsertRow for LinkRow {
    const HEADER: &'static str = "INSERT INTO links (source, destination, dest_name) VALUES ";
    const FOOTER: &'static str = "";
    const COLS: usize = 3;

    fn bind_to<'q>(
        &'q self,
        q: sqlx::query::Query<'q, Sqlite, sqlx::sqlite::SqliteArguments<'q>>,
        paths: &'q [String],
    ) -> sqlx::query::Query<'q, Sqlite, sqlx::sqlite::SqliteArguments<'q>> {
        q.bind(&paths[self.path_idx])
            .bind(&self.destination)
            .bind(&self.dest_name)
    }
}

impl BulkInsertRow for LabelRow {
    const HEADER: &'static str = "INSERT INTO labels (name, path) VALUES ";
    const FOOTER: &'static str = " ON CONFLICT(name, path) DO NOTHING";
    const COLS: usize = 2;

    fn bind_to<'q>(
        &'q self,
        q: sqlx::query::Query<'q, Sqlite, sqlx::sqlite::SqliteArguments<'q>>,
        paths: &'q [String],
    ) -> sqlx::query::Query<'q, Sqlite, sqlx::sqlite::SqliteArguments<'q>> {
        q.bind(&self.name).bind(&paths[self.path_idx])
    }
}

/// Generic chunked multi-row INSERT. Builds `<HEADER>(?, …), (?, …)<FOOTER>`,
/// chunking so binds-per-statement stays under `SQLITE_PARAM_BUDGET`.
async fn bulk_insert<R: BulkInsertRow>(
    tx: &mut Transaction<'_, Sqlite>,
    rows: &[R],
    paths: &[String],
) -> Result<(), DBError> {
    if rows.is_empty() {
        return Ok(());
    }
    let max_rows = SQLITE_PARAM_BUDGET / R::COLS;
    for chunk in rows.chunks(max_rows) {
        let sql = format!(
            "{}{}{}",
            R::HEADER,
            placeholders(chunk.len(), R::COLS),
            R::FOOTER
        );
        let mut q = sqlx::query(&sql);
        for r in chunk {
            q = r.bind_to(q, paths);
        }
        q.execute(&mut **tx).await?;
    }
    Ok(())
}

/// Wraps a user-supplied FTS4 term in double quotes so SQLite treats it
/// as a literal phrase, neutralising any FTS4 metacharacters the user
/// may have typed (`(`, `)`, `*`, `"`, `:`, etc.) that would otherwise
/// cause SQLite to reject the query at runtime.
fn fts4_quote(term: &str) -> String {
    let escaped = term.replace('"', "\"\"");
    format!("\"{}\"", escaped)
}

/// Escapes SQLite LIKE pattern metacharacters (`\`, `%`, `_`) in `s` so the
/// result can be used as a safe literal prefix before appending `%`.
/// Must be paired with `ESCAPE '\\'` in the SQL clause.
fn escape_like_pattern(s: &str) -> String {
    let mut out = String::with_capacity(s.len() + 4);
    for c in s.chars() {
        match c {
            '\\' | '%' | '_' => {
                out.push('\\');
                out.push(c);
            }
            other => out.push(other),
        }
    }
    out
}

async fn rename_note(
    tx: &mut Transaction<'_, Sqlite>,
    from: &VaultPath,
    to: &VaultPath,
) -> Result<(), DBError> {
    let old_note_name = from.get_name();
    let (new_base_path, new_note_name) = to.get_parent_path();

    sqlx::query("UPDATE notes SET path = ?, basePath = ?, noteName = ? WHERE path = ?")
        .bind(to.to_string())
        .bind(new_base_path.to_string())
        .bind(&new_note_name)
        .bind(from.to_string())
        .execute(&mut **tx)
        .await?;

    sqlx::query("UPDATE notesContent SET path = ? WHERE path = ?")
        .bind(to.to_string())
        .bind(from.to_string())
        .execute(&mut **tx)
        .await?;

    sqlx::query("UPDATE links SET source = ? WHERE source = ?")
        .bind(to.to_string())
        .bind(from.to_string())
        .execute(&mut **tx)
        .await?;

    sqlx::query("UPDATE links SET destination = ?, dest_name = ? WHERE destination = ?")
        .bind(to.to_string())
        .bind(&new_note_name)
        .bind(from.to_string())
        .execute(&mut **tx)
        .await?;

    // Update links that reference the note by filename only (wikilinks without path)
    sqlx::query("UPDATE links SET destination = ?, dest_name = ? WHERE destination = ?")
        .bind(&new_note_name)
        .bind(&new_note_name)
        .bind(&old_note_name)
        .execute(&mut **tx)
        .await?;

    sqlx::query("UPDATE labels SET path = ? WHERE path = ?")
        .bind(to.to_string())
        .bind(from.to_string())
        .execute(&mut **tx)
        .await?;

    Ok(())
}

async fn rename_directory(
    tx: &mut Transaction<'_, Sqlite>,
    from: &VaultPath,
    to: &VaultPath,
) -> Result<(), DBError> {
    let from = {
        let s = from.to_string();
        if s.ends_with(PATH_SEPARATOR) {
            s
        } else {
            s + &PATH_SEPARATOR.to_string()
        }
    };
    let to = {
        let s = to.to_string();
        if s.ends_with(PATH_SEPARATOR) {
            s
        } else {
            s + &PATH_SEPARATOR.to_string()
        }
    };

    let from_escaped = escape_like_pattern(&from);

    let notes_sql = "UPDATE notes SET path = ? || SUBSTR(path, LENGTH(?) + 1), basePath = ? || SUBSTR(basePath, LENGTH(?) + 1) WHERE basePath LIKE (? || '%') ESCAPE '\\'";
    sqlx::query(notes_sql)
        .bind(&to)
        .bind(&from)
        .bind(&to)
        .bind(&from)
        .bind(&from_escaped)
        .execute(&mut **tx)
        .await?;

    sqlx::query("UPDATE notesContent SET path = ? || SUBSTR(path, LENGTH(?) + 1) WHERE path LIKE (? || '%') ESCAPE '\\'")
        .bind(&to)
        .bind(&from)
        .bind(&from_escaped)
        .execute(&mut **tx)
        .await?;

    sqlx::query(
        "UPDATE links SET source = ? || SUBSTR(source, LENGTH(?) + 1) WHERE source LIKE (? || '%') ESCAPE '\\'",
    )
    .bind(&to)
    .bind(&from)
    .bind(&from_escaped)
    .execute(&mut **tx)
    .await?;

    sqlx::query("UPDATE links SET destination = ? || SUBSTR(destination, LENGTH(?) + 1) WHERE destination LIKE (? || '%') ESCAPE '\\'")
        .bind(&to)
        .bind(&from)
        .bind(&from_escaped)
        .execute(&mut **tx)
        .await?;

    sqlx::query("UPDATE labels SET path = ? || SUBSTR(path, LENGTH(?) + 1) WHERE path LIKE (? || '%') ESCAPE '\\'")
        .bind(&to)
        .bind(&from)
        .bind(&from_escaped)
        .execute(&mut **tx)
        .await?;

    Ok(())
}

async fn delete_directories(
    tx: &mut Transaction<'_, Sqlite>,
    directories: &[VaultPath],
) -> Result<(), DBError> {
    if !directories.is_empty() {
        for directory in directories {
            delete_directory(tx, directory).await?;
        }
    }
    Ok(())
}

async fn delete_directory(
    tx: &mut Transaction<'_, Sqlite>,
    directory_path: &VaultPath,
) -> Result<(), DBError> {
    let path_str = directory_path.to_string();
    let normalized = if path_str.ends_with(PATH_SEPARATOR) {
        path_str
    } else {
        format!("{path_str}{PATH_SEPARATOR}")
    };
    let pattern = escape_like_pattern(&normalized);

    sqlx::query("DELETE FROM notes WHERE path LIKE (? || '%') ESCAPE '\\'")
        .bind(&pattern)
        .execute(&mut **tx)
        .await?;

    sqlx::query("DELETE FROM notesContent WHERE path LIKE (? || '%') ESCAPE '\\'")
        .bind(&pattern)
        .execute(&mut **tx)
        .await?;

    // Clear both sides of the links table — outbound (source) and inbound
    // (destination) — so backlinks pointing to deleted notes don't linger.
    sqlx::query("DELETE FROM links WHERE source LIKE (? || '%') ESCAPE '\\' OR destination LIKE (? || '%') ESCAPE '\\'")
        .bind(&pattern)
        .bind(&pattern)
        .execute(&mut **tx)
        .await?;

    sqlx::query("DELETE FROM labels WHERE path LIKE (? || '%') ESCAPE '\\'")
        .bind(&pattern)
        .execute(&mut **tx)
        .await?;

    Ok(())
}

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

    #[tokio::test]
    async fn open_creates_parent_dir_for_db_path() {
        let tmp = tempfile::TempDir::new().unwrap();
        let nested = tmp.path().join("nested/dir/cache.kimuncache");
        // Parent dir does not exist yet.
        assert!(!nested.parent().unwrap().exists());

        let db = super::NoteIndex::open(&nested).await.unwrap();
        assert!(nested.parent().unwrap().exists());
        assert!(nested.exists());
        // A fresh file has no schema — open must have healed it.
        assert!(!db.ready());
        db.close().await;
    }

    #[test]
    fn test_search_terms_query_empty() {
        let (sql, params) = build_search_sql_query("");
        assert_eq!(sql, "");
        assert_eq!(params.len(), 0);
    }

    #[test]
    fn test_search_terms_query_simple_terms() {
        let (sql, params) = build_search_sql_query("foo bar");
        assert_eq!(
            sql,
            "SELECT DISTINCT notes.path as path, title, size, modified, hash, noteName FROM notesContent JOIN notes ON notesContent.path = notes.path WHERE notesContent MATCH ?1"
        );
        assert_eq!(params.len(), 1);
        assert_eq!(params[0], "\"foo\" \"bar\"");
    }

    #[test]
    fn test_search_terms_query_single_term() {
        let (sql, params) = build_search_sql_query("keyword");
        assert_eq!(
            sql,
            "SELECT DISTINCT notes.path as path, title, size, modified, hash, noteName FROM notesContent JOIN notes ON notesContent.path = notes.path WHERE notesContent MATCH ?1"
        );
        assert_eq!(params.len(), 1);
        assert_eq!(params[0], "\"keyword\"");
    }

    #[test]
    fn test_search_terms_query_breadcrumb_only() {
        let (sql, params) = build_search_sql_query("@heading");
        assert_eq!(
            sql,
            "SELECT DISTINCT notes.path as path, title, size, modified, hash, noteName FROM notesContent JOIN notes ON notesContent.path = notes.path WHERE notesContent.breadcrumb MATCH ?1"
        );
        assert_eq!(params.len(), 1);
        assert_eq!(params[0], "\"heading\"");
    }

    #[test]
    fn test_search_terms_query_breadcrumb_with_in() {
        let (sql, params) = build_search_sql_query("in:section");
        assert_eq!(
            sql,
            "SELECT DISTINCT notes.path as path, title, size, modified, hash, noteName FROM notesContent JOIN notes ON notesContent.path = notes.path WHERE notesContent.breadcrumb MATCH ?1"
        );
        assert_eq!(params.len(), 1);
        assert_eq!(params[0], "\"section\"");
    }

    #[test]
    fn test_search_terms_query_multiple_breadcrumbs() {
        let (sql, params) = build_search_sql_query("@heading1 in:heading2");
        assert_eq!(
            sql,
            "SELECT DISTINCT notes.path as path, title, size, modified, hash, noteName FROM notesContent JOIN notes ON notesContent.path = notes.path WHERE notesContent.breadcrumb MATCH ?1"
        );
        assert_eq!(params.len(), 1);
        assert_eq!(params[0], "\"heading1\" \"heading2\"");
    }

    #[test]
    fn test_search_terms_query_path_only() {
        let (sql, params) = build_search_sql_query("=filename");
        assert_eq!(
            sql,
            "SELECT DISTINCT notes.path as path, title, size, modified, hash, noteName FROM notesContent JOIN notes ON notesContent.path = notes.path WHERE notes.noteName LIKE ?1 ESCAPE '\\'"
        );
        assert_eq!(params.len(), 1);
        assert_eq!(params[0], "%filename%");
    }

    #[test]
    fn test_search_terms_query_path_with_at() {
        let (sql, params) = build_search_sql_query("name:directory");
        assert_eq!(
            sql,
            "SELECT DISTINCT notes.path as path, title, size, modified, hash, noteName FROM notesContent JOIN notes ON notesContent.path = notes.path WHERE notes.noteName LIKE ?1 ESCAPE '\\'"
        );
        assert_eq!(params.len(), 1);
        assert_eq!(params[0], "%directory%");
    }

    #[test]
    fn test_search_terms_query_multiple_paths() {
        let (sql, params) = build_search_sql_query("=file1 name:file2");
        // Same-type operators AND together (consistent with #, <, >, and the
        // documented "all terms are ANDed" precedence).
        assert_eq!(
            sql,
            "SELECT DISTINCT notes.path as path, title, size, modified, hash, noteName FROM notesContent JOIN notes ON notesContent.path = notes.path WHERE notes.noteName LIKE ?1 ESCAPE '\\' AND notes.noteName LIKE ?2 ESCAPE '\\'"
        );
        assert_eq!(params.len(), 2);
        assert_eq!(params[0], "%file1%");
        assert_eq!(params[1], "%file2%");
    }

    #[test]
    fn test_search_terms_query_terms_and_breadcrumb() {
        let (sql, params) = build_search_sql_query("keyword @section");
        assert_eq!(
            sql,
            "SELECT DISTINCT notes.path as path, title, size, modified, hash, noteName FROM notesContent JOIN notes ON notesContent.path = notes.path WHERE notesContent MATCH ?1 INTERSECT SELECT DISTINCT notes.path as path, title, size, modified, hash, noteName FROM notesContent JOIN notes ON notesContent.path = notes.path WHERE notesContent.breadcrumb MATCH ?2"
        );
        assert_eq!(params.len(), 2);
        assert_eq!(params[0], "\"keyword\"");
        assert_eq!(params[1], "\"section\"");
    }

    #[test]
    fn test_search_terms_query_terms_and_path() {
        let (sql, params) = build_search_sql_query("keyword =file");
        assert_eq!(
            sql,
            "SELECT DISTINCT notes.path as path, title, size, modified, hash, noteName FROM notesContent JOIN notes ON notesContent.path = notes.path WHERE notesContent MATCH ?1 INTERSECT SELECT DISTINCT notes.path as path, title, size, modified, hash, noteName FROM notesContent JOIN notes ON notesContent.path = notes.path WHERE notes.noteName LIKE ?2 ESCAPE '\\'"
        );
        assert_eq!(params.len(), 2);
        assert_eq!(params[0], "\"keyword\"");
        assert_eq!(params[1], "%file%");
    }

    #[test]
    fn test_search_terms_query_breadcrumb_and_path() {
        let (sql, params) = build_search_sql_query("@heading =file");
        assert_eq!(
            sql,
            "SELECT DISTINCT notes.path as path, title, size, modified, hash, noteName FROM notesContent JOIN notes ON notesContent.path = notes.path WHERE notesContent.breadcrumb MATCH ?1 INTERSECT SELECT DISTINCT notes.path as path, title, size, modified, hash, noteName FROM notesContent JOIN notes ON notesContent.path = notes.path WHERE notes.noteName LIKE ?2 ESCAPE '\\'"
        );
        assert_eq!(params.len(), 2);
        assert_eq!(params[0], "\"heading\"");
        assert_eq!(params[1], "%file%");
    }

    #[test]
    fn test_search_terms_query_all_combined() {
        let (sql, params) = build_search_sql_query("keyword @heading =file");
        assert_eq!(
            sql,
            "SELECT DISTINCT notes.path as path, title, size, modified, hash, noteName FROM notesContent JOIN notes ON notesContent.path = notes.path WHERE notesContent MATCH ?1 INTERSECT SELECT DISTINCT notes.path as path, title, size, modified, hash, noteName FROM notesContent JOIN notes ON notesContent.path = notes.path WHERE notesContent.breadcrumb MATCH ?2 INTERSECT SELECT DISTINCT notes.path as path, title, size, modified, hash, noteName FROM notesContent JOIN notes ON notesContent.path = notes.path WHERE notes.noteName LIKE ?3 ESCAPE '\\'"
        );
        assert_eq!(params.len(), 3);
        assert_eq!(params[0], "\"keyword\"");
        assert_eq!(params[1], "\"heading\"");
        assert_eq!(params[2], "%file%");
    }

    #[test]
    fn test_search_terms_query_quoted_terms() {
        let (sql, params) = build_search_sql_query("\"exact phrase\" keyword");
        assert_eq!(
            sql,
            "SELECT DISTINCT notes.path as path, title, size, modified, hash, noteName FROM notesContent JOIN notes ON notesContent.path = notes.path WHERE notesContent MATCH ?1"
        );
        assert_eq!(params.len(), 1);
        assert_eq!(params[0], "\"exact phrase\" \"keyword\"");
    }

    #[test]
    fn test_search_terms_query_order_by_title_asc() {
        let (sql, params) = build_search_sql_query("keyword or:title");
        assert_eq!(
            sql,
            "SELECT DISTINCT notes.path as path, title, size, modified, hash, noteName FROM notesContent JOIN notes ON notesContent.path = notes.path WHERE notesContent MATCH ?1"
        );
        assert_eq!(params.len(), 1);
        assert_eq!(params[0], "\"keyword\"");
    }

    #[test]
    fn test_search_terms_query_order_by_title_desc() {
        let (sql, params) = build_search_sql_query("keyword -or:title");
        assert_eq!(
            sql,
            "SELECT DISTINCT notes.path as path, title, size, modified, hash, noteName FROM notesContent JOIN notes ON notesContent.path = notes.path WHERE notesContent MATCH ?1"
        );
        assert_eq!(params.len(), 1);
        assert_eq!(params[0], "\"keyword\"");
    }

    #[test]
    fn test_search_terms_query_order_by_filename_asc() {
        let (sql, params) = build_search_sql_query("keyword or:filename");
        assert_eq!(
            sql,
            "SELECT DISTINCT notes.path as path, title, size, modified, hash, noteName FROM notesContent JOIN notes ON notesContent.path = notes.path WHERE notesContent MATCH ?1"
        );
        assert_eq!(params.len(), 1);
        assert_eq!(params[0], "\"keyword\"");
    }

    #[test]
    fn test_search_terms_query_order_by_file_shorthand() {
        let (sql, params) = build_search_sql_query("keyword or:f");
        assert_eq!(
            sql,
            "SELECT DISTINCT notes.path as path, title, size, modified, hash, noteName FROM notesContent JOIN notes ON notesContent.path = notes.path WHERE notesContent MATCH ?1"
        );
        assert_eq!(params.len(), 1);
        assert_eq!(params[0], "\"keyword\"");
    }

    #[test]
    fn test_search_terms_query_order_by_title_shorthand() {
        let (sql, params) = build_search_sql_query("keyword or:t");
        assert_eq!(
            sql,
            "SELECT DISTINCT notes.path as path, title, size, modified, hash, noteName FROM notesContent JOIN notes ON notesContent.path = notes.path WHERE notesContent MATCH ?1"
        );
        assert_eq!(params.len(), 1);
        assert_eq!(params[0], "\"keyword\"");
    }

    #[test]
    fn test_search_terms_query_multiple_order_by() {
        let (sql, params) = build_search_sql_query("keyword ^title -^filename");
        assert_eq!(
            sql,
            "SELECT DISTINCT notes.path as path, title, size, modified, hash, noteName FROM notesContent JOIN notes ON notesContent.path = notes.path WHERE notesContent MATCH ?1"
        );
        assert_eq!(params.len(), 1);
        assert_eq!(params[0], "\"keyword\"");
    }

    #[test]
    fn test_search_terms_query_complex_with_order() {
        let (sql, params) = build_search_sql_query("keyword @section =file ^title");
        assert_eq!(
            sql,
            "SELECT DISTINCT notes.path as path, title, size, modified, hash, noteName FROM notesContent JOIN notes ON notesContent.path = notes.path WHERE notesContent MATCH ?1 INTERSECT SELECT DISTINCT notes.path as path, title, size, modified, hash, noteName FROM notesContent JOIN notes ON notesContent.path = notes.path WHERE notesContent.breadcrumb MATCH ?2 INTERSECT SELECT DISTINCT notes.path as path, title, size, modified, hash, noteName FROM notesContent JOIN notes ON notesContent.path = notes.path WHERE notes.noteName LIKE ?3 ESCAPE '\\'"
        );
        assert_eq!(params.len(), 3);
        assert_eq!(params[0], "\"keyword\"");
        assert_eq!(params[1], "\"section\"");
        assert_eq!(params[2], "%file%");
    }

    #[test]
    fn test_search_terms_query_only_order_by() {
        let (sql, params) = build_search_sql_query("^title");
        assert_eq!(sql, "");
        assert_eq!(params.len(), 0);
    }

    #[test]
    fn test_search_terms_query_invalid_order_by_field() {
        let (sql, params) = build_search_sql_query("keyword ^invalid");
        assert_eq!(
            sql,
            "SELECT DISTINCT notes.path as path, title, size, modified, hash, noteName FROM notesContent JOIN notes ON notesContent.path = notes.path WHERE notesContent MATCH ?1"
        );
        assert_eq!(params.len(), 1);
        assert_eq!(params[0], "\"keyword\"");
    }

    #[test]
    fn test_search_terms_query_whitespace_handling() {
        let (sql, params) = build_search_sql_query("  keyword   @section  ");
        assert_eq!(
            sql,
            "SELECT DISTINCT notes.path as path, title, size, modified, hash, noteName FROM notesContent JOIN notes ON notesContent.path = notes.path WHERE notesContent MATCH ?1 INTERSECT SELECT DISTINCT notes.path as path, title, size, modified, hash, noteName FROM notesContent JOIN notes ON notesContent.path = notes.path WHERE notesContent.breadcrumb MATCH ?2"
        );
        assert_eq!(params.len(), 2);
        assert_eq!(params[0], "\"keyword\"");
        assert_eq!(params[1], "\"section\"");
    }

    #[test]
    fn test_fts4_mixed_exclusion_sql_generation() {
        let (sql, params) = build_search_sql_query("meeting -cancelled");

        // Should use NOT IN subquery approach instead of FTS4 native exclusion
        assert!(sql.contains("notesContent MATCH"));
        assert!(sql.contains("NOT IN"));
        assert!(sql.contains(
            "SELECT DISTINCT notesContent.path FROM notesContent WHERE notesContent MATCH"
        ));
        // params: first is the excluded term (NOT IN subquery), second is the positive term
        assert_eq!(params.len(), 2);
        assert!(params.contains(&"\"cancelled\"".to_string()));
        assert!(params.contains(&"\"meeting\"".to_string()));

        assert!(sql.contains("SELECT DISTINCT"));
    }

    #[test]
    fn test_exclusion_only_sql_generation() {
        // Critical test: exclusion-only queries MUST use NOT IN, not pure FTS4 MATCH
        let (sql, params) = build_search_sql_query("-cancelled");

        // Should NOT contain pure FTS4 exclusion (which is invalid)
        assert!(!sql.contains("MATCH \"-cancelled\""));
        // Should use NOT IN subquery approach
        assert!(sql.contains("NOT IN"));
        assert!(sql.contains(
            "SELECT DISTINCT notesContent.path FROM notesContent WHERE notesContent MATCH"
        ));
        assert_eq!(params.len(), 1);
        assert_eq!(params[0], "\"cancelled\"");
    }

    #[test]
    fn test_breadcrumb_exclusion_sql_generation() {
        let (sql, params) = build_search_sql_query("@project -@draft");

        // Positive breadcrumb is a column-scoped MATCH; the exclusion is a
        // robust NOT IN subquery (not the old, broken inline `breadcrumb: -term`).
        assert!(sql.contains("notesContent.breadcrumb MATCH ?1"));
        assert!(sql.contains(
            "notes.path NOT IN (SELECT DISTINCT notesContent.path FROM notesContent WHERE notesContent.breadcrumb MATCH ?2)"
        ));
        assert_eq!(
            params,
            vec!["\"project\"".to_string(), "\"draft\"".to_string()]
        );
    }

    #[test]
    fn test_like_exclusion_sql_generation() {
        let (sql, params) = build_search_sql_query("=2024 -=draft");

        // Should generate filename query with positive and negative conditions
        assert!(sql.contains("notes.noteName LIKE"));
        assert!(sql.contains("notes.noteName NOT LIKE"));
        assert!(params.contains(&"%2024%".to_string()));
        assert!(params.contains(&"%draft%".to_string()));
    }

    #[test]
    fn test_exclusion_only_like_query() {
        let (sql, params) = build_search_sql_query("-=draft -=temp");

        // Exclusion-only should still generate valid WHERE clause
        assert!(sql.contains("notes.noteName NOT LIKE"));
        // The new format embeds % in the param, not in the SQL template
        assert!(!sql.contains("NOT LIKE ('%'"));
        assert_eq!(params.len(), 2);
    }

    #[test]
    fn test_path_exclusion_sql_generation() {
        let (sql, params) = build_search_sql_query("/projects -/archive");

        assert!(sql.contains("notes.basePath LIKE"));
        assert!(sql.contains("notes.basePath NOT LIKE"));
        assert!(params.contains(&"projects".to_string()));
        assert!(params.contains(&"archive".to_string()));
    }

    #[test]
    fn test_exclusion_only_path_query() {
        let (sql, params) = build_search_sql_query("-/draft -/temp");

        assert!(sql.contains("notes.basePath NOT LIKE"));
        assert!(!sql.contains("notes.basePath LIKE ('/'"));
        assert_eq!(params.len(), 2);
    }

    #[tokio::test]
    async fn labels_table_exists_after_create_tables() {
        let tmp = tempfile::TempDir::new().unwrap();
        let path = tmp.path().join("kimun.sqlite");
        let db = super::NoteIndex::open(&path).await.unwrap();

        let row: (i64,) = sqlx::query_as(
            "SELECT count(*) FROM sqlite_master \
             WHERE type='table' AND name='labels'",
        )
        .fetch_one(db.pool())
        .await
        .unwrap();
        assert_eq!(row.0, 1, "labels table should exist");

        // labels_by_name was removed in 0.7; the PK autoindex covers it.
        let idx_name: (i64,) = sqlx::query_as(
            "SELECT count(*) FROM sqlite_master \
             WHERE type='index' AND name='labels_by_name'",
        )
        .fetch_one(db.pool())
        .await
        .unwrap();
        assert_eq!(
            idx_name.0, 0,
            "labels_by_name index must not exist (dropped in 0.7)"
        );

        let idx_path: (i64,) = sqlx::query_as(
            "SELECT count(*) FROM sqlite_master \
             WHERE type='index' AND name='labels_by_path'",
        )
        .fetch_one(db.pool())
        .await
        .unwrap();
        assert_eq!(idx_path.0, 1, "labels_by_path index should exist");

        db.close().await;
    }

    #[tokio::test]
    async fn labels_are_persisted_on_note_insert() {
        use crate::nfs::{NoteEntryData, VaultPath};

        let tmp = tempfile::TempDir::new().unwrap();
        let db_path = tmp.path().join("kimun.sqlite");
        let db = super::NoteIndex::open(&db_path).await.unwrap();

        let path = VaultPath::note_path_from("/n.md");
        let body = "Title\n\nbody with #foo and #Foo and #bar".to_string();
        let entry = NoteEntryData {
            path: path.clone(),
            size: body.len() as u64,
            modified_secs: 0,
        };

        let mut tx = db.pool().begin().await.unwrap();
        super::insert_notes(&mut tx, &[(entry, body)])
            .await
            .unwrap();
        tx.commit().await.unwrap();

        let rows: Vec<(String, String)> =
            sqlx::query_as("SELECT name, path FROM labels ORDER BY name")
                .fetch_all(db.pool())
                .await
                .unwrap();
        assert_eq!(
            rows,
            vec![
                ("bar".to_string(), path.to_string()),
                ("foo".to_string(), path.to_string()),
            ],
            "labels stored deduped + lowercased"
        );

        db.close().await;
    }

    #[tokio::test]
    async fn reindexing_a_note_drops_removed_labels() {
        use crate::nfs::{NoteEntryData, VaultPath};

        let tmp = tempfile::TempDir::new().unwrap();
        let db_path = tmp.path().join("kimun.sqlite");
        let db = super::NoteIndex::open(&db_path).await.unwrap();

        let path = VaultPath::note_path_from("/n.md");
        let body_v1 = "before #draft #keep".to_string();
        let entry_v1 = NoteEntryData {
            path: path.clone(),
            size: body_v1.len() as u64,
            modified_secs: 0,
        };

        let mut tx = db.pool().begin().await.unwrap();
        super::insert_notes(&mut tx, &[(entry_v1, body_v1)])
            .await
            .unwrap();
        tx.commit().await.unwrap();

        let body_v2 = "after #keep only".to_string();
        let entry_v2 = NoteEntryData {
            path: path.clone(),
            size: body_v2.len() as u64,
            modified_secs: 1,
        };

        let mut tx = db.pool().begin().await.unwrap();
        super::update_notes(&mut tx, &[(entry_v2, body_v2)])
            .await
            .unwrap();
        tx.commit().await.unwrap();

        let rows: Vec<(String,)> =
            sqlx::query_as("SELECT name FROM labels WHERE path = ? ORDER BY name")
                .bind(path.to_string())
                .fetch_all(db.pool())
                .await
                .unwrap();
        assert_eq!(
            rows.into_iter().map(|(n,)| n).collect::<Vec<_>>(),
            vec!["keep".to_string()],
            "reindex must drop labels no longer present"
        );

        db.close().await;
    }

    #[tokio::test]
    async fn labels_are_removed_on_note_delete() {
        use crate::nfs::{NoteEntryData, VaultPath};

        let tmp = tempfile::TempDir::new().unwrap();
        let db_path = tmp.path().join("kimun.sqlite");
        let db = super::NoteIndex::open(&db_path).await.unwrap();

        let path = VaultPath::note_path_from("/n.md");
        let body = "x #drop".to_string();
        let entry = NoteEntryData {
            path: path.clone(),
            size: body.len() as u64,
            modified_secs: 0,
        };

        let mut tx = db.pool().begin().await.unwrap();
        super::insert_notes(&mut tx, &[(entry, body)])
            .await
            .unwrap();
        super::delete_notes(&mut tx, std::slice::from_ref(&path))
            .await
            .unwrap();
        tx.commit().await.unwrap();

        let count: (i64,) = sqlx::query_as("SELECT count(*) FROM labels WHERE path = ?")
            .bind(path.to_string())
            .fetch_one(db.pool())
            .await
            .unwrap();
        assert_eq!(count.0, 0);

        db.close().await;
    }

    #[test]
    fn test_search_terms_query_label_only() {
        let (sql, params) = build_search_sql_query("#important");
        assert_eq!(params, vec!["important".to_string()]);
        assert!(
            sql.contains("FROM notes") && sql.contains("labels"),
            "query should join notes with labels: {}",
            sql
        );
    }

    #[test]
    fn test_search_terms_query_two_labels_intersect() {
        let (sql, params) = build_search_sql_query("#a #b");
        assert_eq!(params.len(), 2);
        assert!(
            sql.contains("INTERSECT"),
            "two labels should INTERSECT: {}",
            sql
        );
    }

    #[test]
    fn test_search_terms_query_links_only() {
        let (sql, params) = build_search_sql_query("<projects");
        assert_eq!(params, vec!["projects.md".to_string()]);
        assert!(
            sql.contains("FROM notes")
                && sql.contains("SELECT source FROM links")
                && sql.contains("notes.path IN"),
            "backlinks query should select sources from links: {}",
            sql
        );
        // Bare name (no wildcard) matches the indexed dest_name column with
        // plain equality — no leading-`%` scan.
        assert!(
            sql.contains("dest_name = ?1"),
            "expected indexed dest_name equality: {}",
            sql
        );
    }

    #[test]
    fn test_search_terms_query_links_long_form() {
        let (_sql, params) = build_search_sql_query("lk:projects");
        assert_eq!(params, vec!["projects.md".to_string()]);
    }

    #[test]
    fn test_search_terms_query_links_path_qualified() {
        let (sql, params) = build_search_sql_query("<work/projects");
        assert_eq!(params, vec!["work/projects.md".to_string()]);
        // Path-qualified anchors to the full path (relative or absolute) via
        // indexed equality on `destination`, not the bare-name column.
        assert!(
            sql.contains("destination = ?1 OR destination = ('/' || ?1)"),
            "expected path-anchored equality: {}",
            sql
        );
        assert!(!sql.contains("dest_name"));
    }

    #[test]
    fn test_search_terms_query_links_wildcard() {
        let (sql, params) = build_search_sql_query("<proj*");
        assert_eq!(params, vec!["proj%.md".to_string()]);
        // Wildcard bare name uses a prefix LIKE on the indexed dest_name column.
        assert!(
            sql.contains("dest_name LIKE ?1 ESCAPE '\\'"),
            "expected dest_name LIKE for wildcard: {}",
            sql
        );
    }

    #[test]
    fn test_search_terms_query_links_extension_optional() {
        let (_sql, params) = build_search_sql_query("<projects.md");
        assert_eq!(params, vec!["projects.md".to_string()]);
    }

    #[test]
    fn test_search_terms_query_excluded_links() {
        let (sql, params) = build_search_sql_query("-<draft");
        assert_eq!(params, vec!["draft.md".to_string()]);
        assert!(
            sql.contains("notes.path NOT IN (SELECT source FROM links"),
            "excluded backlinks should use NOT IN: {}",
            sql
        );
    }

    #[test]
    fn test_search_terms_query_two_links_intersect() {
        let (sql, params) = build_search_sql_query("<a <b");
        assert_eq!(params.len(), 2);
        assert!(
            sql.contains("INTERSECT"),
            "two backlinks should INTERSECT: {}",
            sql
        );
    }

    #[test]
    fn test_search_terms_query_links_combined_with_operators() {
        // Free-text term + backlink + label all compose via INTERSECT.
        let (sql, params) = build_search_sql_query("meeting <spec #urgent");
        assert_eq!(sql.matches("INTERSECT").count(), 2);
        assert!(sql.contains("notesContent MATCH"));
        assert!(sql.contains("SELECT source FROM links"));
        assert!(sql.contains("FROM labels WHERE name"));
        // Params follow the fan-out order: content term, label, then backlink.
        assert_eq!(
            params,
            vec![
                "\"meeting\"".to_string(),
                "urgent".to_string(),
                "spec.md".to_string()
            ]
        );
    }

    #[tokio::test]
    async fn search_combining_links_with_other_operators() {
        use crate::nfs::{NoteEntryData, VaultPath};
        let tmp = tempfile::TempDir::new().unwrap();
        let db_path = tmp.path().join("kimun.sqlite");
        let db = super::NoteIndex::open(&db_path).await.unwrap();

        let entries: Vec<(NoteEntryData, String)> = vec![
            (
                NoteEntryData {
                    path: VaultPath::note_path_from("/work/a.md"),
                    size: 10,
                    modified_secs: 0,
                },
                "# Tasks\n[[spec]] meeting #urgent".to_string(),
            ),
            (
                NoteEntryData {
                    path: VaultPath::note_path_from("/b.md"),
                    size: 10,
                    modified_secs: 0,
                },
                "[[spec]] casual".to_string(),
            ),
            (
                NoteEntryData {
                    path: VaultPath::note_path_from("/c.md"),
                    size: 10,
                    modified_secs: 0,
                },
                "#urgent only, no link".to_string(),
            ),
        ];

        let mut tx = db.pool().begin().await.unwrap();
        super::insert_notes(&mut tx, &entries).await.unwrap();
        tx.commit().await.unwrap();

        let paths = |results: &[(NoteEntryData, NoteContentData)]| {
            let mut p: Vec<String> = results.iter().map(|(e, _)| e.path.to_string()).collect();
            p.sort();
            p
        };

        // backlink + free-text term.
        let r = super::search_terms(db.pool(), "<spec meeting")
            .await
            .unwrap();
        assert_eq!(paths(&r), vec!["/work/a.md".to_string()]);

        // backlink + label.
        let r = super::search_terms(db.pool(), "<spec #urgent")
            .await
            .unwrap();
        assert_eq!(paths(&r), vec!["/work/a.md".to_string()]);

        // backlink + excluded label.
        let r = super::search_terms(db.pool(), "<spec -#urgent")
            .await
            .unwrap();
        assert_eq!(paths(&r), vec!["/b.md".to_string()]);

        // backlink + path filter.
        let r = super::search_terms(db.pool(), "<spec /work").await.unwrap();
        assert_eq!(paths(&r), vec!["/work/a.md".to_string()]);

        // backlink + section (breadcrumb) filter.
        let r = super::search_terms(db.pool(), "<spec @tasks")
            .await
            .unwrap();
        assert_eq!(paths(&r), vec!["/work/a.md".to_string()]);

        // backlink + filename filter.
        let r = super::search_terms(db.pool(), "<spec =b").await.unwrap();
        assert_eq!(paths(&r), vec!["/b.md".to_string()]);

        // label without link still matches the non-linking note.
        let r = super::search_terms(db.pool(), "#urgent -spec")
            .await
            .unwrap();
        assert!(paths(&r).contains(&"/c.md".to_string()));

        db.close().await;
    }

    #[tokio::test]
    async fn multiple_filename_terms_are_anded() {
        use crate::nfs::{NoteEntryData, VaultPath};
        let tmp = tempfile::TempDir::new().unwrap();
        let db_path = tmp.path().join("kimun.sqlite");
        let db = super::NoteIndex::open(&db_path).await.unwrap();

        let entries: Vec<(NoteEntryData, String)> = vec![
            (
                NoteEntryData {
                    path: VaultPath::note_path_from("/report-2024.md"),
                    size: 10,
                    modified_secs: 0,
                },
                "x".to_string(),
            ),
            (
                NoteEntryData {
                    path: VaultPath::note_path_from("/report-2023.md"),
                    size: 10,
                    modified_secs: 0,
                },
                "y".to_string(),
            ),
        ];
        let mut tx = db.pool().begin().await.unwrap();
        super::insert_notes(&mut tx, &entries).await.unwrap();
        tx.commit().await.unwrap();

        // =report =2024 must match ONLY the file containing both, not either.
        let r = super::search_terms(db.pool(), "=report =2024")
            .await
            .unwrap();
        let paths: Vec<String> = r.iter().map(|(e, _)| e.path.to_string()).collect();
        assert_eq!(paths, vec!["/report-2024.md".to_string()]);

        db.close().await;
    }

    #[tokio::test]
    async fn link_search_follows_rename() {
        use crate::nfs::{NoteEntryData, VaultPath};
        let tmp = tempfile::TempDir::new().unwrap();
        let db_path = tmp.path().join("kimun.sqlite");
        let db = super::NoteIndex::open(&db_path).await.unwrap();

        let entry = NoteEntryData {
            path: VaultPath::note_path_from("/a.md"),
            size: 10,
            modified_secs: 0,
        };

        let mut tx = db.pool().begin().await.unwrap();
        super::insert_notes(&mut tx, &[(entry, "see [[target]]".to_string())])
            .await
            .unwrap();
        // Rename the linked-to note; links (destination + dest_name) must follow.
        super::rename_note(
            &mut tx,
            &VaultPath::note_path_from("/target.md"),
            &VaultPath::note_path_from("/renamed.md"),
        )
        .await
        .unwrap();
        tx.commit().await.unwrap();

        let r = super::search_terms(db.pool(), "<renamed").await.unwrap();
        let paths: Vec<String> = r.iter().map(|(e, _)| e.path.to_string()).collect();
        assert_eq!(paths, vec!["/a.md".to_string()]);

        // The old name no longer matches.
        let r = super::search_terms(db.pool(), "<target").await.unwrap();
        assert!(r.is_empty());

        db.close().await;
    }

    #[tokio::test]
    async fn search_by_link_returns_linking_notes() {
        use crate::nfs::{NoteEntryData, VaultPath};
        let tmp = tempfile::TempDir::new().unwrap();
        let db_path = tmp.path().join("kimun.sqlite");
        let db = super::NoteIndex::open(&db_path).await.unwrap();

        let entries: Vec<(NoteEntryData, String)> = vec![
            (
                NoteEntryData {
                    path: VaultPath::note_path_from("/index.md"),
                    size: 10,
                    modified_secs: 0,
                },
                "links [[projects]] and [[work/spec]]".to_string(),
            ),
            (
                NoteEntryData {
                    path: VaultPath::note_path_from("/b.md"),
                    size: 10,
                    modified_secs: 0,
                },
                "see [[projects]]".to_string(),
            ),
            (
                NoteEntryData {
                    path: VaultPath::note_path_from("/c.md"),
                    size: 10,
                    modified_secs: 0,
                },
                "no links here".to_string(),
            ),
        ];

        let mut tx = db.pool().begin().await.unwrap();
        super::insert_notes(&mut tx, &entries).await.unwrap();
        tx.commit().await.unwrap();

        let paths = |results: &[(NoteEntryData, NoteContentData)]| {
            let mut p: Vec<String> = results.iter().map(|(e, _)| e.path.to_string()).collect();
            p.sort();
            p
        };

        // Notes that link to "projects" (backlinks).
        let r = super::search_terms(db.pool(), "<projects").await.unwrap();
        assert_eq!(
            paths(&r),
            vec!["/b.md".to_string(), "/index.md".to_string()]
        );

        // Extension optional.
        let r = super::search_terms(db.pool(), "<projects.md")
            .await
            .unwrap();
        assert_eq!(
            paths(&r),
            vec!["/b.md".to_string(), "/index.md".to_string()]
        );

        // Bare name matches a note in a subfolder (name-anywhere).
        let r = super::search_terms(db.pool(), "<spec").await.unwrap();
        assert_eq!(paths(&r), vec!["/index.md".to_string()]);

        // Path-qualified match.
        let r = super::search_terms(db.pool(), "<work/spec").await.unwrap();
        assert_eq!(paths(&r), vec!["/index.md".to_string()]);

        // Wildcard.
        let r = super::search_terms(db.pool(), "<proj*").await.unwrap();
        assert_eq!(
            paths(&r),
            vec!["/b.md".to_string(), "/index.md".to_string()]
        );

        // Exclusion: all notes that do NOT link to projects (index and b both link it).
        let r = super::search_terms(db.pool(), "-<projects").await.unwrap();
        assert_eq!(paths(&r), vec!["/c.md".to_string()]);

        // Unknown target → no results.
        let r = super::search_terms(db.pool(), "<nonexistent")
            .await
            .unwrap();
        assert!(r.is_empty());

        db.close().await;
    }

    #[tokio::test]
    async fn search_by_forward_link_returns_targets() {
        use crate::nfs::{NoteEntryData, VaultPath};
        let tmp = tempfile::TempDir::new().unwrap();
        let db_path = tmp.path().join("kimun.sqlite");
        let db = super::NoteIndex::open(&db_path).await.unwrap();

        // A links to B and C; B and C link nowhere; D links to A.
        let entries: Vec<(NoteEntryData, String)> = vec![
            (
                NoteEntryData {
                    path: VaultPath::note_path_from("/a.md"),
                    size: 10,
                    modified_secs: 0,
                },
                "see [[b]] and [[c]]".to_string(),
            ),
            (
                NoteEntryData {
                    path: VaultPath::note_path_from("/b.md"),
                    size: 10,
                    modified_secs: 0,
                },
                "b body".to_string(),
            ),
            (
                NoteEntryData {
                    path: VaultPath::note_path_from("/c.md"),
                    size: 10,
                    modified_secs: 0,
                },
                "c body".to_string(),
            ),
            (
                NoteEntryData {
                    path: VaultPath::note_path_from("/d.md"),
                    size: 10,
                    modified_secs: 0,
                },
                "points to [[a]]".to_string(),
            ),
        ];

        let mut tx = db.pool().begin().await.unwrap();
        super::insert_notes(&mut tx, &entries).await.unwrap();
        tx.commit().await.unwrap();

        let paths = |results: &[(NoteEntryData, NoteContentData)]| {
            let mut p: Vec<String> = results.iter().map(|(e, _)| e.path.to_string()).collect();
            p.sort();
            p
        };

        // Forward links of A: the notes A links *to* (B and C).
        let r = super::search_terms(db.pool(), ">a").await.unwrap();
        assert_eq!(paths(&r), vec!["/b.md".to_string(), "/c.md".to_string()]);

        // Long form.
        let r = super::search_terms(db.pool(), "fwd:a").await.unwrap();
        assert_eq!(paths(&r), vec!["/b.md".to_string(), "/c.md".to_string()]);

        // Backlinks of B: the notes that link *to* B (A).
        let r = super::search_terms(db.pool(), "<b").await.unwrap();
        assert_eq!(paths(&r), vec!["/a.md".to_string()]);

        // Forward links of D: A.
        let r = super::search_terms(db.pool(), ">d").await.unwrap();
        assert_eq!(paths(&r), vec!["/a.md".to_string()]);

        // Exclusion: notes that are NOT forward links of A (everything but B and C).
        let r = super::search_terms(db.pool(), "->a").await.unwrap();
        assert_eq!(paths(&r), vec!["/a.md".to_string(), "/d.md".to_string()]);

        // A note with no outgoing links has no forward links.
        let r = super::search_terms(db.pool(), ">b").await.unwrap();
        assert!(r.is_empty());

        db.close().await;
    }

    #[tokio::test]
    async fn fts_content_and_breadcrumb_combinations() {
        use crate::nfs::{NoteEntryData, VaultPath};
        let tmp = tempfile::TempDir::new().unwrap();
        let db_path = tmp.path().join("kimun.sqlite");
        let db = super::NoteIndex::open(&db_path).await.unwrap();

        let mk = |p: &str, body: &str| {
            (
                NoteEntryData {
                    path: VaultPath::note_path_from(p),
                    size: 10,
                    modified_secs: 0,
                },
                body.to_string(),
            )
        };
        let entries = vec![
            // "meeting" under a "Work" heading, also says "done".
            mk("/a.md", "# Work\nmeeting notes, all done"),
            // "meeting" but under "Personal", not "Work".
            mk("/b.md", "# Personal\nmeeting with a friend"),
            // "Work" heading but no "meeting".
            mk("/c.md", "# Work\nbudget review"),
        ];
        let mut tx = db.pool().begin().await.unwrap();
        super::insert_notes(&mut tx, &entries).await.unwrap();
        tx.commit().await.unwrap();

        let paths = |r: &[(NoteEntryData, NoteContentData)]| {
            let mut p: Vec<String> = r.iter().map(|(e, _)| e.path.to_string()).collect();
            p.sort();
            p
        };

        // content AND breadcrumb (both must hold).
        let r = super::search_terms(db.pool(), "meeting @work")
            .await
            .unwrap();
        assert_eq!(paths(&r), vec!["/a.md".to_string()]);

        // two content terms AND (only /a.md has both "meeting" and "notes").
        let r = super::search_terms(db.pool(), "meeting notes")
            .await
            .unwrap();
        assert_eq!(paths(&r), vec!["/a.md".to_string()]);

        // content positive + content exclusion.
        let r = super::search_terms(db.pool(), "meeting -done")
            .await
            .unwrap();
        assert_eq!(paths(&r), vec!["/b.md".to_string()]);

        // breadcrumb positive + content exclusion.
        let r = super::search_terms(db.pool(), "@work -budget")
            .await
            .unwrap();
        assert_eq!(paths(&r), vec!["/a.md".to_string()]);

        // breadcrumb positive + breadcrumb exclusion.
        let r = super::search_terms(db.pool(), "@work -@personal")
            .await
            .unwrap();
        assert_eq!(paths(&r), vec!["/a.md".to_string(), "/c.md".to_string()]);

        // pure content exclusion (no positives anywhere).
        let r = super::search_terms(db.pool(), "-meeting").await.unwrap();
        assert_eq!(paths(&r), vec!["/c.md".to_string()]);

        // pure breadcrumb exclusion.
        let r = super::search_terms(db.pool(), "-@work").await.unwrap();
        assert_eq!(paths(&r), vec!["/b.md".to_string()]);

        db.close().await;
    }

    #[tokio::test]
    async fn search_by_label_returns_matching_notes() {
        use crate::nfs::{NoteEntryData, VaultPath};
        let tmp = tempfile::TempDir::new().unwrap();
        let db_path = tmp.path().join("kimun.sqlite");
        let db = super::NoteIndex::open(&db_path).await.unwrap();

        let entries: Vec<(NoteEntryData, String)> = vec![
            (
                NoteEntryData {
                    path: VaultPath::note_path_from("/a.md"),
                    size: 10,
                    modified_secs: 0,
                },
                "a #important #todo".to_string(),
            ),
            (
                NoteEntryData {
                    path: VaultPath::note_path_from("/b.md"),
                    size: 10,
                    modified_secs: 0,
                },
                "b #todo".to_string(),
            ),
            (
                NoteEntryData {
                    path: VaultPath::note_path_from("/c.md"),
                    size: 10,
                    modified_secs: 0,
                },
                "c plain".to_string(),
            ),
        ];

        let mut tx = db.pool().begin().await.unwrap();
        super::insert_notes(&mut tx, &entries).await.unwrap();
        tx.commit().await.unwrap();

        let results = super::search_terms(db.pool(), "#important").await.unwrap();
        let paths: Vec<String> = results.iter().map(|(e, _)| e.path.to_string()).collect();
        assert_eq!(paths, vec!["/a.md".to_string()]);

        let results = super::search_terms(db.pool(), "#important #todo")
            .await
            .unwrap();
        let paths: Vec<String> = results.iter().map(|(e, _)| e.path.to_string()).collect();
        assert_eq!(paths, vec!["/a.md".to_string()]);

        let results = super::search_terms(db.pool(), "#nope").await.unwrap();
        assert!(results.is_empty());

        db.close().await;
    }

    #[tokio::test]
    async fn label_search_uses_index() {
        // Confirms the PK autoindex (sqlite_autoindex_labels_1) is used for
        // label lookups after the explicit labels_by_name index was dropped in
        // 0.7. A hashtag filter must not degrade to a full table scan.
        use crate::nfs::{NoteEntryData, VaultPath};
        let tmp = tempfile::TempDir::new().unwrap();
        let db_path = tmp.path().join("kimun.sqlite");
        let db = super::NoteIndex::open(&db_path).await.unwrap();

        let entry = NoteEntryData {
            path: VaultPath::note_path_from("/a.md"),
            size: 10,
            modified_secs: 0,
        };
        let mut tx = db.pool().begin().await.unwrap();
        super::insert_notes(&mut tx, &[(entry, "x #important".to_string())])
            .await
            .unwrap();
        tx.commit().await.unwrap();

        let (sql, _) = super::build_search_sql_query("#important");
        let plan_sql = format!("EXPLAIN QUERY PLAN {}", sql);
        let rows: Vec<(i64, i64, i64, String)> = sqlx::query_as(&plan_sql)
            .bind("important")
            .fetch_all(db.pool())
            .await
            .unwrap();
        let plan_text = rows
            .iter()
            .map(|(_, _, _, detail)| detail.as_str())
            .collect::<Vec<_>>()
            .join(" | ");
        // The PK autoindex covers WHERE name = ? lookups on (name, path).
        // No explicit labels_by_name index any more (removed in 0.7).
        // Accept any sqlite_autoindex_labels_ suffix to tolerate DROP+CREATE migration changes.
        assert!(
            plan_text.contains("sqlite_autoindex_labels_"),
            "expected PK autoindex on labels in plan: {}",
            plan_text
        );

        db.close().await;
    }

    #[tokio::test]
    async fn rename_note_updates_labels() {
        use crate::nfs::{NoteEntryData, VaultPath};
        let tmp = tempfile::TempDir::new().unwrap();
        let db_path = tmp.path().join("kimun.sqlite");
        let db = super::NoteIndex::open(&db_path).await.unwrap();

        let from = VaultPath::note_path_from("/old.md");
        let to = VaultPath::note_path_from("/new.md");
        let entry = NoteEntryData {
            path: from.clone(),
            size: 10,
            modified_secs: 0,
        };
        let mut tx = db.pool().begin().await.unwrap();
        super::insert_notes(&mut tx, &[(entry, "x #foo".to_string())])
            .await
            .unwrap();
        super::rename_note(&mut tx, &from, &to).await.unwrap();
        tx.commit().await.unwrap();

        let old_rows: (i64,) = sqlx::query_as("SELECT count(*) FROM labels WHERE path = ?")
            .bind(from.to_string())
            .fetch_one(db.pool())
            .await
            .unwrap();
        assert_eq!(old_rows.0, 0, "no label rows should remain at old path");

        let new_rows: Vec<(String,)> =
            sqlx::query_as("SELECT name FROM labels WHERE path = ? ORDER BY name")
                .bind(to.to_string())
                .fetch_all(db.pool())
                .await
                .unwrap();
        assert_eq!(
            new_rows.into_iter().map(|(n,)| n).collect::<Vec<_>>(),
            vec!["foo".to_string()],
        );

        db.close().await;
    }

    #[tokio::test]
    async fn rename_directory_updates_labels() {
        use crate::nfs::{NoteEntryData, VaultPath};
        let tmp = tempfile::TempDir::new().unwrap();
        let db_path = tmp.path().join("kimun.sqlite");
        let db = super::NoteIndex::open(&db_path).await.unwrap();

        let note_path = VaultPath::note_path_from("/old_dir/note.md");
        let entry = NoteEntryData {
            path: note_path.clone(),
            size: 10,
            modified_secs: 0,
        };
        let mut tx = db.pool().begin().await.unwrap();
        super::insert_notes(&mut tx, &[(entry, "x #moved".to_string())])
            .await
            .unwrap();
        super::rename_directory(
            &mut tx,
            &VaultPath::new("/old_dir"),
            &VaultPath::new("/new_dir"),
        )
        .await
        .unwrap();
        tx.commit().await.unwrap();

        let rows: Vec<(String, String)> = sqlx::query_as("SELECT name, path FROM labels")
            .fetch_all(db.pool())
            .await
            .unwrap();
        assert_eq!(
            rows,
            vec![("moved".to_string(), "/new_dir/note.md".to_string())],
        );

        db.close().await;
    }

    #[tokio::test]
    async fn delete_directory_removes_labels() {
        use crate::nfs::{NoteEntryData, VaultPath};
        let tmp = tempfile::TempDir::new().unwrap();
        let db_path = tmp.path().join("kimun.sqlite");
        let db = super::NoteIndex::open(&db_path).await.unwrap();

        let note_path = VaultPath::note_path_from("/sub/note.md");
        let entry = NoteEntryData {
            path: note_path.clone(),
            size: 10,
            modified_secs: 0,
        };
        let mut tx = db.pool().begin().await.unwrap();
        super::insert_notes(&mut tx, &[(entry, "x #gone".to_string())])
            .await
            .unwrap();
        super::delete_directories(&mut tx, &[VaultPath::new("/sub")])
            .await
            .unwrap();
        tx.commit().await.unwrap();

        let count: (i64,) = sqlx::query_as("SELECT count(*) FROM labels")
            .fetch_one(db.pool())
            .await
            .unwrap();
        assert_eq!(count.0, 0);

        db.close().await;
    }

    #[tokio::test]
    async fn delete_directory_with_underscore_does_not_touch_siblings() {
        use crate::nfs::{NoteEntryData, VaultPath};
        let tmp = tempfile::TempDir::new().unwrap();
        let db_path = tmp.path().join("kimun.sqlite");
        let db = super::NoteIndex::open(&db_path).await.unwrap();

        let target = VaultPath::note_path_from("/my_dir/a.md");
        let sibling = VaultPath::note_path_from("/myXdir/b.md");
        let entries = vec![
            (
                NoteEntryData {
                    path: target.clone(),
                    size: 10,
                    modified_secs: 0,
                },
                "x #t".to_string(),
            ),
            (
                NoteEntryData {
                    path: sibling.clone(),
                    size: 10,
                    modified_secs: 0,
                },
                "y #s".to_string(),
            ),
        ];
        let mut tx = db.pool().begin().await.unwrap();
        super::insert_notes(&mut tx, &entries).await.unwrap();
        super::delete_directories(&mut tx, &[VaultPath::new("/my_dir")])
            .await
            .unwrap();
        tx.commit().await.unwrap();

        let remaining: Vec<(String,)> = sqlx::query_as("SELECT path FROM notes ORDER BY path")
            .fetch_all(db.pool())
            .await
            .unwrap();
        assert_eq!(
            remaining.into_iter().map(|(p,)| p).collect::<Vec<_>>(),
            vec![sibling.to_string()],
            "sibling /myXdir/b.md must be untouched"
        );

        let sibling_label: (i64,) = sqlx::query_as("SELECT count(*) FROM labels WHERE path = ?")
            .bind(sibling.to_string())
            .fetch_one(db.pool())
            .await
            .unwrap();
        assert_eq!(sibling_label.0, 1, "sibling label preserved");

        db.close().await;
    }

    #[test]
    fn escape_like_pattern_escapes_metacharacters() {
        assert_eq!(super::escape_like_pattern("/my_dir/"), "/my\\_dir/");
        assert_eq!(super::escape_like_pattern("/a%b/"), "/a\\%b/");
        assert_eq!(super::escape_like_pattern("/a\\b/"), "/a\\\\b/");
        assert_eq!(super::escape_like_pattern("/normal/"), "/normal/");
    }

    /// Verify that `escape_like_pattern` leaves `*` and `.` untouched — a
    /// prerequisite for the escape-then-replace order in the wildcard branch.
    #[test]
    fn escape_like_pattern_leaves_star_and_dot_untouched() {
        assert_eq!(super::escape_like_pattern("task*"), "task*");
        assert_eq!(super::escape_like_pattern("task*.md"), "task*.md");
        assert_eq!(super::escape_like_pattern("*report.md"), "*report.md");
    }

    /// SQL-shape unit test: confirm the bound parameter produced for `=task*`
    /// is `task%.md` and for plain `=task` is `%task%`.
    #[test]
    fn filename_wildcard_produces_correct_pattern_param() {
        // Wildcard term: =task*  → param should be "task%.md"
        let (_, params) = build_search_sql_query("=task*");
        assert_eq!(
            params,
            vec!["task%.md".to_string()],
            "=task* must produce bound param 'task%.md'"
        );

        // Non-wildcard term: =task  → param should be "%task%"
        let (_, params) = build_search_sql_query("=task");
        assert_eq!(
            params,
            vec!["%task%".to_string()],
            "=task must produce bound param '%task%'"
        );

        // Suffix wildcard: =*report → param should be "%report.md"
        let (_, params) = build_search_sql_query("=*report");
        assert_eq!(
            params,
            vec!["%report.md".to_string()],
            "=*report must produce bound param '%report.md'"
        );

        // Mid wildcard: =ta*sk → param should be "ta%sk.md"
        let (_, params) = build_search_sql_query("=ta*sk");
        assert_eq!(
            params,
            vec!["ta%sk.md".to_string()],
            "=ta*sk must produce bound param 'ta%sk.md'"
        );
    }

    #[tokio::test]
    async fn search_by_filename_wildcard() {
        use crate::nfs::{NoteEntryData, VaultPath};
        let tmp = tempfile::TempDir::new().unwrap();
        let db_path = tmp.path().join("kimun.sqlite");
        let db = super::NoteIndex::open(&db_path).await.unwrap();

        let entries: Vec<(NoteEntryData, String)> = vec![
            (
                NoteEntryData {
                    path: VaultPath::note_path_from("/task.md"),
                    size: 10,
                    modified_secs: 0,
                },
                "x".to_string(),
            ),
            (
                NoteEntryData {
                    path: VaultPath::note_path_from("/tasks.md"),
                    size: 10,
                    modified_secs: 0,
                },
                "y".to_string(),
            ),
            (
                NoteEntryData {
                    path: VaultPath::note_path_from("/weekly-report.md"),
                    size: 10,
                    modified_secs: 0,
                },
                "z".to_string(),
            ),
            (
                NoteEntryData {
                    path: VaultPath::note_path_from("/other.md"),
                    size: 10,
                    modified_secs: 0,
                },
                "w".to_string(),
            ),
        ];
        let mut tx = db.pool().begin().await.unwrap();
        super::insert_notes(&mut tx, &entries).await.unwrap();
        tx.commit().await.unwrap();

        let paths = |results: &[(NoteEntryData, NoteContentData)]| {
            let mut p: Vec<String> = results.iter().map(|(e, _)| e.path.to_string()).collect();
            p.sort();
            p
        };

        // Substring (non-wildcard): =task → task.md and tasks.md
        let r = super::search_terms(db.pool(), "=task").await.unwrap();
        assert_eq!(
            paths(&r),
            vec!["/task.md".to_string(), "/tasks.md".to_string()],
            "=task must match task.md and tasks.md as substrings"
        );

        // Prefix wildcard: =task* → task.md and tasks.md, NOT weekly-report.md
        let r = super::search_terms(db.pool(), "=task*").await.unwrap();
        assert_eq!(
            paths(&r),
            vec!["/task.md".to_string(), "/tasks.md".to_string()],
            "=task* must match task.md and tasks.md, not weekly-report.md"
        );

        // Suffix wildcard: =*report → weekly-report.md only
        let r = super::search_terms(db.pool(), "=*report").await.unwrap();
        assert_eq!(
            paths(&r),
            vec!["/weekly-report.md".to_string()],
            "=*report must match only weekly-report.md"
        );

        // Exclusion with wildcard: -=task* → other.md and weekly-report.md
        let r = super::search_terms(db.pool(), "-=task*").await.unwrap();
        assert_eq!(
            paths(&r),
            vec!["/other.md".to_string(), "/weekly-report.md".to_string()],
            "-=task* must exclude task.md and tasks.md"
        );

        db.close().await;
    }

    #[tokio::test]
    async fn search_by_path_wildcard() {
        use crate::nfs::{NoteEntryData, VaultPath};
        let tmp = tempfile::TempDir::new().unwrap();
        let db_path = tmp.path().join("kimun.sqlite");
        let db = super::NoteIndex::open(&db_path).await.unwrap();

        let mk = |p: &str| NoteEntryData {
            path: VaultPath::note_path_from(p),
            size: 10,
            modified_secs: 0,
        };
        let entries: Vec<(NoteEntryData, String)> = vec![
            (mk("/work/a.md"), "a".to_string()),
            (mk("/work/sub/b.md"), "b".to_string()),
            (mk("/personal/c.md"), "c".to_string()),
            (mk("/d.md"), "d".to_string()),
        ];
        let mut tx = db.pool().begin().await.unwrap();
        super::insert_notes(&mut tx, &entries).await.unwrap();
        tx.commit().await.unwrap();

        let paths = |results: &[(NoteEntryData, NoteContentData)]| {
            let mut p: Vec<String> = results.iter().map(|(e, _)| e.path.to_string()).collect();
            p.sort();
            p
        };

        // Prefix (non-wildcard) is unchanged: /work matches the folder + subfolders.
        let r = super::search_terms(db.pool(), "/work").await.unwrap();
        assert_eq!(
            paths(&r),
            vec!["/work/a.md".to_string(), "/work/sub/b.md".to_string()],
        );

        // Wildcard prefix: /wo* behaves like the prefix form.
        let r = super::search_terms(db.pool(), "/wo*").await.unwrap();
        assert_eq!(
            paths(&r),
            vec!["/work/a.md".to_string(), "/work/sub/b.md".to_string()],
        );

        // Suffix wildcard on the folder path: /*sub → only notes whose folder ends in "sub".
        let r = super::search_terms(db.pool(), "/*sub").await.unwrap();
        assert_eq!(paths(&r), vec!["/work/sub/b.md".to_string()]);

        // Subfolder wildcard: /work/* → only notes strictly under /work/.
        let r = super::search_terms(db.pool(), "/work/*").await.unwrap();
        assert_eq!(paths(&r), vec!["/work/sub/b.md".to_string()]);

        // Excluded wildcard: -/wo* drops everything under /work.
        let r = super::search_terms(db.pool(), "-/wo*").await.unwrap();
        assert_eq!(
            paths(&r),
            vec!["/d.md".to_string(), "/personal/c.md".to_string()],
        );

        db.close().await;
    }

    #[tokio::test]
    async fn delete_directory_no_trailing_slash_does_not_match_sibling_prefix() {
        use crate::nfs::{NoteEntryData, VaultPath};
        let tmp = tempfile::TempDir::new().unwrap();
        let db_path = tmp.path().join("kimun.sqlite");
        let db = super::NoteIndex::open(&db_path).await.unwrap();

        let target = VaultPath::note_path_from("/notes/a.md");
        let sibling = VaultPath::note_path_from("/notes_archive/b.md");
        let entries = vec![
            (
                NoteEntryData {
                    path: target.clone(),
                    size: 10,
                    modified_secs: 0,
                },
                "x".to_string(),
            ),
            (
                NoteEntryData {
                    path: sibling.clone(),
                    size: 10,
                    modified_secs: 0,
                },
                "y".to_string(),
            ),
        ];
        let mut tx = db.pool().begin().await.unwrap();
        super::insert_notes(&mut tx, &entries).await.unwrap();
        super::delete_directories(&mut tx, &[VaultPath::new("/notes")])
            .await
            .unwrap();
        tx.commit().await.unwrap();

        let rows: Vec<(String,)> = sqlx::query_as("SELECT path FROM notes ORDER BY path")
            .fetch_all(db.pool())
            .await
            .unwrap();
        let paths: Vec<String> = rows.into_iter().map(|(p,)| p).collect();
        assert_eq!(
            paths,
            vec![sibling.to_string()],
            "sibling /notes_archive/ must not be deleted"
        );
        db.close().await;
    }

    #[tokio::test]
    async fn path_search_with_underscore_does_not_treat_as_wildcard() {
        use crate::nfs::{NoteEntryData, VaultPath};
        let tmp = tempfile::TempDir::new().unwrap();
        let db_path = tmp.path().join("kimun.sqlite");
        let db = super::NoteIndex::open(&db_path).await.unwrap();

        let target = VaultPath::note_path_from("/my_notes/a.md");
        let sibling = VaultPath::note_path_from("/myXnotes/b.md");
        let entries = vec![
            (
                NoteEntryData {
                    path: target.clone(),
                    size: 10,
                    modified_secs: 0,
                },
                "x".to_string(),
            ),
            (
                NoteEntryData {
                    path: sibling.clone(),
                    size: 10,
                    modified_secs: 0,
                },
                "y".to_string(),
            ),
        ];
        let mut tx = db.pool().begin().await.unwrap();
        super::insert_notes(&mut tx, &entries).await.unwrap();
        tx.commit().await.unwrap();

        // pt:my_notes search must only match /my_notes/, not /myXnotes/.
        let results = super::search_terms(db.pool(), "pt:my_notes").await.unwrap();
        let paths: Vec<String> = results.iter().map(|(e, _)| e.path.to_string()).collect();
        assert_eq!(
            paths,
            vec![target.to_string()],
            "underscore must be literal in path search"
        );
        db.close().await;
    }

    #[tokio::test]
    async fn filename_search_with_underscore_does_not_treat_as_wildcard() {
        use crate::nfs::{NoteEntryData, VaultPath};
        let tmp = tempfile::TempDir::new().unwrap();
        let db_path = tmp.path().join("kimun.sqlite");
        let db = super::NoteIndex::open(&db_path).await.unwrap();

        let target = VaultPath::note_path_from("/my_note.md");
        let sibling = VaultPath::note_path_from("/myXnote.md");
        let entries = vec![
            (
                NoteEntryData {
                    path: target.clone(),
                    size: 10,
                    modified_secs: 0,
                },
                "x".to_string(),
            ),
            (
                NoteEntryData {
                    path: sibling.clone(),
                    size: 10,
                    modified_secs: 0,
                },
                "y".to_string(),
            ),
        ];
        let mut tx = db.pool().begin().await.unwrap();
        super::insert_notes(&mut tx, &entries).await.unwrap();
        tx.commit().await.unwrap();

        let results = super::search_terms(db.pool(), "=my_note").await.unwrap();
        let paths: Vec<String> = results.iter().map(|(e, _)| e.path.to_string()).collect();
        assert_eq!(
            paths,
            vec![target.to_string()],
            "underscore must be literal in filename search"
        );
        db.close().await;
    }

    #[tokio::test]
    async fn fts_term_with_metachar_does_not_error() {
        use crate::nfs::{NoteEntryData, VaultPath};
        let tmp = tempfile::TempDir::new().unwrap();
        let db_path = tmp.path().join("kimun.sqlite");
        let db = super::NoteIndex::open(&db_path).await.unwrap();

        let entry = NoteEntryData {
            path: VaultPath::note_path_from("/a.md"),
            size: 10,
            modified_secs: 0,
        };
        let mut tx = db.pool().begin().await.unwrap();
        super::insert_notes(&mut tx, &[(entry, "some meeting note".to_string())])
            .await
            .unwrap();
        tx.commit().await.unwrap();

        // Each of these would have produced an FTS4 syntax error before the fix.
        for q in &[
            "(meeting",
            "*",
            "meet*ing",
            "title:value",
            "a^b",
            "<",
            ">",
            "=",
            "@",
            "-",
            "-<",
            "->",
            "in:",
            "name:",
        ] {
            let res = super::search_terms(db.pool(), q).await;
            assert!(
                res.is_ok(),
                "query {:?} must not error; got {:?}",
                q,
                res.err()
            );
        }

        db.close().await;
    }

    #[tokio::test]
    async fn breadcrumb_term_with_metachar_does_not_error() {
        use crate::nfs::{NoteEntryData, VaultPath};
        let tmp = tempfile::TempDir::new().unwrap();
        let db_path = tmp.path().join("kimun.sqlite");
        let db = super::NoteIndex::open(&db_path).await.unwrap();

        let entry = NoteEntryData {
            path: VaultPath::note_path_from("/a.md"),
            size: 10,
            modified_secs: 0,
        };
        let mut tx = db.pool().begin().await.unwrap();
        super::insert_notes(&mut tx, &[(entry, "# Heading\n\ntext".to_string())])
            .await
            .unwrap();
        tx.commit().await.unwrap();

        for q in &["@(heading", "@*", "in:title:", ">(heading", ">*"] {
            let res = super::search_terms(db.pool(), q).await;
            assert!(
                res.is_ok(),
                "breadcrumb query {:?} must not error; got {:?}",
                q,
                res.err()
            );
        }

        db.close().await;
    }

    #[cfg(test)]
    mod note_columns_consistency {
        #[test]
        fn note_columns_is_path_plus_rest() {
            assert_eq!(
                super::super::NOTE_COLUMNS,
                format!("path, {}", super::super::NOTE_COLUMNS_REST),
                "NOTE_COLUMNS must equal 'path, ' + NOTE_COLUMNS_REST"
            );
        }
    }

    /// On a stored DB version older than the current `VERSION`, reopening the
    /// vault must self-heal the schema: the index comes back valid
    /// but empty, `index_ready` reports `false`, and the next sync pass
    /// (`validate_and_init`) refills it. After the heal, stale `>`-separated
    /// breadcrumb rows are gone and the new `\x1f` separator is in place.
    #[tokio::test(flavor = "multi_thread")]
    async fn reopen_self_heals_outdated_schema() {
        use crate::{NoteVault, VaultConfig};
        use sqlx::Row;

        let dir = tempfile::TempDir::new().unwrap();
        std::fs::write(dir.path().join("note.md"), "# Note\n## Sub\nbody text").unwrap();

        // Bring the index up at the current version with one indexed note.
        {
            let vault = NoteVault::new(VaultConfig::new(dir.path())).await.unwrap();
            vault.validate_and_init().await.unwrap();
            // A brand-new index is healed-on-open, hence not ready; reopening
            // it below (current version) must report ready.

            // Force the schema backwards: stamp version `0.4` and rewrite
            // stored breadcrumbs in the legacy `>`-joined form to simulate a
            // vault upgraded across the separator change.
            let pool = vault.index.pool();
            sqlx::query("UPDATE appData SET value = '0.4' WHERE name = 'version'")
                .execute(pool)
                .await
                .unwrap();
            sqlx::query("UPDATE notesContent SET breadcrumb = REPLACE(breadcrumb, x'1f', '>')")
                .execute(pool)
                .await
                .unwrap();

            // Sanity: the stale row really does contain `>`.
            let stale: Vec<String> =
                sqlx::query("SELECT breadcrumb FROM notesContent WHERE breadcrumb != ''")
                    .fetch_all(pool)
                    .await
                    .unwrap()
                    .into_iter()
                    .map(|r| r.try_get("breadcrumb").unwrap())
                    .collect();
            assert!(
                stale.iter().any(|b| b.contains('>')),
                "expected legacy `>` separator in: {:?}",
                stale
            );
            vault.index.close().await;
        }

        // Reopen: the outdated schema is healed silently; the probe reports
        // not-ready until a sync pass fills the empty index.
        let vault = NoteVault::new(VaultConfig::new(dir.path())).await.unwrap();
        assert!(!vault.index_ready(), "healed index must not report ready");
        vault.validate_and_init().await.unwrap();
        // The sync pass marks the index synced: the SAME instance now
        // reports ready (regression: the old write-once flag kept lying).
        assert!(
            vault.index_ready(),
            "synced index must report ready on the same instance"
        );

        // Post-heal: no row carries the legacy separator; non-empty
        // breadcrumbs use `\x1f`.
        let pool = vault.index.pool();
        let after: Vec<String> =
            sqlx::query("SELECT breadcrumb FROM notesContent WHERE breadcrumb != ''")
                .fetch_all(pool)
                .await
                .unwrap()
                .into_iter()
                .map(|r| r.try_get("breadcrumb").unwrap())
                .collect();
        assert!(
            !after.is_empty(),
            "expected reindexed breadcrumb rows after heal"
        );
        assert!(
            after.iter().all(|b| !b.contains('>')),
            "stale `>` separator survived the heal: {:?}",
            after
        );

        // End-to-end: the public chunk accessor exposes sane breadcrumb
        // leaves after the heal (storage-level separator checks alone would
        // miss an accessor-level splitting bug).
        let chunks = vault
            .get_note_chunks(&crate::nfs::VaultPath::new("/note.md"))
            .await
            .unwrap();
        let leaves: Vec<&str> = chunks
            .values()
            .flatten()
            .filter_map(|c| c.breadcrumb_last())
            .collect();
        assert!(
            leaves.iter().any(|l| *l == "Note" || *l == "Sub"),
            "expected Note/Sub breadcrumb leaves, got: {:?}",
            leaves
        );

        // A second reopen with a current schema must report ready.
        vault.index.close().await;
        drop(vault);
        let vault = NoteVault::new(VaultConfig::new(dir.path())).await.unwrap();
        assert!(vault.index_ready(), "current schema must report ready");

        // recreate_index drops the tables and runs a full sync; the probe
        // must still report ready on the same instance afterwards.
        vault.recreate_index().await.unwrap();
        assert!(
            vault.index_ready(),
            "recreated-and-synced index must report ready"
        );
    }

    /// `open` on a current-version schema must not heal: `ready` is `true`
    /// and existing rows survive.
    #[tokio::test]
    async fn open_preserves_current_schema() {
        let tmp = tempfile::TempDir::new().unwrap();
        let db_path = tmp.path().join("kimun.sqlite");

        // First open heals the fresh file into a current schema.
        let first = super::NoteIndex::open(&db_path).await.unwrap();
        assert!(!first.ready());
        sqlx::query("INSERT INTO appData (name, value) VALUES ('marker', 'kept')")
            .execute(first.pool())
            .await
            .unwrap();
        first.close().await;

        // Second open sees a current schema: no heal, data intact.
        let second = super::NoteIndex::open(&db_path).await.unwrap();
        assert!(second.ready());
        let marker: Option<String> =
            sqlx::query_scalar("SELECT value FROM appData WHERE name = 'marker'")
                .fetch_optional(second.pool())
                .await
                .unwrap();
        assert_eq!(marker.as_deref(), Some("kept"));
        second.close().await;
    }

    /// A recursive browse from the root is a whole-vault sync and must mark
    /// the index synced — the readiness probe reports true afterwards even
    /// though the schema was healed at open (regression for the
    /// browse-only path that previously left the probe stuck on false).
    #[tokio::test(flavor = "multi_thread")]
    async fn whole_vault_browse_marks_index_ready() {
        use crate::{NoteVault, VaultBrowseOptionsBuilder, VaultConfig};

        let dir = tempfile::TempDir::new().unwrap();
        std::fs::write(dir.path().join("note.md"), "# Note\nbody").unwrap();

        let vault = NoteVault::new(VaultConfig::new(dir.path())).await.unwrap();
        assert!(!vault.index_ready(), "fresh index is healed, not ready");

        let (options, rx) = VaultBrowseOptionsBuilder::new(&crate::nfs::VaultPath::root())
            .recursive(true)
            .build();
        vault.browse_vault(options).await.unwrap();
        drop(rx);

        assert!(
            vault.index_ready(),
            "recursive root browse is a whole-vault sync — probe must report ready"
        );
    }
}