magic-shq 0.1.6

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

use std::io::{self, Read, Write};
use std::os::fd::AsRawFd;
use std::process::Command;
use std::time::Instant;

use bird::{
    init, parse_query, CompactOptions, Config, EventFilters, InvocationBatch, InvocationRecord,
    Query, SessionRecord, StorageMode, Store, BIRD_INVOCATION_UUID_VAR, BIRD_PARENT_CLIENT_VAR,
};
use pty_process::blocking::{Command as PtyCommand, open as pty_open};

/// Generate a session ID for grouping related invocations.
fn session_id() -> String {
    // Use parent PID as session identifier (groups invocations in same shell)
    let ppid = std::os::unix::process::parent_id();
    format!("shell-{}", ppid)
}

/// Get the invoker name (typically the shell or calling process).
fn invoker_name() -> String {
    std::env::var("SHELL")
        .map(|s| {
            std::path::Path::new(&s)
                .file_name()
                .map(|n| n.to_string_lossy().to_string())
                .unwrap_or(s)
        })
        .unwrap_or_else(|_| "unknown".to_string())
}

/// Get the invoker PID (parent process).
fn invoker_pid() -> u32 {
    std::os::unix::process::parent_id()
}

/// OSC escape sequence that commands can emit to opt out of recording.
/// Format: ESC ] shq;nosave BEL  (or ESC ] shq;nosave ESC \)
const NOSAVE_OSC: &[u8] = b"\x1b]shq;nosave\x07";
const NOSAVE_OSC_ST: &[u8] = b"\x1b]shq;nosave\x1b\\";

/// Check if output contains the nosave marker.
/// Commands can emit this OSC escape sequence to opt out of being recorded.
fn contains_nosave_marker(data: &[u8]) -> bool {
    data.windows(NOSAVE_OSC.len()).any(|w| w == NOSAVE_OSC)
        || data.windows(NOSAVE_OSC_ST.len()).any(|w| w == NOSAVE_OSC_ST)
}

/// Run a command and capture it to BIRD using PTY for proper job control.
///
/// The command runs in a pseudo-terminal, which means:
/// - Ctrl-Z suspends the command (not shq)
/// - Interactive programs (vim, less) work correctly
/// - Output is displayed in real-time
///
/// `tag`: Optional tag (unique alias) for this invocation.
/// `extract_override`: Some(true) forces extraction, Some(false) disables it, None uses config.
/// `format_override`: Override format detection for event extraction.
/// `auto_compact`: If true, spawn background compaction after saving.
pub fn run(shell_cmd: Option<&str>, cmd_args: &[String], tag: Option<&str>, extract_override: Option<bool>, format_override: Option<&str>, auto_compact: bool) -> bird::Result<()> {
    // Determine command string and build PTY command
    let (cmd_str, shell, args): (String, String, Vec<String>) = match shell_cmd {
        Some(cmd) => {
            let shell = std::env::var("SHELL").unwrap_or_else(|_| "sh".to_string());
            (cmd.to_string(), shell, vec!["-c".to_string(), cmd.to_string()])
        }
        None => {
            if cmd_args.is_empty() {
                return Err(bird::Error::Config(
                    "No command specified. Use -c \"cmd\" or provide command args".to_string(),
                ));
            }
            // If single arg with spaces, treat as shell command (common UX pattern)
            if cmd_args.len() == 1 && cmd_args[0].contains(' ') {
                let shell = std::env::var("SHELL").unwrap_or_else(|_| "sh".to_string());
                (cmd_args[0].clone(), shell, vec!["-c".to_string(), cmd_args[0].clone()])
            } else {
                (cmd_args.join(" "), cmd_args[0].clone(), cmd_args[1..].to_vec())
            }
        }
    };

    let config = Config::load()?;
    let store = Store::open(config.clone())?;

    let cwd = std::env::current_dir()
        .map(|p| p.display().to_string())
        .unwrap_or_else(|_| ".".to_string());

    let invocation_id = uuid::Uuid::now_v7();

    // Allocate PTY
    let (mut pty, pts) = pty_open().map_err(|e| bird::Error::Io(io::Error::other(e)))?;

    // Try to match terminal size
    if let Ok(size) = terminal_size() {
        let _ = pty.resize(pty_process::Size::new(size.0, size.1));
    }

    // Check if stdin is a terminal - if not, disable PTY echo to prevent duplicates
    let stdin_is_tty = unsafe { libc::isatty(libc::STDIN_FILENO) == 1 };
    if !stdin_is_tty {
        disable_pty_echo(pty.as_raw_fd());
    }

    // Build command to run in PTY (builder pattern takes ownership)
    let cmd = PtyCommand::new(&shell)
        .args(&args)
        .env(BIRD_INVOCATION_UUID_VAR, invocation_id.to_string())
        .env(BIRD_PARENT_CLIENT_VAR, "shq");

    // Spawn process in PTY - it becomes session leader with PTY as controlling terminal
    let start = Instant::now();
    let mut child = cmd.spawn(pts)
        .map_err(|e| bird::Error::Io(io::Error::other(e)))?;

    // Set up raw mode for stdin if it's a terminal
    let orig_termios = if stdin_is_tty {
        set_raw_mode(libc::STDIN_FILENO)
    } else {
        None
    };

    // Clone PTY fd for the stdin forwarding thread
    let pty_write_fd = pty.as_raw_fd();

    // Spawn thread to forward stdin to PTY (both tty and piped modes)
    use std::sync::atomic::{AtomicBool, Ordering};
    use std::sync::Arc;

    let running = Arc::new(AtomicBool::new(true));
    let running_clone = running.clone();

    let stdin_handle = std::thread::spawn(move || {
        let mut buf = [0u8; 4096];
        let stdin_fd = libc::STDIN_FILENO;

        // Set stdin to non-blocking
        set_nonblocking(stdin_fd, true);

        while running_clone.load(Ordering::Relaxed) {
            let n = unsafe {
                libc::read(stdin_fd, buf.as_mut_ptr() as *mut libc::c_void, buf.len())
            };

            if n > 0 {
                let _ = unsafe {
                    libc::write(pty_write_fd, buf.as_ptr() as *const libc::c_void, n as usize)
                };
            } else if n == 0 {
                // EOF on stdin - send Ctrl-D to signal EOF to child
                let ctrl_d = [4u8]; // ASCII EOT (Ctrl-D)
                let _ = unsafe {
                    libc::write(pty_write_fd, ctrl_d.as_ptr() as *const libc::c_void, 1)
                };
                break;
            } else {
                // EAGAIN/EWOULDBLOCK - no data available
                std::thread::sleep(std::time::Duration::from_millis(10));
            }
        }
    });

    // Read output from PTY and pass through to our stdout while collecting it
    let mut output_buffer = Vec::new();
    let mut buf = [0u8; 4096];

    // Set PTY to non-blocking for reading
    set_nonblocking(pty.as_raw_fd(), true);

    loop {
        // Check if child has exited
        match child.try_wait() {
            Ok(Some(_status)) => {
                // Child exited, drain remaining output
                set_nonblocking(pty.as_raw_fd(), false);
                while let Ok(n) = pty.read(&mut buf) {
                    if n == 0 { break; }
                    output_buffer.extend_from_slice(&buf[..n]);
                    let _ = io::stdout().write_all(&buf[..n]);
                    let _ = io::stdout().flush();
                }
                break;
            }
            Ok(None) => {
                // Child still running, read available output
                match pty.read(&mut buf) {
                    Ok(0) => {
                        // EOF - child closed PTY
                        break;
                    }
                    Ok(n) => {
                        output_buffer.extend_from_slice(&buf[..n]);
                        let _ = io::stdout().write_all(&buf[..n]);
                        let _ = io::stdout().flush();
                    }
                    Err(e) if e.kind() == io::ErrorKind::WouldBlock => {
                        // No data available, sleep briefly
                        std::thread::sleep(std::time::Duration::from_millis(10));
                    }
                    Err(_) => {
                        // Read error, child may have exited
                        break;
                    }
                }
            }
            Err(_) => break,
        }
    }

    // Stop stdin forwarding thread
    running.store(false, Ordering::Relaxed);
    let _ = stdin_handle.join();

    // Restore terminal mode
    if let Some(termios) = orig_termios {
        restore_termios(libc::STDIN_FILENO, &termios);
    }

    // Wait for child to fully exit and get status
    let status = child.wait().map_err(|e| bird::Error::Io(io::Error::other(e)))?;
    let duration_ms = start.elapsed().as_millis() as i64;
    let exit_code = status.code().unwrap_or(-1);

    // Check for nosave marker
    if contains_nosave_marker(&output_buffer) {
        if !status.success() {
            std::process::exit(exit_code);
        }
        return Ok(());
    }

    // Create and save records
    let sid = session_id();
    let session = SessionRecord::new(
        &sid,
        &config.client_id,
        invoker_name(),
        invoker_pid(),
        "shell",
    );

    let mut record = InvocationRecord::with_id(
        invocation_id,
        &sid,
        &cmd_str,
        &cwd,
        exit_code,
        &config.client_id,
    )
    .with_duration(duration_ms);

    if let Some(t) = tag {
        record = record.with_tag(t);
    }

    let inv_id = record.id;
    let mut batch = InvocationBatch::new(record).with_session(session);

    // PTY merges stdout/stderr into a single stream - store as "combined"
    if !output_buffer.is_empty() {
        batch = batch.with_output("combined", output_buffer);
    }

    store.write_batch(&batch)?;

    // Extract events if enabled
    let should_extract = extract_override.unwrap_or(config.auto_extract);
    if should_extract {
        let count = store.extract_events(&inv_id.to_string(), format_override)?;
        if count > 0 {
            eprintln!("shq: extracted {} events", count);
        }
    }

    // Spawn background compaction if requested
    if auto_compact {
        let session_id = sid.clone();
        let _ = Command::new(std::env::current_exe().unwrap_or_else(|_| "shq".into()))
            .args(["compact", "-s", &session_id, "--today", "-q"])
            .stdin(std::process::Stdio::null())
            .stdout(std::process::Stdio::null())
            .stderr(std::process::Stdio::null())
            .spawn();
    }

    if !status.success() {
        std::process::exit(exit_code);
    }

    Ok(())
}

/// Get terminal size (rows, cols)
fn terminal_size() -> io::Result<(u16, u16)> {
    use std::mem::MaybeUninit;

    let mut size = MaybeUninit::<libc::winsize>::uninit();
    let ret = unsafe { libc::ioctl(libc::STDOUT_FILENO, libc::TIOCGWINSZ, size.as_mut_ptr()) };

    if ret == 0 {
        let size = unsafe { size.assume_init() };
        Ok((size.ws_row, size.ws_col))
    } else {
        Err(io::Error::last_os_error())
    }
}

