powdb-cli 0.11.0

Interactive REPL and CLI for PowDB — 3-10x faster than SQLite on aggregates
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
use powdb_query::executor::Engine;
use powdb_query::lexer::{split_statements, POWQL_KEYWORDS};
use powdb_query::result::QueryResult;
use powdb_server::protocol::Message;
use powdb_storage::types::Value;
use rustyline::completion::{Completer, Pair};
use rustyline::highlight::Highlighter;
use rustyline::hint::Hinter;
use rustyline::validate::Validator;
use rustyline::{Editor, Helper};
use std::borrow::Cow;
use std::io;
use std::path::{Path, PathBuf};
use std::time::Instant;
use tokio::io::{BufReader, BufWriter};
use tokio::net::TcpStream;
use tracing_subscriber::EnvFilter;

// ─── Tab completion helper ─────────────────────────────────────────────────

const CLI_COMMANDS: &[&str] = &["exec", "prepare"];
const DEFAULT_DB_NAME: &str = "default";
const META_COMMANDS: &[&str] = &[".exit", ".help", ".quit", ".schema", ".tables", ".timing"];

fn archive_wal_records_if_sync_enabled(
    data_dir: &Path,
    records: &[powdb_storage::wal::WalRecord],
) -> io::Result<()> {
    match powdb_sync::read_identity(data_dir) {
        Ok(identity) => powdb_sync::archive_wal_records_for_identity(data_dir, identity, records),
        Err(err) if err.kind() == io::ErrorKind::NotFound => Ok(()),
        Err(err) => Err(err),
    }
}

struct PowqlHelper;

impl Helper for PowqlHelper {}

impl Completer for PowqlHelper {
    type Candidate = Pair;

    fn complete(
        &self,
        line: &str,
        pos: usize,
        _ctx: &rustyline::Context<'_>,
    ) -> rustyline::Result<(usize, Vec<Pair>)> {
        let (start, word) = find_word_start(line, pos);
        let lower = word.to_lowercase();

        let mut matches: Vec<Pair> = Vec::new();

        if word.starts_with('.') {
            // Complete meta-commands
            for cmd in META_COMMANDS {
                if cmd.starts_with(&lower) {
                    matches.push(Pair {
                        display: cmd.to_string(),
                        replacement: cmd.to_string(),
                    });
                }
            }
        } else if start == 0 && !word.is_empty() {
            push_keyword_matches(
                POWQL_KEYWORDS.iter().chain(CLI_COMMANDS.iter()),
                &lower,
                first_char_uppercase(word),
                &mut matches,
            );
        } else if !word.is_empty() {
            push_keyword_matches(
                POWQL_KEYWORDS.iter(),
                &lower,
                first_char_uppercase(word),
                &mut matches,
            );
        }

        Ok((start, matches))
    }
}

fn first_char_uppercase(word: &str) -> bool {
    word.chars().next().is_some_and(|c| c.is_uppercase())
}

/// Push completion candidates for every keyword that prefix-matches `lower`,
/// preserving the case style the user typed (leading uppercase is echoed back).
fn push_keyword_matches<'a>(
    keywords: impl Iterator<Item = &'a &'a str>,
    lower: &str,
    uppercase_first: bool,
    matches: &mut Vec<Pair>,
) {
    for kw in keywords {
        if kw.starts_with(lower) {
            let replacement = if uppercase_first {
                let mut s = kw.to_string();
                s[..1].make_ascii_uppercase();
                s
            } else {
                kw.to_string()
            };
            matches.push(Pair {
                display: kw.to_string(),
                replacement,
            });
        }
    }
}

impl Hinter for PowqlHelper {
    type Hint = String;

    fn hint(&self, _line: &str, _pos: usize, _ctx: &rustyline::Context<'_>) -> Option<String> {
        None
    }
}

impl Highlighter for PowqlHelper {
    fn highlight_hint<'h>(&self, hint: &'h str) -> Cow<'h, str> {
        Cow::Borrowed(hint)
    }
}

impl Validator for PowqlHelper {}

fn find_word_start(line: &str, pos: usize) -> (usize, &str) {
    let bytes = &line.as_bytes()[..pos];
    let start = bytes
        .iter()
        .rposition(|&b| b == b' ' || b == b'\t')
        .map(|i| i + 1)
        .unwrap_or(0);
    (start, &line[start..pos])
}

fn history_path() -> PathBuf {
    std::env::var("HOME")
        .map(PathBuf::from)
        .unwrap_or_else(|_| PathBuf::from("."))
        .join(".powdb_history")
}

enum Action {
    /// Default behaviour: REPL or one-shot `--exec`.
    Default,
    /// Snapshot the embedded DB at `--data-dir` into `dest`.
    /// When `base` is set, write an incremental (differential) backup
    /// diffed against the full backup at that directory.
    Backup { dest: String, base: Option<String> },
    /// Rebuild a data dir from a backup. When `apply` is non-empty, treat
    /// `backup_dir` as a full base and apply the ordered increments on top.
    Restore {
        backup_dir: String,
        dest: String,
        apply: Vec<String>,
        sync_mode: powdb_backup::RestoreSyncMode,
    },
    /// Offline sync bootstrap: create this data dir's sync identity.
    SyncEnable,
    /// Restore a sync-enabled backup into a replica and publish its cursor.
    SyncBootstrap {
        backup_dir: String,
        replica_dir: String,
        replica_id: String,
    },
    /// Offline/admin: inspect primary-side sync cursor status.
    SyncStatus { replica_id: Option<String> },
    /// Offline user-admin: create a user in the data dir's UserStore.
    UserAdd { name: String },
    /// Offline user-admin: delete a user from the data dir's UserStore.
    UserDel { name: String },
    /// Offline user-admin: change a user's password in the data dir's UserStore.
    Passwd { name: String },
    /// Offline user-admin: list users (name + role) from the data dir's UserStore.
    Users,
    /// Offline admin: mark-and-sweep reclaim orphaned overflow pages. `table`
    /// is a table name, or "all" to sweep every table. Reports pages reclaimed.
    Sweep { table: String },
}

struct CliArgs {
    data_dir: String,
    remote: Option<String>,
    db: String,
    password: Option<String>,
    user: Option<String>,
    /// Role for the `useradd` subcommand (defaults to "readwrite").
    role: Option<String>,
    exec: Option<String>,
    action: Action,
}

fn set_restore_sync_mode(
    current: &mut powdb_backup::RestoreSyncMode,
    was_set: &mut bool,
    next: powdb_backup::RestoreSyncMode,
    flag: &str,
) {
    if *was_set && *current != next {
        eprintln!("Error: conflicting restore sync identity mode flag: {flag}");
        std::process::exit(2);
    }
    *current = next;
    *was_set = true;
}

fn restore_sync_mode_for_flag(flag: &str) -> Option<powdb_backup::RestoreSyncMode> {
    match flag {
        "--sync-strip" => Some(powdb_backup::RestoreSyncMode::StripSyncIdentity),
        "--sync-preserve" => Some(powdb_backup::RestoreSyncMode::PreserveSyncIdentity),
        "--sync-fork" => Some(powdb_backup::RestoreSyncMode::ForkWithNewSyncIdentity),
        _ => None,
    }
}

