koi-net 0.4.0

Local network toolkit: service discovery, DNS, health monitoring, TLS proxy, and certificate mesh
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
use command_surface::render::writers::{AnsiWriter, PlainWriter};
use command_surface::render::{
    write_catalog, write_command_detail, write_overview, CatalogOptions, ColorSupport,
    OutputWriter, Segment, TerminalProfile, TextStyle,
};
use command_surface::{
    ApiEndpoint, Category, Color, CommandDef, CommandManifest, Confirmation, Example, Glyph,
    Presentation, Scope, Tag,
};
use std::io::{self};
use std::sync::LazyLock;

pub static MANIFEST: LazyLock<CommandManifest<KoiCategory, KoiTag, KoiScope>> =
    LazyLock::new(build_manifest);

pub fn print_command_detail(def: &CommandDef<KoiCategory, KoiTag, KoiScope>) -> io::Result<()> {
    let profile = TerminalProfile::detect_stdout();
    let mut out = io::stdout();

    if profile.color == ColorSupport::None || !profile.interactive {
        let mut writer = PlainWriter::new(&mut out);
        write_command_detail(def, &profile, &mut writer)
    } else {
        let mut writer = AnsiWriter::new(&mut out);
        write_command_detail(def, &profile, &mut writer)
    }
}

pub fn print_catalog(api_endpoint: &str) -> io::Result<()> {
    let profile = TerminalProfile::detect_stdout();
    let mut out = io::stdout();

    if profile.color == ColorSupport::None || !profile.interactive {
        let mut writer = PlainWriter::new(&mut out);
        write_overview(&MANIFEST, &profile, &mut writer)?;
        write_quick_start(&mut writer, &profile)?;
        writer.write_blank()?;
        write_footer(&mut writer, &profile, "koi <group>", "koi <command>?")?;
        write_api_docs_hint(&mut writer, &profile, api_endpoint)
    } else {
        let mut writer = AnsiWriter::new(&mut out);
        write_overview(&MANIFEST, &profile, &mut writer)?;
        write_quick_start(&mut writer, &profile)?;
        writer.write_blank()?;
        write_footer(&mut writer, &profile, "koi <group>", "koi <command>?")?;
        write_api_docs_hint(&mut writer, &profile, api_endpoint)
    }
}

pub fn print_category_catalog(category: KoiCategory, scope: Option<KoiScope>) -> io::Result<()> {
    let profile = TerminalProfile::detect_stdout();
    let mut out = io::stdout();
    let manifest = filtered_manifest(category, scope);

    let cli_name = category_cli_name(category);
    let title = format!("koi {cli_name} \u{2014} available commands");
    let help = format!("koi {cli_name} <command> --help");

    let options = CatalogOptions {
        include_tags: true,
        include_scope: false,
        highlight_only: true,
        strip_prefix: true,
        indent: 2,
    };

    if profile.color == ColorSupport::None || !profile.interactive {
        let detail = format!("koi {cli_name} <command>?");
        let mut writer = PlainWriter::new(&mut out);
        write_title(&mut writer, &profile, &title)?;
        writer.write_blank()?;
        write_catalog(&manifest, &profile, &mut writer, options)?;
        write_curated_examples(category, &mut writer, &profile)?;
        writer.write_blank()?;
        write_footer(&mut writer, &profile, &help, &detail)
    } else {
        let detail = format!("koi {cli_name} <command>?");
        let mut writer = AnsiWriter::new(&mut out);
        write_title(&mut writer, &profile, &title)?;
        writer.write_blank()?;
        write_catalog(&manifest, &profile, &mut writer, options)?;
        write_curated_examples(category, &mut writer, &profile)?;
        writer.write_blank()?;
        write_footer(&mut writer, &profile, &help, &detail)
    }
}
fn write_title<W: OutputWriter>(
    writer: &mut W,
    profile: &TerminalProfile,
    title: &str,
) -> io::Result<()> {
    let mut style = TextStyle::plain();
    style.bold = true;
    if let Some(color) = profile.resolve_color(Color::Accent) {
        style.fg = Some(color);
    }
    writer.write_line(&[Segment::new(title, style)])
}

fn write_curated_examples<W: OutputWriter>(
    category: KoiCategory,
    writer: &mut W,
    profile: &TerminalProfile,
) -> io::Result<()> {
    let examples = curated_examples(category);
    if examples.is_empty() {
        return Ok(());
    }

    writer.write_blank()?;
    let mut header_style = TextStyle::plain();
    header_style.bold = true;
    if let Some(color) = profile.resolve_color(Color::Info) {
        header_style.fg = Some(color);
    }
    writer.write_line(&[Segment::new("Examples", header_style)])?;

    let mut desc_style = TextStyle::plain();
    desc_style.dim = true;

    for example in examples {
        writer.write_line(&[
            Segment::new(format!("  {}", example.command), TextStyle::plain()),
            Segment::new(format!("  # {}", example.description), desc_style),
        ])?;
    }

    Ok(())
}

fn write_quick_start<W: OutputWriter>(writer: &mut W, profile: &TerminalProfile) -> io::Result<()> {
    let examples: &[Example] = &[
        Example {
            command: "koi mdns discover",
            description: "Find services on the network",
        },
        Example {
            command: "koi certmesh status",
            description: "Check certificate mesh health",
        },
        Example {
            command: "koi health watch",
            description: "Live health dashboard",
        },
    ];

    writer.write_blank()?;
    let mut header_style = TextStyle::plain();
    header_style.bold = true;
    if let Some(color) = profile.resolve_color(Color::Info) {
        header_style.fg = Some(color);
    }
    writer.write_line(&[Segment::new("Quick start", header_style)])?;

    let mut desc_style = TextStyle::plain();
    desc_style.dim = true;

    for example in examples {
        writer.write_line(&[
            Segment::new(format!("  {}", example.command), TextStyle::plain()),
            Segment::new(format!("  # {}", example.description), desc_style),
        ])?;
    }

    Ok(())
}

fn write_footer<W: OutputWriter>(
    writer: &mut W,
    profile: &TerminalProfile,
    help: &str,
    detail_hint: &str,
) -> io::Result<()> {
    let mut style = TextStyle::plain();
    style.dim = true;
    if let Some(color) = profile.resolve_color(Color::Muted) {
        style.fg = Some(color);
    }

    writer.write_line(&[Segment::new(
        format!("Run {help} for flags, or {detail_hint} for a guide"),
        style,
    )])
}

fn write_api_docs_hint<W: OutputWriter>(
    writer: &mut W,
    profile: &TerminalProfile,
    api_endpoint: &str,
) -> io::Result<()> {
    let mut style = TextStyle::plain();
    style.dim = true;
    if let Some(color) = profile.resolve_color(Color::Muted) {
        style.fg = Some(color);
    }

    writer.write_line(&[Segment::new(
        format!("API docs:  {api_endpoint}/docs"),
        style,
    )])
}

fn filtered_manifest(
    category: KoiCategory,
    scope: Option<KoiScope>,
) -> CommandManifest<KoiCategory, KoiTag, KoiScope> {
    let mut manifest = CommandManifest::new();
    for def in MANIFEST.by_category(category) {
        if scope.is_none_or(|s| s == def.scope) {
            manifest.add(*def);
        }
    }
    manifest
}

fn category_cli_name(category: KoiCategory) -> &'static str {
    match category {
        KoiCategory::Core => "core",
        KoiCategory::Discovery => "mdns",
        KoiCategory::Trust => "certmesh",
        KoiCategory::Dns => "dns",
        KoiCategory::Health => "health",
        KoiCategory::Proxy => "proxy",
        KoiCategory::Udp => "udp",
    }
}

#[derive(Copy, Clone, Eq, PartialEq, Hash)]
pub enum KoiCategory {
    Core,
    Discovery,
    Trust,
    Dns,
    Health,
    Proxy,
    Udp,
}