/// Set file descriptor to non-blocking mode
fn set_nonblocking(fd: i32, nonblocking: bool) {
    unsafe {
        let flags = libc::fcntl(fd, libc::F_GETFL);
        if nonblocking {
            libc::fcntl(fd, libc::F_SETFL, flags | libc::O_NONBLOCK);
        } else {
            libc::fcntl(fd, libc::F_SETFL, flags & !libc::O_NONBLOCK);
        }
    }
}

/// Set terminal to raw mode, returning original termios for restoration
fn set_raw_mode(fd: i32) -> Option<libc::termios> {
    unsafe {
        let mut orig: libc::termios = std::mem::zeroed();
        if libc::tcgetattr(fd, &mut orig) != 0 {
            return None;
        }

        let mut raw = orig;
        // Disable canonical mode, echo, and signal generation
        raw.c_lflag &= !(libc::ICANON | libc::ECHO | libc::ISIG | libc::IEXTEN);
        // Disable input processing
        raw.c_iflag &= !(libc::IXON | libc::ICRNL | libc::BRKINT | libc::INPCK | libc::ISTRIP);
        // Disable output processing
        raw.c_oflag &= !libc::OPOST;
        // Set character size to 8 bits
        raw.c_cflag |= libc::CS8;
        // Read returns immediately with whatever is available
        raw.c_cc[libc::VMIN] = 0;
        raw.c_cc[libc::VTIME] = 0;

        if libc::tcsetattr(fd, libc::TCSAFLUSH, &raw) != 0 {
            return None;
        }

        Some(orig)
    }
}

/// Restore terminal to original mode
fn restore_termios(fd: i32, termios: &libc::termios) {
    unsafe {
        libc::tcsetattr(fd, libc::TCSAFLUSH, termios);
    }
}

/// Disable echo on PTY (for piped input to prevent duplicates)
fn disable_pty_echo(fd: i32) {
    unsafe {
        let mut termios: libc::termios = std::mem::zeroed();
        if libc::tcgetattr(fd, &mut termios) == 0 {
            termios.c_lflag &= !libc::ECHO;
            libc::tcsetattr(fd, libc::TCSANOW, &termios);
        }
    }
}

/// Save output from stdin or file with an explicit command.
#[allow(clippy::too_many_arguments)]
pub fn save(
    file: Option<&str>,
    command: &str,
    exit_code: i32,
    duration_ms: Option<i64>,
    stream: &str,
    stdout_file: Option<&str>,
    stderr_file: Option<&str>,
    explicit_session_id: Option<&str>,
    explicit_invoker_pid: Option<u32>,
    explicit_invoker: Option<&str>,
    explicit_invoker_type: &str,
    extract: bool,
    compact: bool,
    tag: Option<&str>,
    quiet: bool,
) -> bird::Result<()> {
    use std::process::Command;

    // Read content first so we can check for nosave marker
    let (stdout_content, stderr_content, single_content) = if stdout_file.is_some() || stderr_file.is_some() {
        let stdout = stdout_file.map(std::fs::read).transpose()?;
        let stderr = stderr_file.map(std::fs::read).transpose()?;
        (stdout, stderr, None)
    } else {
        let content = match file {
            Some(path) => std::fs::read(path)?,
            None => {
                let mut buf = Vec::new();
                io::stdin().read_to_end(&mut buf)?;
                buf
            }
        };
        (None, None, Some(content))
    };

    // Check for nosave marker - command opted out of recording
    let has_nosave = stdout_content.as_ref().is_some_and(|c| contains_nosave_marker(c))
        || stderr_content.as_ref().is_some_and(|c| contains_nosave_marker(c))
        || single_content.as_ref().is_some_and(|c| contains_nosave_marker(c));

    if has_nosave {
        return Ok(());
    }

    let config = Config::load()?;
    let store = Store::open(config.clone())?;

    // Get current working directory
    let cwd = std::env::current_dir()
        .map(|p| p.display().to_string())
        .unwrap_or_else(|_| ".".to_string());

    // Use explicit values or fall back to auto-detected
    let sid = explicit_session_id
        .map(|s| s.to_string())
        .unwrap_or_else(session_id);
    let inv_pid = explicit_invoker_pid.unwrap_or_else(invoker_pid);
    let inv_name = explicit_invoker
        .map(|s| s.to_string())
        .unwrap_or_else(invoker_name);

    // Create session and invocation records
    let session = SessionRecord::new(
        &sid,
        &config.client_id,
        &inv_name,
        inv_pid,
        explicit_invoker_type,
    );

    let mut inv_record = InvocationRecord::new(
        &sid,
        command,
        &cwd,
        exit_code,
        &config.client_id,
    );
    if let Some(ms) = duration_ms {
        inv_record = inv_record.with_duration(ms);
    }
    if let Some(t) = tag {
        inv_record = inv_record.with_tag(t);
    }
    let inv_id = inv_record.id;

    // Build batch with all related records
    let mut batch = InvocationBatch::new(inv_record).with_session(session);

    if let Some(content) = stdout_content {
        batch = batch.with_output("stdout", content);
    }
    if let Some(content) = stderr_content {
        batch = batch.with_output("stderr", content);
    }
    if let Some(content) = single_content {
        batch = batch.with_output(stream, content);
    }

    // Write everything atomically
    store.write_batch(&batch)?;

    // Extract events if requested (uses config default or explicit flag)
    let should_extract = extract || config.auto_extract;
    if should_extract {
        let count = store.extract_events(&inv_id.to_string(), None)?;
        if !quiet && count > 0 {
            eprintln!("shq: extracted {} events", count);
        }
    }

    // Spawn background compaction if requested
    if compact {
        let session_id = sid.clone();
        let _ = Command::new(std::env::current_exe().unwrap_or_else(|_| "shq".into()))
            .args(["compact", "-s", &session_id, "--today", "-q"])
            .stdin(std::process::Stdio::null())
            .stdout(std::process::Stdio::null())
            .stderr(std::process::Stdio::null())
            .spawn();
    }

    Ok(())
}

/// Options for the output command.
#[derive(Default)]
pub struct OutputOptions {
    pub pager: bool,
    pub strip_ansi: bool,
    pub head: Option<usize>,
    pub tail: Option<usize>,
}

/// Show captured output from invocation(s).
pub fn output(query_str: &str, stream_filter: Option<&str>, opts: &OutputOptions) -> bird::Result<()> {
    use std::io::Write;
    use std::process::{Command, Stdio};

    let config = Config::load()?;
    let store = Store::open(config)?;

    // Parse query
    let query = parse_query(query_str);

    // Normalize stream filter aliases
    let (db_filter, combine_to_stdout) = match stream_filter {
        Some("O") | Some("o") => (Some("stdout"), false),
        Some("E") | Some("e") => (Some("stderr"), false),
        Some("A") | Some("a") | Some("all") => (None, true), // No filter, but combine to stdout
        Some(s) => (Some(s), false),
        None => (None, false), // No filter, route to original streams
    };

    // First try to find by ID (short or full), then fall back to query system
    let invocation_id = if let Some(id) = try_find_by_id(&store, query_str)? {
        id
    } else {
        match resolve_query_to_invocation(&store, &query) {
            Ok(id) => id,
            Err(bird::Error::NotFound(_)) => {
                eprintln!("No matching invocation found");
                return Ok(());
            }
            Err(e) => return Err(e),
        }
    };

    // Get outputs for the invocation (optionally filtered by stream)
    let outputs = store.get_outputs(&invocation_id, db_filter)?;

    if outputs.is_empty() {
        eprintln!("No output found for invocation {}", invocation_id);
        return Ok(());
    }

    // Collect content per stream
    let mut stdout_content = Vec::new();
    let mut stderr_content = Vec::new();
    for output_info in &outputs {
        match store.read_output_content(output_info) {
            Ok(content) => {
                if output_info.stream == "stderr" {
                    stderr_content.extend_from_slice(&content);
                } else {
                    stdout_content.extend_from_slice(&content);
                }
            }
            Err(e) => {
                eprintln!("Failed to read output for stream '{}': {}", output_info.stream, e);
            }
        }
    }

    // Helper to process content (strip ANSI, limit lines)
    let process_content = |content: Vec<u8>| -> String {
        let content = if opts.strip_ansi {
            strip_ansi_escapes(&content)
        } else {
            content
        };

        let content_str = String::from_utf8_lossy(&content);

        if opts.head.is_some() || opts.tail.is_some() {
            let lines: Vec<&str> = content_str.lines().collect();
            let selected: Vec<&str> = if let Some(n) = opts.head {
                lines.into_iter().take(n).collect()
            } else if let Some(n) = opts.tail {
                let skip = lines.len().saturating_sub(n);
                lines.into_iter().skip(skip).collect()
            } else {
                lines
            };
            selected.join("\n") + if content_str.ends_with('\n') { "\n" } else { "" }
        } else {
            content_str.into_owned()
        }
    };

    // Output via pager or directly
    if opts.pager {
        // Combine all content for pager
        let mut all_content = stdout_content;
        all_content.extend_from_slice(&stderr_content);
        let final_content = process_content(all_content);

        let pager_cmd = std::env::var("PAGER").unwrap_or_else(|_| "less -R".to_string());
        let parts: Vec<&str> = pager_cmd.split_whitespace().collect();
        if let Some((cmd, args)) = parts.split_first() {
            let mut child = Command::new(cmd)
                .args(args)
                .stdin(Stdio::piped())
                .spawn()
                .map_err(bird::Error::Io)?;

            if let Some(mut stdin) = child.stdin.take() {
                let _ = stdin.write_all(final_content.as_bytes());
            }
            let _ = child.wait();
        }
    } else if combine_to_stdout {
        // Combine all to stdout
        let mut all_content = stdout_content;
        all_content.extend_from_slice(&stderr_content);
        let final_content = process_content(all_content);
        io::stdout().write_all(final_content.as_bytes())?;
    } else {
        // Route to original streams
        if !stdout_content.is_empty() {
            let content = process_content(stdout_content);
            io::stdout().write_all(content.as_bytes())?;
        }
        if !stderr_content.is_empty() {
            let content = process_content(stderr_content);
            io::stderr().write_all(content.as_bytes())?;
        }
    }

    Ok(())
}

