compsys 0.8.15

Zsh-compatible completion system for zshrs
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
//! SQLite-backed cache for compsys
//!
//! MAXIMUM SPEED OPTIMIZATIONS:
//! - FTS5 for prefix search (O(1) vs O(n) LIKE)
//! - WAL mode for concurrent reads
//! - Memory-mapped I/O (mmap)
//! - No JOINs, no GROUP BY, no subqueries
//! - Denormalized flat tables with covering indexes
//! - Prepared statement caching

use rusqlite::{params, Connection, OptionalExtension};
use std::collections::HashMap;
use std::path::{Path, PathBuf};

/// SQLite cache for completion system
pub struct CompsysCache {
    conn: Connection,
}

/// Returns the default cache path: ~/.cache/zshrs/compsys.db
pub fn default_cache_path() -> PathBuf {
    let home = std::env::var("HOME").unwrap_or_else(|_| "/tmp".to_string());
    PathBuf::from(home).join(".cache/zshrs/compsys.db")
}

impl CompsysCache {
    /// Open or create cache database with maximum performance settings
    pub fn open(path: impl AsRef<Path>) -> rusqlite::Result<Self> {
        let conn = Connection::open(path)?;
        let cache = Self { conn };
        cache.configure_for_speed()?;
        cache.init_schema()?;
        Ok(cache)
    }

    /// In-memory cache (for testing)
    pub fn memory() -> rusqlite::Result<Self> {
        let conn = Connection::open_in_memory()?;
        let cache = Self { conn };
        cache.configure_for_speed()?;
        cache.init_schema()?;
        Ok(cache)
    }

    /// Configure SQLite for maximum read performance (called on every open)
    fn configure_for_speed(&self) -> rusqlite::Result<()> {
        // WAL mode persists, but cache/mmap need to be set each session
        self.conn.execute_batch(
            r#"
            PRAGMA journal_mode = WAL;
            PRAGMA synchronous = NORMAL;
            PRAGMA cache_size = -64000;
            PRAGMA mmap_size = 268435456;
            PRAGMA temp_store = MEMORY;
            "#,
        )
    }

    fn init_schema(&self) -> rusqlite::Result<()> {
        self.conn.execute_batch(
            r#"
            -- Autoloads: flat table, PRIMARY KEY = clustered index
            -- body stores actual function definition - NO filesystem access on autoload -Xz
            -- compinit reads from .zwc or plain files ONCE, stores body here
            CREATE TABLE IF NOT EXISTS autoloads (
                name TEXT PRIMARY KEY,
                source TEXT NOT NULL,
                offset INTEGER NOT NULL,
                size INTEGER NOT NULL,
                body TEXT
            ) WITHOUT ROWID;

            -- zstyle: flat lookup by pattern+style
            CREATE TABLE IF NOT EXISTS zstyles (
                pattern TEXT NOT NULL,
                style TEXT NOT NULL,
                value TEXT NOT NULL,
                eval INTEGER DEFAULT 0,
                PRIMARY KEY (pattern, style)
            ) WITHOUT ROWID;

            -- Completion mappings: direct key lookup
            CREATE TABLE IF NOT EXISTS comps (
                command TEXT PRIMARY KEY,
                function TEXT NOT NULL
            ) WITHOUT ROWID;

            -- Pattern completions
            CREATE TABLE IF NOT EXISTS patcomps (
                pattern TEXT PRIMARY KEY,
                function TEXT NOT NULL
            ) WITHOUT ROWID;

            -- Key completions
            CREATE TABLE IF NOT EXISTS keycomps (
                key TEXT PRIMARY KEY,
                function TEXT NOT NULL
            ) WITHOUT ROWID;

            -- Services
            CREATE TABLE IF NOT EXISTS services (
                command TEXT PRIMARY KEY,
                service TEXT NOT NULL
            ) WITHOUT ROWID;

            -- Result cache
            CREATE TABLE IF NOT EXISTS cache (
                context TEXT PRIMARY KEY,
                data BLOB NOT NULL,
                mtime INTEGER NOT NULL
            ) WITHOUT ROWID;

            -- PATH executables: flat, fast prefix via FTS5
            CREATE TABLE IF NOT EXISTS executables (
                name TEXT PRIMARY KEY,
                path TEXT NOT NULL
            ) WITHOUT ROWID;

            -- Named directories
            CREATE TABLE IF NOT EXISTS named_dirs (
                name TEXT PRIMARY KEY,
                path TEXT NOT NULL
            ) WITHOUT ROWID;

            -- Shell functions
            CREATE TABLE IF NOT EXISTS shell_functions (
                name TEXT PRIMARY KEY,
                source TEXT NOT NULL
            ) WITHOUT ROWID;

            -- Metadata
            CREATE TABLE IF NOT EXISTS metadata (
                key TEXT PRIMARY KEY,
                value TEXT NOT NULL
            ) WITHOUT ROWID;

            -- FTS5 for lightning-fast prefix search (standalone, not content-synced)
            CREATE VIRTUAL TABLE IF NOT EXISTS fts_comps USING fts5(
                command,
                tokenize='unicode61'
            );

            CREATE VIRTUAL TABLE IF NOT EXISTS fts_executables USING fts5(
                name,
                tokenize='unicode61'
            );

            CREATE VIRTUAL TABLE IF NOT EXISTS fts_shell_functions USING fts5(
                name,
                tokenize='unicode61'
            );

            -- Covering index for comps prefix search (fallback if FTS unavailable)
            CREATE INDEX IF NOT EXISTS idx_comps_cmd ON comps(command);
            CREATE INDEX IF NOT EXISTS idx_comps_func ON comps(function);
            CREATE INDEX IF NOT EXISTS idx_executables_name ON executables(name);
            CREATE INDEX IF NOT EXISTS idx_shell_functions_name ON shell_functions(name);
            CREATE INDEX IF NOT EXISTS idx_named_dirs_name ON named_dirs(name);
        "#,
        )?;
        Ok(())
    }

    // =========================================================================
    // Autoloads - function stubs
    // =========================================================================

    /// Register an autoload stub (without body)
    pub fn add_autoload(
        &self,
        name: &str,
        source: &str,
        offset: i64,
        size: i64,
    ) -> rusqlite::Result<()> {
        self.conn.execute(
            "INSERT OR REPLACE INTO autoloads (name, source, offset, size, body) VALUES (?1, ?2, ?3, ?4, NULL)",
            params![name, source, offset, size],
        )?;
        Ok(())
    }

    /// Register an autoload with full function body (for instant loading)
    pub fn add_autoload_with_body(
        &self,
        name: &str,
        source: &str,
        body: &str,
    ) -> rusqlite::Result<()> {
        self.conn.execute(
            "INSERT OR REPLACE INTO autoloads (name, source, offset, size, body) VALUES (?1, ?2, 0, ?3, ?4)",
            params![name, source, body.len() as i64, body],
        )?;
        Ok(())
    }