impl Category for KoiCategory {
    fn label(&self) -> &'static str {
        match self {
            Self::Core => "Core",
            Self::Discovery => "Discovery (mDNS)",
            Self::Trust => "Trust (Certmesh)",
            Self::Dns => "DNS",
            Self::Health => "Health",
            Self::Proxy => "Proxy",
            Self::Udp => "UDP",
        }
    }

    fn order(&self) -> u8 {
        match self {
            Self::Core => 0,
            Self::Discovery => 1,
            Self::Trust => 2,
            Self::Dns => 3,
            Self::Health => 4,
            Self::Proxy => 5,
            Self::Udp => 6,
        }
    }

    fn cli_prefix(&self) -> &'static str {
        match self {
            Self::Core => "",
            Self::Discovery => "mdns ",
            Self::Trust => "certmesh ",
            Self::Dns => "dns ",
            Self::Health => "health ",
            Self::Proxy => "proxy ",
            Self::Udp => "udp ",
        }
    }

    fn cli_name(&self) -> &'static str {
        match self {
            Self::Core => "status",
            Self::Discovery => "mdns",
            Self::Trust => "certmesh",
            Self::Dns => "dns",
            Self::Health => "health",
            Self::Proxy => "proxy",
            Self::Udp => "udp",
        }
    }

    fn description(&self) -> &'static str {
        match self {
            Self::Core => "Service lifecycle and system info",
            Self::Discovery => "Discover and announce services on the local network",
            Self::Trust => "Zero-config TLS certificate mesh",
            Self::Dns => "Local DNS resolver with static records",
            Self::Health => "Service health checks and monitoring",
            Self::Proxy => "TLS-terminating reverse proxy",
            Self::Udp => "UDP datagram bridging for containers",
        }
    }
}

impl Glyph for KoiCategory {
    fn presentations(&self) -> &'static [Presentation] {
        match self {
            Self::Core => &[Presentation::Emoji(""), Presentation::Ascii("[core]")],
            Self::Discovery => &[Presentation::Emoji("🐠"), Presentation::Ascii("[koi]")],
            Self::Trust => &[Presentation::Emoji("🔐"), Presentation::Ascii("[trust]")],
            Self::Dns => &[Presentation::Emoji("🌐"), Presentation::Ascii("[dns]")],
            Self::Health => &[Presentation::Emoji("💓"), Presentation::Ascii("[health]")],
            Self::Proxy => &[Presentation::Emoji("🔀"), Presentation::Ascii("[proxy]")],
            Self::Udp => &[Presentation::Emoji("📡"), Presentation::Ascii("[udp]")],
        }
    }

    fn color(&self) -> Option<Color> {
        Some(Color::Accent)
    }
}

#[derive(Copy, Clone, Eq, PartialEq, Hash)]
pub enum KoiTag {
    Streaming,
    Destructive,
    Mutating,
    ReadOnly,
    Elevated,
    Admin,
    CliOnly,
}

impl Tag for KoiTag {
    fn label(&self) -> &'static str {
        match self {
            Self::Streaming => "Streaming",
            Self::Destructive => "Destructive",
            Self::Mutating => "Mutating",
            Self::ReadOnly => "Read-only",
            Self::Elevated => "Elevated",
            Self::Admin => "Admin",
            Self::CliOnly => "CLI-only",
        }
    }

    fn highlight(&self) -> bool {
        matches!(
            self,
            Self::Destructive | Self::Elevated | Self::Streaming | Self::CliOnly
        )
    }
}

impl Glyph for KoiTag {
    fn presentations(&self) -> &'static [Presentation] {
        match self {
            Self::Streaming => &[Presentation::Emoji(""), Presentation::Ascii(">>")],
            Self::Destructive => &[Presentation::Emoji(""), Presentation::Ascii("!!")],
            Self::Elevated => &[Presentation::Emoji("🔒"), Presentation::Ascii("^^")],
            Self::CliOnly => &[Presentation::Emoji(""), Presentation::Ascii("[cli]")],
            _ => &[],
        }
    }

    fn color(&self) -> Option<Color> {
        match self {
            Self::Destructive => Some(Color::Danger),
            Self::Elevated => Some(Color::Warning),
            Self::Streaming => Some(Color::Info),
            Self::Admin => Some(Color::Warning),
            Self::CliOnly => Some(Color::Muted),
            _ => None,
        }
    }

    fn badge(&self) -> Option<&'static str> {
        match self {
            Self::Streaming => Some("streaming"),
            Self::Destructive => Some("!destructive"),
            Self::Mutating => Some("mutating"),
            Self::ReadOnly => Some("read-only"),
            Self::Elevated => Some("elevated"),
            Self::Admin => Some("admin"),
            Self::CliOnly => Some("cli-only"),
        }
    }
}

#[derive(Copy, Clone, Eq, PartialEq, Hash)]
#[allow(dead_code)]
pub enum KoiScope {
    Public,
    Admin,
    Internal,
}

impl Scope for KoiScope {
    fn label(&self) -> &'static str {
        match self {
            Self::Public => "Public",
            Self::Admin => "Admin",
            Self::Internal => "Internal",
        }
    }

    fn is_default(&self) -> bool {
        matches!(self, Self::Public)
    }
}

impl Glyph for KoiScope {
    fn badge(&self) -> Option<&'static str> {
        match self {
            Self::Admin => Some("admin"),
            Self::Internal => Some("internal"),
            _ => None,
        }
    }

    fn color(&self) -> Option<Color> {
        match self {
            Self::Internal => Some(Color::Muted),
            _ => None,
        }
    }
}

/// Curated getting-started examples per category (3-5 each).
/// These tell a workflow story, not an exhaustive command reference.
fn curated_examples(category: KoiCategory) -> &'static [Example] {
    match category {
        KoiCategory::Core => &[
            Example {
                command: "koi launch",
                description: "Open the dashboard",
            },
            Example {
                command: "koi status",
                description: "Quick look at all capabilities",
            },
            Example {
                command: "koi install",
                description: "Install as a system service",
            },
        ],
        KoiCategory::Discovery => &[
            Example {
                command: "koi mdns announce \"My API\" _http._tcp 8080",
                description: "Publish a service",
            },
            Example {
                command: "koi mdns discover",
                description: "Find services on the network",
            },
            Example {
                command: "koi mdns subscribe _http._tcp",
                description: "Watch for service changes",
            },
            Example {
                command: "koi mdns admin ls",
                description: "List all daemon registrations",
            },
        ],
        KoiCategory::Trust => &[
            Example {
                command: "koi certmesh create --profile team --operator ops",
                description: "Bootstrap a new mesh",
            },
            Example {
                command: "koi certmesh join http://ca-host:5641",
                description: "Enroll into an existing mesh",
            },
            Example {
                command: "koi certmesh status",
                description: "Check mesh health",
            },
            Example {
                command: "koi certmesh backup mesh.koi",
                description: "Create an encrypted backup",
            },
            Example {
                command: "koi certmesh open-enrollment --until 2h",
                description: "Let new members join",
            },
        ],
        KoiCategory::Dns => &[
            Example {
                command: "koi dns add app.lan 10.0.0.5",
                description: "Create a static record",
            },
            Example {
                command: "koi dns lookup app.lan",
                description: "Test name resolution",
            },
            Example {
                command: "koi dns serve",
                description: "Start the resolver",
            },
        ],
        KoiCategory::Health => &[
            Example {
                command: "koi health add api --http https://app/health",
                description: "Register an HTTP check",
            },
            Example {
                command: "koi health status",
                description: "Snapshot of all checks",
            },
            Example {
                command: "koi health watch --interval 5",
                description: "Live dashboard",
            },
        ],
        KoiCategory::Proxy => &[
            Example {
                command: "koi proxy add web --listen 8443 --backend 127.0.0.1:8080",
                description: "TLS-terminate in front of a TCP backend",
            },
            Example {
                command: "koi proxy status",
                description: "Show listeners and their real state",
            },
            Example {
                command: "koi proxy list",
                description: "List all proxy entries",
            },
        ],
        KoiCategory::Udp => &[
            Example {
                command: "koi udp bind --port 5353",
                description: "Bind a host UDP port",
            },
            Example {
                command: "koi udp status",
                description: "Show active bindings",
            },
            Example {
                command: "koi udp send <id> --dest 10.0.0.5:5353 'hello'",
                description: "Send a datagram",
            },
        ],
    }
}