/// Strip ANSI escape codes from bytes.
fn strip_ansi_escapes(input: &[u8]) -> Vec<u8> {
    let mut output = Vec::with_capacity(input.len());
    let mut i = 0;
    while i < input.len() {
        if input[i] == 0x1b && i + 1 < input.len() && input[i + 1] == b'[' {
            // Skip CSI sequence: ESC [ ... final_byte
            i += 2;
            while i < input.len() {
                let c = input[i];
                i += 1;
                if (0x40..=0x7e).contains(&c) {
                    break; // Final byte found
                }
            }
        } else if input[i] == 0x1b && i + 1 < input.len() && input[i + 1] == b']' {
            // Skip OSC sequence: ESC ] ... ST (or BEL)
            i += 2;
            while i < input.len() {
                if input[i] == 0x07 {
                    // BEL terminates OSC
                    i += 1;
                    break;
                } else if input[i] == 0x1b && i + 1 < input.len() && input[i + 1] == b'\\' {
                    // ST (ESC \) terminates OSC
                    i += 2;
                    break;
                }
                i += 1;
            }
        } else {
            output.push(input[i]);
            i += 1;
        }
    }
    output
}

pub fn init(mode: &str, force: bool) -> bird::Result<()> {
    // Parse storage mode
    let storage_mode: StorageMode = mode.parse()?;

    let mut config = Config::default_location()?;

    if init::is_initialized(&config) {
        if force {
            // Delete existing database directory
            let db_dir = config.bird_root.join("db");
            if db_dir.exists() {
                std::fs::remove_dir_all(&db_dir)?;
                println!("Removed existing database at {}", db_dir.display());
            }
        } else {
            println!("BIRD already initialized at {}", config.bird_root.display());
            println!("Use --force to re-initialize (this will delete all data)");
            return Ok(());
        }
    }

    // Set storage mode before initialization
    config.storage_mode = storage_mode;

    init::initialize(&config)?;
    println!("BIRD initialized at {}", config.bird_root.display());
    println!("Client ID: {}", config.client_id);
    println!("Storage mode: {}", config.storage_mode);

    Ok(())
}

/// Update DuckDB extensions to latest versions.
pub fn update_extensions(dry_run: bool) -> bird::Result<()> {
    let config = Config::load()?;
    let store = Store::open(config)?;

    let extensions = [
        ("scalarfs", "data: URL support for inline blobs"),
        ("duck_hunt", "log/output parsing for event extraction"),
    ];

    if dry_run {
        println!("Would update the following extensions:");
        for (name, desc) in &extensions {
            println!("  {} - {}", name, desc);
        }
        return Ok(());
    }

    println!("Updating DuckDB extensions...\n");

    for (name, desc) in &extensions {
        print!("  {} ({})... ", name, desc);
        match store.query(&format!("FORCE INSTALL {} FROM community", name)) {
            Ok(_) => {
                // Reload the extension
                match store.query(&format!("LOAD {}", name)) {
                    Ok(_) => println!("updated"),
                    Err(e) => println!("installed but failed to load: {}", e),
                }
            }
            Err(e) => println!("failed: {}", e),
        }
    }

    println!("\nExtensions updated. New features available:");
    println!("  - duck_hunt: compression support (.gz/.zst), duck_hunt_detect_format(),");
    println!("               duck_hunt_diagnose_read(), severity_threshold parameter");

    Ok(())
}

/// List invocation history.
pub fn invocations(query_str: &str, format: &str, limit: Option<usize>) -> bird::Result<()> {
    let config = Config::load()?;
    let store = Store::open(config)?;

    // Parse query and apply filters
    let mut query = parse_query(query_str);

    // Override range if -n/--limit is provided
    if let Some(n) = limit {
        query.range = Some(bird::RangeSelector { start: n, end: None });
    }

    let invocations = store.query_invocations(&query)?;

    if invocations.is_empty() {
        println!("No invocations recorded yet.");
        return Ok(());
    }

    // Get output info for all invocations (which streams have data)
    let inv_ids: Vec<&str> = invocations.iter().map(|i| i.id.as_str()).collect();
    let output_info = get_output_info_batch(&store, &inv_ids)?;

    match format {
        "json" => {
            // JSON output
            println!("[");
            for (i, inv) in invocations.iter().enumerate() {
                let comma = if i < invocations.len() - 1 { "," } else { "" };
                let out_state = output_info.get(inv.id.as_str()).copied().unwrap_or_default();
                println!(
                    r#"  {{"id": "{}", "timestamp": "{}", "cmd": "{}", "exit_code": {}, "duration_ms": {}, "has_stdout": {}, "has_stderr": {}, "has_combined": {}}}{}"#,
                    inv.id,
                    inv.timestamp,
                    inv.cmd.replace('\\', "\\\\").replace('"', "\\\""),
                    inv.exit_code,
                    inv.duration_ms.unwrap_or(0),
                    out_state.has_stdout,
                    out_state.has_stderr,
                    out_state.has_combined,
                    comma
                );
            }
            println!("]");
        }
        "table" => {
            // Detailed table output
            println!("{:<20} {:<6} {:<10} {:<4} COMMAND", "TIMESTAMP", "EXIT", "DURATION", "OUT");
            println!("{}", "-".repeat(80));

            for inv in invocations {
                let duration = inv
                    .duration_ms
                    .map(|d| format!("{}ms", d))
                    .unwrap_or_else(|| "-".to_string());

                // Truncate timestamp to just time portion if today
                let timestamp = if inv.timestamp.len() > 19 {
                    &inv.timestamp[11..19]
                } else {
                    &inv.timestamp
                };

                // Output indicator
                let out_state = output_info.get(inv.id.as_str()).copied().unwrap_or_default();
                let out_indicator = out_state.glyph();

                // Truncate command if too long
                let cmd_display = if inv.cmd.len() > 50 {
                    format!("{}...", &inv.cmd[..47])
                } else {
                    inv.cmd.clone()
                };

                println!(
                    "{:<20} {:<6} {:<10} {:<4} {}",
                    timestamp, inv.exit_code, duration, out_indicator, cmd_display
                );
            }
        }
        "commands" => {
            // Just commands, nothing else
            for inv in invocations {
                println!("{}", inv.cmd);
            }
        }
        _ => {
            // Compact color output (default)
            // Format: ✓ abcd1234 command... ●
            for inv in invocations {
                // Status glyph with color
                let (status_glyph, color_code) = if inv.exit_code == 0 {
                    ("", "\x1b[32m") // Green
                } else {
                    ("", "\x1b[31m") // Red
                };
                let reset = "\x1b[0m";
                let dim = "\x1b[2m";

                // Short ID (last 8 chars - more unique for UUIDv7)
                let id_len = inv.id.len();
                let short_id = if id_len >= 8 {
                    &inv.id[id_len - 8..]
                } else {
                    &inv.id
                };

                // Output indicator
                let out_state = output_info.get(inv.id.as_str()).copied().unwrap_or_default();
                let out_glyph = out_state.glyph();

                // Truncate command for terminal width (leave room for prefix)
                let max_cmd_len = 65;
                let cmd_display = if inv.cmd.len() > max_cmd_len {
                    format!("{}", &inv.cmd[..max_cmd_len - 1])
                } else {
                    inv.cmd.clone()
                };

                println!(
                    "{}{}{} {}{}{} {} {}",
                    color_code, status_glyph, reset,
                    dim, short_id, reset,
                    out_glyph,
                    cmd_display
                );
            }
        }
    }

    Ok(())
}

/// Output capture state for display
#[derive(Debug, Clone, Copy, Default)]
struct OutputState {
    has_stdout: bool,
    has_stderr: bool,
    has_combined: bool,
    has_empty: bool,  // Has entry but 0 bytes
}

impl OutputState {
    /// Get display glyph for output state
    fn glyph(&self) -> &'static str {
        if self.has_combined {
            ""  // Combined (merged, can't separate)
        } else if self.has_stdout && self.has_stderr {
            ""  // Both separate streams
        } else if self.has_stdout {
            ""  // Stdout only
        } else if self.has_stderr {
            ""  // Stderr only
        } else if self.has_empty {
            ""  // Captured but empty
        } else {
            "·"  // Not captured
        }
    }
}

/// Get output info for a batch of invocation IDs.
fn get_output_info_batch(store: &Store, inv_ids: &[&str]) -> bird::Result<std::collections::HashMap<String, OutputState>> {
    use std::collections::HashMap;

    if inv_ids.is_empty() {
        return Ok(HashMap::new());
    }

    // Build SQL to query output streams for all invocations at once
    let ids_sql = inv_ids.iter().map(|id| format!("'{}'", id)).collect::<Vec<_>>().join(", ");
    let sql = format!(
        "SELECT invocation_id, stream, byte_length FROM outputs WHERE invocation_id IN ({})",
        ids_sql
    );

    let result = store.query(&sql)?;

    let mut info: HashMap<String, OutputState> = HashMap::new();
    for row in &result.rows {
        if row.len() >= 3 {
            let inv_id = row[0].clone();
            let stream = row[1].clone();
            let byte_length: i64 = row[2].parse().unwrap_or(0);

            let entry = info.entry(inv_id).or_default();
            if byte_length > 0 {
                match stream.as_str() {
                    "stdout" => entry.has_stdout = true,
                    "stderr" => entry.has_stderr = true,
                    "combined" => entry.has_combined = true,
                    _ => {}
                }
            } else {
                entry.has_empty = true;
            }
        }
    }

    Ok(info)
}

/// Show quick reference for commands and query syntax.
pub fn quick_help() -> bird::Result<()> {
    print!("{}", QUICK_HELP);
    Ok(())
}

pub fn sql(query: &str) -> bird::Result<()> {
    let config = Config::load()?;
    let store = Store::open(config)?;

    let result = store.query(query)?;

    if result.rows.is_empty() {
        println!("No results.");
        return Ok(());
    }

    // Calculate column widths
    let mut widths: Vec<usize> = result.columns.iter().map(|c| c.len()).collect();
    for row in &result.rows {
        for (i, val) in row.iter().enumerate() {
            widths[i] = widths[i].max(val.len().min(50));
        }
    }

    // Print header
    for (i, col) in result.columns.iter().enumerate() {
        print!("{:width$} ", col, width = widths[i]);
    }
    println!();

    // Print separator
    for width in &widths {
        print!("{} ", "-".repeat(*width));
    }
    println!();

    // Print rows
    for row in &result.rows {
        for (i, val) in row.iter().enumerate() {
            let display = if val.len() > 50 {
                format!("{}...", &val[..47])
            } else {
                val.clone()
            };
            print!("{:width$} ", display, width = widths[i]);
        }
        println!();
    }

    println!("\n({} rows)", result.rows.len());

    Ok(())
}