    /// Bulk insert autoloads (much faster)
    pub fn add_autoloads_bulk(
        &mut self,
        autoloads: &[(String, String, i64, i64)],
    ) -> rusqlite::Result<()> {
        let tx = self.conn.transaction()?;
        {
            let mut stmt = tx.prepare(
                "INSERT OR REPLACE INTO autoloads (name, source, offset, size, body) VALUES (?1, ?2, ?3, ?4, NULL)"
            )?;
            for (name, source, offset, size) in autoloads {
                stmt.execute(params![name, source, offset, size])?;
            }
        }
        tx.commit()?;
        Ok(())
    }

    /// Bulk insert autoloads with bodies (for compinit to cache function definitions)
    pub fn add_autoloads_with_bodies_bulk(
        &mut self,
        autoloads: &[(String, String, String)], // (name, source, body)
    ) -> rusqlite::Result<()> {
        let tx = self.conn.transaction()?;
        {
            let mut stmt = tx.prepare(
                "INSERT OR REPLACE INTO autoloads (name, source, offset, size, body) VALUES (?1, ?2, 0, ?3, ?4)"
            )?;
            for (name, source, body) in autoloads {
                stmt.execute(params![name, source, body.len() as i64, body])?;
            }
        }
        tx.commit()?;
        Ok(())
    }

    /// Lookup autoload by name
    pub fn get_autoload(&self, name: &str) -> rusqlite::Result<Option<AutoloadStub>> {
        self.conn
            .query_row(
                "SELECT source, offset, size, body FROM autoloads WHERE name = ?1",
                params![name],
                |row| {
                    Ok(AutoloadStub {
                        name: name.to_string(),
                        source: row.get(0)?,
                        offset: row.get(1)?,
                        size: row.get(2)?,
                        body: row.get(3)?,
                    })
                },
            )
            .optional()
    }

    /// Get function body directly (fast path for autoload -Xz)
    pub fn get_autoload_body(&self, name: &str) -> rusqlite::Result<Option<String>> {
        self.conn
            .query_row(
                "SELECT body FROM autoloads WHERE name = ?1",
                params![name],
                |row| row.get(0),
            )
            .optional()
    }

    /// Get function body with ZWC fallback
    /// 1. If body column has content, return it (fast path)
    /// 2. If body is NULL but source/offset/size exist, read from ZWC file
    /// 3. Returns None if function not found or ZWC read fails
    pub fn get_autoload_body_or_zwc(&self, name: &str) -> Option<String> {
        let stub = self.get_autoload(name).ok()??;

        // Fast path: body is cached
        if let Some(body) = stub.body {
            return Some(body);
        }

        // Fallback: read from ZWC file
        if stub.size > 0 && !stub.source.is_empty() {
            return Self::read_function_from_zwc(&stub.source, stub.offset, stub.size);
        }

        None
    }

    /// Read function body from ZWC file at given offset/size
    fn read_function_from_zwc(zwc_path: &str, offset: i64, size: i64) -> Option<String> {
        use std::io::{Read, Seek, SeekFrom};

        let mut file = std::fs::File::open(zwc_path).ok()?;
        file.seek(SeekFrom::Start(offset as u64)).ok()?;

        let mut buf = vec![0u8; size as usize];
        file.read_exact(&mut buf).ok()?;

        // ZWC stores tokenized strings - need to untokenize
        // For now, just try to interpret as UTF-8 (works for most cases)
        // TODO: proper untokenization like zwc.rs does
        match String::from_utf8(buf) {
            Ok(s) => Some(s),
            Err(e) => Some(String::from_utf8_lossy(e.as_bytes()).into_owned()),
        }
    }

    /// Count autoloads
    pub fn autoload_count(&self) -> rusqlite::Result<i64> {
        self.conn
            .query_row("SELECT COUNT(*) FROM autoloads", [], |row| row.get(0))
    }

    /// List all autoload names (for debugging)
    pub fn list_autoloads(&self, limit: usize) -> rusqlite::Result<Vec<String>> {
        let mut stmt = self.conn.prepare("SELECT name FROM autoloads LIMIT ?1")?;
        let rows = stmt.query_map(params![limit as i64], |row| row.get(0))?;
        rows.collect()
    }

    /// List all autoload names (no limit)
    pub fn list_autoload_names(&self) -> rusqlite::Result<Vec<String>> {
        let mut stmt = self.conn.prepare("SELECT name FROM autoloads")?;
        let rows = stmt.query_map([], |row| row.get(0))?;
        rows.collect()
    }

    // =========================================================================
    // zstyle database
    // =========================================================================

    /// Set a zstyle
    pub fn set_zstyle(
        &self,
        pattern: &str,
        style: &str,
        values: &[String],
        eval: bool,
    ) -> rusqlite::Result<()> {
        let value_json = serde_values_to_json(values);
        self.conn.execute(
            "INSERT OR REPLACE INTO zstyles (pattern, style, value, eval) VALUES (?1, ?2, ?3, ?4)",
            params![pattern, style, value_json, eval as i32],
        )?;
        Ok(())
    }

    /// Bulk insert zstyles
    pub fn set_zstyles_bulk(
        &mut self,
        styles: &[(String, String, Vec<String>, bool)],
    ) -> rusqlite::Result<()> {
        let tx = self.conn.transaction()?;
        {
            let mut stmt = tx.prepare(
                "INSERT OR REPLACE INTO zstyles (pattern, style, value, eval) VALUES (?1, ?2, ?3, ?4)"
            )?;
            for (pattern, style, values, eval) in styles {
                let value_json = serde_values_to_json(values);
                stmt.execute(params![pattern, style, value_json, *eval as i32])?;
            }
        }
        tx.commit()?;
        Ok(())
    }

    /// Delete a zstyle
    pub fn delete_zstyle(&self, pattern: &str, style: Option<&str>) -> rusqlite::Result<usize> {
        if let Some(s) = style {
            self.conn.execute(
                "DELETE FROM zstyles WHERE pattern = ?1 AND style = ?2",
                params![pattern, s],
            )
        } else {
            self.conn
                .execute("DELETE FROM zstyles WHERE pattern = ?1", params![pattern])
        }
    }

    /// Lookup zstyle - returns all matching patterns sorted by specificity
    pub fn lookup_zstyle(
        &self,
        context: &str,
        style: &str,
    ) -> rusqlite::Result<Option<ZStyleEntry>> {
        let mut stmt = self
            .conn
            .prepare("SELECT pattern, value, eval FROM zstyles WHERE style = ?1")?;

        let entries: Vec<(String, String, bool)> = stmt
            .query_map(params![style], |row| {
                Ok((row.get(0)?, row.get(1)?, row.get::<_, i32>(2)? != 0))
            })?
            .filter_map(|r| r.ok())
            .collect();

        // Find best match by specificity
        let mut best: Option<(i32, String, bool)> = None;
        for (pattern, value, eval) in entries {
            if pattern_matches_context(&pattern, context) {
                let weight = calculate_pattern_weight(&pattern);
                if best.is_none() || weight > best.as_ref().unwrap().0 {
                    best = Some((weight, value, eval));
                }
            }
        }

        Ok(best.map(|(_, value, eval)| ZStyleEntry {
            values: serde_json_to_values(&value),
            eval,
        }))
    }