fn parse_args() -> CliArgs {
    let mut data_dir = "./powdb_data".to_string();
    let mut remote: Option<String> = None;
    let mut db: String = DEFAULT_DB_NAME.to_string();
    let mut password: Option<String> = std::env::var("POWDB_PASSWORD")
        .ok()
        .filter(|s| !s.is_empty());
    let mut user: Option<String> = None;
    let mut role: Option<String> = None;
    let mut exec: Option<String> = None;
    let mut exec_file: Option<String> = None;
    let mut action = Action::Default;
    // Accumulators for backup/restore modifier flags, which may appear after
    // the subcommand and its positionals.
    let mut backup_base: Option<String> = None;
    let mut restore_apply: Vec<String> = Vec::new();
    let mut restore_sync_mode = powdb_backup::RestoreSyncMode::StripSyncIdentity;
    let mut restore_sync_mode_was_set = false;

    // `std::env::args()` panics on a non-UTF-8 argument (it unwraps the
    // OsString→String conversion internally). Use `args_os` and reject
    // invalid UTF-8 with a clean error + exit code instead of a panic.
    let argv: Vec<String> = match std::env::args_os()
        .map(|a| a.into_string())
        .collect::<Result<Vec<_>, _>>()
    {
        Ok(v) => v,
        Err(bad) => {
            eprintln!(
                "Error: argument is not valid UTF-8: {}",
                bad.to_string_lossy()
            );
            std::process::exit(2);
        }
    };
    let mut i = 1;
    let mut saw_positional = false;
    while i < argv.len() {
        match argv[i].as_str() {
            "--exec" | "-c" => {
                i += 1;
                if i >= argv.len() {
                    eprintln!("Error: --exec requires a PowQL query");
                    std::process::exit(2);
                }
                exec = Some(argv[i].clone());
            }
            "--exec-file" => {
                i += 1;
                if i >= argv.len() {
                    eprintln!("Error: --exec-file requires a path ('-' for stdin)");
                    std::process::exit(2);
                }
                exec_file = Some(argv[i].clone());
            }
            "--remote" | "-r" => {
                i += 1;
                if i >= argv.len() {
                    eprintln!("Error: --remote requires host:port");
                    std::process::exit(2);
                }
                remote = Some(argv[i].clone());
            }
            "--db" => {
                i += 1;
                if i >= argv.len() {
                    eprintln!("Error: --db requires a name");
                    std::process::exit(2);
                }
                db = argv[i].clone();
            }
            "--password" => {
                i += 1;
                if i >= argv.len() {
                    eprintln!("Error: --password requires a value");
                    std::process::exit(2);
                }
                password = Some(argv[i].clone());
            }
            "--user" | "-u" => {
                i += 1;
                if i >= argv.len() {
                    eprintln!("Error: --user requires a name");
                    std::process::exit(2);
                }
                user = Some(argv[i].clone());
            }
            "--role" => {
                i += 1;
                if i >= argv.len() {
                    eprintln!("Error: --role requires a value");
                    std::process::exit(2);
                }
                role = Some(argv[i].clone());
            }
            "--data-dir" | "-d" => {
                i += 1;
                if i >= argv.len() {
                    eprintln!("Error: --data-dir requires a path");
                    std::process::exit(2);
                }
                data_dir = argv[i].clone();
            }
            "--version" | "-V" => {
                println!("powdb-cli {}", env!("CARGO_PKG_VERSION"));
                std::process::exit(0);
            }
            "--help" | "-h" => {
                println!("powdb-cli — PowQL interactive shell");
                println!();
                println!("USAGE:");
                println!("    powdb-cli [OPTIONS] [DATA_DIR]");
                println!("    powdb-cli --data-dir <DIR> backup <DEST_DIR> [--base <FULL_DIR>]");
                println!(
                    "    powdb-cli restore <BACKUP_DIR> <DEST_DATA_DIR> [--apply <INC_DIR>]... [--sync-strip|--sync-preserve|--sync-fork]"
                );
                println!(
                    "    powdb-cli --data-dir <PRIMARY_DIR> sync-bootstrap <BACKUP_DIR> <REPLICA_DIR> <REPLICA_ID>"
                );
                println!("    powdb-cli --data-dir <PRIMARY_DIR> sync-status [REPLICA_ID]");
                println!();
                println!("OPTIONS:");
                println!("    -c, --exec <QUERY>         Run one or more `;`-separated PowQL statements and exit");
                println!("        --exec-file <PATH>     Run PowQL read from a file ('-' for stdin) and exit");
                println!("    -r, --remote <HOST:PORT>   Connect to a remote server over TCP");
                println!("        --db <NAME>            Database name (default: default)");
                println!("        --password <PW>        Password for remote auth");
                println!("    -u, --user <NAME>          Username for multi-user remote auth");
                println!(
                    "    -d, --data-dir <PATH>      Embedded data dir (default: ./powdb_data)"
                );
                println!("    -h, --help                 Print this message");
                println!("    -V, --version              Print version and exit");
                println!();
                println!("MODES:");
                println!("    Embedded REPL:       powdb-cli ./mydata");
                println!(
                    "    Remote REPL:         powdb-cli --remote 127.0.0.1:5433 --password secret"
                );
                println!("    One-shot:            powdb-cli --exec 'count(User)'");
                println!("    Load a file:         powdb-cli --data-dir ./sandbox --exec-file dump.powql");
                println!("    One-shot (remote):   powdb-cli -r 127.0.0.1:5433 -c 'User filter .age > 25 limit 5'");
                println!();
                println!("SUBCOMMANDS:");
                println!("    backup <DEST_DIR> [--base <FULL_DIR>]");
                println!("        Snapshot --data-dir into DEST_DIR. With --base, write an");
                println!("        incremental (differential) backup of only the 4 KB pages that");
                println!("        changed since the full backup at FULL_DIR.");
                println!("    restore <BKP> <DEST_DIR> [--apply <INC_DIR>]...");
                println!("        Rebuild a data dir from a backup. Pass --apply once per");
                println!("        increment (in order) to chain-restore a full base plus");
                println!("        incrementals for coarse point-in-time restore.");
                println!("        Sync identity modes for sync-enabled backups:");
                println!("          --sync-strip     Default. Restore data without sync identity.");
                println!(
                    "          --sync-preserve  Disaster recovery: keep source sync identity."
                );
                println!("          --sync-fork      Clone/fork: mint a fresh sync identity.");
                println!("    sync-enable");
                println!("        Offline/admin: create sync identity and checkpoint retained WAL");
                println!("        for --data-dir so future backups can bootstrap replicas.");
                println!("    sync-bootstrap <BKP> <REPLICA_DIR> <REPLICA_ID>");
                println!("        Offline/admin: restore a sync-enabled full backup into");
                println!("        REPLICA_DIR and publish REPLICA_ID's primary-side cursor.");
                println!("    sync-status [REPLICA_ID]");
                println!("        Offline/admin: show primary-side cursor, lag, and repair");
                println!("        action for one replica or every registered replica.");
                println!("        Uses sync-aware open and may archive/checkpoint pending WAL.");
                println!();
                println!("USER ADMIN (offline — operate on --data-dir's user store):");
                println!("    useradd <NAME> --role <ROLE> --password <PW>");
                println!("        Create a user. --role defaults to readwrite (admin|readwrite|");
                println!("        readonly). Password from --password or POWDB_NEW_PASSWORD.");
                println!("    userdel <NAME>");
                println!("        Delete a user.");
                println!("    passwd <NAME> --password <PW>");
                println!("        Change a user's password (or via POWDB_NEW_PASSWORD).");
                println!("    users");
                println!("        List users (name + role).");
                println!("    sweep <TABLE|all>");
                println!("        Reclaim orphaned overflow pages for a table (or all).");
                std::process::exit(0);
            }
            "backup" => {
                i += 1;
                if i >= argv.len() {
                    eprintln!("Error: backup requires a destination dir");
                    std::process::exit(2);
                }
                action = Action::Backup {
                    dest: argv[i].clone(),
                    base: None,
                };
            }
            "--base" => {
                i += 1;
                if i >= argv.len() {
                    eprintln!("Error: --base requires a full backup dir");
                    std::process::exit(2);
                }
                backup_base = Some(argv[i].clone());
            }
            "--apply" => {
                i += 1;
                if i >= argv.len() {
                    eprintln!("Error: --apply requires an increment dir");
                    std::process::exit(2);
                }
                restore_apply.push(argv[i].clone());
            }
            "--sync-strip" => {
                set_restore_sync_mode(
                    &mut restore_sync_mode,
                    &mut restore_sync_mode_was_set,
                    powdb_backup::RestoreSyncMode::StripSyncIdentity,
                    "--sync-strip",
                );
            }
            "--sync-preserve" => {
                set_restore_sync_mode(
                    &mut restore_sync_mode,
                    &mut restore_sync_mode_was_set,
                    powdb_backup::RestoreSyncMode::PreserveSyncIdentity,
                    "--sync-preserve",
                );
            }
            "--sync-fork" => {
                set_restore_sync_mode(
                    &mut restore_sync_mode,
                    &mut restore_sync_mode_was_set,
                    powdb_backup::RestoreSyncMode::ForkWithNewSyncIdentity,
                    "--sync-fork",
                );
            }
            "restore" => {
                i += 1;
                let mut backup_dir: Option<String> = None;
                let mut dest: Option<String> = None;
                while i < argv.len() {
                    let arg = argv[i].as_str();
                    if arg == "--apply" {
                        i += 1;
                        if i >= argv.len() {
                            eprintln!("Error: --apply requires an increment dir");
                            std::process::exit(2);
                        }
                        restore_apply.push(argv[i].clone());
                    } else if let Some(next) = restore_sync_mode_for_flag(arg) {
                        set_restore_sync_mode(
                            &mut restore_sync_mode,
                            &mut restore_sync_mode_was_set,
                            next,
                            arg,
                        );
                    } else if arg.starts_with('-') {
                        eprintln!("Error: unknown restore argument: {arg}");
                        eprintln!("try --help");
                        std::process::exit(2);
                    } else if backup_dir.is_none() {
                        backup_dir = Some(argv[i].clone());
                    } else if dest.is_none() {
                        dest = Some(argv[i].clone());
                    } else {
                        eprintln!("Error: unexpected restore argument: {arg}");
                        eprintln!("try --help");
                        std::process::exit(2);
                    }
                    i += 1;
                }
                let Some(backup_dir) = backup_dir else {
                    eprintln!("Error: restore requires a backup dir and a destination data dir");
                    std::process::exit(2);
                };
                let Some(dest) = dest else {
                    eprintln!("Error: restore requires a destination data dir");
                    std::process::exit(2);
                };
                action = Action::Restore {
                    backup_dir,
                    dest,
                    apply: Vec::new(),
                    sync_mode: powdb_backup::RestoreSyncMode::StripSyncIdentity,
                };
            }
            "sync-enable" => {
                action = Action::SyncEnable;
            }
            "sync-bootstrap" => {
                i += 1;
                if i >= argv.len() {
                    eprintln!("Error: sync-bootstrap requires a backup dir");
                    std::process::exit(2);
                }
                let backup_dir = argv[i].clone();
                i += 1;
                if i >= argv.len() {
                    eprintln!("Error: sync-bootstrap requires a replica data dir");
                    std::process::exit(2);
                }
                let replica_dir = argv[i].clone();
                i += 1;
                if i >= argv.len() {
                    eprintln!("Error: sync-bootstrap requires a replica id");
                    std::process::exit(2);
                }
                action = Action::SyncBootstrap {
                    backup_dir,
                    replica_dir,
                    replica_id: argv[i].clone(),
                };
            }
            "sync-status" => {
                i += 1;
                let mut replica_id: Option<String> = None;
                while i < argv.len() {
                    let arg = argv[i].as_str();
                    if arg.starts_with('-') {
                        eprintln!("Error: unknown sync-status argument: {arg}");
                        eprintln!("try --help");
                        std::process::exit(2);
                    } else if replica_id.is_none() {
                        replica_id = Some(argv[i].clone());
                    } else {
                        eprintln!("Error: unexpected sync-status argument: {arg}");
                        eprintln!("try --help");
                        std::process::exit(2);
                    }
                    i += 1;
                }
                action = Action::SyncStatus { replica_id };
            }
            "useradd" => {
                i += 1;
                if i >= argv.len() {
                    eprintln!("Error: useradd requires a user name");
                    std::process::exit(2);
                }
                action = Action::UserAdd {
                    name: argv[i].clone(),
                };
            }
            "userdel" => {
                i += 1;
                if i >= argv.len() {
                    eprintln!("Error: userdel requires a user name");
                    std::process::exit(2);
                }
                action = Action::UserDel {
                    name: argv[i].clone(),
                };
            }
            "passwd" => {
                i += 1;
                if i >= argv.len() {
                    eprintln!("Error: passwd requires a user name");
                    std::process::exit(2);
                }
                action = Action::Passwd {
                    name: argv[i].clone(),
                };
            }
            "users" => {
                action = Action::Users;
            }
            "sweep" => {
                i += 1;
                if i >= argv.len() {
                    eprintln!("Error: sweep requires a table name or \"all\"");
                    std::process::exit(2);
                }
                action = Action::Sweep {
                    table: argv[i].clone(),
                };
            }
            other if !other.starts_with('-') && !saw_positional => {
                data_dir = other.to_string();
                saw_positional = true;
            }
            other => {
                eprintln!("Error: unknown argument: {other}");
                eprintln!("try --help");
                std::process::exit(2);
            }
        }
        i += 1;
    }

    // Fold the modifier flags into their subcommand actions.
    match &mut action {
        Action::Backup { base, .. } => {
            if !restore_apply.is_empty() {
                eprintln!("Error: --apply is only valid with the `restore` subcommand");
                std::process::exit(2);
            }
            if restore_sync_mode_was_set {
                eprintln!("Error: --sync-strip, --sync-preserve, and --sync-fork are only valid with the `restore` subcommand");
                std::process::exit(2);
            }
            *base = backup_base;
        }
        Action::Restore {
            apply, sync_mode, ..
        } => {
            if backup_base.is_some() {
                eprintln!("Error: --base is only valid with the `backup` subcommand");
                std::process::exit(2);
            }
            *apply = restore_apply;
            *sync_mode = restore_sync_mode;
        }
        _ => {
            if backup_base.is_some() {
                eprintln!("Error: --base is only valid with the `backup` subcommand");
                std::process::exit(2);
            }
            if !restore_apply.is_empty() {
                eprintln!("Error: --apply is only valid with the `restore` subcommand");
                std::process::exit(2);
            }
            if restore_sync_mode_was_set {
                eprintln!("Error: --sync-strip, --sync-preserve, and --sync-fork are only valid with the `restore` subcommand");
                std::process::exit(2);
            }
        }
    }

    // `--exec-file <PATH>` reads a whole PowQL file (or stdin for `-`) and
    // feeds it through the same one-shot path as `--exec`, sidestepping the
    // ARG_MAX ceiling on large loads. The two flags are mutually exclusive.
    if let Some(path) = exec_file {
        if exec.is_some() {
            eprintln!("Error: --exec and --exec-file are mutually exclusive");
            std::process::exit(2);
        }
        let contents = if path == "-" {
            let mut buf = String::new();
            if let Err(e) = io::Read::read_to_string(&mut io::stdin(), &mut buf) {
                eprintln!("Error: failed to read PowQL from stdin: {e}");
                std::process::exit(1);
            }
            buf
        } else {
            match std::fs::read_to_string(&path) {
                Ok(c) => c,
                Err(e) => {
                    eprintln!("Error: failed to read {path}: {e}");
                    std::process::exit(1);
                }
            }
        };
        exec = Some(contents);
    }

    CliArgs {
        data_dir,
        remote,
        db,
        password,
        user,
        role,
        exec,
        action,
    }
}