/// Statistics about the BIRD store.
#[derive(serde::Serialize)]
pub struct BirdStats {
    pub root: String,
    pub client_id: String,
    pub storage_mode: String,
    pub current_session: CurrentSession,
    pub invocations: InvocationStats,
    pub sessions: SessionStats,
    pub events: EventStats,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub remotes: Vec<RemoteInfo>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub schemas: Option<SchemaStats>,
}

#[derive(serde::Serialize)]
pub struct RemoteInfo {
    pub name: String,
    pub remote_type: String,
    pub uri: String,
    pub auto_attach: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub invocations: Option<i64>,
}

#[derive(serde::Serialize)]
pub struct SchemaStats {
    pub local: SchemaCounts,
    pub caches: SchemaCounts,
    pub remotes: SchemaCounts,
    pub main: SchemaCounts,
    pub unified: SchemaCounts,
}

#[derive(serde::Serialize)]
pub struct SchemaCounts {
    pub invocations: i64,
    pub sessions: i64,
    pub outputs: i64,
    pub events: i64,
}

#[derive(serde::Serialize)]
pub struct InvocationStats {
    pub total: i64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub last: Option<LastInvocation>,
}

#[derive(serde::Serialize)]
pub struct LastInvocation {
    pub id: String,
    pub cmd: String,
    pub exit_code: i32,
    pub timestamp: String,
}

#[derive(serde::Serialize)]
pub struct SessionStats {
    pub total: i64,
}

#[derive(serde::Serialize)]
pub struct CurrentSession {
    pub hostname: String,
    pub username: String,
    pub shell: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub session_id: Option<String>,
}

#[derive(serde::Serialize)]
pub struct EventStats {
    pub total: i64,
    pub errors: i64,
    pub warnings: i64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub last_error: Option<LastError>,
}

#[derive(serde::Serialize)]
pub struct LastError {
    pub id: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub file: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub line: Option<i32>,
}

pub fn stats(format: &str, details: bool, field: Option<&str>) -> bird::Result<()> {
    let config = Config::load()?;
    let store = Store::open(config.clone())?;

    // Use a single connection for all queries to avoid multiple connection issues
    let conn = store.connection()?;

    // Get current session info from client_id (username@hostname) and environment
    let (username, hostname) = config.client_id.split_once('@')
        .map(|(u, h)| (u.to_string(), h.to_string()))
        .unwrap_or_else(|| (config.client_id.clone(), "unknown".to_string()));
    let shell = std::env::var("SHELL").unwrap_or_else(|_| "unknown".to_string());
    let session_id = std::env::var("__shq_session_id").ok();

    // Gather basic stats using the single connection
    let inv_count: i64 = conn
        .query_row("SELECT COUNT(*) FROM main.invocations", [], |r| r.get(0))
        .unwrap_or(0);
    let session_count: i64 = conn
        .query_row("SELECT COUNT(*) FROM main.sessions", [], |r| r.get(0))
        .unwrap_or(0);

    // Get last invocation
    let last_inv: Option<bird::InvocationSummary> = conn
        .query_row(
            "SELECT id, cmd, exit_code, timestamp FROM main.invocations ORDER BY timestamp DESC LIMIT 1",
            [],
            |row| {
                Ok(bird::InvocationSummary {
                    id: row.get::<_, String>(0)?,
                    cmd: row.get::<_, String>(1)?,
                    exit_code: row.get::<_, i32>(2)?,
                    timestamp: row.get::<_, String>(3)?,
                    duration_ms: None,
                })
            },
        )
        .ok();

    // Event counts
    let event_count: i64 = conn
        .query_row("SELECT COUNT(*) FROM main.events", [], |r| r.get(0))
        .unwrap_or(0);
    let error_count: i64 = conn
        .query_row("SELECT COUNT(*) FROM main.events WHERE severity = 'error'", [], |r| r.get(0))
        .unwrap_or(0);
    let warning_count: i64 = conn
        .query_row("SELECT COUNT(*) FROM main.events WHERE severity = 'warning'", [], |r| r.get(0))
        .unwrap_or(0);

    // Last error event (simplified - not querying for now)
    let last_error: Option<LastError> = None;

    // Gather remote info
    let remotes: Vec<RemoteInfo> = config
        .remotes
        .iter()
        .map(|r| {
            // Try to get invocation count from remote
            let inv_count = conn
                .query_row(
                    &format!("SELECT COUNT(*) FROM {}.invocations", r.quoted_schema_name()),
                    [],
                    |row| row.get::<_, i64>(0),
                )
                .ok();
            RemoteInfo {
                name: r.name.clone(),
                remote_type: format!("{:?}", r.remote_type).to_lowercase(),
                uri: r.uri.clone(),
                auto_attach: r.auto_attach,
                invocations: inv_count,
            }
        })
        .collect();

    // Gather per-schema stats (only if we have remotes or cached data)
    let schemas = if !config.remotes.is_empty() {
        // Helper to get counts from schema.table pattern
        let get_schema_counts = |schema: &str| -> SchemaCounts {
            SchemaCounts {
                invocations: conn
                    .query_row(&format!("SELECT COUNT(*) FROM {}.invocations", schema), [], |r| r.get(0))
                    .unwrap_or(0),
                sessions: conn
                    .query_row(&format!("SELECT COUNT(*) FROM {}.sessions", schema), [], |r| r.get(0))
                    .unwrap_or(0),
                outputs: conn
                    .query_row(&format!("SELECT COUNT(*) FROM {}.outputs", schema), [], |r| r.get(0))
                    .unwrap_or(0),
                events: conn
                    .query_row(&format!("SELECT COUNT(*) FROM {}.events", schema), [], |r| r.get(0))
                    .unwrap_or(0),
            }
        };

        // Helper to get counts from table-returning macros (remotes use macros)
        let get_macro_counts = |prefix: &str| -> SchemaCounts {
            SchemaCounts {
                invocations: conn
                    .query_row(&format!("SELECT COUNT(*) FROM {}_invocations()", prefix), [], |r| r.get(0))
                    .unwrap_or(0),
                sessions: conn
                    .query_row(&format!("SELECT COUNT(*) FROM {}_sessions()", prefix), [], |r| r.get(0))
                    .unwrap_or(0),
                outputs: conn
                    .query_row(&format!("SELECT COUNT(*) FROM {}_outputs()", prefix), [], |r| r.get(0))
                    .unwrap_or(0),
                events: conn
                    .query_row(&format!("SELECT COUNT(*) FROM {}_events()", prefix), [], |r| r.get(0))
                    .unwrap_or(0),
            }
        };

        Some(SchemaStats {
            local: get_schema_counts("local"),
            caches: get_schema_counts("caches"),
            remotes: get_macro_counts("remotes"),  // Uses remotes_*() macros
            main: get_schema_counts("main"),
            unified: get_schema_counts("unified"),
        })
    } else {
        None
    };

    // Build stats struct
    let stats = BirdStats {
        root: config.bird_root.display().to_string(),
        client_id: config.client_id.clone(),
        storage_mode: config.storage_mode.to_string(),
        current_session: CurrentSession {
            hostname,
            username,
            shell,
            session_id,
        },
        invocations: InvocationStats {
            total: inv_count,
            last: last_inv.map(|inv| LastInvocation {
                id: inv.id.clone(),
                cmd: inv.cmd.clone(),
                exit_code: inv.exit_code,
                timestamp: inv.timestamp.clone(),
            }),
        },
        sessions: SessionStats {
            total: session_count,
        },
        events: EventStats {
            total: event_count,
            errors: error_count,
            warnings: warning_count,
            last_error,
        },
        remotes,
        schemas,
    };

    // Handle --field option for scripting
    if let Some(field_name) = field {
        let value = match field_name {
            "root" => stats.root.clone(),
            "client_id" => stats.client_id.clone(),
            "storage_mode" => stats.storage_mode.clone(),
            "hostname" => stats.current_session.hostname.clone(),
            "username" => stats.current_session.username.clone(),
            "shell" => stats.current_session.shell.clone(),
            "session_id" => stats.current_session.session_id.clone().unwrap_or_default(),
            "invocations" | "invocations.total" => stats.invocations.total.to_string(),
            "sessions" | "sessions.total" => stats.sessions.total.to_string(),
            "events" | "events.total" => stats.events.total.to_string(),
            "errors" | "events.errors" => stats.events.errors.to_string(),
            "warnings" | "events.warnings" => stats.events.warnings.to_string(),
            _ => {
                eprintln!("Unknown field: {}", field_name);
                eprintln!("Available fields: root, client_id, storage_mode, hostname, username, shell, session_id, invocations, sessions, events, errors, warnings");
                return Ok(());
            }
        };
        println!("{}", value);
        return Ok(());
    }

    match format {
        "json" => {
            println!("{}", serde_json::to_string_pretty(&stats).unwrap());
        }
        _ => {
            println!("Root:         {}", stats.root);
            println!("Client ID:    {}", stats.client_id);
            println!("Storage mode: {}", stats.storage_mode);
            if details {
                println!("Hostname:     {}", stats.current_session.hostname);
                println!("Username:     {}", stats.current_session.username);
                println!("Shell:        {}", stats.current_session.shell);
                if let Some(ref sid) = stats.current_session.session_id {
                    println!("Session ID:   {}", sid);
                }
            }
            println!();
            println!("Total invocations: {}", stats.invocations.total);
            println!("Total sessions:    {}", stats.sessions.total);
            if let Some(ref inv) = stats.invocations.last {
                println!("Last command:      {} (exit {})", inv.cmd, inv.exit_code);
            }
            println!();
            println!("Total events:      {}", stats.events.total);
            println!("  Errors:          {}", stats.events.errors);
            println!("  Warnings:        {}", stats.events.warnings);
            if let Some(ref err) = stats.events.last_error {
                let location = match (&err.file, err.line) {
                    (Some(f), Some(l)) => format!(" at {}:{}", f, l),
                    (Some(f), None) => format!(" in {}", f),
                    _ => String::new(),
                };
                let msg = err.message.as_deref().unwrap_or("-");
                println!("  Last error:      {}{}", truncate_string(msg, 40), location);
            }

            // Show remotes
            if !stats.remotes.is_empty() {
                println!();
                println!("Remotes:");
                for r in &stats.remotes {
                    let inv_str = r.invocations.map(|n| format!(" ({} invocations)", n)).unwrap_or_default();
                    let attach = if r.auto_attach { "" } else { " [manual]" };
                    println!("  {} [{}]: {}{}{}", r.name, r.remote_type, r.uri, inv_str, attach);
                }
            }

            // Show per-schema stats
            if let Some(ref s) = stats.schemas {
                println!();
                println!("Schema Summary:");
                println!("  {:12} {:>10} {:>10} {:>10} {:>10}", "SCHEMA", "INVOCS", "SESSIONS", "OUTPUTS", "EVENTS");
                println!("  {:12} {:>10} {:>10} {:>10} {:>10}", "local", s.local.invocations, s.local.sessions, s.local.outputs, s.local.events);
                println!("  {:12} {:>10} {:>10} {:>10} {:>10}", "caches", s.caches.invocations, s.caches.sessions, s.caches.outputs, s.caches.events);
                println!("  {:12} {:>10} {:>10} {:>10} {:>10}", "remotes", s.remotes.invocations, s.remotes.sessions, s.remotes.outputs, s.remotes.events);
                println!("  {:12} {:>10} {:>10} {:>10} {:>10}", "main", s.main.invocations, s.main.sessions, s.main.outputs, s.main.events);
                println!("  {:12} {:>10} {:>10} {:>10} {:>10}", "unified", s.unified.invocations, s.unified.sessions, s.unified.outputs, s.unified.events);
            }
        }
    }

    Ok(())
}