    /// List all zstyles (for `zstyle -L`)
    pub fn list_zstyles(&self) -> rusqlite::Result<Vec<(String, String, Vec<String>, bool)>> {
        let mut stmt = self
            .conn
            .prepare("SELECT pattern, style, value, eval FROM zstyles ORDER BY pattern, style")?;
        let rows = stmt.query_map([], |row| {
            let pattern: String = row.get(0)?;
            let style: String = row.get(1)?;
            let value: String = row.get(2)?;
            let eval: bool = row.get::<_, i32>(3)? != 0;
            Ok((pattern, style, serde_json_to_values(&value), eval))
        })?;
        rows.collect()
    }

    /// Count zstyles
    pub fn zstyle_count(&self) -> rusqlite::Result<i64> {
        self.conn
            .query_row("SELECT COUNT(*) FROM zstyles", [], |row| row.get(0))
    }

    // =========================================================================
    // Completion mappings (_comps)
    // =========================================================================

    /// Register a completion function for a command
    pub fn set_comp(&self, command: &str, function: &str) -> rusqlite::Result<()> {
        self.conn.execute(
            "INSERT OR REPLACE INTO comps (command, function) VALUES (?1, ?2)",
            params![command, function],
        )?;
        Ok(())
    }

    /// Bulk insert comps + populate FTS5 index
    pub fn set_comps_bulk(&mut self, comps: &[(String, String)]) -> rusqlite::Result<()> {
        let tx = self.conn.transaction()?;
        // Clear and repopulate both tables
        tx.execute("DELETE FROM comps", [])?;
        tx.execute("DELETE FROM fts_comps", [])?;
        {
            let mut stmt = tx.prepare("INSERT INTO comps (command, function) VALUES (?1, ?2)")?;
            let mut fts_stmt = tx.prepare("INSERT INTO fts_comps (command) VALUES (?1)")?;
            for (command, function) in comps {
                stmt.execute(params![command, function])?;
                fts_stmt.execute(params![command])?;
            }
        }
        tx.commit()
    }

    /// Fast prefix search using FTS5 (O(log n) vs O(n) for LIKE)
    pub fn comps_prefix_fts(&self, prefix: &str) -> rusqlite::Result<Vec<(String, String)>> {
        if prefix.is_empty() {
            return self.comps_kv();
        }
        // FTS5 prefix search: "git*" matches git, github, gitk, etc.
        let pattern = format!("{}*", prefix);
        let mut stmt = self.conn.prepare(
            "SELECT c.command, c.function FROM fts_comps f, comps c WHERE f.command MATCH ?1 AND c.command = f.command"
        )?;
        let rows = stmt.query_map(params![pattern], |row| Ok((row.get(0)?, row.get(1)?)))?;
        rows.collect()
    }

    /// Fast prefix search (LIKE with index scan, ORDER BY is free on indexed column)
    pub fn comps_prefix(&self, prefix: &str) -> rusqlite::Result<Vec<(String, String)>> {
        if prefix.is_empty() {
            return self.comps_kv();
        }
        let pattern = format!("{}%", prefix);
        let mut stmt = self.conn.prepare(
            "SELECT command, function FROM comps WHERE command LIKE ?1 ORDER BY command",
        )?;
        let rows = stmt.query_map(params![pattern], |row| Ok((row.get(0)?, row.get(1)?)))?;
        rows.collect()
    }

    /// Lookup completion function for command
    pub fn get_comp(&self, command: &str) -> rusqlite::Result<Option<String>> {
        self.conn
            .query_row(
                "SELECT function FROM comps WHERE command = ?1",
                params![command],
                |row| row.get(0),
            )
            .optional()
    }

    /// Get all comps as HashMap (for compatibility)
    pub fn get_all_comps(&self) -> rusqlite::Result<HashMap<String, String>> {
        let mut stmt = self.conn.prepare("SELECT command, function FROM comps")?;
        let rows = stmt.query_map([], |row| {
            Ok((row.get::<_, String>(0)?, row.get::<_, String>(1)?))
        })?;
        let mut map = HashMap::new();
        for row in rows {
            let (k, v) = row?;
            map.insert(k, v);
        }
        Ok(map)
    }

    /// Count comps
    pub fn comp_count(&self) -> rusqlite::Result<i64> {
        self.conn
            .query_row("SELECT COUNT(*) FROM comps", [], |row| row.get(0))
    }

    /// Delete a completion registration
    pub fn delete_comp(&self, command: &str) -> rusqlite::Result<usize> {
        self.conn
            .execute("DELETE FROM comps WHERE command = ?1", params![command])
    }

    // =========================================================================
    // Pattern completions (_patcomps)
    // =========================================================================

    /// Register a pattern completion
    pub fn set_patcomp(&self, pattern: &str, function: &str) -> rusqlite::Result<()> {
        self.conn.execute(
            "INSERT OR REPLACE INTO patcomps (pattern, function) VALUES (?1, ?2)",
            params![pattern, function],
        )?;
        Ok(())
    }

    /// Find matching pattern completion
    pub fn find_patcomp(&self, command: &str) -> rusqlite::Result<Option<String>> {
        let mut stmt = self
            .conn
            .prepare("SELECT pattern, function FROM patcomps")?;
        let rows = stmt.query_map([], |row| {
            Ok((row.get::<_, String>(0)?, row.get::<_, String>(1)?))
        })?;

        for row in rows {
            let (pattern, function) = row?;
            if glob_matches(&pattern, command) {
                return Ok(Some(function));
            }
        }
        Ok(None)
    }

    // =========================================================================
    // Key completions
    // =========================================================================

    /// Register a key completion (for -K)
    pub fn set_keycomp(&self, key: &str, function: &str) -> rusqlite::Result<()> {
        self.conn.execute(
            "INSERT OR REPLACE INTO keycomps (key, function) VALUES (?1, ?2)",
            params![key, function],
        )?;
        Ok(())
    }

    /// Lookup key completion
    pub fn get_keycomp(&self, key: &str) -> rusqlite::Result<Option<String>> {
        self.conn
            .query_row(
                "SELECT function FROM keycomps WHERE key = ?1",
                params![key],
                |row| row.get(0),
            )
            .optional()
    }

    // =========================================================================
    // Result cache
    // =========================================================================

    /// Cache completion results
    pub fn cache_results(&self, context: &str, data: &[u8], mtime: i64) -> rusqlite::Result<()> {
        self.conn.execute(
            "INSERT OR REPLACE INTO cache (context, data, mtime) VALUES (?1, ?2, ?3)",
            params![context, data, mtime],
        )?;
        Ok(())
    }

    /// Get cached results if not stale
    pub fn get_cached(&self, context: &str, max_age: i64) -> rusqlite::Result<Option<Vec<u8>>> {
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs() as i64;

        self.conn
            .query_row(
                "SELECT data FROM cache WHERE context = ?1 AND mtime > ?2",
                params![context, now - max_age],
                |row| row.get(0),
            )
            .optional()
    }

    /// Clear old cache entries
    pub fn clear_stale_cache(&self, max_age: i64) -> rusqlite::Result<usize> {
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs() as i64;

        self.conn
            .execute("DELETE FROM cache WHERE mtime < ?1", params![now - max_age])
    }

    /// Clear all cache
    pub fn clear_cache(&self) -> rusqlite::Result<()> {
        self.conn.execute("DELETE FROM cache", [])?;
        Ok(())
    }