fn main() {
    // Tracing for the CLI (mostly off by default; users can set RUST_LOG=debug).
    tracing_subscriber::fmt()
        .with_env_filter(
            EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("warn")),
        )
        .with_target(false)
        .init();

    let args = parse_args();

    match &args.action {
        Action::Backup { dest, base } => {
            std::process::exit(run_backup(&args.data_dir, dest, base.as_deref()));
        }
        Action::Restore {
            backup_dir,
            dest,
            apply,
            sync_mode,
        } => {
            std::process::exit(run_restore(backup_dir, dest, apply, *sync_mode));
        }
        Action::SyncEnable => {
            std::process::exit(run_sync_enable(&args.data_dir));
        }
        Action::SyncBootstrap {
            backup_dir,
            replica_dir,
            replica_id,
        } => {
            std::process::exit(run_sync_bootstrap(
                &args.data_dir,
                backup_dir,
                replica_dir,
                replica_id,
            ));
        }
        Action::SyncStatus { replica_id } => {
            std::process::exit(run_sync_status(&args.data_dir, replica_id.as_deref()));
        }
        Action::UserAdd { name } => {
            std::process::exit(run_useradd(
                &args.data_dir,
                name,
                args.role.as_deref(),
                args.password.as_deref(),
            ));
        }
        Action::UserDel { name } => {
            std::process::exit(run_userdel(&args.data_dir, name));
        }
        Action::Passwd { name } => {
            std::process::exit(run_passwd(&args.data_dir, name, args.password.as_deref()));
        }
        Action::Users => {
            std::process::exit(run_users(&args.data_dir));
        }
        Action::Sweep { table } => {
            std::process::exit(run_sweep(&args.data_dir, table));
        }
        Action::Default => {}
    }

    if let Some(remote_addr) = &args.remote {
        let rt = match tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
        {
            Ok(rt) => rt,
            Err(e) => {
                eprintln!("Error: failed to build tokio runtime: {e}");
                std::process::exit(1);
            }
        };
        if let Some(query) = args.exec.clone() {
            let code = rt.block_on(exec_remote(
                remote_addr.clone(),
                args.db.clone(),
                args.password.clone(),
                args.user.clone(),
                query,
            ));
            std::process::exit(code);
        }
        rt.block_on(run_remote(
            remote_addr.clone(),
            args.db.clone(),
            args.password.clone(),
            args.user.clone(),
        ));
    } else if let Some(query) = args.exec {
        std::process::exit(exec_embedded(&args.data_dir, &query));
    } else {
        run_embedded(&args.data_dir);
    }
}