/// Move old data from recent to archive.
pub fn archive(days: u32, dry_run: bool, extract_first: bool) -> bird::Result<()> {
    let config = Config::load()?;
    let store = Store::open(config)?;

    if dry_run {
        println!("Dry run - no changes will be made\n");
    }

    // Optionally extract events from invocations before archiving
    if extract_first && !dry_run {
        println!("Extracting events from invocations to be archived...");
        let cutoff_date = chrono::Utc::now().date_naive() - chrono::Duration::days(days as i64);
        let invocations = store.invocations_without_events(Some(cutoff_date), None)?;

        if !invocations.is_empty() {
            let mut total_events = 0;
            for inv in &invocations {
                let count = store.extract_events(&inv.id, None)?;
                total_events += count;
            }
            println!(
                "  Extracted {} events from {} invocations",
                total_events,
                invocations.len()
            );
        }
    }

    let stats = store.archive_old_data(days, dry_run)?;

    if stats.partitions_archived > 0 {
        println!(
            "Archived {} partitions ({} files, {})",
            stats.partitions_archived,
            stats.files_moved,
            format_bytes(stats.bytes_moved)
        );
    } else {
        println!("Nothing to archive.");
    }

    Ok(())
}

/// Compact parquet files to reduce storage and improve performance.
#[allow(clippy::too_many_arguments)]
pub fn compact(
    file_threshold: usize,
    recompact_threshold: usize,
    consolidate: bool,
    extract_first: bool,
    session: Option<&str>,
    today_only: bool,
    quiet: bool,
    recent_only: bool,
    archive_only: bool,
    dry_run: bool,
) -> bird::Result<()> {
    let config = Config::load()?;
    let store = Store::open(config)?;

    if dry_run && !quiet {
        println!("Dry run - no changes will be made\n");
    }

    // Extract events first if requested
    if extract_first && !dry_run {
        if !quiet {
            println!("Extracting events from invocations before compacting...");
        }
        let invocations = store.invocations_without_events(None, None)?;
        let mut extracted = 0;
        for inv in &invocations {
            let count = store.extract_events(&inv.id, None)?;
            extracted += count;
        }
        if !quiet && extracted > 0 {
            println!("  Extracted {} events from {} invocations\n", extracted, invocations.len());
        }
    }

    let opts = CompactOptions {
        file_threshold,
        recompact_threshold,
        consolidate,
        dry_run,
        session_filter: session.map(|s| s.to_string()),
    };

    // Session-specific compaction (lightweight, used by shell hooks)
    if let Some(session_id) = session {
        let stats = if today_only {
            // today_only uses legacy API (no recompact support for shell hooks)
            store.compact_session_today(session_id, file_threshold, dry_run)?
        } else {
            store.compact_for_session_with_opts(session_id, &opts)?
        };

        if stats.sessions_compacted > 0 {
            let action = if consolidate { "Consolidated" } else { "Compacted" };
            println!("{} session '{}':", action, session_id);
            println!("  {} files -> {} files", stats.files_before, stats.files_after);
            println!(
                "  {} -> {} ({})",
                format_bytes(stats.bytes_before),
                format_bytes(stats.bytes_after),
                format_reduction(stats.bytes_before, stats.bytes_after)
            );
        } else if !quiet {
            println!("Nothing to compact for session '{}'.", session_id);
        }
        return Ok(());
    }

    // Global compaction
    let mut total_stats = bird::CompactStats::default();

    if !archive_only {
        let stats = store.compact_recent_with_opts(&opts)?;
        total_stats.add(&stats);
    }

    if !recent_only {
        let stats = store.compact_archive_with_opts(&opts)?;
        total_stats.add(&stats);
    }

    if total_stats.sessions_compacted > 0 {
        let action = if consolidate { "Consolidated" } else { "Compacted" };
        println!(
            "{} {} sessions across {} partitions",
            action, total_stats.sessions_compacted, total_stats.partitions_compacted
        );
        println!(
            "  {} files -> {} files",
            total_stats.files_before, total_stats.files_after
        );
        println!(
            "  {} -> {} ({})",
            format_bytes(total_stats.bytes_before),
            format_bytes(total_stats.bytes_after),
            format_reduction(total_stats.bytes_before, total_stats.bytes_after)
        );
    } else if !quiet {
        println!("Nothing to compact.");
    }

    Ok(())
}

/// Format bytes for display.
fn format_bytes(bytes: u64) -> String {
    const KB: u64 = 1024;
    const MB: u64 = KB * 1024;
    const GB: u64 = MB * 1024;

    if bytes >= GB {
        format!("{:.1} GB", bytes as f64 / GB as f64)
    } else if bytes >= MB {
        format!("{:.1} MB", bytes as f64 / MB as f64)
    } else if bytes >= KB {
        format!("{:.1} KB", bytes as f64 / KB as f64)
    } else {
        format!("{} bytes", bytes)
    }
}

/// Format byte reduction as percentage.
fn format_reduction(before: u64, after: u64) -> String {
    if before == 0 {
        return "0%".to_string();
    }
    if after >= before {
        let increase = ((after - before) as f64 / before as f64) * 100.0;
        format!("+{:.1}%", increase)
    } else {
        let reduction = ((before - after) as f64 / before as f64) * 100.0;
        format!("-{:.1}%", reduction)
    }
}

/// Output ignore patterns for shell hooks (colon-separated).
pub fn hook_ignore_patterns() -> bird::Result<()> {
    let config = Config::load()?;
    let patterns = config.hooks.ignore_patterns.join(":");
    println!("{}", patterns);
    Ok(())
}

/// Output shell integration code.
pub fn hook_init(shell: Option<&str>, inactive: bool, prompt_indicator: bool, quiet: bool) -> bird::Result<()> {
    use crate::hooks::{self, Shell, Mode};

    // Auto-detect shell from $SHELL if not specified
    let shell_str = shell
        .map(|s| s.to_string())
        .or_else(|| std::env::var("SHELL").ok())
        .unwrap_or_default();

    let shell_type = if shell_str.contains("zsh") {
        Shell::Zsh
    } else if shell_str.contains("bash") {
        Shell::Bash
    } else {
        eprintln!("Unknown shell type. Use --shell zsh or --shell bash");
        std::process::exit(1);
    };

    let mode = if inactive { Mode::Inactive } else { Mode::Active };

    // Output quiet mode variable if requested
    if quiet {
        println!("__shq_quiet=1");
    }

    // Generate and output the hook
    print!("{}", hooks::generate(shell_type, mode, prompt_indicator));

    Ok(())
}

/// Order for limiting results.
#[derive(Clone, Copy, Debug)]
pub enum LimitOrder {
    Any,   // Just limit, no specific order
    First, // First N (head)
    Last,  // Last N (tail)
}

/// Query parsed events from invocation outputs.
#[allow(clippy::too_many_arguments)]
pub fn events(
    query_str: &str,
    severity: Option<&str>,
    count_only: bool,
    limit: usize,
    order: LimitOrder,
    reparse: bool,
    extract: bool,
    format: Option<&str>,
) -> bird::Result<()> {
    let config = Config::load()?;
    let store = Store::open(config)?;

    // Parse query (filters and range applied by query_invocations)
    let query = parse_query(query_str);

    // Handle reparse mode: re-extract events from outputs
    if reparse {
        let invocations = store.query_invocations(&query)?;
        let mut total_events = 0;

        for inv in &invocations {
            // Delete existing events for this invocation
            store.delete_events_for_invocation(&inv.id)?;
            // Re-extract
            let count = store.extract_events(&inv.id, format)?;
            total_events += count;
        }

        println!(
            "Re-extracted {} events from {} invocations",
            total_events,
            invocations.len()
        );
        return Ok(());
    }

    // Get invocations matching query (with filters applied)
    let invocations = store.query_invocations(&query)?;
    if invocations.is_empty() {
        println!("No invocations found.");
        return Ok(());
    }

    // Extract events if requested and not already extracted
    if extract {
        for inv in &invocations {
            let existing = store.event_count(&EventFilters {
                invocation_id: Some(inv.id.clone()),
                ..Default::default()
            })?;
            if existing == 0 {
                let _ = store.extract_events(&inv.id, format);
            }
        }
    }

    // Build filters
    let inv_ids: Vec<String> = invocations.iter().map(|inv| inv.id.clone()).collect();
    let filters = EventFilters {
        severity: severity.map(|s| s.to_string()),
        invocation_ids: Some(inv_ids),
        limit: Some(limit),
        // TODO: Add order support to EventFilters when needed
        ..Default::default()
    };
    let _ = order; // Will be used when EventFilters supports ordering

    // Count only mode
    if count_only {
        let count = store.event_count(&filters)?;
        println!("{}", count);
        return Ok(());
    }

    // Query events
    let events = store.query_events(&filters)?;

    if events.is_empty() {
        println!("No events found.");
        return Ok(());
    }

    // Display events
    println!(
        "{:<8} {:<40} {:<30} MESSAGE",
        "SEVERITY", "FILE:LINE", "CODE"
    );
    println!("{}", "-".repeat(100));

    for event in &events {
        let sev = event.severity.as_deref().unwrap_or("-");
        let location = match (&event.ref_file, event.ref_line) {
            (Some(f), Some(l)) => format!("{}:{}", truncate_path(f, 35), l),
            (Some(f), None) => truncate_path(f, 40).to_string(),
            _ => "-".to_string(),
        };
        let code = event
            .error_code
            .as_deref()
            .or(event.test_name.as_deref())
            .unwrap_or("-");
        let message = event
            .message
            .as_deref()
            .map(|m| truncate_string(m, 50))
            .unwrap_or_else(|| "-".to_string());

        // Color based on severity
        let severity_display = match sev {
            "error" => format!("\x1b[31m{:<8}\x1b[0m", sev),
            "warning" => format!("\x1b[33m{:<8}\x1b[0m", sev),
            _ => format!("{:<8}", sev),
        };

        println!(
            "{} {:<40} {:<30} {}",
            severity_display, location, code, message
        );
    }

    println!("\n({} events)", events.len());

    Ok(())
}