    // =========================================================================
    // Maintenance
    // =========================================================================

    /// Vacuum database
    pub fn vacuum(&self) -> rusqlite::Result<()> {
        self.conn.execute("VACUUM", [])?;
        Ok(())
    }

    /// Get database stats
    pub fn stats(&self) -> rusqlite::Result<CacheStats> {
        Ok(CacheStats {
            autoloads: self.autoload_count()?,
            zstyles: self.zstyle_count()?,
            comps: self.comp_count()?,
            patcomps: self
                .conn
                .query_row("SELECT COUNT(*) FROM patcomps", [], |r| r.get(0))?,
            keycomps: self
                .conn
                .query_row("SELECT COUNT(*) FROM keycomps", [], |r| r.get(0))?,
            services: self
                .conn
                .query_row("SELECT COUNT(*) FROM services", [], |r| r.get(0))?,
            cache_entries: self
                .conn
                .query_row("SELECT COUNT(*) FROM cache", [], |r| r.get(0))?,
        })
    }
}

/// Autoload stub info
#[derive(Debug, Clone)]
pub struct AutoloadStub {
    pub name: String,
    pub source: String,
    pub offset: i64,
    pub size: i64,
    /// Cached function body - if present, no need to read from source file
    pub body: Option<String>,
}

/// zstyle entry
#[derive(Debug, Clone)]
pub struct ZStyleEntry {
    pub values: Vec<String>,
    pub eval: bool,
}

/// Cache statistics
#[derive(Debug)]
pub struct CacheStats {
    pub autoloads: i64,
    pub zstyles: i64,
    pub comps: i64,
    pub patcomps: i64,
    pub keycomps: i64,
    pub services: i64,
    pub cache_entries: i64,
}

// Helper: serialize values to JSON
fn serde_values_to_json(values: &[String]) -> String {
    let escaped: Vec<String> = values
        .iter()
        .map(|s| format!("\"{}\"", s.replace('\\', "\\\\").replace('"', "\\\"")))
        .collect();
    format!("[{}]", escaped.join(","))
}

// Helper: deserialize JSON to values
fn serde_json_to_values(json: &str) -> Vec<String> {
    let trimmed = json.trim();
    if !trimmed.starts_with('[') || !trimmed.ends_with(']') {
        return vec![json.to_string()];
    }

    let inner = &trimmed[1..trimmed.len() - 1];
    if inner.is_empty() {
        return vec![];
    }

    let mut values = Vec::new();
    let mut current = String::new();
    let mut in_string = false;
    let mut escape = false;

    for c in inner.chars() {
        if escape {
            current.push(c);
            escape = false;
        } else if c == '\\' {
            escape = true;
        } else if c == '"' {
            in_string = !in_string;
        } else if c == ',' && !in_string {
            values.push(current.trim().to_string());
            current = String::new();
        } else {
            current.push(c);
        }
    }
    if !current.is_empty() {
        values.push(current.trim().to_string());
    }

    values
}

// Helper: check if zstyle pattern matches context
fn pattern_matches_context(pattern: &str, context: &str) -> bool {
    let pat_parts: Vec<&str> = pattern.split(':').collect();
    let ctx_parts: Vec<&str> = context.split(':').collect();

    if pat_parts.len() > ctx_parts.len() {
        return false;
    }

    for (p, c) in pat_parts.iter().zip(ctx_parts.iter()) {
        if *p != "*" && *p != *c {
            return false;
        }
    }

    true
}

// Helper: calculate pattern weight for specificity
fn calculate_pattern_weight(pattern: &str) -> i32 {
    let parts: Vec<&str> = pattern.split(':').filter(|s| !s.is_empty()).collect();
    let mut weight = parts.len() as i32 * 100;

    for part in &parts {
        if *part != "*" {
            weight += 10;
        }
    }

    weight
}

// Helper: glob matching for patcomps
fn glob_matches(pattern: &str, text: &str) -> bool {
    let mut pat_chars = pattern.chars().peekable();
    let mut txt_chars = text.chars().peekable();

    while let Some(p) = pat_chars.next() {
        match p {
            '*' => {
                if pat_chars.peek().is_none() {
                    return true;
                }
                while txt_chars.peek().is_some() {
                    if glob_matches(
                        &pat_chars.clone().collect::<String>(),
                        &txt_chars.clone().collect::<String>(),
                    ) {
                        return true;
                    }
                    txt_chars.next();
                }
                return false;
            }
            '?' => {
                if txt_chars.next().is_none() {
                    return false;
                }
            }
            c => {
                if txt_chars.next() != Some(c) {
                    return false;
                }
            }
        }
    }

    txt_chars.peek().is_none()
}

// =========================================================================
// Shell-visible arrays (_comps, _services, _patcomps, etc.)
// These back the zsh special arrays that users query with $#_comps etc.
// =========================================================================

impl CompsysCache {
    /// Get count of _comps entries (for $#_comps)
    pub fn comps_count(&self) -> rusqlite::Result<i64> {
        self.comp_count()
    }

    /// Get all _comps keys (for ${(k)_comps}) - ORDER BY is free on PRIMARY KEY
    pub fn comps_keys(&self) -> rusqlite::Result<Vec<String>> {
        let mut stmt = self
            .conn
            .prepare("SELECT command FROM comps ORDER BY command")?;
        let rows = stmt.query_map([], |row| row.get(0))?;
        rows.collect()
    }

    /// Get all _comps values (for ${(v)_comps})
    pub fn comps_values(&self) -> rusqlite::Result<Vec<String>> {
        let mut stmt = self
            .conn
            .prepare("SELECT function FROM comps ORDER BY command")?;
        let rows = stmt.query_map([], |row| row.get(0))?;
        rows.collect()
    }

    /// Get _comps as key-value pairs (for ${(kv)_comps})
    pub fn comps_kv(&self) -> rusqlite::Result<Vec<(String, String)>> {
        let mut stmt = self
            .conn
            .prepare("SELECT command, function FROM comps ORDER BY command")?;
        let rows = stmt.query_map([], |row| Ok((row.get(0)?, row.get(1)?)))?;
        rows.collect()
    }

    // --- _patcomps ---

    /// Get count of _patcomps
    pub fn patcomps_count(&self) -> rusqlite::Result<i64> {
        self.conn
            .query_row("SELECT COUNT(*) FROM patcomps", [], |row| row.get(0))
    }

    /// Get all _patcomps keys
    pub fn patcomps_keys(&self) -> rusqlite::Result<Vec<String>> {
        let mut stmt = self.conn.prepare("SELECT pattern FROM patcomps")?;
        let rows = stmt.query_map([], |row| row.get(0))?;
        rows.collect()
    }

    /// Get all _patcomps as kv
    pub fn patcomps_kv(&self) -> rusqlite::Result<Vec<(String, String)>> {
        let mut stmt = self
            .conn
            .prepare("SELECT pattern, function FROM patcomps")?;
        let rows = stmt.query_map([], |row| Ok((row.get(0)?, row.get(1)?)))?;
        rows.collect()
    }

    // --- _services ---