// ─── Backup / restore ───────────────────────────────────────────────────────

fn run_backup(data_dir: &str, dest: &str, base: Option<&str>) -> i32 {
    let mut catalog = match powdb_sync::open_preserving_retained_segments(Path::new(data_dir)) {
        Ok(c) => c,
        Err(e) => {
            eprintln!("Error: failed to open data dir {data_dir}: {e}");
            return 1;
        }
    };

    match base {
        // ── Incremental (differential) backup against a full base ──────────
        Some(full_dir) => {
            let base_manifest = match powdb_backup::BackupManifest::read(Path::new(full_dir)) {
                Ok(m) => m,
                Err(e) => {
                    eprintln!("Error: failed to read base backup {full_dir}: {e}");
                    return 1;
                }
            };
            let base_lsn = base_manifest.source_lsn;
            match powdb_backup::incremental_backup(&mut catalog, &base_manifest, Path::new(dest)) {
                Ok(m) => {
                    use powdb_backup::ChangedFile;
                    let mut delta_pages: usize = 0;
                    let mut whole_files: usize = 0;
                    for cf in &m.changed {
                        match cf {
                            ChangedFile::Pages { page_indices, .. } => {
                                delta_pages += page_indices.len();
                            }
                            ChangedFile::Whole { .. } => {
                                whole_files += 1;
                            }
                        }
                    }
                    println!(
                        "incremental backup: {} changed files ({whole_files} whole, {} paged), \
                         {delta_pages} delta pages, base lsn {base_lsn} -> lsn {} -> {dest}",
                        m.changed.len(),
                        m.changed.len() - whole_files,
                        m.source_lsn
                    );
                    0
                }
                Err(e) => {
                    eprintln!("Error: incremental backup failed: {e}");
                    1
                }
            }
        }
        // ── Full backup ────────────────────────────────────────────────────
        None => match powdb_backup::full_backup(&mut catalog, Path::new(dest)) {
            Ok(m) => {
                let total_bytes: u64 = m.files.iter().map(|f| f.len).sum();
                println!(
                    "backed up {} files ({total_bytes} bytes) at lsn {} -> {dest}",
                    m.files.len(),
                    m.source_lsn
                );
                0
            }
            Err(e) => {
                eprintln!("Error: backup failed: {e}");
                1
            }
        },
    }
}

fn run_restore(
    backup_dir: &str,
    dest: &str,
    apply: &[String],
    sync_mode: powdb_backup::RestoreSyncMode,
) -> i32 {
    if apply.is_empty() {
        // Full restore.
        return match powdb_backup::restore_with_sync_mode(
            Path::new(backup_dir),
            Path::new(dest),
            sync_mode,
        ) {
            Ok(()) => {
                println!("restored backup {backup_dir} -> {dest}");
                0
            }
            Err(e) => {
                eprintln!("Error: restore failed: {e}");
                1
            }
        };
    }

    // Chain restore: full base + ordered increments.
    let increments: Vec<&Path> = apply.iter().map(|s| Path::new(s.as_str())).collect();
    match powdb_backup::restore_chain_with_sync_mode(
        Path::new(backup_dir),
        &increments,
        Path::new(dest),
        sync_mode,
    ) {
        Ok(()) => {
            println!(
                "restored backup {backup_dir} + {} increment(s) -> {dest}",
                apply.len()
            );
            for inc in apply {
                println!("  applied {inc}");
            }
            0
        }
        Err(e) => {
            eprintln!("Error: chain restore failed: {e}");
            1
        }
    }
}

fn run_sync_enable(data_dir: &str) -> i32 {
    let mut catalog = match powdb_sync::open_preserving_retained_segments(Path::new(data_dir)) {
        Ok(catalog) => catalog,
        Err(e) => {
            eprintln!("Error: failed to open data dir {data_dir}: {e}");
            return 1;
        }
    };
    if let Err(e) = powdb_sync::checkpoint_with_retained_segments(&mut catalog) {
        eprintln!("Error: sync enable failed: {e}");
        return 1;
    }
    match powdb_sync::read_identity_snapshot(Path::new(data_dir)) {
        Ok(Some(snapshot)) => {
            println!(
                "sync enabled: database {} generation {} at lsn {}",
                snapshot.database_id,
                snapshot.primary_generation,
                catalog.max_lsn()
            );
            0
        }
        Ok(None) => {
            eprintln!("Error: sync enable did not create identity metadata");
            1
        }
        Err(e) => {
            eprintln!("Error: failed to read sync identity: {e}");
            1
        }
    }
}