fn build_manifest() -> CommandManifest<KoiCategory, KoiTag, KoiScope> {
    let mut m = CommandManifest::new();

    // ── Core ─────────────────────────────────────────────────────────
    m.add(CommandDef {
        name: "install",
        summary: "Install Koi as a system service",
        long_description: "\
Registers Koi as a system service so it starts automatically on boot.

On Windows this creates a Windows Service via the Service Control Manager.
On Linux this writes a systemd unit file. On macOS a launchd plist is created.

The daemon runs in the background and exposes the HTTP API on the configured
port (default 5641) and the IPC pipe for local CLI communication.

Requires elevated privileges (Administrator / sudo).",
        category: KoiCategory::Core,
        tags: &[KoiTag::Elevated, KoiTag::Mutating, KoiTag::CliOnly],
        scope: KoiScope::Admin,
        examples: &[Example {
            command: "koi install",
            description: "Install the system service",
        }],
        see_also: &["uninstall"],
        api: &[],
        confirmation: None,
    })
    .add(CommandDef {
        name: "uninstall",
        summary: "Uninstall the Koi system service",
        long_description: "\
Removes the Koi system service registration. The daemon will be stopped
if it is currently running, and the service entry will be deleted.

State and configuration files are NOT removed - only the service
registration itself. You can re-install later with 'koi install'.

Requires elevated privileges (Administrator / sudo).",
        category: KoiCategory::Core,
        tags: &[
            KoiTag::Elevated,
            KoiTag::Destructive,
            KoiTag::Mutating,
            KoiTag::CliOnly,
        ],
        scope: KoiScope::Admin,
        examples: &[Example {
            command: "koi uninstall",
            description: "Remove the system service",
        }],
        see_also: &["install", "factory-reset"],
        api: &[],
        confirmation: None,
    })
    .add(CommandDef {
        name: "version",
        summary: "Show version information",
        long_description: "\
Prints the Koi version and build platform. Use --json for machine-readable
output.",
        category: KoiCategory::Core,
        tags: &[KoiTag::ReadOnly],
        scope: KoiScope::Public,
        examples: &[
            Example {
                command: "koi version",
                description: "Show version and platform",
            },
            Example {
                command: "koi version --json",
                description: "JSON output",
            },
        ],
        see_also: &["status"],
        api: &[ApiEndpoint {
            method: "GET",
            path: crate::adapters::http::paths::UNIFIED_STATUS,
        }],
        confirmation: None,
    })
    .add(CommandDef {
        name: "launch",
        summary: "Open the dashboard in a web browser",
        long_description: "\
Opens the Koi dashboard in the default web browser. The dashboard shows
a live overview of all capabilities, health checks, DNS records, certmesh
status, proxy entries, and an activity feed.

By default opens http://localhost:5641. If you are using a custom port
(--port or KOI_PORT), that port is used instead.

The daemon must be running for the dashboard to load.",
        category: KoiCategory::Core,
        tags: &[KoiTag::ReadOnly, KoiTag::CliOnly],
        scope: KoiScope::Public,
        examples: &[
            Example {
                command: "koi launch",
                description: "Open dashboard in the default browser",
            },
            Example {
                command: "koi launch --port 8080",
                description: "Open dashboard on a custom port",
            },
        ],
        see_also: &["status"],
        api: &[],
        confirmation: None,
    })
    .add(CommandDef {
        name: "status",
        summary: "Show status of all capabilities",
        long_description: "\
Displays a dashboard of all Koi capabilities: mDNS, certmesh, DNS,
health, and proxy. Shows whether each subsystem is running, along with
key metrics (registration counts, CA state, listener counts, etc.).

When the daemon is running, status is fetched via IPC. In standalone
mode, it reads local state files directly.",
        category: KoiCategory::Core,
        tags: &[KoiTag::ReadOnly],
        scope: KoiScope::Public,
        examples: &[
            Example {
                command: "koi status",
                description: "Show capability status",
            },
            Example {
                command: "koi status --json",
                description: "JSON output for scripting",
            },
        ],
        see_also: &["version"],
        api: &[ApiEndpoint {
            method: "GET",
            path: crate::adapters::http::paths::UNIFIED_STATUS,
        }],
        confirmation: None,
    })
    .add(CommandDef {
        name: "token show",
        summary: "Print the daemon access token",
        long_description: "\
Prints the current daemon access token (DAT) — the secret that authorizes
mutating HTTP requests (anything that is not a GET). The token is read from
the breadcrumb file the daemon writes on startup.

For safety it refuses to print to a non-tty (where it could be captured in
logs or scrollback) unless you pass --force. To hand the token to a
container, prefer `koi token write`.",
        category: KoiCategory::Core,
        tags: &[KoiTag::ReadOnly, KoiTag::CliOnly],
        scope: KoiScope::Admin,
        examples: &[
            Example {
                command: "koi token show",
                description: "Print the token (tty only)",
            },
            Example {
                command: "koi token show --json",
                description: "Emit {\"token\": \"...\"} for scripting",
            },
        ],
        see_also: &["token write"],
        api: &[],
        confirmation: None,
    })
    .add(CommandDef {
        name: "token write",
        summary: "Write the daemon token to a 0600 file",
        long_description: "\
Writes the current daemon access token to a file with owner-only
permissions (0600 on Unix; ACL-restricted on Windows), ready to mount into
a container as a secret. Pair with `--http-bind` to expose the daemon and
let containers authenticate their requests.",
        category: KoiCategory::Core,
        tags: &[KoiTag::CliOnly],
        scope: KoiScope::Admin,
        examples: &[Example {
            command: "koi token write /run/koi/token",
            description: "Write the token for mounting into a container",
        }],
        see_also: &["token show"],
        api: &[],
        confirmation: None,
    });

    // ── Discovery (mDNS) ─────────────────────────────────────────────
    m.add(CommandDef {
        name: "mdns discover",
        summary: "Discover services on the local network",
        long_description: "\
Performs a multicast DNS browse on the local network and streams discovered
services to the terminal. By default it browses for all service types.
Provide a service type to filter (e.g. _http._tcp).

The command runs as a streaming operation - it will keep discovering
services until you press Ctrl+C or the --timeout expires.",
        category: KoiCategory::Discovery,
        tags: &[KoiTag::Streaming, KoiTag::ReadOnly],
        scope: KoiScope::Public,
        examples: &[
            Example {
                command: "koi mdns discover",
                description: "Discover all service types",
            },
            Example {
                command: "koi mdns discover --timeout 10",
                description: "Stop after 10 seconds",
            },
        ],
        see_also: &["mdns subscribe", "mdns resolve"],
        api: &[ApiEndpoint {
            method: "GET",
            path: koi_mdns::http::paths::DISCOVER,
        }],
        confirmation: None,
    })
    .add(CommandDef {
        name: "mdns announce",
        summary: "Announce a service on the local network",
        long_description: "\
Publishes a service on the local network via multicast DNS so that other
devices can discover it. The service stays registered in the daemon until
explicitly unregistered or the daemon shuts down.

Arguments: <name> <service-type> <port> [--txt key=value ...]

The name is a human-readable label. The service type follows the mDNS
convention (e.g. _http._tcp, _ssh._tcp). TXT records can carry metadata.",
        category: KoiCategory::Discovery,
        tags: &[KoiTag::Mutating],
        scope: KoiScope::Public,
        examples: &[
            Example {
                command: "koi mdns announce \"My App\" _http._tcp 8080",
                description: "Announce an HTTP service",
            },
            Example {
                command: "koi mdns announce \"NAS\" _smb._tcp 445 --txt version=3",
                description: "With TXT record",
            },
        ],
        see_also: &["mdns unregister", "mdns discover"],
        api: &[ApiEndpoint {
            method: "POST",
            path: koi_mdns::http::paths::ANNOUNCE,
        }],
        confirmation: None,
    })
    .add(CommandDef {
        name: "mdns unregister",
        summary: "Unregister a previously announced service",
        long_description: "\
Removes a service registration that was created with 'koi mdns announce'.
The registration ID was returned when the service was announced and can
also be found via 'koi mdns admin ls'.",
        category: KoiCategory::Discovery,
        tags: &[KoiTag::Mutating],
        scope: KoiScope::Public,
        examples: &[Example {
            command: "koi mdns unregister abc123",
            description: "Remove a registration by ID",
        }],
        see_also: &["mdns announce", "mdns admin ls"],
        api: &[ApiEndpoint {
            method: "DELETE",
            path: koi_mdns::http::paths::UNREGISTER,
        }],
        confirmation: None,
    })
    .add(CommandDef {
        name: "mdns resolve",
        summary: "Resolve a specific service instance",
        long_description: "\
Performs a targeted mDNS resolve for a specific service instance name.
Returns the host, port, IP addresses, and TXT records for that instance.

The instance name is the full mDNS name including the service type and
.local. suffix (e.g. \"My App._http._tcp.local.\").",
        category: KoiCategory::Discovery,
        tags: &[KoiTag::ReadOnly],
        scope: KoiScope::Public,
        examples: &[Example {
            command: "koi mdns resolve \"My App._http._tcp.local.\"",
            description: "Resolve a service instance",
        }],
        see_also: &["mdns discover"],
        api: &[ApiEndpoint {
            method: "GET",
            path: koi_mdns::http::paths::RESOLVE,
        }],
        confirmation: None,
    })
    .add(CommandDef {
        name: "mdns subscribe",
        summary: "Subscribe to lifecycle events for a service type",
        long_description: "\
Streams real-time lifecycle events (found, lost, updated) for services
matching a given type. Useful for building reactive automations.

The subscription runs until Ctrl+C or --timeout. Each event is emitted
as a line of JSON when --json is used, or as a formatted line otherwise.",
        category: KoiCategory::Discovery,
        tags: &[KoiTag::Streaming, KoiTag::ReadOnly],
        scope: KoiScope::Public,
        examples: &[
            Example {
                command: "koi mdns subscribe _http._tcp",
                description: "Stream service events",
            },
            Example {
                command: "koi mdns subscribe _http._tcp --json",
                description: "NDJSON for piping",
            },
        ],
        see_also: &["mdns discover"],
        api: &[ApiEndpoint {
            method: "GET",
            path: koi_mdns::http::paths::SUBSCRIBE,
        }],
        confirmation: None,
    })
    .add(CommandDef {
        name: "mdns admin status",
        summary: "Show daemon status",
        long_description: "\
Shows the internal mDNS daemon state: number of active registrations,
how many are alive vs draining, and whether the mDNS engine is running.",
        category: KoiCategory::Discovery,
        tags: &[KoiTag::Admin, KoiTag::ReadOnly],
        scope: KoiScope::Admin,
        examples: &[Example {
            command: "koi mdns admin status",
            description: "Check daemon registration status",
        }],
        see_also: &["mdns admin ls"],
        api: &[ApiEndpoint {
            method: "GET",
            path: koi_mdns::http::paths::ADMIN_STATUS,
        }],
        confirmation: None,
    })
    .add(CommandDef {
        name: "mdns admin ls",
        summary: "List all registrations",
        long_description: "\
Lists every active service registration in the daemon, including
registration IDs, service types, ports, and current state (alive/draining).",
        category: KoiCategory::Discovery,
        tags: &[KoiTag::Admin, KoiTag::ReadOnly],
        scope: KoiScope::Admin,
        examples: &[Example {
            command: "koi mdns admin ls",
            description: "List all registrations",
        }],
        see_also: &["mdns admin inspect", "mdns admin status"],
        api: &[ApiEndpoint {
            method: "GET",
            path: koi_mdns::http::paths::ADMIN_LS,
        }],
        confirmation: None,
    })
    .add(CommandDef {
        name: "mdns admin inspect",
        summary: "Inspect a registration by ID or prefix",
        long_description: "\
Shows detailed information about a single registration, including its
full mDNS advertisement, TXT records, creation time, and current state.
You can use a full ID or a unique prefix.",
        category: KoiCategory::Discovery,
        tags: &[KoiTag::Admin, KoiTag::ReadOnly],
        scope: KoiScope::Admin,
        examples: &[Example {
            command: "koi mdns admin inspect abc123",
            description: "Inspect a registration",
        }],
        see_also: &["mdns admin ls"],
        api: &[ApiEndpoint {
            method: "GET",
            path: koi_mdns::http::paths::ADMIN_INSPECT,
        }],
        confirmation: None,
    })
    .add(CommandDef {
        name: "mdns admin unregister",
        summary: "Force-unregister a registration",
        long_description: "\
Immediately removes a registration from the daemon without a graceful
drain period. The service will stop being advertised on the network
instantly. Use 'mdns admin drain' for a graceful removal.",
        category: KoiCategory::Discovery,
        tags: &[KoiTag::Admin, KoiTag::Destructive, KoiTag::Mutating],
        scope: KoiScope::Admin,
        examples: &[Example {
            command: "koi mdns admin unregister abc123",
            description: "Force-unregister a registration",
        }],
        see_also: &["mdns admin drain", "mdns admin ls"],
        api: &[ApiEndpoint {
            method: "DELETE",
            path: koi_mdns::http::paths::ADMIN_UNREGISTER,
        }],
        confirmation: None,
    })
    .add(CommandDef {
        name: "mdns admin drain",
        summary: "Force-drain a registration",
        long_description: "\
Puts a registration into draining state. The service continues to respond
to existing queries but no new announcements are sent. After the drain
period expires, the registration is fully removed.",
        category: KoiCategory::Discovery,
        tags: &[KoiTag::Admin, KoiTag::Mutating],
        scope: KoiScope::Admin,
        examples: &[Example {
            command: "koi mdns admin drain abc123",
            description: "Drain a registration",
        }],
        see_also: &["mdns admin revive", "mdns admin unregister"],
        api: &[ApiEndpoint {
            method: "POST",
            path: koi_mdns::http::paths::ADMIN_DRAIN,
        }],
        confirmation: None,
    })
    .add(CommandDef {
        name: "mdns admin revive",
        summary: "Revive a draining registration",
        long_description: "\
Moves a registration out of draining state back to alive. The service
resumes normal mDNS announcements on the network.",
        category: KoiCategory::Discovery,
        tags: &[KoiTag::Admin, KoiTag::Mutating],
        scope: KoiScope::Admin,
        examples: &[Example {
            command: "koi mdns admin revive abc123",
            description: "Revive a registration",
        }],
        see_also: &["mdns admin drain"],
        api: &[ApiEndpoint {
            method: "POST",
            path: koi_mdns::http::paths::ADMIN_REVIVE,
        }],
        confirmation: None,
    });

    // ── Trust (Certmesh) ─────────────────────────────────────────────
    m.add(CommandDef {
        name: "certmesh create",
        summary: "Create a new certificate mesh",
        long_description: "\
Initializes a new certificate mesh on this node, making it the root CA.
This generates the root keypair, self-signed certificate, and local
configuration.

Profiles control enrollment defaults and approval policy:
    just-me       - open enrollment, no operator requirement
    team          - open enrollment, operator required
    organization  - closed enrollment by default, operator required

Without flags, this command runs an interactive wizard that guides
profile selection and CA passphrase setup.

Optional policy overrides:
    --enrollment open|closed
    --require-approval true|false",
        category: KoiCategory::Trust,
        tags: &[KoiTag::Mutating],
        scope: KoiScope::Admin,
        examples: &[
            Example {
                command: "koi certmesh create --profile team --operator ops",
                description: "Initialize a CA mesh",
            },
            Example {
                command: "koi certmesh create --profile just-me --enrollment closed --require-approval false",
                description: "Override default enrollment policy",
            },
            Example {
                command: "koi certmesh create",
                description: "Run the guided interactive wizard",
            },
        ],
        see_also: &[
            "certmesh join",
            "certmesh open-enrollment",
            "certmesh status",
        ],
        api: &[ApiEndpoint {
            method: "POST",
            path: koi_certmesh::http::paths::CREATE,
        }],
        confirmation: None,
    })
    .add(CommandDef {
        name: "certmesh join",
        summary: "Join an existing certificate mesh",
        long_description: "\
Enrolls this node into an existing certificate mesh by contacting the
CA node's enrollment endpoint. The CA must have an open enrollment
window (see 'certmesh open-enrollment').

During enrollment, this node generates a keypair, sends a CSR to the CA,
and receives a signed certificate. The node then participates in the
mesh's automatic renewal cycle.",
        category: KoiCategory::Trust,
        tags: &[KoiTag::Mutating],
        scope: KoiScope::Admin,
        examples: &[
            Example {
                command: "koi certmesh join http://ca-host:5641",
                description: "Join a remote CA",
            },
            Example {
                command: "koi certmesh join http://10.0.0.1:5641 --totp 123456",
                description: "With TOTP auth",
            },
        ],
        see_also: &["certmesh create", "certmesh status"],
        api: &[ApiEndpoint {
            method: "POST",
            path: koi_certmesh::http::paths::JOIN,
        }],
        confirmation: None,
    })
    .add(CommandDef {
        name: "certmesh status",
        summary: "Show certificate mesh status",
        long_description: "\
Displays the current state of the certificate mesh: CA role, certificate
expiry dates, number of enrolled members, renewal status, and whether
the enrollment window is open.",
        category: KoiCategory::Trust,
        tags: &[KoiTag::ReadOnly],
        scope: KoiScope::Public,
        examples: &[
            Example {
                command: "koi certmesh status",
                description: "Show mesh status",
            },
            Example {
                command: "koi certmesh status --json",
                description: "JSON for scripting",
            },
        ],
        see_also: &["certmesh compliance", "certmesh log"],
        api: &[ApiEndpoint {
            method: "GET",
            path: koi_certmesh::http::paths::STATUS,
        }],
        confirmation: None,
    })
    .add(CommandDef {
        name: "certmesh log",
        summary: "Show the audit log",
        long_description: "\
Displays the certmesh audit trail: certificate issuances, renewals,
revocations, enrollment events, and CA operations. Each entry includes
a timestamp and actor.",
        category: KoiCategory::Trust,
        tags: &[KoiTag::ReadOnly],
        scope: KoiScope::Public,
        examples: &[Example {
            command: "koi certmesh log",
            description: "Show audit log",
        }],
        see_also: &["certmesh status", "certmesh compliance"],
        api: &[ApiEndpoint {
            method: "GET",
            path: koi_certmesh::http::paths::LOG,
        }],
        confirmation: None,
    })
    .add(CommandDef {
        name: "certmesh compliance",
        summary: "Show compliance summary",
        long_description: "\
Checks the certificate mesh against best-practice compliance rules:
certificate lifetimes, key strengths, renewal coverage, enrollment
window state, and backup freshness. Useful for audits.",
        category: KoiCategory::Trust,
        tags: &[KoiTag::ReadOnly],
        scope: KoiScope::Public,
        examples: &[Example {
            command: "koi certmesh compliance",
            description: "Show compliance summary",
        }],
        see_also: &["certmesh status", "certmesh log"],
        api: &[ApiEndpoint {
            method: "GET",
            path: koi_certmesh::http::paths::COMPLIANCE,
        }],
        confirmation: None,
    })
    .add(CommandDef {
        name: "certmesh unlock",
        summary: "Unlock the CA",
        long_description: "\
Unlocks the CA private key so the mesh can issue and renew certificates.
The CA starts in a locked state after daemon restart for security.
Unlocking requires the CA passphrase if one was set during creation.",
        category: KoiCategory::Trust,
        tags: &[KoiTag::Mutating],
        scope: KoiScope::Admin,
        examples: &[Example {
            command: "koi certmesh unlock",
            description: "Unlock the CA",
        }],
        see_also: &["certmesh status"],
        api: &[ApiEndpoint {
            method: "POST",
            path: koi_certmesh::http::paths::UNLOCK,
        }],
        confirmation: None,
    })
    .add(CommandDef {
        name: "certmesh set-hook",
        summary: "Set a post-renewal reload hook",
        long_description: "\
Configures a shell command that runs after each successful certificate
renewal. Typically used to reload services that need to pick up the
new certificate (e.g. nginx, HAProxy, Envoy).

The hook runs as the Koi daemon user. Use --reload for a reload command
or --exec for a custom script.",
        category: KoiCategory::Trust,
        tags: &[KoiTag::Mutating],
        scope: KoiScope::Admin,
        examples: &[
            Example {
                command: "koi certmesh set-hook --reload \"systemctl restart nginx\"",
                description: "Reload nginx after renewal",
            },
            Example {
                command: "koi certmesh set-hook --exec /opt/hooks/on-renew.sh",
                description: "Run a custom script",
            },
        ],
        see_also: &["certmesh status"],
        api: &[ApiEndpoint {
            method: "PUT",
            path: koi_certmesh::http::paths::SET_HOOK,
        }],
        confirmation: None,
    })
    .add(CommandDef {
        name: "certmesh promote",
        summary: "Promote a member to standby CA",
        long_description: "\
Promotes a mesh member to standby CA role. The standby receives a copy
of the CA signing key and can take over if the primary CA goes offline.

This is the key operation for high-availability certmesh deployments.",
        category: KoiCategory::Trust,
        tags: &[KoiTag::Mutating],
        scope: KoiScope::Admin,
        examples: &[Example {
            command: "koi certmesh promote http://ca-host:5641",
            description: "Promote to standby CA",
        }],
        see_also: &["certmesh create"],
        api: &[ApiEndpoint {
            method: "POST",
            path: koi_certmesh::http::paths::PROMOTE,
        }],
        confirmation: None,
    })
    .add(CommandDef {
        name: "certmesh open-enrollment",
        summary: "Open the enrollment window",
        long_description: "\
Opens a time-limited window during which new nodes can join the mesh.
The window closes automatically after the specified duration or when
explicitly closed with 'certmesh close-enrollment'.

This is a security gate: enrollment should only be open when you are
actively adding nodes to the mesh.",
        category: KoiCategory::Trust,
        tags: &[KoiTag::Mutating],
        scope: KoiScope::Admin,
        examples: &[
            Example {
                command: "koi certmesh open-enrollment --until 2h",
                description: "Open enrollment for 2 hours",
            },
            Example {
                command: "koi certmesh open-enrollment --until 15m",
                description: "15-minute window",
            },
        ],
        see_also: &["certmesh close-enrollment", "certmesh join"],
        api: &[ApiEndpoint {
            method: "POST",
            path: koi_certmesh::http::paths::OPEN_ENROLLMENT,
        }],
        confirmation: None,
    })
    .add(CommandDef {
        name: "certmesh close-enrollment",
        summary: "Close the enrollment window",
        long_description: "\
Immediately closes the enrollment window. No new nodes can join the mesh
until enrollment is re-opened. Existing members are unaffected.",
        category: KoiCategory::Trust,
        tags: &[KoiTag::Mutating],
        scope: KoiScope::Admin,
        examples: &[Example {
            command: "koi certmesh close-enrollment",
            description: "Close enrollment",
        }],
        see_also: &["certmesh open-enrollment"],
        api: &[ApiEndpoint {
            method: "POST",
            path: koi_certmesh::http::paths::CLOSE_ENROLLMENT,
        }],
        confirmation: None,
    })
    .add(CommandDef {
        name: "certmesh set-policy",
        summary: "Set enrollment scope constraints",
        long_description: "\
Restricts which nodes can enroll based on domain name, IP range, or
other criteria. Policies are checked at enrollment time - existing
members are not retroactively affected.

Use --domain to restrict enrollment to a specific domain suffix.
Use --cidr to restrict by IP range.",
        category: KoiCategory::Trust,
        tags: &[KoiTag::Mutating],
        scope: KoiScope::Admin,
        examples: &[
            Example {
                command: "koi certmesh set-policy --domain lab.local",
                description: "Restrict enrollment to a domain",
            },
            Example {
                command: "koi certmesh set-policy --cidr 10.0.0.0/24",
                description: "Restrict by network",
            },
        ],
        see_also: &["certmesh open-enrollment"],
        api: &[ApiEndpoint {
            method: "PUT",
            path: koi_certmesh::http::paths::SET_POLICY,
        }],
        confirmation: None,
    })
    .add(CommandDef {
        name: "certmesh rotate-totp",
        summary: "Rotate the TOTP enrollment secret",
        long_description: "\
Generates a new TOTP secret for enrollment authentication. The old
secret is immediately invalidated. Share the new secret with operators
who need to enroll new nodes.",
        category: KoiCategory::Trust,
        tags: &[KoiTag::Mutating],
        scope: KoiScope::Admin,
        examples: &[Example {
            command: "koi certmesh rotate-auth",
            description: "Rotate enrollment credential",
        }],
        see_also: &["certmesh open-enrollment"],
        api: &[ApiEndpoint {
            method: "POST",
            path: koi_certmesh::http::paths::ROTATE_AUTH,
        }],
        confirmation: None,
    })
    .add(CommandDef {
        name: "certmesh backup",
        summary: "Create an encrypted backup bundle",
        long_description: "\
Creates an encrypted backup of the certmesh state, including the CA
keypair, issued certificates, enrollment configuration, and audit log.

The backup file (.koi) is encrypted with a passphrase and can be
restored on any node with 'certmesh restore'. Regular backups are
critical for disaster recovery - if the CA key is lost and no backup
exists, the entire mesh must be recreated.",
        category: KoiCategory::Trust,
        tags: &[KoiTag::Mutating],
        scope: KoiScope::Admin,
        examples: &[
            Example {
                command: "koi certmesh backup mesh.koi",
                description: "Write a backup bundle",
            },
            Example {
                command: "koi certmesh backup /mnt/backup/mesh-$(date +%F).koi",
                description: "Date-stamped backup",
            },
        ],
        see_also: &["certmesh restore", "certmesh compliance"],
        api: &[ApiEndpoint {
            method: "POST",
            path: koi_certmesh::http::paths::BACKUP,
        }],
        confirmation: None,
    })
    .add(CommandDef {
        name: "certmesh restore",
        summary: "Restore certmesh state from a backup bundle",
        long_description: "\
Restores a certmesh backup created with 'certmesh backup'. This replaces
the current certmesh state on this node with the backup contents.

WARNING: Any current certmesh state on this node will be overwritten.
The restore will prompt for the backup passphrase.",
        category: KoiCategory::Trust,
        tags: &[KoiTag::Mutating],
        scope: KoiScope::Admin,
        examples: &[Example {
            command: "koi certmesh restore mesh.koi",
            description: "Restore from a backup bundle",
        }],
        see_also: &["certmesh backup"],
        api: &[ApiEndpoint {
            method: "POST",
            path: koi_certmesh::http::paths::RESTORE,
        }],
        confirmation: None,
    })
    .add(CommandDef {
        name: "certmesh revoke",
        summary: "Revoke a member from the mesh",
        long_description: "\
Revokes a member's certificate, immediately preventing it from
participating in the mesh. The member's certificate is added to the
CRL (Certificate Revocation List).

A --reason is required for audit purposes. Common reasons: lost,
compromised, superseded, departed.",
        category: KoiCategory::Trust,
        tags: &[KoiTag::Mutating],
        scope: KoiScope::Admin,
        examples: &[
            Example {
                command: "koi certmesh revoke host1 --reason lost",
                description: "Revoke a member",
            },
            Example {
                command: "koi certmesh revoke db-02 --reason compromised",
                description: "Revoke a compromised node",
            },
        ],
        see_also: &["certmesh status", "certmesh log"],
        api: &[ApiEndpoint {
            method: "POST",
            path: koi_certmesh::http::paths::REVOKE,
        }],
        confirmation: None,
    })
    .add(CommandDef {
        name: "certmesh destroy",
        summary: "Destroy the certificate mesh",
        long_description: "\
Permanently removes all certmesh state on this node: CA keypair,
certificates, enrollments, audit log, and configuration. This action
is IRREVERSIBLE.

If this node is the root CA, all mesh members will lose their ability
to renew certificates. Create a backup first with 'certmesh backup'.",
        category: KoiCategory::Trust,
        tags: &[KoiTag::Destructive, KoiTag::Mutating],
        scope: KoiScope::Admin,
        examples: &[Example {
            command: "koi certmesh destroy",
            description: "Remove CA state",
        }],
        see_also: &["certmesh backup"],
        api: &[ApiEndpoint {
            method: "POST",
            path: koi_certmesh::http::paths::DESTROY,
        }],
        confirmation: Some(Confirmation::TypeToken {
            message: "\
This will PERMANENTLY DELETE all certmesh state including CA keys,\n\
certificates, enrollments, and audit logs.\n\
If this node is the root CA, all mesh members will lose their\n\
ability to renew certificates.",
            token: "DESTROY",
        }),
    });

    // ── DNS ──────────────────────────────────────────────────────────
    m.add(CommandDef {
        name: "dns serve",
        summary: "Start the DNS resolver",
        long_description: "\
Starts the local DNS resolver on the configured port (default 53).
The resolver handles queries for static records added via 'dns add'
and can forward unknown queries upstream.

Requires elevated privileges because port 53 is a privileged port.",
        category: KoiCategory::Dns,
        tags: &[KoiTag::Elevated, KoiTag::Mutating],
        scope: KoiScope::Admin,
        examples: &[Example {
            command: "koi dns serve",
            description: "Start the resolver",
        }],
        see_also: &["dns stop", "dns status", "dns add"],
        api: &[ApiEndpoint {
            method: "POST",
            path: koi_dns::http::paths::SERVE,
        }],
        confirmation: None,
    })
    .add(CommandDef {
        name: "dns stop",
        summary: "Stop the DNS resolver",
        long_description: "\
Stops the local DNS resolver. Static records are preserved and will be
served again when the resolver is restarted.",
        category: KoiCategory::Dns,
        tags: &[KoiTag::Mutating],
        scope: KoiScope::Admin,
        examples: &[Example {
            command: "koi dns stop",
            description: "Stop the resolver",
        }],
        see_also: &["dns serve"],
        api: &[ApiEndpoint {
            method: "POST",
            path: koi_dns::http::paths::STOP,
        }],
        confirmation: None,
    })
    .add(CommandDef {
        name: "dns status",
        summary: "Show DNS resolver status",
        long_description: "\
Shows whether the DNS resolver is running, which port and zone it is
configured for, and the number of static and mDNS-derived records.",
        category: KoiCategory::Dns,
        tags: &[KoiTag::ReadOnly],
        scope: KoiScope::Public,
        examples: &[Example {
            command: "koi dns status",
            description: "Check resolver status",
        }],
        see_also: &["dns serve", "dns list"],
        api: &[ApiEndpoint {
            method: "GET",
            path: koi_dns::http::paths::STATUS,
        }],
        confirmation: None,
    })
    .add(CommandDef {
        name: "dns lookup",
        summary: "Lookup a name through the resolver",
        long_description: "\
Queries the local DNS resolver for a name. Supports A, AAAA, CNAME,
TXT, and SRV record types via --record-type.

This is useful for testing that static records and mDNS-derived entries
resolve correctly before pointing production traffic at the resolver.",
        category: KoiCategory::Dns,
        tags: &[KoiTag::ReadOnly],
        scope: KoiScope::Public,
        examples: &[
            Example {
                command: "koi dns lookup example.lan",
                description: "Query default (A) record",
            },
            Example {
                command: "koi dns lookup example.lan --record-type AAAA",
                description: "Query IPv6",
            },
        ],
        see_also: &["dns add", "dns list"],
        api: &[ApiEndpoint {
            method: "GET",
            path: koi_dns::http::paths::LOOKUP,
        }],
        confirmation: None,
    })
    .add(CommandDef {
        name: "dns add",
        summary: "Add a static DNS entry",
        long_description: "\
Creates a static DNS record in the local resolver. The entry is persisted
to disk and survives daemon restarts.

Arguments: <name> <ip>

The name should be within the configured DNS zone (default: .lan).",
        category: KoiCategory::Dns,
        tags: &[KoiTag::Mutating],
        scope: KoiScope::Admin,
        examples: &[
            Example {
                command: "koi dns add example.lan 10.0.0.10",
                description: "Add a static record",
            },
            Example {
                command: "koi dns add db.lan 10.0.0.20",
                description: "Add another",
            },
        ],
        see_also: &["dns remove", "dns list", "dns lookup"],
        api: &[ApiEndpoint {
            method: "POST",
            path: koi_dns::http::paths::ADD,
        }],
        confirmation: None,
    })
    .add(CommandDef {
        name: "dns remove",
        summary: "Remove a static DNS entry",
        long_description: "\
Removes a static DNS record from the resolver. The change takes effect
immediately and is persisted to disk.",
        category: KoiCategory::Dns,
        tags: &[KoiTag::Mutating],
        scope: KoiScope::Admin,
        examples: &[Example {
            command: "koi dns remove example.lan",
            description: "Remove a static record",
        }],
        see_also: &["dns add", "dns list"],
        api: &[ApiEndpoint {
            method: "DELETE",
            path: koi_dns::http::paths::REMOVE,
        }],
        confirmation: None,
    })
    .add(CommandDef {
        name: "dns list",
        summary: "List all resolvable names",
        long_description: "\
Shows all names the DNS resolver can answer for: static records added
via 'dns add' and entries derived from mDNS service discovery.",
        category: KoiCategory::Dns,
        tags: &[KoiTag::ReadOnly],
        scope: KoiScope::Public,
        examples: &[Example {
            command: "koi dns list",
            description: "List static records",
        }],
        see_also: &["dns add", "dns lookup"],
        api: &[ApiEndpoint {
            method: "GET",
            path: koi_dns::http::paths::LIST,
        }],
        confirmation: None,
    });

    // ── Health ────────────────────────────────────────────────────────
    m.add(CommandDef {
        name: "health status",
        summary: "Show current health status",
        long_description: "\
Shows the current state of all registered health checks: service name,
check type (HTTP/TCP/process), last result, and last transition time.",
        category: KoiCategory::Health,
        tags: &[KoiTag::ReadOnly],
        scope: KoiScope::Public,
        examples: &[
            Example {
                command: "koi health status",
                description: "Show health snapshot",
            },
            Example {
                command: "koi health status --json",
                description: "JSON for monitoring",
            },
        ],
        see_also: &["health watch", "health log"],
        api: &[ApiEndpoint {
            method: "GET",
            path: koi_health::http::paths::STATUS,
        }],
        confirmation: None,
    })
    .add(CommandDef {
        name: "health watch",
        summary: "Live terminal watch view",
        long_description: "\
Displays a live-updating dashboard of all health checks. The screen
refreshes at the configured interval (default: 2 seconds). Press
Ctrl+C to exit.

Use --interval to control refresh rate.",
        category: KoiCategory::Health,
        tags: &[KoiTag::Streaming, KoiTag::ReadOnly],
        scope: KoiScope::Public,
        examples: &[
            Example {
                command: "koi health watch",
                description: "Default refresh rate",
            },
            Example {
                command: "koi health watch --interval 5",
                description: "Refresh every 5 seconds",
            },
        ],
        see_also: &["health status"],
        api: &[ApiEndpoint {
            method: "GET",
            path: koi_health::http::paths::STATUS,
        }],
        confirmation: None,
    })
    .add(CommandDef {
        name: "health add",
        summary: "Add a service health check",
        long_description: "\
Registers a new health check with the daemon. Supported check types:

  --http <url>     HTTP GET, expects 2xx response
  --tcp <host:port> TCP connection check
  --process <name>  Process existence check

The check runs at the daemon's configured interval and reports state
transitions (healthy → unhealthy and back).",
        category: KoiCategory::Health,
        tags: &[KoiTag::Mutating],
        scope: KoiScope::Admin,
        examples: &[
            Example {
                command: "koi health add api --http https://example.com/health",
                description: "Add an HTTP check",
            },
            Example {
                command: "koi health add db --tcp 127.0.0.1:5432",
                description: "TCP port check",
            },
        ],
        see_also: &["health remove", "health status"],
        api: &[ApiEndpoint {
            method: "POST",
            path: koi_health::http::paths::ADD,
        }],
        confirmation: None,
    })
    .add(CommandDef {
        name: "health remove",
        summary: "Remove a service health check",
        long_description: "\
Removes a health check registration. The check stops running immediately
and its history is removed from the transition log.",
        category: KoiCategory::Health,
        tags: &[KoiTag::Mutating],
        scope: KoiScope::Admin,
        examples: &[Example {
            command: "koi health remove api",
            description: "Remove a check",
        }],
        see_also: &["health add", "health status"],
        api: &[ApiEndpoint {
            method: "DELETE",
            path: koi_health::http::paths::REMOVE,
        }],
        confirmation: None,
    })
    .add(CommandDef {
        name: "health log",
        summary: "Show health transition log",
        long_description: "\
Shows the history of health state transitions: when services went from
healthy to unhealthy and back. Each entry includes a timestamp, service
name, old state, new state, and reason.",
        category: KoiCategory::Health,
        tags: &[KoiTag::ReadOnly],
        scope: KoiScope::Public,
        examples: &[Example {
            command: "koi health log",
            description: "Show transition log",
        }],
        see_also: &["health status", "health watch"],
        api: &[ApiEndpoint {
            method: "GET",
            path: koi_health::http::paths::STATUS,
        }],
        confirmation: None,
    });

    // ── Proxy ─────────────────────────────────────────────────────────
    m.add(CommandDef {
        name: "proxy add",
        summary: "Add or update a proxy entry",
        long_description: "\
Adds a TLS-terminating TCP passthrough. Koi binds the listen port, terminates
TLS with a certmesh certificate (if one is on disk) or a generated self-signed
cert, then pipes raw bytes to the backend — so WebSockets and any bidirectional
protocol work transparently.

It is passthrough only: no path routing, no header injection, no rewrites. For
those, point this proxy at Caddy/Traefik/nginx and let them do L7 routing.

Arguments: <name> --listen <port> --backend <host:port>

If a proxy with the same name exists, it is updated in place.",
        category: KoiCategory::Proxy,
        tags: &[KoiTag::Mutating],
        scope: KoiScope::Admin,
        examples: &[
            Example {
                command: "koi proxy add web --listen 8443 --backend 127.0.0.1:8080",
                description: "Add a TLS passthrough",
            },
            Example {
                command: "koi proxy add api --listen 9443 --backend 127.0.0.1:3000",
                description: "Another passthrough",
            },
        ],
        see_also: &["proxy remove", "proxy list", "proxy status"],
        api: &[ApiEndpoint {
            method: "POST",
            path: koi_proxy::http::paths::ADD,
        }],
        confirmation: None,
    })
    .add(CommandDef {
        name: "proxy remove",
        summary: "Remove a proxy entry",
        long_description: "\
Removes a proxy entry by name. The listener stops immediately and the
port is released.",
        category: KoiCategory::Proxy,
        tags: &[KoiTag::Mutating],
        scope: KoiScope::Admin,
        examples: &[Example {
            command: "koi proxy remove web",
            description: "Remove a proxy",
        }],
        see_also: &["proxy add", "proxy list"],
        api: &[ApiEndpoint {
            method: "DELETE",
            path: koi_proxy::http::paths::REMOVE,
        }],
        confirmation: None,
    })
    .add(CommandDef {
        name: "proxy status",
        summary: "Show proxy status",
        long_description: "\
Shows all proxy listeners: name, listen port, backend, TLS certificate source
(certmesh or self-signed), and real STATE — running, or the bind/accept error
(e.g. 'address in use') when a listener failed to start.",
        category: KoiCategory::Proxy,
        tags: &[KoiTag::ReadOnly],
        scope: KoiScope::Public,
        examples: &[Example {
            command: "koi proxy status",
            description: "Show proxy status",
        }],
        see_also: &["proxy list", "proxy add"],
        api: &[ApiEndpoint {
            method: "GET",
            path: koi_proxy::http::paths::STATUS,
        }],
        confirmation: None,
    })
    .add(CommandDef {
        name: "proxy list",
        summary: "List configured proxies",
        long_description: "\
Lists all configured proxy entries with their names, listen ports,
and backends. Use 'proxy status' for runtime details.",
        category: KoiCategory::Proxy,
        tags: &[KoiTag::ReadOnly],
        scope: KoiScope::Public,
        examples: &[Example {
            command: "koi proxy list",
            description: "List proxies",
        }],
        see_also: &["proxy status", "proxy add"],
        api: &[ApiEndpoint {
            method: "GET",
            path: koi_proxy::http::paths::LIST,
        }],
        confirmation: None,
    })
    // ── UDP ───────────────────────────────────────────────────────────
    .add(CommandDef {
        name: "udp bind",
        summary: "Bind a host UDP port",
        long_description: "\
Creates a new UDP socket on the host and returns a binding ID. The
binding is lease-based: it expires after `--lease` seconds unless
renewed with 'udp heartbeat'.

Containers cannot bind host UDP ports directly, so this command
provides a bridge: bind via Koi, then send/receive datagrams
through the binding ID.",
        category: KoiCategory::Udp,
        tags: &[KoiTag::Mutating],
        scope: KoiScope::Public,
        examples: &[
            Example {
                command: "koi udp bind --port 5353",
                description: "Bind port 5353",
            },
            Example {
                command: "koi udp bind --port 0 --lease 600",
                description: "OS-assigned port, 10 min lease",
            },
        ],
        see_also: &["udp unbind", "udp status", "udp heartbeat"],
        api: &[ApiEndpoint {
            method: "POST",
            path: koi_udp::http::paths::BIND,
        }],
        confirmation: None,
    })
    .add(CommandDef {
        name: "udp unbind",
        summary: "Unbind (close) a UDP binding",
        long_description: "\
Closes a previously bound UDP socket and releases the port. Any
in-progress recv streams are terminated.",
        category: KoiCategory::Udp,
        tags: &[KoiTag::Mutating],
        scope: KoiScope::Public,
        examples: &[Example {
            command: "koi udp unbind <id>",
            description: "Close a binding",
        }],
        see_also: &["udp bind", "udp status"],
        api: &[ApiEndpoint {
            method: "DELETE",
            path: koi_udp::http::paths::UNBIND,
        }],
        confirmation: None,
    })
    .add(CommandDef {
        name: "udp send",
        summary: "Send a datagram through a binding",
        long_description: "\
Sends a UDP datagram through an existing binding. The payload is
provided as a string on the CLI (base64-encoded in the HTTP API).
The destination is a host:port pair.",
        category: KoiCategory::Udp,
        tags: &[KoiTag::Mutating],
        scope: KoiScope::Public,
        examples: &[Example {
            command: "koi udp send <id> --dest 10.0.0.5:5353 'hello'",
            description: "Send a datagram",
        }],
        see_also: &["udp bind"],
        api: &[ApiEndpoint {
            method: "POST",
            path: koi_udp::http::paths::SEND,
        }],
        confirmation: None,
    })
    .add(CommandDef {
        name: "udp status",
        summary: "Show active UDP bindings",
        long_description: "\
Lists all active UDP bindings with their IDs, local addresses, and
remaining lease times.",
        category: KoiCategory::Udp,
        tags: &[KoiTag::ReadOnly],
        scope: KoiScope::Public,
        examples: &[Example {
            command: "koi udp status",
            description: "List bindings",
        }],
        see_also: &["udp bind", "udp unbind"],
        api: &[ApiEndpoint {
            method: "GET",
            path: koi_udp::http::paths::STATUS,
        }],
        confirmation: None,
    })
    .add(CommandDef {
        name: "udp heartbeat",
        summary: "Renew a binding's lease",
        long_description: "\
Extends the lease of an active UDP binding, preventing it from
expiring. The lease is reset to its original duration.",
        category: KoiCategory::Udp,
        tags: &[KoiTag::Mutating],
        scope: KoiScope::Public,
        examples: &[Example {
            command: "koi udp heartbeat <id>",
            description: "Renew lease",
        }],
        see_also: &["udp bind", "udp status"],
        api: &[ApiEndpoint {
            method: "PUT",
            path: koi_udp::http::paths::HEARTBEAT,
        }],
        confirmation: None,
    })
    .add(CommandDef {
        name: "factory-reset",
        summary: "Wipe all state and restart the service",
        long_description: "\
Destroys the Koi program data folder and recreates it from scratch,
then restarts the system service. This removes ALL local state:

  • mDNS registrations
  • CA private keys and issued certificates
  • DNS records
  • Health-check configurations
  • Proxy routes
  • config.toml

Log files are preserved by default. Use --include-logs to remove them too.

This is irreversible. If this node is the certmesh CA root, every
certificate it ever issued becomes unverifiable.

Requires elevated privileges (Administrator / sudo).",
        category: KoiCategory::Core,
        tags: &[
            KoiTag::Elevated,
            KoiTag::Destructive,
            KoiTag::Mutating,
            KoiTag::CliOnly,
        ],
        scope: KoiScope::Admin,
        examples: &[
            Example {
                command: "koi factory-reset",
                description: "Wipe all state and restart",
            },
            Example {
                command: "koi factory-reset --include-logs",
                description: "Wipe everything including logs",
            },
        ],
        see_also: &["uninstall", "install"],
        api: &[],
        confirmation: Some(Confirmation::TypeToken {
            message: "\
This will PERMANENTLY DELETE all Koi state including CA keys,\n\
certificates, DNS records, and configuration.\n\
If this node is the certmesh CA root, all issued certificates\n\
will become unverifiable.",
            token: "RESET",
        }),
    });

    m
}