    /// Set a service mapping
    pub fn set_service(&self, command: &str, service: &str) -> rusqlite::Result<()> {
        self.conn.execute(
            "INSERT OR REPLACE INTO services (command, service) VALUES (?1, ?2)",
            params![command, service],
        )?;
        Ok(())
    }

    /// Get service for command
    pub fn get_service(&self, command: &str) -> rusqlite::Result<Option<String>> {
        self.conn
            .query_row(
                "SELECT service FROM services WHERE command = ?1",
                params![command],
                |row| row.get(0),
            )
            .optional()
    }

    /// Get count of _services
    pub fn services_count(&self) -> rusqlite::Result<i64> {
        self.conn
            .query_row("SELECT COUNT(*) FROM services", [], |row| row.get(0))
    }

    /// Get all _services keys
    pub fn services_keys(&self) -> rusqlite::Result<Vec<String>> {
        let mut stmt = self.conn.prepare("SELECT command FROM services")?;
        let rows = stmt.query_map([], |row| row.get(0))?;
        rows.collect()
    }

    /// Bulk insert services
    pub fn set_services_bulk(&mut self, services: &[(String, String)]) -> rusqlite::Result<()> {
        let tx = self.conn.transaction()?;
        {
            let mut stmt =
                tx.prepare("INSERT OR REPLACE INTO services (command, service) VALUES (?1, ?2)")?;
            for (command, service) in services {
                stmt.execute(params![command, service])?;
            }
        }
        tx.commit()?;
        Ok(())
    }

    // --- _compautos (autoloaded completion functions) ---

    /// Get count of autoloaded functions
    pub fn compautos_count(&self) -> rusqlite::Result<i64> {
        self.autoload_count()
    }

    /// Get all autoload names (for ${(k)_compautos})
    pub fn compautos_keys(&self) -> rusqlite::Result<Vec<String>> {
        let mut stmt = self.conn.prepare("SELECT name FROM autoloads")?;
        let rows = stmt.query_map([], |row| row.get(0))?;
        rows.collect()
    }

    // =========================================================================
    // PATH executables cache
    // =========================================================================

    /// Check if executables cache is populated
    pub fn has_executables(&self) -> rusqlite::Result<bool> {
        let count: i64 = self
            .conn
            .query_row("SELECT COUNT(*) FROM executables", [], |row| row.get(0))?;
        Ok(count > 0)
    }

    /// Store executables in bulk + populate FTS5 index
    pub fn set_executables_bulk(
        &mut self,
        executables: &[(String, String)],
    ) -> rusqlite::Result<()> {
        let tx = self.conn.transaction()?;
        tx.execute("DELETE FROM executables", [])?;
        tx.execute("DELETE FROM fts_executables", [])?;
        {
            let mut stmt =
                tx.prepare("INSERT OR IGNORE INTO executables (name, path) VALUES (?1, ?2)")?;
            let mut fts_stmt =
                tx.prepare("INSERT OR IGNORE INTO fts_executables (name) VALUES (?1)")?;
            for (name, path) in executables {
                stmt.execute(params![name, path])?;
                fts_stmt.execute(params![name])?;
            }
        }
        tx.commit()
    }

    /// Get all executable names (fast lookup set)
    pub fn get_executable_names(&self) -> rusqlite::Result<std::collections::HashSet<String>> {
        let mut stmt = self.conn.prepare("SELECT name FROM executables")?;
        let rows = stmt.query_map([], |row| row.get::<_, String>(0))?;
        rows.collect::<Result<std::collections::HashSet<_>, _>>()
    }

    /// Check if an executable exists in cache (O(1) lookup)
    pub fn has_executable(&self, name: &str) -> rusqlite::Result<bool> {
        // Use EXISTS for faster check (stops at first match)
        let exists: i64 = self.conn.query_row(
            "SELECT EXISTS(SELECT 1 FROM executables WHERE name = ?1)",
            params![name],
            |row| row.get(0),
        )?;
        Ok(exists == 1)
    }

    /// Get executable path by name (direct key lookup)
    pub fn get_executable_path(&self, name: &str) -> rusqlite::Result<Option<String>> {
        self.conn
            .query_row(
                "SELECT path FROM executables WHERE name = ?1",
                params![name],
                |row| row.get(0),
            )
            .optional()
    }

    /// Fast prefix search using FTS5
    pub fn get_executables_prefix_fts(
        &self,
        prefix: &str,
    ) -> rusqlite::Result<Vec<(String, String)>> {
        if prefix.is_empty() {
            let mut stmt = self.conn.prepare("SELECT name, path FROM executables")?;
            let rows = stmt.query_map([], |row| Ok((row.get(0)?, row.get(1)?)))?;
            return rows.collect();
        }
        let pattern = format!("{}*", prefix);
        let mut stmt = self.conn.prepare(
            "SELECT e.name, e.path FROM fts_executables f, executables e WHERE f.name MATCH ?1 AND e.name = f.name"
        )?;
        let rows = stmt.query_map(params![pattern], |row| Ok((row.get(0)?, row.get(1)?)))?;
        rows.collect()
    }

    /// Get executables matching prefix (LIKE with index, ORDER BY free on PRIMARY KEY)
    pub fn get_executables_prefix(&self, prefix: &str) -> rusqlite::Result<Vec<(String, String)>> {
        if prefix.is_empty() {
            let mut stmt = self
                .conn
                .prepare("SELECT name, path FROM executables ORDER BY name")?;
            let rows = stmt.query_map([], |row| Ok((row.get(0)?, row.get(1)?)))?;
            return rows.collect();
        }
        let pattern = format!("{}%", prefix);
        let mut stmt = self
            .conn
            .prepare("SELECT name, path FROM executables WHERE name LIKE ?1 ORDER BY name")?;
        let rows = stmt.query_map(params![pattern], |row| Ok((row.get(0)?, row.get(1)?)))?;
        rows.collect()
    }

    /// Count executables
    pub fn executables_count(&self) -> rusqlite::Result<i64> {
        self.conn
            .query_row("SELECT COUNT(*) FROM executables", [], |row| row.get(0))
    }

    // =========================================================================
    // Named directories cache (hash -d)
    // =========================================================================

    /// Check if named_dirs cache is populated
    pub fn has_named_dirs(&self) -> rusqlite::Result<bool> {
        let count: i64 = self
            .conn
            .query_row("SELECT COUNT(*) FROM named_dirs", [], |row| row.get(0))?;
        Ok(count > 0)
    }

    /// Store named directories in bulk (clears existing)
    pub fn set_named_dirs_bulk(&mut self, dirs: &[(String, String)]) -> rusqlite::Result<()> {
        let tx = self.conn.transaction()?;
        tx.execute("DELETE FROM named_dirs", [])?;
        {
            let mut stmt = tx.prepare("INSERT INTO named_dirs (name, path) VALUES (?1, ?2)")?;
            for (name, path) in dirs {
                stmt.execute(params![name, path])?;
            }
        }
        tx.commit()
    }

    /// Get all named directories (ORDER BY free on PRIMARY KEY)
    pub fn get_named_dirs(&self) -> rusqlite::Result<Vec<(String, String)>> {
        let mut stmt = self
            .conn
            .prepare("SELECT name, path FROM named_dirs ORDER BY name")?;
        let rows = stmt.query_map([], |row| Ok((row.get(0)?, row.get(1)?)))?;
        rows.collect()
    }