fn run_sync_bootstrap(
    primary_dir: &str,
    backup_dir: &str,
    replica_dir: &str,
    replica_id: &str,
) -> i32 {
    let mut primary = match powdb_sync::open_preserving_retained_segments(Path::new(primary_dir)) {
        Ok(catalog) => catalog,
        Err(e) => {
            eprintln!("Error: failed to open primary data dir {primary_dir}: {e}");
            return 1;
        }
    };
    match powdb_backup::bootstrap_replica_from_full_backup(
        &mut primary,
        Path::new(backup_dir),
        Path::new(replica_dir),
        replica_id,
    ) {
        Ok(summary) => {
            println!(
                "sync replica bootstrapped: {} snapshot lsn {} remote lsn {} retained units {} -> {}",
                summary.replica_id,
                summary.snapshot_lsn,
                summary.remote_lsn,
                summary.retained_units_available,
                replica_dir
            );
            0
        }
        Err(e) => {
            eprintln!("Error: sync bootstrap failed: {e}");
            1
        }
    }
}

fn format_optional_u64(value: Option<u64>) -> String {
    value.map_or_else(|| "null".to_string(), |n| n.to_string())
}

fn format_sync_repair_action(action: powdb_sync::SyncRepairAction) -> &'static str {
    match action {
        powdb_sync::SyncRepairAction::None => "none",
        powdb_sync::SyncRepairAction::Pull => "pull",
        powdb_sync::SyncRepairAction::AwaitArchive => "awaitArchive",
        powdb_sync::SyncRepairAction::Rebootstrap => "rebootstrap",
    }
}

fn print_replica_sync_status(status: &powdb_sync::ReplicaSyncStatus) {
    println!("replica {}", status.replica_id);
    println!("  active: {}", status.active);
    println!(
        "  lastAppliedLsn: {}",
        format_optional_u64(status.last_applied_lsn)
    );
    println!("  remoteLsn: {}", status.remote_lsn);
    println!(
        "  servableLsn: {}",
        format_optional_u64(status.servable_lsn)
    );
    println!(
        "  unarchivedLsn: {}",
        format_optional_u64(status.unarchived_lsn)
    );
    println!("  lagLsn: {}", format_optional_u64(status.lag_lsn));
    println!("  lagBytes: {}", format_optional_u64(status.lag_bytes));
    println!("  lagMs: {}", format_optional_u64(status.lag_ms));
    println!("  stale: {}", status.stale);
    println!(
        "  repairAction: {}",
        format_sync_repair_action(status.repair_action)
    );
    if let Some(err) = &status.last_sync_error {
        println!("  lastSyncError: {err}");
    }
}

fn run_sync_status(data_dir: &str, replica_id: Option<&str>) -> i32 {
    let catalog = match powdb_sync::open_preserving_retained_segments(Path::new(data_dir)) {
        Ok(catalog) => catalog,
        Err(e) => {
            eprintln!("Error: failed to open data dir {data_dir}: {e}");
            return 1;
        }
    };
    let remote_lsn = catalog.max_lsn();

    let identity = match powdb_sync::read_identity_snapshot_if_exists(Path::new(data_dir)) {
        Ok(Some(identity)) => identity,
        Ok(None) => {
            eprintln!("Error: sync-status requires a sync-enabled data dir; run sync-enable first");
            return 1;
        }
        Err(e) => {
            eprintln!("Error: failed to read sync identity: {e}");
            return 1;
        }
    };

    println!(
        "sync status: database {} generation {} remoteLsn {}",
        identity.database_id, identity.primary_generation, remote_lsn
    );

    if let Some(replica_id) = replica_id {
        match powdb_sync::replica_sync_status(Path::new(data_dir), replica_id, remote_lsn) {
            Ok(status) => {
                print_replica_sync_status(&status);
                0
            }
            Err(e) => {
                eprintln!("Error: failed to read sync status for {replica_id}: {e}");
                1
            }
        }
    } else {
        let mut cursors = match powdb_sync::read_replica_cursors(Path::new(data_dir)) {
            Ok(cursors) => cursors,
            Err(e) => {
                eprintln!("Error: failed to read replica cursors: {e}");
                return 1;
            }
        };
        cursors.sort_by(|a, b| a.replica_id.cmp(&b.replica_id));
        println!("replicas: {}", cursors.len());
        for cursor in cursors {
            match powdb_sync::replica_sync_status(
                Path::new(data_dir),
                &cursor.replica_id,
                remote_lsn,
            ) {
                Ok(status) => print_replica_sync_status(&status),
                Err(e) => {
                    eprintln!(
                        "Error: failed to read sync status for {}: {e}",
                        cursor.replica_id
                    );
                    return 1;
                }
            }
        }
        0
    }
}

// ─── User administration (offline / embedded) ───────────────────────────────

/// Resolve a new-user/new-password value from `--password` or, failing that,
/// the `POWDB_NEW_PASSWORD` env var. Returns `None` when neither is set.
fn resolve_new_password(flag: Option<&str>) -> Option<String> {
    flag.map(|s| s.to_string()).or_else(|| {
        std::env::var("POWDB_NEW_PASSWORD")
            .ok()
            .filter(|s| !s.is_empty())
    })
}

fn run_useradd(data_dir: &str, name: &str, role: Option<&str>, password: Option<&str>) -> i32 {
    let role = role.unwrap_or("readwrite");
    let Some(pw) = resolve_new_password(password) else {
        eprintln!(
            "Error: a password is required \u{2014} pass --password <PW> or set POWDB_NEW_PASSWORD"
        );
        return 2;
    };
    let dir = Path::new(data_dir);
    let mut store = match powdb_auth::UserStore::load(dir) {
        Ok(s) => s,
        Err(e) => {
            eprintln!("Error: failed to load user store from {data_dir}: {e}");
            return 1;
        }
    };
    if let Err(e) = store.create_user(name, &pw, role) {
        eprintln!("Error: {e}");
        return 1;
    }
    if let Err(e) = store.save(dir) {
        eprintln!("Error: failed to save user store: {e}");
        return 1;
    }
    println!("user '{name}' created (role {role})");
    0
}

fn run_userdel(data_dir: &str, name: &str) -> i32 {
    let dir = Path::new(data_dir);
    let mut store = match powdb_auth::UserStore::load(dir) {
        Ok(s) => s,
        Err(e) => {
            eprintln!("Error: failed to load user store from {data_dir}: {e}");
            return 1;
        }
    };
    if let Err(e) = store.delete_user(name) {
        eprintln!("Error: {e}");
        return 1;
    }
    if let Err(e) = store.save(dir) {
        eprintln!("Error: failed to save user store: {e}");
        return 1;
    }
    println!("user '{name}' deleted");
    0
}

fn run_passwd(data_dir: &str, name: &str, password: Option<&str>) -> i32 {
    let Some(pw) = resolve_new_password(password) else {
        eprintln!(
            "Error: a password is required \u{2014} pass --password <PW> or set POWDB_NEW_PASSWORD"
        );
        return 2;
    };
    let dir = Path::new(data_dir);
    let mut store = match powdb_auth::UserStore::load(dir) {
        Ok(s) => s,
        Err(e) => {
            eprintln!("Error: failed to load user store from {data_dir}: {e}");
            return 1;
        }
    };
    if let Err(e) = store.set_password(name, &pw) {
        eprintln!("Error: {e}");
        return 1;
    }
    if let Err(e) = store.save(dir) {
        eprintln!("Error: failed to save user store: {e}");
        return 1;
    }
    println!("password updated for user '{name}'");
    0
}