/// Extract events from an invocation's output.
#[allow(clippy::too_many_arguments)]
pub fn extract_events(
    selector: &str,
    format: Option<&str>,
    quiet: bool,
    force: bool,
    all: bool,
    since: Option<&str>,
    limit: Option<usize>,
    dry_run: bool,
) -> bird::Result<()> {
    let config = Config::load()?;
    let store = Store::open(config)?;

    // Backfill mode: extract from all invocations without events
    if all {
        return extract_events_backfill(&store, format, quiet, since, limit, dry_run);
    }

    // Single invocation mode
    let invocation_id = resolve_invocation_id(&store, selector)?;

    // Check if events already exist
    let existing_count = store.event_count(&EventFilters {
        invocation_id: Some(invocation_id.clone()),
        ..Default::default()
    })?;

    if existing_count > 0 && !force {
        if !quiet {
            println!(
                "Events already exist for invocation {} ({} events). Use --force to re-extract.",
                invocation_id, existing_count
            );
        }
        return Ok(());
    }

    // Delete existing events if forcing
    if force && existing_count > 0 {
        store.delete_events_for_invocation(&invocation_id)?;
    }

    // Extract events
    let count = store.extract_events(&invocation_id, format)?;

    if !quiet {
        if count > 0 {
            println!("Extracted {} events from invocation {}", count, invocation_id);
        } else {
            println!("No events found in invocation {}", invocation_id);
        }
    }

    Ok(())
}

/// Backfill events from all invocations that don't have events yet.
fn extract_events_backfill(
    store: &Store,
    format: Option<&str>,
    quiet: bool,
    since: Option<&str>,
    limit: Option<usize>,
    dry_run: bool,
) -> bird::Result<()> {
    use chrono::NaiveDate;

    // Parse since date if provided
    let since_date = if let Some(date_str) = since {
        Some(
            NaiveDate::parse_from_str(date_str, "%Y-%m-%d")
                .map_err(|e| bird::Error::Config(format!("Invalid date '{}': {}", date_str, e)))?,
        )
    } else {
        None
    };

    // Get invocations without events
    let invocations = store.invocations_without_events(since_date, limit)?;

    if invocations.is_empty() {
        if !quiet {
            println!("No invocations found without events.");
        }
        return Ok(());
    }

    if dry_run {
        println!("Would extract events from {} invocations:", invocations.len());
        for inv in &invocations {
            let cmd_preview: String = inv.cmd.chars().take(60).collect();
            let suffix = if inv.cmd.len() > 60 { "..." } else { "" };
            println!("  {} {}{}", &inv.id[..8], cmd_preview, suffix);
        }
        return Ok(());
    }

    let mut total_events = 0;
    let mut processed = 0;

    for inv in &invocations {
        let count = store.extract_events(&inv.id, format)?;
        total_events += count;
        processed += 1;

        if !quiet && count > 0 {
            println!("  {} events from: {}", count, truncate_cmd(&inv.cmd, 50));
        }
    }

    if !quiet {
        println!(
            "Extracted {} events from {} invocations.",
            total_events, processed
        );
    }

    Ok(())
}

/// Truncate a command string for display.
fn truncate_cmd(cmd: &str, max_len: usize) -> String {
    if cmd.len() <= max_len {
        cmd.to_string()
    } else {
        format!("{}...", &cmd[..max_len])
    }
}

/// Resolve a selector (negative offset, ~N syntax, or UUID) to an invocation ID.
fn resolve_invocation_id(store: &Store, selector: &str) -> bird::Result<String> {
    // Handle ~N syntax (e.g., ~1 for most recent, ~2 for second-to-last)
    if let Some(stripped) = selector.strip_prefix('~') {
        if let Ok(n) = stripped.parse::<usize>() {
            if n > 0 {
                let invocations = store.recent_invocations(n)?;
                if let Some(inv) = invocations.last() {
                    return Ok(inv.id.clone());
                } else {
                    return Err(bird::Error::NotFound(format!(
                        "No invocation found at offset ~{}",
                        n
                    )));
                }
            }
        }
    }

    // Handle negative offset (e.g., -1 for last, -2 for second-to-last)
    if let Ok(offset) = selector.parse::<i64>() {
        if offset < 0 {
            let n = (-offset) as usize;
            let invocations = store.recent_invocations(n)?;
            if let Some(inv) = invocations.last() {
                return Ok(inv.id.clone());
            } else {
                return Err(bird::Error::NotFound(format!(
                    "No invocation found at offset {}",
                    offset
                )));
            }
        }
    }

    // Try short ID lookup
    if let Some(id) = try_find_by_id(store, selector)? {
        return Ok(id);
    }

    // Assume it's a full UUID
    Ok(selector.to_string())
}

/// Truncate a path for display, keeping the filename visible.
fn truncate_path(path: &str, max_len: usize) -> &str {
    if path.len() <= max_len {
        return path;
    }
    // Try to keep at least the filename
    if let Some(pos) = path.rfind('/') {
        let filename = &path[pos + 1..];
        if filename.len() < max_len {
            return &path[path.len() - max_len..];
        }
    }
    &path[path.len() - max_len..]
}

/// Truncate a string for display.
fn truncate_string(s: &str, max_len: usize) -> String {
    if s.len() <= max_len {
        s.to_string()
    } else {
        format!("{}...", &s[..max_len - 3])
    }
}

/// Check if a string looks like a hex ID (short or full UUID).
fn looks_like_hex_id(s: &str) -> bool {
    // Must be at least 4 chars and only contain hex digits and dashes
    s.len() >= 4 && s.chars().all(|c| c.is_ascii_hexdigit() || c == '-')
}

/// Try to find an invocation by tag, short ID, or full ID.
/// Returns Some(full_id) if found, None if not found or query doesn't look like an ID/tag.
fn try_find_by_id(store: &Store, query_str: &str) -> bird::Result<Option<String>> {
    let trimmed = query_str.trim();

    // Check for tag lookup (:tagname)
    if let Some(tag) = trimmed.strip_prefix(':') {
        if let Some(id) = store.find_by_tag(tag)? {
            return Ok(Some(id));
        }
        // Tag not found - this is an explicit error, not a fallback to query
        return Err(bird::Error::NotFound(format!("Tag '{}' not found", tag)));
    }

    if !looks_like_hex_id(trimmed) {
        return Ok(None);
    }

    // Try exact match first (full UUID)
    let result = store.query(&format!(
        "SELECT id::VARCHAR FROM invocations WHERE id::VARCHAR = '{}' LIMIT 1",
        trimmed
    ))?;

    if !result.rows.is_empty() {
        return Ok(Some(result.rows[0][0].clone()));
    }

    // Try suffix match (short ID - we show last 8 chars of UUIDv7)
    let result = store.query(&format!(
        "SELECT id::VARCHAR FROM invocations WHERE suffix(id::VARCHAR, '{}') ORDER BY timestamp DESC LIMIT 1",
        trimmed
    ))?;

    if !result.rows.is_empty() {
        return Ok(Some(result.rows[0][0].clone()));
    }

    Ok(None)
}

/// Resolve a query to a single invocation ID.
fn resolve_query_to_invocation(store: &Store, query: &Query) -> bird::Result<String> {
    // Apply filters and get matching invocations (default to 1 for single-item commands)
    let invocations = store.query_invocations_with_limit(query, 1)?;

    // The range.start indicates how many results we want, and we take the last one
    // e.g., ~1 means "last 1" so we get 1 result and take it
    // e.g., ~5 means "last 5" so we get 5 results and take the 5th (oldest of those)
    let n = query.range.map(|r| r.start).unwrap_or(1);
    let idx = n.min(invocations.len()).saturating_sub(1);

    if let Some(inv) = invocations.get(idx) {
        Ok(inv.id.clone())
    } else {
        Err(bird::Error::NotFound("No matching invocation found".to_string()))
    }
}