    /// Get named directories matching prefix
    pub fn get_named_dirs_prefix(&self, prefix: &str) -> rusqlite::Result<Vec<(String, String)>> {
        if prefix.is_empty() {
            return self.get_named_dirs();
        }
        let pattern = format!("{}%", prefix);
        let mut stmt = self
            .conn
            .prepare("SELECT name, path FROM named_dirs WHERE name LIKE ?1 ORDER BY name")?;
        let rows = stmt.query_map(params![pattern], |row| Ok((row.get(0)?, row.get(1)?)))?;
        rows.collect()
    }

    /// Count named directories
    pub fn named_dirs_count(&self) -> rusqlite::Result<i64> {
        self.conn
            .query_row("SELECT COUNT(*) FROM named_dirs", [], |row| row.get(0))
    }

    // =========================================================================
    // Shell functions cache (FPATH)
    // =========================================================================

    /// Check if shell_functions cache is populated
    pub fn has_shell_functions(&self) -> rusqlite::Result<bool> {
        let count: i64 =
            self.conn
                .query_row("SELECT COUNT(*) FROM shell_functions", [], |row| row.get(0))?;
        Ok(count > 0)
    }

    /// Store shell functions in bulk + populate FTS5 index
    pub fn set_shell_functions_bulk(&mut self, funcs: &[(String, String)]) -> rusqlite::Result<()> {
        let tx = self.conn.transaction()?;
        tx.execute("DELETE FROM shell_functions", [])?;
        tx.execute("DELETE FROM fts_shell_functions", [])?;
        {
            let mut stmt =
                tx.prepare("INSERT OR IGNORE INTO shell_functions (name, source) VALUES (?1, ?2)")?;
            let mut fts_stmt =
                tx.prepare("INSERT OR IGNORE INTO fts_shell_functions (name) VALUES (?1)")?;
            for (name, source) in funcs {
                stmt.execute(params![name, source])?;
                fts_stmt.execute(params![name])?;
            }
        }
        tx.commit()
    }

    /// Get all shell function names (ORDER BY free on PRIMARY KEY)
    pub fn get_shell_function_names(&self) -> rusqlite::Result<Vec<String>> {
        let mut stmt = self
            .conn
            .prepare("SELECT name FROM shell_functions ORDER BY name")?;
        let rows = stmt.query_map([], |row| row.get(0))?;
        rows.collect()
    }

    /// Get shell functions with source paths
    pub fn get_shell_functions(&self) -> rusqlite::Result<Vec<(String, String)>> {
        let mut stmt = self
            .conn
            .prepare("SELECT name, source FROM shell_functions ORDER BY name")?;
        let rows = stmt.query_map([], |row| Ok((row.get(0)?, row.get(1)?)))?;
        rows.collect()
    }

    /// Fast prefix search using FTS5 (note: FTS5 doesn't preserve order, needs post-sort)
    pub fn get_shell_functions_prefix_fts(
        &self,
        prefix: &str,
    ) -> rusqlite::Result<Vec<(String, String)>> {
        if prefix.is_empty() {
            return self.get_shell_functions();
        }
        let pattern = format!("{}*", prefix);
        let mut stmt = self.conn.prepare(
            "SELECT s.name, s.source FROM fts_shell_functions f, shell_functions s WHERE f.name MATCH ?1 AND s.name = f.name ORDER BY s.name"
        )?;
        let rows = stmt.query_map(params![pattern], |row| Ok((row.get(0)?, row.get(1)?)))?;
        rows.collect()
    }

    /// Get shell functions matching prefix (LIKE with index, ORDER BY free)
    pub fn get_shell_functions_prefix(
        &self,
        prefix: &str,
    ) -> rusqlite::Result<Vec<(String, String)>> {
        if prefix.is_empty() {
            return self.get_shell_functions();
        }
        let pattern = format!("{}%", prefix);
        let mut stmt = self
            .conn
            .prepare("SELECT name, source FROM shell_functions WHERE name LIKE ?1 ORDER BY name")?;
        let rows = stmt.query_map(params![pattern], |row| Ok((row.get(0)?, row.get(1)?)))?;
        rows.collect()
    }

    /// Count shell functions
    pub fn shell_functions_count(&self) -> rusqlite::Result<i64> {
        self.conn
            .query_row("SELECT COUNT(*) FROM shell_functions", [], |row| row.get(0))
    }

    // =========================================================================
    // Metadata for cache versioning/invalidation
    // =========================================================================

    /// Set metadata key-value
    pub fn set_metadata(&self, key: &str, value: &str) -> rusqlite::Result<()> {
        self.conn.execute(
            "INSERT OR REPLACE INTO metadata (key, value) VALUES (?1, ?2)",
            params![key, value],
        )?;
        Ok(())
    }

    /// Get metadata value
    pub fn get_metadata(&self, key: &str) -> rusqlite::Result<Option<String>> {
        self.conn
            .query_row(
                "SELECT value FROM metadata WHERE key = ?1",
                params![key],
                |row| row.get(0),
            )
            .optional()
    }

    // =========================================================================
    // Zstyle helpers
    // =========================================================================

    /// Check if zstyles cache is populated
    pub fn has_zstyles(&self) -> rusqlite::Result<bool> {
        let count: i64 = self
            .conn
            .query_row("SELECT COUNT(*) FROM zstyles", [], |row| row.get(0))?;
        Ok(count > 0)
    }

    /// Count zstyles
    pub fn zstyles_count(&self) -> rusqlite::Result<i64> {
        self.conn
            .query_row("SELECT COUNT(*) FROM zstyles", [], |row| row.get(0))
    }