fn run_users(data_dir: &str) -> i32 {
    let dir = Path::new(data_dir);
    let store = match powdb_auth::UserStore::load(dir) {
        Ok(s) => s,
        Err(e) => {
            eprintln!("Error: failed to load user store from {data_dir}: {e}");
            return 1;
        }
    };
    let users = store.list_users();
    if users.is_empty() {
        println!("(no users \u{2014} shared-password mode)");
        return 0;
    }
    // Simple table: name + role. Never print password hashes.
    let name_w = users.iter().map(|(n, _)| n.len()).max().unwrap_or(4).max(4);
    let role_w = users.iter().map(|(_, r)| r.len()).max().unwrap_or(4).max(4);
    println!(" {:<name_w$} | {:<role_w$} ", "Name", "Role");
    println!("-{}-+-{}-", "-".repeat(name_w), "-".repeat(role_w));
    for (n, r) in &users {
        println!(" {n:<name_w$} | {r:<role_w$} ");
    }
    println!(
        "({} user{})",
        users.len(),
        if users.len() == 1 { "" } else { "s" }
    );
    0
}

/// Offline overflow reclamation. Opens the catalog at `data_dir` directly
/// (like the other offline admin commands) and mark-and-sweeps orphaned
/// overflow-chain pages for one table, or all tables when `table == "all"`.
fn run_sweep(data_dir: &str, table: &str) -> i32 {
    let dir = Path::new(data_dir);
    let mut cat = match powdb_storage::catalog::Catalog::open(dir) {
        Ok(c) => c,
        Err(e) => {
            eprintln!("Error: failed to open catalog at {data_dir}: {e}");
            return 1;
        }
    };
    let result = if table == "all" {
        cat.sweep_all()
    } else {
        cat.sweep(table)
    };
    match result {
        Ok(reclaimed) => {
            let scope = if table == "all" {
                "all tables".to_string()
            } else {
                format!("table '{table}'")
            };
            println!("sweep {scope}: reclaimed {reclaimed} overflow page(s)");
            // Checkpoint so the reclamation (and its OverflowFree record) is
            // durable and the WAL is truncated cleanly.
            if let Err(e) = cat.checkpoint() {
                eprintln!("Warning: checkpoint after sweep failed: {e}");
            }
            0
        }
        Err(e) => {
            eprintln!("Error: sweep failed: {e}");
            1
        }
    }
}

// ─── One-shot execution (embedded) ──────────────────────────────────────────

fn exec_embedded(data_dir: &str, query: &str) -> i32 {
    let mut engine = match Engine::new_with_wal_archive(
        Path::new(data_dir),
        archive_wal_records_if_sync_enabled,
    ) {
        Ok(e) => e,
        Err(e) => {
            eprintln!("Error: failed to initialize engine: {e}");
            return 1;
        }
    };
    // Statement-aware splitting (#150): a `;` inside a string literal or a
    // `#` comment is not a boundary, so text-heavy rows load intact.
    let statements = split_statements(query);
    for stmt in &statements {
        if stmt.starts_with('.') {
            let cmd = stmt.split_whitespace().next().unwrap_or(stmt);
            eprintln!(
                "Error: '{}' is a REPL-only command \u{2014} start the interactive REPL without -c to use it",
                cmd
            );
            return 1;
        }
        match engine.execute_powql(stmt) {
            Ok(result) => {
                print_local_result(&result);
            }
            Err(e) => {
                eprintln!("Error: {e}");
                return 1;
            }
        }
    }
    0
}

// ─── One-shot execution (remote) ────────────────────────────────────────────

async fn exec_remote(
    addr: String,
    db: String,
    password: Option<String>,
    username: Option<String>,
    query: String,
) -> i32 {
    let stream = match TcpStream::connect(&addr).await {
        Ok(s) => s,
        Err(e) => {
            eprintln!("Error: connection failed: {e}");
            return 1;
        }
    };
    let (reader, writer) = stream.into_split();
    let mut reader = BufReader::new(reader);
    let mut writer = BufWriter::new(writer);

    let connect = Message::Connect {
        db_name: db,
        password: password.map(Into::into),
        username,
    };
    if connect.write_to(&mut writer).await.is_err()
        || tokio::io::AsyncWriteExt::flush(&mut writer).await.is_err()
    {
        eprintln!("Error: failed to send CONNECT");
        return 1;
    }
    match Message::read_from(&mut reader).await {
        Ok(Some(Message::ConnectOk { .. })) => {}
        Ok(Some(Message::Error { message })) => {
            eprintln!("Error: server rejected connection: {message}");
            return 1;
        }
        _ => {
            eprintln!("Error: handshake failed");
            return 1;
        }
    }

    // The wire protocol carries one statement per `Query` message, so split
    // client-side (#150) and send each in turn, stopping on the first error.
    let mut code = 0;
    for stmt in split_statements(&query) {
        if stmt.starts_with('.') {
            let cmd = stmt.split_whitespace().next().unwrap_or(stmt);
            eprintln!(
                "Error: '{}' is a REPL-only command \u{2014} start the interactive REPL without -c to use it",
                cmd
            );
            code = 1;
            break;
        }

        let q = Message::Query {
            query: stmt.to_string(),
        };
        if q.write_to(&mut writer).await.is_err()
            || tokio::io::AsyncWriteExt::flush(&mut writer).await.is_err()
        {
            eprintln!("Error: write failed");
            code = 1;
            break;
        }

        match Message::read_from(&mut reader).await {
            Ok(Some(msg)) => {
                let is_error = matches!(msg, Message::Error { .. });
                print_remote_result(&msg);
                if is_error {
                    code = 1;
                    break;
                }
            }
            Ok(None) => {
                eprintln!("Error: server closed connection");
                code = 1;
                break;
            }
            Err(e) => {
                eprintln!("Error: read failed: {e}");
                code = 1;
                break;
            }
        }
    }

    let _ = Message::Disconnect.write_to(&mut writer).await;
    let _ = tokio::io::AsyncWriteExt::flush(&mut writer).await;
    code
}

// ─── Multi-line input ───────────────────────────────────────────────────────

/// True when `buffer` has unbalanced `{`/`(` outside string literals, i.e. the
/// REPL should read another line before executing. String literals follow the
/// lexer's rules (`crates/query/src/lexer.rs`): a backslash escapes the next
/// character, so `\"` inside a string does not terminate it.
fn needs_continuation(buffer: &str) -> bool {
    let mut depth: i64 = 0;
    let mut in_str = false;
    let mut chars = buffer.chars();
    while let Some(c) = chars.next() {
        match c {
            '"' if in_str => in_str = false,
            '"' => in_str = true,
            // Lexer treats backslash as an escape inside strings; skip the
            // escaped char so `\"` doesn't toggle the string state.
            '\\' if in_str => {
                chars.next();
            }
            '{' | '(' if !in_str => depth += 1,
            '}' | ')' if !in_str => depth -= 1,
            _ => {}
        }
    }
    depth > 0 || in_str
}

// ─── Embedded mode ──────────────────────────────────────────────────────────