/// Show detailed info about an invocation.
pub fn info(query_str: &str, format: &str, field: Option<&str>) -> bird::Result<()> {
    let config = Config::load()?;
    let store = Store::open(config)?;

    // First try to find by ID (short or full)
    let invocation_id = if let Some(id) = try_find_by_id(&store, query_str)? {
        id
    } else {
        // Fall back to query system
        let query = parse_query(query_str);
        resolve_query_to_invocation(&store, &query)?
    };

    // Get full invocation details via SQL
    let result = store.query(&format!(
        "SELECT id, cmd, cwd, exit_code, timestamp, duration_ms, session_id, tag
         FROM invocations
         WHERE id = '{}'",
        invocation_id
    ))?;

    if result.rows.is_empty() {
        return Err(bird::Error::NotFound(format!("Invocation {} not found", invocation_id)));
    }

    let row = &result.rows[0];
    let id = &row[0];
    let cmd = &row[1];
    let cwd = &row[2];
    let exit_code = &row[3];
    let timestamp = &row[4];
    let duration_ms = &row[5];
    let session_id = &row[6];
    let tag = &row[7];

    // Get output info
    let outputs = store.get_outputs(&invocation_id, None)?;
    let stdout_size: i64 = outputs.iter().filter(|o| o.stream == "stdout").map(|o| o.byte_length).sum();
    let stderr_size: i64 = outputs.iter().filter(|o| o.stream == "stderr").map(|o| o.byte_length).sum();

    // Get event count
    let event_count = store.event_count(&EventFilters {
        invocation_id: Some(invocation_id.clone()),
        ..Default::default()
    })?;

    // If a specific field is requested, just print that value (for scripting)
    if let Some(f) = field {
        let value = match f.to_lowercase().as_str() {
            "id" => id.to_string(),
            "cmd" | "command" => cmd.to_string(),
            "cwd" | "dir" | "working_dir" => cwd.to_string(),
            "exit" | "exit_code" => exit_code.to_string(),
            "timestamp" | "time" => timestamp.to_string(),
            "duration" | "duration_ms" => duration_ms.to_string(),
            "session" | "session_id" => session_id.to_string(),
            "tag" => tag.to_string(),
            "stdout" | "stdout_bytes" => stdout_size.to_string(),
            "stderr" | "stderr_bytes" => stderr_size.to_string(),
            "events" | "event_count" => event_count.to_string(),
            _ => return Err(bird::Error::Config(format!("Unknown field: {}", f))),
        };
        println!("{}", value);
        return Ok(());
    }

    match format {
        "json" => {
            println!(r#"{{"#);
            println!(r#"  "id": "{}","#, id);
            println!(r#"  "timestamp": "{}","#, timestamp);
            println!(r#"  "cmd": "{}","#, cmd.replace('\\', "\\\\").replace('"', "\\\""));
            println!(r#"  "cwd": "{}","#, cwd.replace('\\', "\\\\").replace('"', "\\\""));
            println!(r#"  "exit_code": {},"#, exit_code);
            println!(r#"  "duration_ms": {},"#, duration_ms);
            println!(r#"  "session_id": "{}","#, session_id);
            if tag != "NULL" && !tag.is_empty() {
                println!(r#"  "tag": "{}","#, tag);
            }
            println!(r#"  "stdout_bytes": {},"#, stdout_size);
            println!(r#"  "stderr_bytes": {},"#, stderr_size);
            println!(r#"  "event_count": {}"#, event_count);
            println!(r#"}}"#);
        }
        _ => {
            // Table format
            println!("ID:          {}", id);
            println!("Timestamp:   {}", timestamp);
            println!("Command:     {}", cmd);
            println!("Working Dir: {}", cwd);
            println!("Exit Code:   {}", exit_code);
            println!("Duration:    {}ms", duration_ms);
            println!("Session:     {}", session_id);
            if tag != "NULL" && !tag.is_empty() {
                println!("Tag:         {}", tag);
            }
            println!("Stdout:      {} bytes", stdout_size);
            println!("Stderr:      {} bytes", stderr_size);
            println!("Events:      {}", event_count);
        }
    }

    Ok(())
}

/// Re-run a previous command.
pub fn rerun(query_str: &str, dry_run: bool, no_capture: bool) -> bird::Result<()> {
    use std::io::Write;

    let config = Config::load()?;
    let store = Store::open(config)?;

    // First try to find by ID (short or full)
    let invocation_id = if let Some(id) = try_find_by_id(&store, query_str)? {
        id
    } else {
        // Fall back to query system
        let query = parse_query(query_str);
        resolve_query_to_invocation(&store, &query)?
    };

    // Get full invocation details via SQL (need cmd and cwd)
    let result = store.query(&format!(
        "SELECT cmd, cwd FROM invocations WHERE id = '{}'",
        invocation_id
    ))?;

    if result.rows.is_empty() {
        return Err(bird::Error::NotFound(format!("Invocation {} not found", invocation_id)));
    }

    let cmd = &result.rows[0][0];
    let cwd = &result.rows[0][1];

    if dry_run {
        println!("Would run: {}", cmd);
        println!("In directory: {}", cwd);
        return Ok(());
    }

    // Print the command being re-run
    eprintln!("\x1b[2m$ {}\x1b[0m", cmd);

    if no_capture {
        // Just execute without capturing
        let shell = std::env::var("SHELL").unwrap_or_else(|_| "sh".to_string());
        let status = Command::new(&shell)
            .arg("-c")
            .arg(cmd)
            .current_dir(cwd)
            .status()?;

        if !status.success() {
            std::process::exit(status.code().unwrap_or(1));
        }
    } else {
        // Use shq run to capture the command
        let start = std::time::Instant::now();
        let shell = std::env::var("SHELL").unwrap_or_else(|_| "sh".to_string());
        let output = Command::new(&shell)
            .arg("-c")
            .arg(cmd)
            .current_dir(cwd)
            .output()?;
        let duration_ms = start.elapsed().as_millis() as i64;

        // Display output
        if !output.stdout.is_empty() {
            io::stdout().write_all(&output.stdout)?;
        }
        if !output.stderr.is_empty() {
            io::stderr().write_all(&output.stderr)?;
        }

        let exit_code = output.status.code().unwrap_or(-1);

        // Save to BIRD
        let config = Config::load()?;
        let store = Store::open(config.clone())?;
        let sid = session_id();

        let session = SessionRecord::new(
            &sid,
            &config.client_id,
            invoker_name(),
            invoker_pid(),
            "shell",
        );

        let record = InvocationRecord::new(
            &sid,
            cmd,
            cwd,
            exit_code,
            &config.client_id,
        )
        .with_duration(duration_ms);

        // Build batch with all related records
        let mut batch = InvocationBatch::new(record).with_session(session);

        if !output.stdout.is_empty() {
            batch = batch.with_output("stdout", output.stdout.clone());
        }
        if !output.stderr.is_empty() {
            batch = batch.with_output("stderr", output.stderr.clone());
        }

        // Write everything atomically
        store.write_batch(&batch)?;

        if !output.status.success() {
            std::process::exit(exit_code);
        }
    }

    Ok(())
}

const QUICK_HELP: &str = r#"
SHQ QUICK REFERENCE
===================

COMMANDS                                    EXAMPLES
────────────────────────────────────────────────────────────────────────────────
output (o, show)   Show captured output     shq o ~1          shq o %/make/~1
invocations (i)    List command history     shq i ~20         shq i %exit<>0~10
events (e)         Show parsed events       shq e ~10         shq e -s error ~5
info (I)           Invocation details       shq I ~1          shq I %/test/~1
rerun (R, !!)      Re-run a command         shq R ~1          shq R %/make/~1
run (r)            Run and capture          shq r cargo test  shq r -c "make all"
sql (q)            Execute SQL query        shq q "SELECT * FROM invocations LIMIT 5"

QUERY SYNTAX: [source][path][filters][range]
────────────────────────────────────────────────────────────────────────────────
RANGE         1 or ~1     Last command
              5 or ~5     Last 5 commands
              ~10:5       Commands 10 to 5 ago

SOURCE        Format: host:type:client:session:
              shell:           Shell commands on this host
              shell:bash:      Bash shells only
              shell:zsh:       Zsh shells only
              myhost:shell::   All shells on myhost
              *:*:*:*:         Everything everywhere (all hosts, all types)
              *:shell:*:*:     All shell commands on all hosts

PATH          .           Current directory
              ~/Projects/ Home-relative
              /tmp/       Absolute path

FILTERS       %failed     Non-zero exit code (alias for %exit<>0)
              %success    Successful commands (alias for %exit=0)
              %ok         Same as %success
              %exit<>0    Non-zero exit code
              %exit=0     Successful commands only
              %duration>5000   Took > 5 seconds
              %cmd~=test  Command matches regex
              %cwd~=/src/ Working dir matches

CMD REGEX     %/make/     Commands containing "make"
              %/^cargo/   Commands starting with "cargo"
              %/test$/    Commands ending with "test"

OPERATORS     =  equals      <>  not equals     ~=  regex match
              >  greater     <   less           >=  gte    <=  lte

EXAMPLES
────────────────────────────────────────────────────────────────────────────────
shq o                        Show output of last command (default: 1)
shq o 1                      Same as above
shq o -E 1                   Show only stderr of last command
shq o %/make/~1              Output of last make command
shq o %exit<>0~1             Output of last failed command

shq i                        Last 20 commands (default)
shq i 50                     Last 50 commands
shq i %failed~20             Last 20 failed commands
shq i %failed                All failed commands (up to default limit)
shq i %duration>10000~10     Last 10 commands that took >10s
shq i %/cargo/~10            Last 10 cargo commands

shq e                        Events from last 10 commands (default)
shq e 5                      Events from last 5 commands
shq e -s error 10            Only errors from last 10 commands
shq e %/cargo build/~1       Events from last cargo build

shq R                        Re-run last command
shq R 3                      Re-run 3rd-last command
shq R %/make test/~1         Re-run last "make test"
shq R -n %/deploy/~1         Dry-run: show what would run

shq I                        Details about last command
shq I -f json 1              Details as JSON

.~5                          Last 5 commands in current directory
~/Projects/foo/~10           Last 10 in ~/Projects/foo/
shell:%exit<>0~5             Last 5 failed shell commands

"#;



// ZSH hook without prompt indicator - same as ZSH_HOOK but without PS1 modification

// Format hints management

/// List format hints (user-defined and optionally built-in).
pub fn format_hints_list(show_builtin: bool, show_user: bool, filter: Option<&str>) -> bird::Result<()> {
    let config = Config::load()?;
    let store = Store::open(config)?;

    let hints = store.load_format_hints()?;

    // Helper to check if a hint matches the filter
    let matches_filter = |pattern: &str, format: &str| -> bool {
        match filter {
            None => true,
            Some(f) => {
                let f_lower = f.to_lowercase();
                pattern.to_lowercase().contains(&f_lower) || format.to_lowercase().contains(&f_lower)
            }
        }
    };

    // Show user-defined hints
    if show_user {
        let user_hints: Vec<_> = hints.hints()
            .iter()
            .filter(|h| matches_filter(&h.pattern, &h.format))
            .collect();

        if user_hints.is_empty() {
            if filter.is_some() {
                println!("No user-defined format hints matching filter.");
            } else {
                println!("No user-defined format hints.");
            }
        } else {
            println!("User-defined format hints:");
            println!("{:<6} {:<30} FORMAT", "PRI", "PATTERN");
            println!("{}", "-".repeat(60));
            for hint in user_hints {
                println!("{:<6} {:<30} {}", hint.priority, hint.pattern, hint.format);
            }
        }
        println!();
    }

    // Show built-in formats from duck_hunt
    if show_builtin {
        match store.list_builtin_formats() {
            Ok(formats) => {
                let filtered: Vec<_> = formats.iter()
                    .filter(|f| matches_filter(&f.pattern, &f.format))
                    .collect();

                if filtered.is_empty() {
                    if filter.is_some() {
                        println!("No built-in formats matching filter.");
                    } else {
                        println!("No built-in formats available.");
                    }
                } else {
                    println!("Available formats (from duck_hunt):");
                    println!("{:<6} {:<20} DESCRIPTION", "PRI", "FORMAT");
                    println!("{}", "-".repeat(70));
                    for fmt in filtered {
                        // pattern field contains description for builtin formats
                        let desc = if fmt.pattern.len() > 45 {
                            format!("{}...", &fmt.pattern[..42])
                        } else {
                            fmt.pattern.clone()
                        };
                        println!("{:<6} {:<20} {}", fmt.priority, fmt.format, desc);
                    }
                }
            }
            Err(e) => {
                eprintln!("Warning: Could not list built-in formats: {}", e);
            }
        }
    }

    Ok(())
}

/// Add a format hint.
pub fn format_hints_add(pattern: &str, format: &str, priority: Option<i32>) -> bird::Result<()> {
    let config = Config::load()?;
    let store = Store::open(config)?;

    let mut hints = store.load_format_hints()?;

    let priority = priority.unwrap_or(bird::format_hints::DEFAULT_PRIORITY);
    let hint = bird::FormatHint::with_priority(pattern, format, priority);

    // Check if pattern already exists
    if hints.get(pattern).is_some() {
        println!("Updating existing pattern: {}", pattern);
    }

    hints.add(hint);
    store.save_format_hints(&hints)?;

    println!("Added: {} -> {} (priority {})", pattern, format, priority);
    Ok(())
}

/// Remove a format hint by pattern.
pub fn format_hints_remove(pattern: &str) -> bird::Result<()> {
    let config = Config::load()?;
    let store = Store::open(config)?;

    let mut hints = store.load_format_hints()?;

    if hints.remove(pattern) {
        store.save_format_hints(&hints)?;
        println!("Removed: {}", pattern);
    } else {
        println!("Pattern not found: {}", pattern);
    }

    Ok(())
}

/// Check which format would be detected for a command.
pub fn format_hints_check(cmd: &str) -> bird::Result<()> {
    use bird::FormatSource;

    let config = Config::load()?;
    let store = Store::open(config)?;

    let result = store.check_format(cmd)?;

    println!("Command: {}", cmd);
    println!("Format:  {}", result.format);

    match result.source {
        FormatSource::UserDefined { pattern, priority } => {
            println!("Source:  user-defined (pattern: {}, priority: {})", pattern, priority);
        }
        FormatSource::Builtin { pattern, priority } => {
            println!("Source:  built-in (pattern: {}, priority: {})", pattern, priority);
        }
        FormatSource::Default => {
            println!("Source:  default (no pattern matched)");
        }
    }

    Ok(())
}

/// Set the default format.
pub fn format_hints_set_default(format: &str) -> bird::Result<()> {
    let config = Config::load()?;
    let store = Store::open(config)?;

    let mut hints = store.load_format_hints()?;
    hints.set_default_format(format);
    store.save_format_hints(&hints)?;

    println!("Default format set to: {}", format);
    Ok(())
}

// Remote management commands

/// Add a remote storage connection.
pub fn remote_add(
    name: &str,
    remote_type: &str,
    uri: &str,
    read_only: bool,
    credential_provider: Option<&str>,
    auto_attach: bool,
) -> bird::Result<()> {
    use bird::{RemoteConfig, RemoteMode, RemoteType};
    use std::str::FromStr;

    let mut config = Config::load()?;

    let rtype = RemoteType::from_str(remote_type)?;
    let mut remote = RemoteConfig::new(name, rtype, uri);

    if read_only {
        remote.mode = RemoteMode::ReadOnly;
    }
    if let Some(provider) = credential_provider {
        remote.credential_provider = Some(provider.to_string());
    }
    remote.auto_attach = auto_attach;

    // Check if updating existing
    let updating = config.get_remote(name).is_some();

    config.add_remote(remote);
    config.save()?;

    if updating {
        println!("Updated remote: {}", name);
    } else {
        println!("Added remote: {}", name);
    }
    println!("  Type: {}", remote_type);
    println!("  URI:  {}", uri);
    println!("  Mode: {}", if read_only { "read-only" } else { "read-write" });
    if let Some(provider) = credential_provider {
        println!("  Credentials: {}", provider);
    }
    println!("  Auto-attach: {}", auto_attach);

    Ok(())
}

/// List configured remotes.
pub fn remote_list() -> bird::Result<()> {
    let config = Config::load()?;

    if config.remotes.is_empty() {
        println!("No remotes configured.");
        println!();
        println!("Add a remote with:");
        println!("  shq remote add <name> --type s3 --uri s3://bucket/path/bird.duckdb");
        return Ok(());
    }

    println!("{:<12} {:<12} {:<10} {:<8} URI", "NAME", "TYPE", "MODE", "ATTACH");
    println!("{}", "-".repeat(70));

    for remote in &config.remotes {
        println!(
            "{:<12} {:<12} {:<10} {:<8} {}",
            remote.name,
            remote.remote_type,
            remote.mode,
            if remote.auto_attach { "auto" } else { "manual" },
            remote.uri
        );
    }

    Ok(())
}

/// Remove a remote configuration.
pub fn remote_remove(name: &str) -> bird::Result<()> {
    let mut config = Config::load()?;

    if config.remove_remote(name) {
        config.save()?;
        println!("Removed remote: {}", name);
    } else {
        println!("Remote not found: {}", name);
    }

    Ok(())
}

/// Test connection to a remote.
pub fn remote_test(name: Option<&str>) -> bird::Result<()> {
    let config = Config::load()?;
    let store = Store::open(config.clone())?;

    let remotes_to_test: Vec<_> = if let Some(n) = name {
        match config.get_remote(n) {
            Some(r) => vec![r],
            None => {
                println!("Remote not found: {}", n);
                return Ok(());
            }
        }
    } else {
        config.remotes.iter().collect()
    };

    if remotes_to_test.is_empty() {
        println!("No remotes configured.");
        return Ok(());
    }

    for remote in remotes_to_test {
        print!("Testing {}... ", remote.name);
        match store.test_remote(remote) {
            Ok(()) => println!("OK"),
            Err(e) => println!("FAILED: {}", e),
        }
    }

    Ok(())
}

/// Manually attach a remote (shows SQL to run).
pub fn remote_attach(name: &str) -> bird::Result<()> {
    let config = Config::load()?;

    match config.get_remote(name) {
        Some(remote) => {
            println!("To attach this remote in SQL:");
            println!();
            if remote.credential_provider.is_some() {
                println!("LOAD httpfs;");
                println!(
                    "CREATE SECRET IF NOT EXISTS \"bird_{}\" (TYPE s3, PROVIDER credential_chain);",
                    remote.name
                );
            }
            println!("{};", remote.attach_sql());
            println!();
            println!("Then query with: SELECT * FROM {}.invocations LIMIT 10;", remote.quoted_schema_name());
        }
        None => {
            println!("Remote not found: {}", name);
        }
    }

    Ok(())
}

/// Show remote sync status.
pub fn remote_status() -> bird::Result<()> {
    use bird::PushOptions;

    let config = Config::load()?;
    let store = Store::open(config.clone())?;

    println!("Sync Configuration:");
    println!("  Default remote:    {}", config.sync.default_remote.as_deref().unwrap_or("(none)"));
    println!("  Push on compact:   {}", config.sync.push_on_compact);
    println!("  Push on archive:   {}", config.sync.push_on_archive);
    println!("  Sync invocations:  {}", config.sync.sync_invocations);
    println!("  Sync outputs:      {}", config.sync.sync_outputs);
    println!("  Sync events:       {}", config.sync.sync_events);
    println!("  Sync blobs:        {}", config.sync.sync_blobs);
    if config.sync.sync_blobs {
        println!("  Blob min size:     {} bytes", config.sync.blob_sync_min_bytes);
    }
    println!();

    println!("Blob Roots (search order):");
    for (i, root) in config.blob_roots().iter().enumerate() {
        println!("  {}. {}", i + 1, root);
    }
    println!();

    if config.remotes.is_empty() {
        println!("No remotes configured.");
    } else {
        println!("Configured Remotes:");
        for remote in &config.remotes {
            println!("  {} ({}, {})", remote.name, remote.remote_type, remote.mode);

            // Show pending sync stats (dry-run)
            let opts = PushOptions {
                since: None,
                dry_run: true,
                sync_blobs: true,
            };
            match store.push(remote, opts) {
                Ok(stats) => {
                    let total = stats.sessions + stats.invocations + stats.outputs + stats.events;
                    if total > 0 || stats.blobs.count > 0 {
                        println!("    Pending push: {}", stats);
                    } else {
                        println!("    Pending push: (up to date)");
                    }
                }
                Err(e) => {
                    println!("    Status: error - {}", e);
                }
            }
        }
    }

    Ok(())
}

// Push/Pull commands

/// Push local data to a remote.
pub fn push(remote: Option<&str>, since: Option<&str>, dry_run: bool, sync_blobs: bool) -> bird::Result<()> {
    use bird::{parse_since, PushOptions};

    let config = Config::load()?;
    let store = Store::open(config.clone())?;

    // Resolve remote
    let remote_name = remote
        .map(String::from)
        .or_else(|| config.sync.default_remote.clone())
        .ok_or_else(|| bird::Error::Config(
            "No remote specified and no default remote configured. Use --remote <name> or set sync.default_remote in config.".to_string()
        ))?;

    let remote_config = config.get_remote(&remote_name)
        .ok_or_else(|| bird::Error::Config(format!("Remote '{}' not found", remote_name)))?;

    // Parse since date
    let since_date = since.map(parse_since).transpose()?;

    let opts = PushOptions {
        since: since_date,
        dry_run,
        sync_blobs,
    };

    let stats = store.push(remote_config, opts)?;

    if dry_run {
        println!("Would push to '{}': {}", remote_name, stats);
    } else {
        println!("Pushed to '{}': {}", remote_name, stats);
    }

    Ok(())
}

/// Pull data from a remote to local.
pub fn pull(remote: Option<&str>, client: Option<&str>, since: Option<&str>, sync_blobs: bool) -> bird::Result<()> {
    use bird::{parse_since, PullOptions};

    let config = Config::load()?;
    let store = Store::open(config.clone())?;

    // Resolve remote
    let remote_name = remote
        .map(String::from)
        .or_else(|| config.sync.default_remote.clone())
        .ok_or_else(|| bird::Error::Config(
            "No remote specified and no default remote configured. Use --remote <name> or set sync.default_remote in config.".to_string()
        ))?;

    let remote_config = config.get_remote(&remote_name)
        .ok_or_else(|| bird::Error::Config(format!("Remote '{}' not found", remote_name)))?;

    // Parse since date
    let since_date = since.map(parse_since).transpose()?;

    let opts = PullOptions {
        since: since_date,
        client_id: client.map(String::from),
        sync_blobs,
    };

    let stats = store.pull(remote_config, opts)?;

    println!("Pulled from '{}': {}", remote_name, stats);

    Ok(())
}