    /// Get all zstyles (for debugging)
    pub fn get_all_zstyles(&self) -> rusqlite::Result<Vec<(String, String, String)>> {
        let mut stmt = self
            .conn
            .prepare("SELECT pattern, style, value FROM zstyles ORDER BY pattern, style")?;
        let rows = stmt.query_map([], |row| Ok((row.get(0)?, row.get(1)?, row.get(2)?)))?;
        rows.collect()
    }
}

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

    #[test]
    fn test_cache_basic() {
        let cache = CompsysCache::memory().unwrap();

        cache
            .add_autoload("_git", "more_src.zwc", 1024, 5000)
            .unwrap();
        cache
            .add_autoload("_docker", "more_src.zwc", 6024, 3000)
            .unwrap();

        let stub = cache.get_autoload("_git").unwrap().unwrap();
        assert_eq!(stub.source, "more_src.zwc");
        assert_eq!(stub.offset, 1024);

        assert!(cache.get_autoload("_nonexistent").unwrap().is_none());
    }

    #[test]
    fn test_zstyle_cache() {
        let cache = CompsysCache::memory().unwrap();

        cache
            .set_zstyle(":completion:*", "menu", &["select".to_string()], false)
            .unwrap();
        cache
            .set_zstyle(
                ":completion:*:descriptions",
                "format",
                &["%d".to_string()],
                false,
            )
            .unwrap();

        let entry = cache
            .lookup_zstyle(":completion:foo", "menu")
            .unwrap()
            .unwrap();
        assert_eq!(entry.values, vec!["select"]);

        let entry = cache
            .lookup_zstyle(":completion:foo:descriptions", "format")
            .unwrap()
            .unwrap();
        assert_eq!(entry.values, vec!["%d"]);
    }

    #[test]
    fn test_zstyle_specificity() {
        let cache = CompsysCache::memory().unwrap();

        cache
            .set_zstyle(":completion:*", "menu", &["no".to_string()], false)
            .unwrap();
        cache
            .set_zstyle(
                ":completion:*:*:*:default",
                "menu",
                &["yes".to_string()],
                false,
            )
            .unwrap();

        let entry = cache
            .lookup_zstyle(":completion:foo:bar:baz:default", "menu")
            .unwrap()
            .unwrap();
        assert_eq!(entry.values, vec!["yes"]);
    }

    #[test]
    fn test_comps_cache() {
        let mut cache = CompsysCache::memory().unwrap();

        let comps = vec![
            ("git".to_string(), "_git".to_string()),
            ("docker".to_string(), "_docker".to_string()),
            ("cargo".to_string(), "_cargo".to_string()),
        ];
        cache.set_comps_bulk(&comps).unwrap();

        assert_eq!(cache.get_comp("git").unwrap(), Some("_git".to_string()));
        assert_eq!(
            cache.get_comp("docker").unwrap(),
            Some("_docker".to_string())
        );
        assert!(cache.get_comp("nonexistent").unwrap().is_none());

        assert_eq!(cache.comp_count().unwrap(), 3);
    }

    #[test]
    fn test_bulk_autoloads() {
        let mut cache = CompsysCache::memory().unwrap();

        let autoloads: Vec<(String, String, i64, i64)> = (0..1000)
            .map(|i| (format!("_func{}", i), "test.zwc".to_string(), i * 100, 100))
            .collect();

        cache.add_autoloads_bulk(&autoloads).unwrap();
        assert_eq!(cache.autoload_count().unwrap(), 1000);

        let stub = cache.get_autoload("_func500").unwrap().unwrap();
        assert_eq!(stub.offset, 50000);
        assert!(stub.body.is_none()); // No body when bulk inserted without
    }

    #[test]
    fn test_autoload_with_body() {
        let cache = CompsysCache::memory().unwrap();

        let body = r#"
local -a opts
opts=(--help --version --verbose)
_arguments $opts
"#;
        cache
            .add_autoload_with_body("_mycommand", "/usr/share/zsh/functions/_mycommand", body)
            .unwrap();

        let stub = cache.get_autoload("_mycommand").unwrap().unwrap();
        assert_eq!(stub.body.as_deref(), Some(body));
        assert_eq!(stub.size, body.len() as i64);

        // Fast path: get body directly
        let direct_body = cache.get_autoload_body("_mycommand").unwrap();
        assert_eq!(direct_body.as_deref(), Some(body));
    }

    #[test]
    fn test_bulk_autoloads_with_bodies() {
        let mut cache = CompsysCache::memory().unwrap();

        let autoloads: Vec<(String, String, String)> = (0..100)
            .map(|i| {
                (
                    format!("_func{}", i),
                    format!("/path/to/_func{}", i),
                    format!("# Function {}\necho hello", i),
                )
            })
            .collect();

        cache.add_autoloads_with_bodies_bulk(&autoloads).unwrap();
        assert_eq!(cache.autoload_count().unwrap(), 100);

        let stub = cache.get_autoload("_func50").unwrap().unwrap();
        assert!(stub.body.is_some());
        assert!(stub.body.unwrap().contains("Function 50"));
    }

    #[test]
    fn test_get_autoload_body_or_zwc_with_body() {
        let cache = CompsysCache::memory().unwrap();

        let body = "echo from sqlite";
        cache
            .add_autoload_with_body("_cached", "/some/path", body)
            .unwrap();

        // Should return body from SQLite (fast path)
        let result = cache.get_autoload_body_or_zwc("_cached");
        assert_eq!(result, Some(body.to_string()));
    }

    #[test]
    fn test_get_autoload_body_or_zwc_no_body() {
        let cache = CompsysCache::memory().unwrap();

        // Add autoload without body (just ZWC reference)
        cache
            .add_autoload("_nocache", "nonexistent.zwc", 0, 100)
            .unwrap();

        // Should return None since ZWC file doesn't exist
        let result = cache.get_autoload_body_or_zwc("_nocache");
        assert!(result.is_none());
    }

    #[test]
    fn test_get_autoload_body_or_zwc_not_found() {
        let cache = CompsysCache::memory().unwrap();

        // Function doesn't exist at all
        let result = cache.get_autoload_body_or_zwc("_nonexistent");
        assert!(result.is_none());
    }

    #[test]
    fn test_patcomp() {
        let cache = CompsysCache::memory().unwrap();

        cache.set_patcomp("git-*", "_git").unwrap();
        cache.set_patcomp("docker-*", "_docker").unwrap();

        assert_eq!(
            cache.find_patcomp("git-commit").unwrap(),
            Some("_git".to_string())
        );
        assert_eq!(
            cache.find_patcomp("docker-compose").unwrap(),
            Some("_docker".to_string())
        );
        assert!(cache.find_patcomp("cargo").unwrap().is_none());
    }

    #[test]
    fn test_glob_matches() {
        assert!(glob_matches("git-*", "git-commit"));
        assert!(glob_matches("*-compose", "docker-compose"));
        assert!(glob_matches("*.rs", "main.rs"));
        assert!(!glob_matches("git-*", "docker-compose"));
        assert!(glob_matches("???", "abc"));
        assert!(!glob_matches("???", "abcd"));
    }

    #[test]
    fn test_json_serde() {
        let values = vec!["hello".to_string(), "world".to_string()];
        let json = serde_values_to_json(&values);
        let back = serde_json_to_values(&json);
        assert_eq!(back, values);

        let values = vec!["with \"quotes\"".to_string()];
        let json = serde_values_to_json(&values);
        let back = serde_json_to_values(&json);
        assert_eq!(back, vec!["with \"quotes\""]);
    }

    #[test]
    fn test_stats() {
        let mut cache = CompsysCache::memory().unwrap();

        cache.add_autoload("_git", "test.zwc", 0, 100).unwrap();
        cache
            .set_zstyle(":completion:*", "menu", &["select".to_string()], false)
            .unwrap();
        cache.set_comp("git", "_git").unwrap();

        let stats = cache.stats().unwrap();
        assert_eq!(stats.autoloads, 1);
        assert_eq!(stats.zstyles, 1);
        assert_eq!(stats.comps, 1);
    }

    #[test]
    fn test_large_scale() {
        let mut cache = CompsysCache::memory().unwrap();

        // Simulate 500k autoloads
        let autoloads: Vec<(String, String, i64, i64)> = (0..10000)
            .map(|i| {
                (
                    format!("_func{}", i),
                    format!("src{}.zwc", i % 10),
                    i * 50,
                    50,
                )
            })
            .collect();

        cache.add_autoloads_bulk(&autoloads).unwrap();

        // Fast lookup
        let stub = cache.get_autoload("_func9999").unwrap().unwrap();
        assert_eq!(stub.offset, 9999 * 50);

        assert_eq!(cache.autoload_count().unwrap(), 10000);
    }

    #[test]
    fn test_executables_cache() {
        let mut cache = CompsysCache::memory().unwrap();

        let executables = vec![
            ("ls".to_string(), "/bin/ls".to_string()),
            ("cat".to_string(), "/bin/cat".to_string()),
            ("git".to_string(), "/usr/bin/git".to_string()),
        ];
        cache.set_executables_bulk(&executables).unwrap();

        assert!(cache.has_executables().unwrap());
        assert!(cache.has_executable("ls").unwrap());
        assert!(cache.has_executable("git").unwrap());
        assert!(!cache.has_executable("nonexistent").unwrap());

        assert_eq!(
            cache.get_executable_path("ls").unwrap(),
            Some("/bin/ls".to_string())
        );
        assert_eq!(cache.executables_count().unwrap(), 3);
    }

    #[test]
    fn test_executables_prefix_search() {
        let mut cache = CompsysCache::memory().unwrap();

        let executables = vec![
            ("git".to_string(), "/usr/bin/git".to_string()),
            ("gitk".to_string(), "/usr/bin/gitk".to_string()),
            ("grep".to_string(), "/bin/grep".to_string()),
            ("gzip".to_string(), "/bin/gzip".to_string()),
        ];
        cache.set_executables_bulk(&executables).unwrap();

        // FTS prefix search returns (name, path) tuples
        let git_cmds = cache.get_executables_prefix_fts("git").unwrap();
        assert_eq!(git_cmds.len(), 2);
        assert!(git_cmds.iter().any(|(name, _)| name == "git"));
        assert!(git_cmds.iter().any(|(name, _)| name == "gitk"));

        let g_cmds = cache.get_executables_prefix_fts("g").unwrap();
        assert_eq!(g_cmds.len(), 4);
    }

    #[test]
    fn test_named_dirs_cache() {
        let mut cache = CompsysCache::memory().unwrap();

        let dirs = vec![
            ("proj".to_string(), "/home/user/projects".to_string()),
            ("docs".to_string(), "/home/user/documents".to_string()),
        ];
        cache.set_named_dirs_bulk(&dirs).unwrap();

        assert!(cache.has_named_dirs().unwrap());

        let all = cache.get_named_dirs().unwrap();
        assert_eq!(all.len(), 2);

        let p_dirs = cache.get_named_dirs_prefix("p").unwrap();
        assert_eq!(p_dirs.len(), 1);
        assert_eq!(p_dirs[0].0, "proj");
    }

    #[test]
    fn test_shell_functions_cache() {
        let mut cache = CompsysCache::memory().unwrap();

        let functions = vec![
            ("myFunc".to_string(), "/home/user/.zshrc".to_string()),
            (
                "zpwrClearList".to_string(),
                "/home/user/.zpwr/autoload".to_string(),
            ),
            (
                "zpwrTop".to_string(),
                "/home/user/.zpwr/autoload".to_string(),
            ),
        ];
        cache.set_shell_functions_bulk(&functions).unwrap();

        assert!(cache.has_shell_functions().unwrap());
        assert_eq!(cache.shell_functions_count().unwrap(), 3);

        let zpwr = cache.get_shell_functions_prefix("zpwr").unwrap();
        assert_eq!(zpwr.len(), 2);
        // Results are tuples (name, source)
        assert!(zpwr.iter().any(|(name, _)| name == "zpwrClearList"));
        assert!(zpwr.iter().any(|(name, _)| name == "zpwrTop"));
    }

    #[test]
    fn test_metadata() {
        let cache = CompsysCache::memory().unwrap();

        cache.set_metadata("version", "1.0.0").unwrap();
        cache.set_metadata("build_time", "2026-04-22").unwrap();

        assert_eq!(
            cache.get_metadata("version").unwrap(),
            Some("1.0.0".to_string())
        );
        assert_eq!(
            cache.get_metadata("build_time").unwrap(),
            Some("2026-04-22".to_string())
        );
        assert_eq!(cache.get_metadata("nonexistent").unwrap(), None);
    }

    #[test]
    fn test_comps_keys() {
        let mut cache = CompsysCache::memory().unwrap();

        let comps = vec![
            ("git".to_string(), "_git".to_string()),
            ("docker".to_string(), "_docker".to_string()),
        ];
        cache.set_comps_bulk(&comps).unwrap();

        let keys = cache.comps_keys().unwrap();
        assert_eq!(keys.len(), 2);
        assert!(keys.contains(&"docker".to_string()));
        assert!(keys.contains(&"git".to_string()));
    }

    #[test]
    fn test_comps_prefix() {
        let mut cache = CompsysCache::memory().unwrap();

        let comps = vec![
            ("git".to_string(), "_git".to_string()),
            ("gitk".to_string(), "_gitk".to_string()),
            ("docker".to_string(), "_docker".to_string()),
        ];
        cache.set_comps_bulk(&comps).unwrap();

        let git_comps = cache.comps_prefix("git").unwrap();
        assert_eq!(git_comps.len(), 2);
    }

    #[test]
    fn test_zstyles_bulk() {
        let mut cache = CompsysCache::memory().unwrap();

        let styles = vec![
            (
                ":completion:*".to_string(),
                "menu".to_string(),
                vec!["select".to_string()],
                false,
            ),
            (
                ":completion:*".to_string(),
                "verbose".to_string(),
                vec!["yes".to_string()],
                false,
            ),
            (
                ":completion:*:descriptions".to_string(),
                "format".to_string(),
                vec!["%d".to_string()],
                false,
            ),
        ];
        cache.set_zstyles_bulk(&styles).unwrap();

        assert!(cache.has_zstyles().unwrap());
        assert_eq!(cache.zstyles_count().unwrap(), 3);
    }

    #[test]
    fn test_services() {
        let cache = CompsysCache::memory().unwrap();

        cache.set_service("git", "scm").unwrap();
        cache.set_service("hg", "scm").unwrap();

        assert_eq!(cache.get_service("git").unwrap(), Some("scm".to_string()));
        assert_eq!(cache.get_service("unknown").unwrap(), None);
    }

    #[test]
    fn test_cache_overwrite() {
        let cache = CompsysCache::memory().unwrap();

        cache.set_comp("git", "_git_old").unwrap();
        assert_eq!(cache.get_comp("git").unwrap(), Some("_git_old".to_string()));

        cache.set_comp("git", "_git_new").unwrap();
        assert_eq!(cache.get_comp("git").unwrap(), Some("_git_new".to_string()));
    }

    #[test]
    fn test_executable_names() {
        let mut cache = CompsysCache::memory().unwrap();

        let executables = vec![
            ("alpha".to_string(), "/bin/alpha".to_string()),
            ("beta".to_string(), "/bin/beta".to_string()),
            ("gamma".to_string(), "/bin/gamma".to_string()),
        ];
        cache.set_executables_bulk(&executables).unwrap();

        let names = cache.get_executable_names().unwrap();
        assert_eq!(names.len(), 3);
        // Returns a HashSet, so check contains
        assert!(names.contains("alpha"));
        assert!(names.contains("beta"));
        assert!(names.contains("gamma"));
    }
}