fn run_embedded(data_dir: &str) {
    eprintln!("PowDB v{} — embedded mode", env!("CARGO_PKG_VERSION"));
    eprintln!("Data directory: {data_dir}");
    eprintln!("Type PowQL queries. Use Ctrl-D to exit. Type .help for commands.\n");

    let mut engine = match Engine::new_with_wal_archive(
        Path::new(data_dir),
        archive_wal_records_if_sync_enabled,
    ) {
        Ok(engine) => engine,
        Err(e) => {
            eprintln!("Error: failed to initialize engine: {e}");
            std::process::exit(1);
        }
    };

    let mut rl = match Editor::new() {
        Ok(rl) => rl,
        Err(e) => {
            eprintln!("Error: failed to initialize readline: {e}");
            std::process::exit(1);
        }
    };
    rl.set_helper(Some(PowqlHelper));

    let hist = history_path();
    rl.load_history(&hist).ok();

    let mut timing_enabled = false;
    let mut buffer = String::new();

    loop {
        let prompt = if buffer.is_empty() {
            "powql> "
        } else {
            "  ...> "
        };
        let line = match rl.readline(prompt) {
            Ok(line) => line,
            Err(rustyline::error::ReadlineError::Eof) => break,
            Err(rustyline::error::ReadlineError::Interrupted) => {
                // Abandon any partial multi-line statement.
                buffer.clear();
                continue;
            }
            Err(e) => {
                eprintln!("Error: {e}");
                break;
            }
        };

        // Meta-commands are only recognized at the start of a statement.
        if buffer.is_empty() {
            let trimmed = line.trim();
            if trimmed.is_empty() {
                continue;
            }
            // ── Meta-commands ──────────────────────────────────────────
            if trimmed.starts_with('.') {
                rl.add_history_entry(trimmed).ok();
                match trimmed {
                    ".quit" | ".exit" => break,
                    ".help" => {
                        println!("Meta-commands:");
                        println!("  .tables          List all tables");
                        println!("  .schema <TABLE>  Show columns and types for a table");
                        println!("  .timing          Toggle query timing on/off");
                        println!("  .help            Show this help");
                        println!("  .quit / .exit    Exit the REPL");
                    }
                    ".tables" => {
                        let tables = engine.catalog().list_tables();
                        if tables.is_empty() {
                            println!("(no tables)");
                        } else {
                            for t in &tables {
                                println!("  {t}");
                            }
                            println!(
                                "({} table{})",
                                tables.len(),
                                if tables.len() == 1 { "" } else { "s" }
                            );
                        }
                    }
                    ".timing" => {
                        timing_enabled = !timing_enabled;
                        println!("Timing is {}.", if timing_enabled { "on" } else { "off" });
                    }
                    cmd if cmd.starts_with(".schema") => {
                        let table_name = cmd.strip_prefix(".schema").unwrap().trim();
                        if table_name.is_empty() {
                            eprintln!("Usage: .schema <TABLE_NAME>");
                        } else if let Some(schema) = engine.catalog().schema(table_name) {
                            println!("Table: {}", schema.table_name);
                            println!("  {:<20} {:<12} Required", "Column", "Type");
                            println!("  {:-<20} {:-<12} {:-<8}", "", "", "");
                            for col in &schema.columns {
                                println!(
                                    "  {:<20} {:<12} {}",
                                    col.name,
                                    match col.type_id {
                                        powdb_storage::types::TypeId::Int => "int",
                                        powdb_storage::types::TypeId::Float => "float",
                                        powdb_storage::types::TypeId::Bool => "bool",
                                        powdb_storage::types::TypeId::Str => "str",
                                        powdb_storage::types::TypeId::DateTime => "datetime",
                                        powdb_storage::types::TypeId::Uuid => "uuid",
                                        powdb_storage::types::TypeId::Bytes => "bytes",
                                        powdb_storage::types::TypeId::Empty => "empty",
                                    },
                                    if col.required { "yes" } else { "no" }
                                );
                            }
                        } else {
                            eprintln!("Error: table '{table_name}' not found");
                        }
                    }
                    other => {
                        eprintln!("Unknown meta-command: {other}");
                        eprintln!("Type .help for available commands.");
                    }
                }
                continue;
            }
        }

        // Accumulate input until braces/parens balance outside strings.
        buffer.push_str(&line);
        buffer.push('\n');
        if needs_continuation(&buffer) {
            continue;
        }

        let statement = buffer.trim().to_string();
        buffer.clear();
        if statement.is_empty() {
            continue;
        }
        rl.add_history_entry(&statement).ok();

        // ── Execute PowQL query ────────────────────────────────────────
        let start = Instant::now();
        match engine.execute_powql(&statement) {
            Ok(result) => {
                print_local_result(&result);
                if timing_enabled {
                    let elapsed = start.elapsed();
                    if elapsed.as_secs() >= 1 {
                        println!("Time: {:.2}s", elapsed.as_secs_f64());
                    } else {
                        println!("Time: {:.2}ms", elapsed.as_secs_f64() * 1000.0);
                    }
                }
            }
            Err(e) => eprintln!("Error: {e}"),
        }
    }

    rl.save_history(&hist).ok();
    eprintln!("\nBye!");
}

// ─── Remote (wire protocol) mode ────────────────────────────────────────────

async fn run_remote(addr: String, db: String, password: Option<String>, username: Option<String>) {
    eprintln!("PowDB v{} — remote mode", env!("CARGO_PKG_VERSION"));
    eprintln!("Connecting to {addr} ...");

    let stream = match TcpStream::connect(&addr).await {
        Ok(s) => s,
        Err(e) => {
            eprintln!("Error: connection failed: {e}");
            std::process::exit(1);
        }
    };

    let (reader, writer) = stream.into_split();
    let mut reader = BufReader::new(reader);
    let mut writer = BufWriter::new(writer);

    // Send CONNECT
    let connect = Message::Connect {
        db_name: db.clone(),
        password: password.map(Into::into),
        username,
    };
    if let Err(e) = connect.write_to(&mut writer).await {
        eprintln!("Error: failed to send CONNECT: {e}");
        std::process::exit(1);
    }
    if let Err(e) = tokio::io::AsyncWriteExt::flush(&mut writer).await {
        eprintln!("Error: flush failed: {e}");
        std::process::exit(1);
    }

    // Read CONNECT_OK or ERROR
    match Message::read_from(&mut reader).await {
        Ok(Some(Message::ConnectOk { version })) => {
            eprintln!("Connected to db `{db}` (server v{version})");
            eprintln!("Type PowQL queries. Use Ctrl-D to exit.\n");
        }
        Ok(Some(Message::Error { message })) => {
            eprintln!("Error: server rejected connection: {message}");
            std::process::exit(1);
        }
        Ok(Some(other)) => {
            eprintln!("Error: unexpected handshake reply: {other:?}");
            std::process::exit(1);
        }
        Ok(None) => {
            eprintln!("Error: server closed connection during handshake");
            std::process::exit(1);
        }
        Err(e) => {
            eprintln!("Error: handshake read failed: {e}");
            std::process::exit(1);
        }
    }

    let mut rl = match Editor::new() {
        Ok(rl) => rl,
        Err(e) => {
            eprintln!("Error: failed to initialize readline: {e}");
            std::process::exit(1);
        }
    };
    rl.set_helper(Some(PowqlHelper));

    let hist = history_path();
    rl.load_history(&hist).ok();

    let mut timing_enabled = false;
    let mut buffer = String::new();

    loop {
        let prompt = if buffer.is_empty() {
            "powql> "
        } else {
            "  ...> "
        };
        let line = match rl.readline(prompt) {
            Ok(line) => line,
            Err(rustyline::error::ReadlineError::Eof) => break,
            Err(rustyline::error::ReadlineError::Interrupted) => {
                buffer.clear();
                continue;
            }
            Err(e) => {
                eprintln!("Error: {e}");
                break;
            }
        };

        // Meta-commands are only recognized at the start of a statement.
        if buffer.is_empty() {
            let trimmed = line.trim();
            if trimmed.is_empty() {
                continue;
            }
            // Handle local-only meta-commands in remote mode
            if trimmed.starts_with('.') {
                rl.add_history_entry(trimmed).ok();
                match trimmed {
                    ".quit" | ".exit" => break,
                    ".timing" => {
                        timing_enabled = !timing_enabled;
                        println!("Timing is {}.", if timing_enabled { "on" } else { "off" });
                    }
                    ".help" => {
                        println!("Meta-commands (remote mode):");
                        println!("  .timing          Toggle query timing on/off");
                        println!("  .help            Show this help");
                        println!("  .quit / .exit    Exit the REPL");
                        println!();
                        println!("Note: .tables and .schema are only available in embedded mode.");
                    }
                    _ => {
                        eprintln!(
                            "Meta-commands (.tables, .schema) are not supported in remote mode."
                        );
                        eprintln!("Type .help for available commands.");
                    }
                }
                continue;
            }
        }

        // Accumulate input until braces/parens balance outside strings.
        buffer.push_str(&line);
        buffer.push('\n');
        if needs_continuation(&buffer) {
            continue;
        }

        let statement = buffer.trim().to_string();
        buffer.clear();
        if statement.is_empty() {
            continue;
        }
        rl.add_history_entry(&statement).ok();

        let q = Message::Query { query: statement };
        if q.write_to(&mut writer).await.is_err() {
            eprintln!("Error: write failed — disconnected");
            break;
        }
        if tokio::io::AsyncWriteExt::flush(&mut writer).await.is_err() {
            eprintln!("Error: flush failed — disconnected");
            break;
        }

        let start = Instant::now();
        match Message::read_from(&mut reader).await {
            Ok(Some(msg)) => {
                print_remote_result(&msg);
                if timing_enabled {
                    let elapsed = start.elapsed();
                    if elapsed.as_secs() >= 1 {
                        println!("Time: {:.2}s", elapsed.as_secs_f64());
                    } else {
                        println!("Time: {:.2}ms", elapsed.as_secs_f64() * 1000.0);
                    }
                }
            }
            Ok(None) => {
                eprintln!("Error: server closed connection");
                break;
            }
            Err(e) => {
                eprintln!("Error: read failed: {e}");
                break;
            }
        }
    }

    // Best-effort goodbye
    let _ = Message::Disconnect.write_to(&mut writer).await;
    let _ = tokio::io::AsyncWriteExt::flush(&mut writer).await;

    rl.save_history(&hist).ok();
    eprintln!("\nBye!");
}

// ─── Output formatting ──────────────────────────────────────────────────────

fn print_local_result(result: &QueryResult) {
    match result {
        QueryResult::Rows { columns, rows } => {
            if rows.is_empty() {
                println!("(empty set)");
                return;
            }
            let str_rows: Vec<Vec<String>> = rows
                .iter()
                .map(|row| row.iter().map(format_value).collect())
                .collect();
            print_table(columns, &str_rows);
        }
        QueryResult::Scalar(val) => {
            println!("{}", format_value(val));
        }
        QueryResult::Modified(n) => {
            println!("{n} row{} affected", if *n == 1 { "" } else { "s" });
        }
        QueryResult::Created(name) => {
            println!("type {name} created");
        }
        QueryResult::Executed { message } => {
            println!("{message}");
        }
    }
}

/// Render one wire cell for display. The server serializes NULL as the
/// bareword "null" (the sentinel the TS client's typed decoder matches);
/// remote mode renders it as `NULL`, matching the embedded REPL. A string
/// column whose *value* is literally "null" is indistinguishable on the
/// untyped wire — same tradeoff the TS client documents.
fn render_remote_cell(cell: &str) -> String {
    if cell == "null" {
        "NULL".into()
    } else {
        cell.into()
    }
}

fn print_remote_result(msg: &Message) {
    match msg {
        Message::ResultRows { columns, rows } => {
            if rows.is_empty() {
                println!("(empty set)");
                return;
            }
            let rendered: Vec<Vec<String>> = rows
                .iter()
                .map(|row| row.iter().map(|c| render_remote_cell(c)).collect())
                .collect();
            print_table(columns, &rendered);
        }
        Message::ResultScalar { value } => {
            println!("{}", render_remote_cell(value));
        }
        Message::ResultOk { affected } => {
            println!(
                "{affected} row{} affected",
                if *affected == 1 { "" } else { "s" }
            );
        }
        Message::ResultMessage { message } => {
            println!("{message}");
        }
        Message::Error { message } => {
            eprintln!("Error: {message}");
        }
        other => {
            eprintln!("Error: unexpected response: {other:?}");
        }
    }
}

fn print_table(columns: &[String], rows: &[Vec<String>]) {
    let mut widths: Vec<usize> = columns.iter().map(|c| c.len()).collect();
    for row in rows {
        for (i, val) in row.iter().enumerate() {
            if i < widths.len() && val.len() > widths[i] {
                widths[i] = val.len();
            }
        }
    }

    let header: Vec<String> = columns
        .iter()
        .enumerate()
        .map(|(i, c)| format!("{:width$}", c, width = widths[i]))
        .collect();
    println!(" {} ", header.join(" | "));
    let sep: Vec<String> = widths.iter().map(|w| "-".repeat(*w)).collect();
    println!("-{}-", sep.join("-+-"));

    for row in rows {
        let cells: Vec<String> = row
            .iter()
            .enumerate()
            .map(|(i, v)| format!("{:width$}", v, width = widths[i]))
            .collect();
        println!(" {} ", cells.join(" | "));
    }

    println!(
        "({} row{})",
        rows.len(),
        if rows.len() == 1 { "" } else { "s" }
    );
}

fn format_value(v: &Value) -> String {
    match v {
        Value::Int(n) => n.to_string(),
        Value::Float(n) => format!("{n}"),
        Value::Bool(b) => b.to_string(),
        Value::Str(s) => s.clone(),
        Value::DateTime(t) => format!("{t}"),
        Value::Uuid(u) => format!(
            "{:02x}{:02x}{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}",
            u[0], u[1], u[2], u[3], u[4], u[5], u[6], u[7],
            u[8], u[9], u[10], u[11], u[12], u[13], u[14], u[15]
        ),
        Value::Bytes(b) => format!("<{} bytes>", b.len()),
        Value::Empty => "NULL".into(),
    }
}

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

    #[test]
    fn remote_null_sentinel_renders_as_null_like_embedded() {
        // The wire sends NULL as the bareword "null"; remote display must
        // match the embedded REPL's `NULL`.
        assert_eq!(render_remote_cell("null"), "NULL");
        assert_eq!(format_value(&Value::Empty), "NULL");
        // Ordinary values pass through untouched.
        assert_eq!(render_remote_cell("42"), "42");
        assert_eq!(render_remote_cell(""), "");
        assert_eq!(render_remote_cell("NULL"), "NULL");
    }

    #[test]
    fn remote_db_default_matches_ts_client() {
        assert_eq!(DEFAULT_DB_NAME, "default");
    }

    #[test]
    fn completion_keywords_include_current_lexer_surface() {
        for required in [
            "upsert",
            "conflict",
            "row_number",
            "dense_rank",
            "over",
            "partition",
            "date_add",
            "date_diff",
            "unique",
            "materialized",
            "explain",
        ] {
            assert!(
                POWQL_KEYWORDS.contains(&required),
                "lexer keyword missing from CLI completion source: {required}"
            );
        }
        assert!(CLI_COMMANDS.contains(&"exec"));
    }

    #[test]
    fn continuation_tracking() {
        assert!(needs_continuation("type User {"));
        assert!(needs_continuation("type User {\n  required name: str,"));
        assert!(!needs_continuation("type User { required name: str }"));
        // Brace inside a string literal must not count.
        assert!(!needs_continuation(r#"insert U { s := "}" }"#));
        assert!(needs_continuation(r#"insert U { s := "}" "#));
        // Parens.
        assert!(needs_continuation("count(User filter ("));
        assert!(!needs_continuation("count(User)"));
        // Nested.
        assert!(needs_continuation("insert U { a := (1 + "));
        // Over-closed input is NOT a continuation — let the parser error.
        assert!(!needs_continuation("User }"));
        // Escaped quote inside a string must not end the string.
        assert!(needs_continuation(r#"insert U { s := "a\" "#));
        assert!(!needs_continuation(r#"insert U { s := "a\"b" }"#));
    }
}