silicon-iam-cli 1.2.0

Command-line client for Silicon IAM, built on the silicon-iam-client crate.
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
//! The command grammar.
//!
//! Nouns then verbs -- `iam tag create`, `iam member remove` -- because that
//! is what a person guesses, and because it keeps related commands together in
//! `--help`. Global flags come before the command and are accepted anywhere.

use std::path::PathBuf;

use clap::{Args, Parser, Subcommand, ValueEnum};
use uuid::Uuid;

use crate::output::Format;

/// The Silicon IAM command-line client.
#[derive(Debug, Parser)]
#[command(
    name = "iam",
    version,
    about = "Silicon IAM from the command line",
    long_about = "Silicon IAM from the command line.\n\n\
        Sign in once with `iam login`; the session is stored under \
        ~/.silicon-iam/ and renewed automatically. Most commands act on an \
        organization: pass --org, or set a default with \
        `iam config set org <handle>`.",
    propagate_version = true,
    disable_help_subcommand = false
)]
pub struct Cli {
    /// Global options.
    #[command(flatten)]
    pub global: Global,

    /// The command to run.
    #[command(subcommand)]
    pub command: Command,
}

/// Options accepted by every command.
#[derive(Debug, Args)]
pub struct Global {
    /// Service base URL. HTTPS is required except for localhost or a loopback
    /// IP address; credentials, port zero, query, and fragment are rejected.
    #[arg(long, global = true, env = "SILICON_IAM_URL")]
    pub url: Option<String>,

    /// Stored profile to use.
    #[arg(long, global = true, env = "SILICON_IAM_PROFILE")]
    pub profile: Option<String>,

    /// Organization handle to act on. Falls back to `SILICON_IAM_ORG`, then the
    /// current profile's stored organization.
    #[arg(long, global = true)]
    pub org: Option<String>,

    /// Ignore any stored or environment organization for this invocation.
    /// Useful with a canonical app ID for an unscoped Application login.
    #[arg(long, global = true, conflicts_with = "org")]
    pub no_org: bool,

    /// Run inside a testing environment, by its hyphenated UUID (never its
    /// root key).
    #[arg(
        long,
        global = true,
        env = "SILICON_IAM_TEST",
        value_name = "ENVIRONMENT_ID",
        value_parser = parse_testing_environment_id
    )]
    pub test: Option<Uuid>,

    /// Step-up assertion minted by `iam step-up <ACTION> <RESOURCE_ID>`.
    #[arg(long, global = true)]
    pub step_up: Option<String>,

    /// Output format.
    #[arg(long, short = 'o', global = true, value_enum, default_value_t = Format::Text)]
    pub output: Format,
}

/// Everything the CLI can do.
#[derive(Debug, Subcommand)]
pub enum Command {
    /// Sign in as a Carbon.
    Login(LoginArgs),
    /// Sign in as a Silicon with its credential.
    SiliconLogin(SiliconLoginArgs),
    /// End a Carbon session remotely; forget a Silicon session locally.
    Logout(LogoutArgs),
    /// Show who is signed in.
    Whoami,
    /// Mint a one-action, one-resource step-up token.
    StepUp(StepUpArgs),
    /// Create a Carbon account.
    Signup(SignupArgs),
    /// Your Carbon profile and public Carbon lookup.
    #[command(subcommand)]
    Carbon(CarbonCommand),
    /// Print every command; --output json includes its usage, arguments and full help.
    Commands,
    /// Read or search the bundled CLI, API and client documentation offline.
    Docs {
        /// Documentation topic from `iam docs`, such as cli, testing, applications or obo.
        topic: Option<String>,
        /// Find text across the bundled documentation, or within the selected topic.
        #[arg(long, value_name = "TEXT")]
        search: Option<String>,
    },

    /// Organizations.
    #[command(subcommand)]
    Org(OrgCommand),
    /// Organization single sign-on.
    #[command(subcommand)]
    Sso(SsoCommand),
    /// Members of an organization.
    #[command(subcommand)]
    Member(MemberCommand),
    /// Invitations, from both ends.
    #[command(subcommand)]
    Invite(InviteCommand),
    /// Organization tags.
    #[command(subcommand)]
    Tag(TagCommand),
    /// Advisory trust.
    #[command(subcommand)]
    Trust(TrustCommand),
    /// Approvals, direct changes, and history.
    #[command(subcommand)]
    Approval(ApprovalCommand),
    /// Silicons.
    #[command(subcommand)]
    Silicon(SiliconCommand),
    /// Applications.
    #[command(subcommand)]
    App(AppCommand),
    /// Testing environments.
    ///
    /// Lifecycle commands use a production Carbon session and organization;
    /// omit --test for them. `env current`, `app import`, and `env clean`
    /// without an explicit environment ID are test-only and require --test.
    #[command(subcommand)]
    Env(EnvCommand),
    /// Your own sessions and login history.
    #[command(subcommand)]
    Session(SessionCommand),
    /// Stored settings.
    #[command(subcommand)]
    Config(ConfigCommand),
    /// The service itself.
    #[command(subcommand)]
    System(SystemCommand),
}

/// Arguments for signing in.
#[derive(Debug, Args)]
#[command(group(
    clap::ArgGroup::new("identity")
        .args(["email", "phone", "carbon_id"])
        .required(false)
        .multiple(false)
))]
pub struct LoginArgs {
    /// Email address to sign in with.
    #[arg(long)]
    pub email: Option<String>,
    /// Phone number, in E.164 form.
    #[arg(long)]
    pub phone: Option<String>,
    /// Carbon ID.
    #[arg(long)]
    pub carbon_id: Option<String>,
    /// Verification code, if you already have it. Prompted for otherwise.
    #[arg(long)]
    pub code: Option<String>,
    /// Application to sign in to. Prints a short-lived token for it.
    #[arg(long = "app-id", value_name = "APP_ID")]
    pub app_id: Option<String>,
}

/// Arguments for signing out.
#[derive(Debug, Args)]
pub struct LogoutArgs {
    /// End every Carbon session. Needs --step-up: action
    /// `account.sessions_revoke_all`, resource = the signed-in Carbon's
    /// principal UUID. Every affected session must be at least 12 hours old.
    #[arg(long, conflicts_with = "local_only")]
    pub all: bool,
    /// Only forget this device's stored credential; do not call IAM.
    #[arg(long, conflicts_with = "all")]
    pub local_only: bool,
}

/// Arguments for signing a Silicon in.
#[derive(Debug, Args)]
pub struct SiliconLoginArgs {
    /// Silicon ID, in `handle:org` form. With only --app-id, reuse the stored Silicon session.
    #[arg(long = "sid")]
    pub sid: Option<String>,
    /// Silicon token. Prompted for when omitted, so it stays out of shell history.
    #[arg(long = "stk")]
    pub stk: Option<String>,
    /// Application to sign in to. Prints an SLT; omit --sid and --stk to reuse the stored Silicon session.
    #[arg(long = "app-id", value_name = "APP_ID")]
    pub app_id: Option<String>,
}

/// Arguments for creating an account.
#[derive(Debug, Args)]
pub struct SignupArgs {
    /// Email address to verify.
    #[arg(long)]
    pub email: String,
    /// Phone number to verify, in E.164 form.
    #[arg(long)]
    pub phone: String,
    /// Carbon ID: 3-30 lowercase letters, digits 1-9, underscores or hyphens (no 0).
    #[arg(long, value_parser = parse_carbon_id)]
    pub carbon_id: String,
    /// Display name. Defaults to the Carbon ID.
    #[arg(long)]
    pub display_name: Option<String>,
    /// IANA time zone, such as `Asia/Kolkata`.
    #[arg(long)]
    pub timezone: Option<String>,
}

/// Carbon profile and lookup commands.
#[derive(Debug, Subcommand)]
pub enum CarbonCommand {
    /// Show the signed-in Carbon's complete profile.
    Show,
    /// Update the signed-in Carbon's profile.
    #[command(group(
        clap::ArgGroup::new("changes")
            .args([
                "display_name",
                "timezone",
                "description",
                "clear_description",
                "profile_photo",
                "clear_profile_photo"
            ])
            .required(true)
            .multiple(true)
    ))]
    Update {
        /// New display name.
        #[arg(long)]
        display_name: Option<String>,
        /// New IANA time zone, such as `Asia/Kolkata`.
        #[arg(long)]
        timezone: Option<String>,
        /// New profile description.
        #[arg(long)]
        description: Option<String>,
        /// Remove the current profile description.
        #[arg(long, conflicts_with = "description")]
        clear_description: bool,
        /// New profile-photo URL.
        #[arg(long)]
        profile_photo: Option<String>,
        /// Restore the generated default profile photo.
        #[arg(long, conflicts_with = "profile_photo")]
        clear_profile_photo: bool,
    },
    /// Check whether a Carbon ID can be claimed.
    Available {
        /// Carbon ID to check; this does not reserve it.
        carbon_id: String,
    },
    /// Suggest public Carbon IDs matching a partial handle.
    Search {
        /// Non-empty partial Carbon ID, up to 100 characters.
        query: String,
        /// Maximum suggestions to return, from 1 through 10.
        #[arg(long, value_parser = clap::value_parser!(u16).range(1..=10))]
        limit: Option<u16>,
    },
    /// Resolve an exact verified email address to a Carbon ID.
    ResolveEmail {
        /// Exact verified email address.
        email: String,
    },
    /// Resolve an exact verified E.164 phone number to a Carbon ID.
    ResolvePhone {
        /// Exact verified phone number, such as `+12025550123`.
        phone: String,
    },
}

/// Arguments for verified-channel step-up.
#[derive(Debug, Args)]
pub struct StepUpArgs {
    /// Sensitive action the token will authorize.
    #[arg(value_enum)]
    pub action: StepUpActionArg,
    /// Internal UUID of the exact resource being changed.
    pub resource_id: Uuid,
    /// Verified channel that receives the code.
    #[arg(long, value_enum, default_value_t = StepUpChannel::Email)]
    pub channel: StepUpChannel,
    /// Verification code, if already known. Prompted for otherwise.
    #[arg(long)]
    pub code: Option<String>,
}

/// Sensitive actions supported by the IAM step-up contract.
#[derive(Clone, Copy, Debug, ValueEnum)]
pub enum StepUpActionArg {
    /// Revoke one of the current Carbon's sessions.
    #[value(name = "account.session_revoke")]
    AccountSessionRevoke,
    /// Revoke every session belonging to the current Carbon.
    #[value(name = "account.sessions_revoke_all")]
    AccountSessionsRevokeAll,
    /// Transfer an organization's ownership.
    #[value(name = "organization.transfer_ownership")]
    OrganizationTransferOwnership,
    /// Change organization roles, capabilities, members, or Silicon access.
    #[value(name = "organization.authorization_change")]
    OrganizationAuthorizationChange,
    /// Change organization SSO configuration.
    #[value(name = "organization.sso_change")]
    OrganizationSsoChange,
    /// Create, redirect, delete, or resubscribe a Silicon webhook.
    #[value(name = "organization.silicon_webhook.redirect")]
    OrganizationSiliconWebhookRedirect,
    /// Rotate an Application client secret.
    #[value(name = "application.client_secret.rotate")]
    ApplicationClientSecretRotate,
    /// Rotate an Application webhook signing secret.
    #[value(name = "application.webhook_secret.rotate")]
    ApplicationWebhookSecretRotate,
    /// Rotate a Silicon credential.
    #[value(name = "silicon.rotate_token")]
    SiliconRotateToken,
    /// Change an organization's platform SSO entitlement.
    #[value(name = "platform_admin.sso_entitlement")]
    PlatformAdminSsoEntitlement,
    /// Record a platform Application review decision.
    #[value(name = "platform_admin.application_review")]
    PlatformAdminApplicationReview,
}

/// Verified channel used by step-up.
#[derive(Clone, Copy, Debug, Default, ValueEnum)]
pub enum StepUpChannel {
    /// Send the code to the primary email address.
    #[default]
    Email,
    /// Send the code to the primary phone number.
    Phone,
}

/// Organization commands.
#[derive(Debug, Subcommand)]
pub enum OrgCommand {
    /// List organizations you belong to.
    List {
        /// Only active or removed memberships.
        #[arg(long, value_parser = ["active", "removed"])]
        status: Option<String>,
        /// Paging.
        #[command(flatten)]
        page: PageArgs,
    },
    /// Create an organization.
    Create {
        /// Handle to claim.
        handle: String,
        /// Display name.
        #[arg(long)]
        name: String,
        /// Description.
        #[arg(long)]
        description: Option<String>,
    },
    /// Show one organization.
    Show {
        /// Handle. Defaults to --org.
        handle: Option<String>,
    },
    /// Rename or re-describe an organization.
    #[command(group(
        clap::ArgGroup::new("changes")
            .args([
                "name",
                "logo",
                "clear_logo",
                "description",
                "clear_description",
                "join_method"
            ])
            .required(true)
            .multiple(true)
    ))]
    Update {
        /// Handle. Defaults to --org.
        handle: Option<String>,
        /// New display name.
        #[arg(long)]
        name: Option<String>,
        /// New logo URL.
        #[arg(long)]
        logo: Option<String>,
        /// Remove the current logo.
        #[arg(long, conflicts_with = "logo")]
        clear_logo: bool,
        /// New description.
        #[arg(long)]
        description: Option<String>,
        /// Remove the current description.
        #[arg(long, conflicts_with = "description")]
        clear_description: bool,
        /// Join method: `email` or `sso`.
        #[arg(long, value_parser = ["email", "sso"])]
        join_method: Option<String>,
    },
    /// Check whether a handle can be claimed.
    Available {
        /// Handle to check.
        handle: String,
    },
    /// Hand ownership to another member. Needs --step-up: action
    /// `organization.transfer_ownership`, resource = the organization UUID.
    Transfer {
        /// Membership that becomes the owner.
        membership_id: Uuid,
        /// Reserved for handler compatibility; use the global --org option.
        #[arg(skip)]
        org: Option<String>,
    },
}

/// Organization SSO commands.
#[derive(Debug, Subcommand)]
pub enum SsoCommand {
    /// Show the selected organization's SSO configuration. Requires `sso.manage`.
    Show,
    /// Create a five-minute `WorkOS` setup link. Requires entitlement and `sso.manage`.
    SetupLink,
    /// Check the active `WorkOS` connection end to end. Requires `sso.manage`.
    Test,
    /// Disable SSO. Requires `sso.manage` and --step-up: action
    /// `organization.sso_change`, resource = the organization UUID.
    Disable,
}

/// Member commands.
#[derive(Debug, Subcommand)]
pub enum MemberCommand {
    /// List members.
    List {
        /// Only Carbons, or only Silicons.
        #[arg(long, value_parser = ["carbon", "silicon"])]
        principal_type: Option<String>,
        /// Only members carrying this tag UUID from `iam tag list`.
        #[arg(long, value_name = "TAG_ID", value_parser = parse_tag_id)]
        tag: Option<Uuid>,
        /// Only members in this state.
        #[arg(long, value_parser = ["active", "removed"])]
        status: Option<String>,
        /// Paging.
        #[command(flatten)]
        page: PageArgs,
    },
    /// Show one member.
    Show {
        /// Membership identifier.
        membership_id: Uuid,
    },
    /// Show a member's role and capabilities.
    Authorization {
        /// Membership identifier.
        membership_id: Uuid,
    },
    /// Update a member's directory metadata.
    ///
    /// `--first-silicon` applies only to Carbon memberships. Reporting-line
    /// and profile-photo options apply only to Silicon memberships.
    #[command(group(
        clap::ArgGroup::new("changes")
            .args([
                "first_silicon",
                "clear_first_silicon",
                "reports_to",
                "clear_reports_to",
                "profile_photo",
                "clear_profile_photo"
            ])
            .required(true)
            .multiple(true)
    ))]
    Update {
        /// Membership identifier.
        membership_id: Uuid,
        /// Carbon only: assign the Carbon's first Silicon membership.
        #[arg(long)]
        first_silicon: Option<Uuid>,
        /// Carbon only: unassign the Carbon's first Silicon.
        #[arg(long, conflicts_with = "first_silicon")]
        clear_first_silicon: bool,
        /// Silicon only: assign a new reporting line.
        #[arg(long)]
        reports_to: Option<Uuid>,
        /// Silicon only: remove the current reporting line.
        #[arg(long, conflicts_with = "reports_to")]
        clear_reports_to: bool,
        /// Silicon only: set a profile-photo override URL.
        #[arg(long)]
        profile_photo: Option<String>,
        /// Silicon only: remove the profile-photo override.
        #[arg(long, conflicts_with = "profile_photo")]
        clear_profile_photo: bool,
    },
    /// Remove a member. Needs --step-up: action
    /// `organization.authorization_change`, resource = this membership UUID.
    Remove {
        /// Membership identifier.
        membership_id: Uuid,
        /// Membership to inherit anyone reporting to them.
        #[arg(long)]
        reassign_reports_to: Option<Uuid>,
    },
    /// Promote a member to administrator. Needs --step-up: action
    /// `organization.authorization_change`, resource = this membership UUID.
    Promote {
        /// Membership identifier.
        membership_id: Uuid,
    },
    /// Demote an administrator. Needs --step-up: action
    /// `organization.authorization_change`, resource = this membership UUID.
    Demote {
        /// Membership identifier.
        membership_id: Uuid,
    },
    /// Replace an administrator's capabilities. Needs --step-up: action
    /// `organization.authorization_change`, resource = this membership UUID.
    Capabilities {
        /// Membership identifier.
        membership_id: Uuid,
        /// The complete set to grant; anything omitted is revoked.
        #[arg(
            long = "capability",
            value_name = "CAPABILITY",
            value_parser = [
                "organization.update",
                "members.invite",
                "members.update_directory",
                "members.remove",
                "silicons.create",
                "silicons.update_directory",
                "silicons.manage_hierarchy",
                "silicons.remove",
                "silicons.rotate_token",
                "tags.manage",
                "trust.manage",
                "roles.request",
                "roles.approve",
                "admins.create",
                "admins.manage",
                "sso.manage"
            ]
        )]
        capabilities: Vec<String>,
    },
    /// Show the organization directory.
    Directory {
        /// Comma-separated fields: name,id,role,org,tags,trust.
        #[arg(long, value_parser = parse_directory_fields)]
        fields: Option<String>,
        /// Paging.
        #[command(flatten)]
        page: PageArgs,
    },
    /// Show your own directory entry.
    Self_ {
        /// Comma-separated fields: name,id,role,org,tags,trust.
        #[arg(long, value_parser = parse_directory_fields)]
        fields: Option<String>,
    },
    /// Show one member through the sparse organization directory.
    DirectoryMember {
        /// Membership identifier.
        membership_id: Uuid,
        /// Comma-separated fields: name,id,role,org,tags,trust.
        #[arg(long, value_parser = parse_directory_fields)]
        fields: Option<String>,
    },
}

/// Invitation commands.
#[derive(Debug, Subcommand)]
pub enum InviteCommand {
    /// List invitations this organization issued.
    List {
        /// Only invitations in this state.
        #[arg(
            long,
            value_parser = ["pending", "accepted", "revoked", "expired"]
        )]
        status: Option<String>,
        /// Paging.
        #[command(flatten)]
        page: PageArgs,
    },
    /// Invite a Carbon by handle or email.
    #[command(group(
        clap::ArgGroup::new("identity")
            .args(["carbon_id", "email"])
            .required(true)
            .multiple(false)
    ))]
    Create {
        /// Carbon ID to invite.
        #[arg(long)]
        carbon_id: Option<String>,
        /// Email address to invite.
        #[arg(long)]
        email: Option<String>,
        /// Job role granted on acceptance.
        #[arg(long)]
        job_role: String,
        /// Trust boundary the new member starts with.
        #[arg(
            long,
            default_value = "internal",
            value_parser = ["internal", "external"]
        )]
        boundary: String,
        /// Trust level the new member starts with.
        #[arg(
            long,
            default_value = "not_trusted",
            value_parser = ["not_trusted", "needs_approval", "trusted"]
        )]
        level: String,
    },
    /// Show one invitation.
    Show {
        /// Invitation identifier.
        invite_id: Uuid,
    },
    /// Revoke a pending invitation.
    Revoke {
        /// Invitation identifier.
        invite_id: Uuid,
    },
    /// Send yourself the verification code for an email invitation.
    Code {
        /// The invited email address.
        email: String,
    },
    /// Accept an invitation and join.
    Accept {
        /// The invitation being accepted.
        invite_id: Uuid,
        /// Verification code from the invitation email.
        #[arg(long)]
        code: String,
    },
}

/// Tag commands.
#[derive(Debug, Subcommand)]
pub enum TagCommand {
    /// List tags.
    List(PageArgs),
    /// Create a tag.
    Create {
        /// Tag name.
        name: String,
    },
    /// Show one tag.
    Show {
        /// Tag identifier.
        tag_id: Uuid,
    },
    /// Rename a tag.
    Rename {
        /// Tag identifier.
        tag_id: Uuid,
        /// New name.
        name: String,
    },
    /// Delete a tag, and everything it conferred.
    Delete {
        /// Tag identifier.
        tag_id: Uuid,
    },
    /// List members carrying a tag.
    Members {
        /// Tag identifier.
        tag_id: Uuid,
        /// Paging.
        #[command(flatten)]
        page: PageArgs,
    },
}

/// Trust commands.
#[derive(Debug, Subcommand)]
pub enum TrustCommand {
    /// Show the organization-wide default.
    Default,
    /// Replace the organization-wide default.
    SetDefault {
        /// `internal` or `external`.
        #[arg(long, value_parser = ["internal", "external"])]
        boundary: String,
        /// `not_trusted`, `needs_approval`, or `trusted`.
        #[arg(
            long,
            value_parser = ["not_trusted", "needs_approval", "trusted"]
        )]
        level: String,
    },
    /// List trust rules.
    List(PageArgs),
    /// Create a trust rule.
    #[command(
        group(
            clap::ArgGroup::new("subject")
                .args(["subject_tag", "subject_membership"])
                .required(true)
                .multiple(false)
        ),
        group(
            clap::ArgGroup::new("target")
                .args(["target_tag", "target_membership"])
                .required(true)
                .multiple(false)
        )
    )]
    Create {
        /// Subject tag UUID from `iam tag list`.
        #[arg(long, value_name = "TAG_ID")]
        subject_tag: Option<Uuid>,
        /// Subject membership UUID from `iam member list`.
        #[arg(long, value_name = "MEMBERSHIP_ID")]
        subject_membership: Option<Uuid>,
        /// Target tag UUID from `iam tag list`.
        #[arg(long, value_name = "TAG_ID")]
        target_tag: Option<Uuid>,
        /// Target active Silicon membership UUID from `iam silicon show`.
        #[arg(long, value_name = "SILICON_MEMBERSHIP_ID")]
        target_membership: Option<Uuid>,
        /// `internal` or `external`.
        #[arg(long, value_parser = ["internal", "external"])]
        boundary: String,
        /// `not_trusted`, `needs_approval`, or `trusted`.
        #[arg(
            long,
            value_parser = ["not_trusted", "needs_approval", "trusted"]
        )]
        level: String,
    },
    /// Show one trust rule.
    Show {
        /// Rule identifier.
        rule_id: Uuid,
    },
    /// Change a rule's trust value.
    Update {
        /// Rule identifier.
        rule_id: Uuid,
        /// `internal` or `external`.
        #[arg(long, value_parser = ["internal", "external"])]
        boundary: String,
        /// `not_trusted`, `needs_approval`, or `trusted`.
        #[arg(
            long,
            value_parser = ["not_trusted", "needs_approval", "trusted"]
        )]
        level: String,
    },
    /// Archive a trust rule.
    Delete {
        /// Rule identifier.
        rule_id: Uuid,
    },
    /// Explain the trust between a subject and a target Silicon.
    Evaluate {
        /// Subject membership UUID from `iam member list`.
        #[arg(long, value_name = "MEMBERSHIP_ID")]
        subject: Uuid,
        /// Target active Silicon membership UUID from `iam silicon show`.
        #[arg(long, value_name = "SILICON_MEMBERSHIP_ID")]
        target: Uuid,
    },
}

/// Governance commands.
#[derive(Debug, Subcommand)]
pub enum ApprovalCommand {
    /// List approval requests.
    List {
        /// Only requests in this state.
        #[arg(
            long,
            value_parser = ["pending", "approved", "rejected", "completed"]
        )]
        status: Option<String>,
        /// Only requests of this kind.
        #[arg(
            long,
            value_parser = [
                "carbon_job_role_change",
                "silicon_job_role_change",
                "carbon_tag_change",
                "silicon_tag_change",
                "silicon_token_rotation"
            ]
        )]
        kind: Option<String>,
        /// Only requests you can decide now.
        #[arg(long)]
        mine: bool,
        /// Paging.
        #[command(flatten)]
        page: PageArgs,
    },
    /// Show one approval request.
    Show {
        /// Request identifier.
        request_id: Uuid,
    },
    /// Approve or reject a request. A Silicon token rotation needs --step-up:
    /// action `silicon.rotate_token`, resource = the Silicon principal UUID.
    Decide {
        /// Request identifier.
        request_id: Uuid,
        /// `approve` or `reject`.
        #[arg(long, value_parser = ["approve", "reject"])]
        decision: String,
        /// Reason recorded with the decision.
        #[arg(long)]
        reason: Option<String>,
    },
    /// Request a Silicon job-role change. Silicon-only; Carbon callers are forbidden.
    RequestRole {
        /// Membership whose role should change.
        #[arg(long)]
        membership_id: Uuid,
        /// The role being asked for.
        #[arg(long)]
        job_role: String,
    },
    /// Request a Silicon tag change. Silicon-only; Carbon callers are forbidden.
    #[command(group(
        clap::ArgGroup::new("changes")
            .args(["add", "remove"])
            .required(true)
            .multiple(true)
    ))]
    RequestTags {
        /// Membership whose tags should change.
        #[arg(long)]
        membership_id: Uuid,
        /// Tags to add.
        #[arg(long = "add", value_name = "TAG_ID")]
        add: Vec<Uuid>,
        /// Tags to remove.
        #[arg(long = "remove", value_name = "TAG_ID")]
        remove: Vec<Uuid>,
    },
    /// Set a member's job role directly.
    SetRole {
        /// Membership identifier.
        membership_id: Uuid,
        /// The role to set.
        job_role: String,
    },
    /// Replace a member's tags directly.
    SetTags {
        /// Membership identifier.
        membership_id: Uuid,
        /// The complete tag set; anything omitted is removed.
        #[arg(long = "tag", value_name = "TAG_ID")]
        tags: Vec<Uuid>,
    },
    /// Show a member's job-role history.
    RoleHistory {
        /// Membership identifier.
        membership_id: Uuid,
        /// Paging.
        #[command(flatten)]
        page: PageArgs,
    },
    /// Show a member's tag history.
    TagHistory {
        /// Membership identifier.
        membership_id: Uuid,
        /// Paging.
        #[command(flatten)]
        page: PageArgs,
    },
}

/// Silicon commands.
#[derive(Debug, Subcommand)]
pub enum SiliconCommand {
    /// List Silicons.
    List {
        /// Only Silicons carrying this tag UUID from `iam tag list`.
        #[arg(long, value_name = "TAG_ID", value_parser = parse_tag_id)]
        tag: Option<Uuid>,
        /// Paging.
        #[command(flatten)]
        page: PageArgs,
    },
    /// Create a Silicon, returning its credential once.
    Create {
        /// Local handle using --org, or canonical `handle:org`. A canonical ID
        /// supplies its organization when none is selected and must match one
        /// that is selected.
        handle: String,
        /// Job role this Silicon holds.
        #[arg(long)]
        job_role: String,
        /// Display name.
        #[arg(long)]
        display_name: Option<String>,
        /// Active Silicon membership this Silicon reports to.
        #[arg(long)]
        reports_to: Option<Uuid>,
        /// Tags to assign at creation.
        #[arg(long = "tag", value_name = "TAG_ID")]
        tags: Vec<Uuid>,
    },
    /// Show one Silicon.
    Show {
        /// Local handle, or global `handle:org`; local uses --org.
        silicon_id: String,
    },
    /// Update a Silicon's directory configuration.
    #[command(group(
        clap::ArgGroup::new("changes")
            .args([
                "display_name",
                "timezone",
                "description",
                "clear_description",
                "profile_photo",
                "clear_profile_photo",
                "reports_to",
                "clear_reports_to"
            ])
            .required(true)
            .multiple(true)
    ))]
    Update {
        /// Local handle, or global `handle:org`; local uses --org.
        silicon_id: String,
        /// New display name.
        #[arg(long)]
        display_name: Option<String>,
        /// New IANA time-zone identifier.
        #[arg(long)]
        timezone: Option<String>,
        /// New description.
        #[arg(long)]
        description: Option<String>,
        /// Remove the current description.
        #[arg(long, conflicts_with = "description")]
        clear_description: bool,
        /// New profile photo URL.
        #[arg(long)]
        profile_photo: Option<String>,
        /// Remove the profile-photo override.
        #[arg(long, conflicts_with = "profile_photo")]
        clear_profile_photo: bool,
        /// New active Silicon membership to report to.
        #[arg(long)]
        reports_to: Option<Uuid>,
        /// Remove the current reporting line.
        #[arg(long, conflicts_with = "reports_to")]
        clear_reports_to: bool,
    },
    /// Remove a Silicon. Needs --step-up: action
    /// `organization.authorization_change`, resource = its membership UUID.
    Remove {
        /// Local handle, or global `handle:org`; local uses --org.
        silicon_id: String,
        /// Active Silicon membership to inherit anyone reporting to it.
        #[arg(long)]
        reassign_reports_to: Option<Uuid>,
    },
    /// Request credential rotation. Needs --step-up: action
    /// `silicon.rotate_token`, resource = the Silicon principal UUID.
    RotateRequest {
        /// Local handle, or global `handle:org`; local uses --org.
        silicon_id: String,
    },
    /// Complete an approved rotation. Needs --step-up: action
    /// `silicon.rotate_token`, resource = the Silicon principal UUID.
    RotateComplete {
        /// Local handle, or global `handle:org`; local uses --org.
        silicon_id: String,
        /// The approved request.
        request_id: Uuid,
    },
    /// Show the webhook endpoint.
    Webhook {
        /// Local handle, or global `handle:org`; local uses --org.
        silicon_id: String,
    },
    /// Configure or replace the webhook endpoint. Needs --step-up: action
    /// `organization.silicon_webhook.redirect`, resource = its membership UUID.
    SetWebhook {
        /// Local handle, or global `handle:org`; local uses --org.
        silicon_id: String,
        /// HTTPS endpoint to deliver to.
        #[arg(long = "webhook-url")]
        webhook_url: String,
    },
    /// Remove the webhook endpoint. Needs --step-up: action
    /// `organization.silicon_webhook.redirect`, resource = its membership UUID.
    DeleteWebhook {
        /// Local handle, or global `handle:org`; local uses --org.
        silicon_id: String,
    },
    /// Show the webhook subscription.
    Subscription {
        /// Local handle, or global `handle:org`; local uses --org.
        silicon_id: String,
    },
    /// Replace the webhook subscription. Needs --step-up: action
    /// `organization.silicon_webhook.redirect`, resource = its membership UUID.
    SetSubscription {
        /// Local handle, or global `handle:org`; local uses --org.
        silicon_id: String,
        /// `all` for every event, or `selected` with --topic.
        #[arg(long, default_value = "all", value_parser = ["all", "selected"])]
        mode: String,
        /// Topics to receive when mode is `selected`.
        #[arg(
            long = "topic",
            value_name = "TOPIC",
            value_parser = [
                "membership_lifecycle",
                "member_updates",
                "trust_updates"
            ],
            required_if_eq("mode", "selected")
        )]
        topics: Vec<String>,
        /// Additional tags whose events should also be delivered.
        #[arg(long = "tag", value_name = "TAG_ID")]
        tags: Vec<Uuid>,
        /// Filter to the Silicon's own event-time tags, with no additional tags.
        #[arg(long, conflicts_with = "tags")]
        own_tags_only: bool,
    },
    /// Remove the webhook subscription. Needs --step-up: action
    /// `organization.silicon_webhook.redirect`, resource = its membership UUID.
    DeleteSubscription {
        /// Local handle, or global `handle:org`; local uses --org.
        silicon_id: String,
    },
    /// List deliveries that exhausted their retries.
    DeadLetters {
        /// Local handle, or global `handle:org`; local uses --org.
        silicon_id: String,
        /// Paging.
        #[command(flatten)]
        page: PageArgs,
    },
    /// Re-queue dead-lettered deliveries.
    Replay {
        /// Local handle, or global `handle:org`; local uses --org.
        silicon_id: String,
        /// Deliveries to replay.
        #[arg(long = "delivery", value_name = "DELIVERY_ID", required = true)]
        deliveries: Vec<Uuid>,
    },
}

/// Application commands.
#[derive(Debug, Subcommand)]
pub enum AppCommand {
    /// List applications across all organizations you can administer.
    ///
    /// This is an intentionally cross-organization view. --org does not
    /// filter it; use --status to filter by Application state.
    List {
        /// Only applications in this state.
        #[arg(
            long,
            value_parser = [
                "under_review",
                "verified",
                "rejected",
                "suspended",
                "deleted"
            ]
        )]
        status: Option<String>,
        /// Paging.
        #[command(flatten)]
        page: PageArgs,
    },
    /// Register an application, returning its generated client secret once.
    Create {
        /// Local handle using --org, or canonical `org>handle`. A canonical ID
        /// supplies its organization when none is selected and must match one
        /// that is selected.
        app_id: String,
        /// Display name.
        #[arg(long)]
        name: String,
        /// Reserved for handler compatibility; use the global --org option.
        #[arg(skip)]
        org: Option<String>,
        /// HTTPS endpoint the service delivers webhooks to.
        #[arg(long)]
        webhook_url: String,
        /// Caller-chosen secret: 32-512 non-whitespace ASCII characters. IAM never generates it.
        #[arg(long)]
        webhook_secret: String,
        /// Pathless public origin without a trailing slash, credentials,
        /// query, or fragment. HTTP localhost/loopback IP is for a local IAM
        /// runtime; the hosted edge may require a public HTTPS origin.
        #[arg(long)]
        base_url: String,
        /// JSON array of OBO endpoint definitions.
        #[arg(long, value_name = "JSON")]
        obo_endpoints: Option<String>,
    },
    /// Show one application.
    Show {
        /// Local handle, or canonical `org>handle`; local uses --org.
        app_id: String,
    },
    /// Update an application.
    #[command(group(
        clap::ArgGroup::new("changes")
            .args([
                "name",
                "clear_name",
                "logo",
                "clear_logo",
                "base_url",
                "obo_endpoints"
            ])
            .required(true)
            .multiple(true)
    ))]
    Update {
        /// Local handle, or canonical `org>handle`; local uses --org.
        app_id: String,
        /// New display name.
        #[arg(long)]
        name: Option<String>,
        /// Remove the current display name.
        #[arg(long, conflicts_with = "name")]
        clear_name: bool,
        /// New logo URL.
        #[arg(long)]
        logo: Option<String>,
        /// Remove the current logo.
        #[arg(long, conflicts_with = "logo")]
        clear_logo: bool,
        /// New pathless public origin without a trailing slash, credentials,
        /// query, or fragment. HTTP localhost/loopback IP is for a local IAM
        /// runtime; the hosted edge may require a public HTTPS origin.
        #[arg(long)]
        base_url: Option<String>,
        /// Complete replacement OBO endpoint array as JSON.
        #[arg(long, value_name = "JSON")]
        obo_endpoints: Option<String>,
    },
    /// Rotate the client secret. Needs --step-up: action
    /// `application.client_secret.rotate`, resource = the Application UUID.
    RotateSecret {
        /// Local handle, or canonical `org>handle`; local uses --org.
        app_id: String,
    },
    /// Rotate the webhook signing secret. Needs --step-up: action
    /// `application.webhook_secret.rotate`, resource = the Application UUID.
    RotateWebhookSecret {
        /// Local handle, or canonical `org>handle`; local uses --org.
        app_id: String,
        /// Caller-chosen secret: 32-512 non-whitespace ASCII characters. IAM never generates it.
        #[arg(long)]
        webhook_secret: String,
    },
    /// Discover an application's base URL as another application.
    Discover {
        /// Target local handle or canonical `org>handle`; local uses --org.
        app_id: String,
        /// Requester local handle or canonical `org>handle`; local uses --org.
        #[arg(long = "as-app-id", value_name = "APP_ID")]
        requester_app_id: String,
        /// Requester's application secret. Prompted for when omitted.
        #[arg(long)]
        app_secret: Option<String>,
    },
    /// Application token exchange, refresh, introspection, and revocation.
    #[command(subcommand)]
    Token(AppTokenCommand),
    /// Same-organization on-behalf-of access.
    #[command(subcommand)]
    Obo(AppOboCommand),
    /// Verify a captured webhook locally, before parsing or acting on it.
    VerifyWebhook {
        /// File containing the exact delivered body bytes; use `-` for stdin.
        #[arg(value_name = "BODY_FILE")]
        body_file: PathBuf,
        /// Value of `X-Silicon-IAM-Event-ID`.
        #[arg(long)]
        event_id: String,
        /// Value of `X-Silicon-IAM-Timestamp`.
        #[arg(long)]
        timestamp: String,
        /// Value of `X-Silicon-IAM-Key-Version`.
        #[arg(long)]
        key_version: String,
        /// Value of `X-Silicon-IAM-Signature`.
        #[arg(long)]
        signature: String,
        /// Signing secret for this key version: 32-512 non-whitespace ASCII characters.
        #[arg(long)]
        webhook_secret: String,
        /// Maximum accepted clock distance in seconds.
        #[arg(long, default_value_t = 300)]
        tolerance_seconds: u64,
    },
    /// Import a production application into the selected testing environment.
    /// Requires --test and a signed-in test Carbon; an existing target
    /// organization must be administered by that Carbon.
    Import {
        /// Canonical production application identifier, such as `google>drive`.
        app_id: String,
    },
    /// Show the webhook endpoint.
    Webhook {
        /// Local handle, or canonical `org>handle`; local uses --org.
        app_id: String,
    },
    /// Propose a webhook endpoint.
    SetWebhook {
        /// Local handle, or canonical `org>handle`; local uses --org.
        app_id: String,
        /// HTTPS endpoint to deliver to.
        #[arg(long = "webhook-url")]
        webhook_url: String,
        /// New 32-512 non-whitespace ASCII secret; required when replacing an
        /// imported test webhook.
        #[arg(long)]
        webhook_secret: Option<String>,
    },
    /// List deliveries that exhausted their retries.
    DeadLetters {
        /// Local handle, or canonical `org>handle`; local uses --org.
        app_id: String,
        /// Paging.
        #[command(flatten)]
        page: PageArgs,
    },
    /// Re-queue dead-lettered deliveries.
    Replay {
        /// Local handle, or canonical `org>handle`; local uses --org.
        app_id: String,
        /// Deliveries to replay.
        #[arg(long = "delivery", value_name = "DELIVERY_ID", required = true)]
        deliveries: Vec<Uuid>,
    },
    /// Show logins performed through an application.
    History {
        /// Local handle, or canonical `org>handle`; local uses --org.
        app_id: String,
        /// Paging.
        #[command(flatten)]
        page: PageArgs,
    },
}

/// Application token commands.
#[derive(Debug, Subcommand)]
pub enum AppTokenCommand {
    /// Fetch current organization authorization, including permitted role/tags.
    ///
    /// Use immediately after login or to rebuild an empty application cache.
    /// Requires an organization-bound Application access token and its own
    /// Application secret. Role requires roles.read; tags require memberships.read.
    /// Undisclosed fields grant no authority. Inactive, mismatched, refresh and
    /// unscoped tokens return no organization snapshot. No webhook or directory
    /// edit is needed. The backend must support authorization snapshots.
    Authorization {
        /// Local handle, or canonical `org>handle`; local uses --org.
        app_id: String,
        /// Application access token. Prompted for when omitted.
        #[arg(long)]
        token: Option<String>,
        /// Exact organization handle; a mismatch returns no authority.
        #[arg(long)]
        org_context: Option<String>,
        /// Application secret. Prompted for when omitted.
        #[arg(long)]
        app_secret: Option<String>,
    },
    /// Exchange a single-use short-lived token for an Application session.
    Exchange {
        /// Local handle, or canonical `org>handle`; local uses --org.
        app_id: String,
        /// Short-lived token. Prompted for when omitted.
        #[arg(long)]
        slt: Option<String>,
        /// Application secret. Prompted for when omitted.
        #[arg(long)]
        app_secret: Option<String>,
        /// A 16-255 character visible-ASCII key. Reuse it after an uncertain
        /// exchange of the same short-lived token; generated when omitted.
        #[arg(long)]
        idempotency_key: Option<String>,
    },
    /// Rotate an Application refresh token and its access token.
    Refresh {
        /// Local handle, or canonical `org>handle`; local uses --org.
        app_id: String,
        /// Refresh token. Prompted for when omitted.
        #[arg(long)]
        refresh_token: Option<String>,
        /// Application secret. Prompted for when omitted.
        #[arg(long)]
        app_secret: Option<String>,
        /// A 16-255 character visible-ASCII key. Reuse it after an uncertain
        /// refresh; never retry with a new key. Generated when omitted.
        #[arg(long)]
        idempotency_key: Option<String>,
    },
    /// Ask IAM for a token's current, authoritative state.
    Introspect {
        /// Local handle, or canonical `org>handle`; local uses --org.
        app_id: String,
        /// Access or refresh token. Prompted for when omitted.
        #[arg(long)]
        token: Option<String>,
        /// Hint which kind of token is being checked.
        #[arg(long, value_enum)]
        token_type: Option<AppTokenType>,
        /// Exact organization handle sent as `X-Org-ID`; a mismatch makes the token inactive.
        #[arg(long)]
        org_context: Option<String>,
        /// Application secret. Prompted for when omitted.
        #[arg(long)]
        app_secret: Option<String>,
    },
    /// Revoke one access token, or the complete family of a refresh token.
    Revoke {
        /// Local handle, or canonical `org>handle`; local uses --org.
        app_id: String,
        /// Access or refresh token. Prompted for when omitted.
        #[arg(long)]
        token: Option<String>,
        /// Hint which kind of token is being revoked.
        #[arg(long, value_enum)]
        token_type: Option<AppTokenType>,
        /// Application secret. Prompted for when omitted.
        #[arg(long)]
        app_secret: Option<String>,
        /// A 16-255 character visible-ASCII key. Reuse it after an uncertain
        /// revocation of the same token; generated when omitted.
        #[arg(long)]
        idempotency_key: Option<String>,
    },
}

/// Token type hints accepted by Application introspection and revocation.
#[derive(Clone, Copy, Debug, ValueEnum)]
pub enum AppTokenType {
    /// An Application access token.
    AccessToken,
    /// An Application refresh token.
    RefreshToken,
}

/// OBO commands called by Applications as themselves.
#[derive(Debug, Subcommand)]
pub enum AppOboCommand {
    /// Discover an Application's callable OBO endpoint catalog.
    Endpoints {
        /// Audience local handle or canonical `org>handle`; local uses --org.
        audience_app_id: String,
        /// Requester local handle or canonical `org>handle`; local uses --org.
        #[arg(long = "as-app-id", value_name = "APP_ID")]
        requester_app_id: String,
        /// Requester's Application secret. Prompted for when omitted.
        #[arg(long)]
        app_secret: Option<String>,
    },
    /// Bind a single-use proof to one exact downstream request.
    Exchange {
        /// Audience local handle or canonical `org>handle`; local uses --org.
        audience_app_id: String,
        /// Registered endpoint identifier from `app obo endpoints`.
        endpoint_id: String,
        /// Requester local handle or canonical `org>handle`; local uses --org.
        #[arg(long = "as-app-id", value_name = "APP_ID")]
        requester_app_id: String,
        /// Requester's Application secret. Prompted for when omitted.
        #[arg(long)]
        app_secret: Option<String>,
        /// Actor-bound Application access token. Prompted for when omitted.
        #[arg(long)]
        subject_token: Option<String>,
        /// Downstream HTTP method; normalized to uppercase.
        #[arg(long)]
        method: String,
        /// JSON object required by the registered endpoint.
        #[arg(long, default_value = "{}")]
        metadata: String,
        /// A 16-255 character visible-ASCII key. Reuse it with the same request
        /// after an uncertain exchange; the timestamp may refresh. Generated
        /// when omitted.
        #[arg(long)]
        idempotency_key: Option<String>,
        /// Override the OBO Unix timestamp; normally omit so the client uses now.
        #[arg(long)]
        timestamp: Option<i64>,
        /// Exact downstream body bytes.
        #[command(flatten)]
        body: RequestBodyArgs,
    },
    /// Consume and verify an OBO proof as its audience Application.
    Verify {
        /// Audience local handle or canonical `org>handle`; local uses --org.
        audience_app_id: String,
        /// Audience Application secret. Prompted for when omitted.
        #[arg(long)]
        app_secret: Option<String>,
        /// Single-use OBO proof. Prompted for when omitted.
        #[arg(long)]
        access_proof: Option<String>,
        /// Actual downstream HTTP method; normalized to uppercase.
        #[arg(long)]
        method: String,
        /// Exact registered path of the actual downstream request.
        #[arg(long)]
        path: String,
        /// Exact downstream body bytes.
        #[command(flatten)]
        body: RequestBodyArgs,
    },
}

/// A downstream request body supplied losslessly from a file or conveniently inline.
#[derive(Debug, Args)]
pub struct RequestBodyArgs {
    /// UTF-8 request body given directly; defaults to an empty body.
    #[arg(long, conflicts_with = "body_file")]
    pub body: Option<String>,
    /// File containing exact request bytes; use `-` for stdin.
    #[arg(long, value_name = "PATH", conflicts_with = "body")]
    pub body_file: Option<PathBuf>,
}

/// Testing environment commands.
#[derive(Debug, Subcommand)]
pub enum EnvCommand {
    /// List environments.
    List {
        /// `active`, `deleted`, or `all`.
        #[arg(long, value_parser = ["active", "deleted", "all"])]
        status: Option<String>,
        /// Paging.
        #[command(flatten)]
        page: PageArgs,
    },
    /// Create an environment, returning its key.
    Create {
        /// Environment name.
        name: String,
        /// Description.
        #[arg(long)]
        description: Option<String>,
    },
    /// Show one environment.
    Show {
        /// Environment identifier.
        environment_id: Uuid,
    },
    /// Rename or re-describe an environment.
    #[command(group(
        clap::ArgGroup::new("changes")
            .args(["name", "description", "clear_description"])
            .required(true)
            .multiple(true)
    ))]
    Update {
        /// Environment identifier.
        environment_id: Uuid,
        /// New name.
        #[arg(long)]
        name: Option<String>,
        /// New description.
        #[arg(long)]
        description: Option<String>,
        /// Remove the current description.
        #[arg(long, conflicts_with = "description")]
        clear_description: bool,
    },
    /// Retire an environment, keeping it recoverable.
    Delete {
        /// Environment identifier.
        environment_id: Uuid,
    },
    /// Bring a retired environment back.
    Restore {
        /// Environment identifier.
        environment_id: Uuid,
    },
    /// Show an environment's key.
    Key {
        /// Environment identifier.
        environment_id: Uuid,
    },
    /// Issue a new key, invalidating the old one.
    RotateKey {
        /// Environment identifier.
        environment_id: Uuid,
    },
    /// Erase everything inside an environment.
    ///
    /// With an ID, run outside --test using the production control plane.
    /// Without an ID, --test is required and authorizes cleaning with the
    /// locally stored environment key.
    Clean {
        /// Environment identifier. Omit to clean the one selected by --test.
        environment_id: Option<Uuid>,
    },
    /// Describe the environment selected by --test. Requires --test.
    Current,
}

/// Session commands.
#[derive(Debug, Subcommand)]
pub enum SessionCommand {
    /// List active and recently revoked sessions.
    List(PageArgs),
    /// Revoke one of your sessions. Needs --step-up: action
    /// `account.session_revoke`, resource = this session UUID. The target,
    /// and the current session when different, must be at least 12 hours old.
    Revoke {
        /// Session identifier.
        session_id: Uuid,
    },
    /// Show your login history.
    History(PageArgs),
}

/// Settings commands.
#[derive(Debug, Subcommand)]
pub enum ConfigCommand {
    /// Show the current settings.
    Show,
    /// List configured profiles.
    Profiles,
    /// Set a value on the current profile.
    Set {
        /// One of `url`, `org`, `auto-update`.
        #[arg(value_parser = ["url", "org", "auto-update"])]
        key: String,
        /// The value to store. For auto-update use on/off; URL follows --url's
        /// security rules; org is an organization handle.
        value: String,
    },
    /// Clear a value on the current profile.
    Unset {
        /// `org`, or `auto-update` to restore its default-on policy.
        #[arg(value_parser = ["org", "auto-update"])]
        key: String,
    },
    /// Switch the default profile.
    Use {
        /// Profile name.
        profile: String,
    },
}

/// Service commands.
#[derive(Debug, Subcommand)]
pub enum SystemCommand {
    /// Show the service's version, and agree an API version.
    Version,
    /// Check crates.io now and install the latest CLI release.
    Update,
    /// Check that the service is alive and ready.
    Health,
}

/// Where a listing starts, and how much of it to take.
#[derive(Debug, Args, Default)]
pub struct PageArgs {
    /// Continue from a cursor a previous page returned.
    #[arg(long)]
    pub cursor: Option<String>,
    /// Maximum entries to return.
    #[arg(long, value_parser = clap::value_parser!(u16).range(1..=100))]
    pub limit: Option<u16>,
}

/// Validate and normalize the directory's comma-separated field selector.
fn parse_carbon_id(value: &str) -> Result<String, String> {
    silicon_iam_client::api::signup::validate_carbon_id(value)
        .map_err(|error| error.to_string())?;
    Ok(value.to_owned())
}

fn parse_directory_fields(value: &str) -> Result<String, String> {
    const ALLOWED: [&str; 6] = ["name", "id", "role", "org", "tags", "trust"];

    let fields = value.split(',').map(str::trim).collect::<Vec<_>>();
    if fields.iter().any(|field| field.is_empty()) {
        return Err(format!(
            "expected a comma-separated field list: {}",
            ALLOWED.join(",")
        ));
    }
    if let Some(invalid) = fields.iter().find(|field| !ALLOWED.contains(field)) {
        return Err(format!(
            "unknown directory field `{invalid}`; expected one of {}",
            ALLOWED.join(",")
        ));
    }
    Ok(fields.join(","))
}

/// Parses a tag filter with a message that points to the command that supplies
/// the UUID. A tag name is deliberately not accepted here.
fn parse_tag_id(value: &str) -> Result<Uuid, String> {
    Uuid::parse_str(value).map_err(|_| "expected a tag UUID from `iam tag list`".to_owned())
}

/// Accepts only the familiar hyphenated UUID form.
///
/// `uuid` deliberately also parses a 32-hex-digit simple UUID. Testing root
/// keys are 32 alphanumeric characters, so accepting that alternate form here
/// would let some secrets be mistaken for public environment ids.
fn parse_testing_environment_id(value: &str) -> Result<Uuid, String> {
    let bytes = value.as_bytes();
    let hyphenated = bytes.len() == 36
        && [8, 13, 18, 23]
            .into_iter()
            .all(|index| bytes.get(index) == Some(&b'-'));
    if !hyphenated {
        return Err(
            "expected a hyphenated testing-environment UUID, never its root key".to_owned(),
        );
    }
    Uuid::parse_str(value).map_err(|_| "expected a valid testing-environment UUID".to_owned())
}

impl PageArgs {
    /// The client's paging value for these arguments.
    #[must_use]
    pub fn paging(&self) -> silicon_iam_client::Paging {
        let mut paging = silicon_iam_client::Paging::new();
        if let Some(cursor) = &self.cursor {
            paging = paging.after(cursor.clone());
        }
        if let Some(limit) = self.limit {
            paging = paging.limit(limit);
        }
        paging
    }
}

#[cfg(test)]
mod tests {
    use clap::CommandFactory as _;

    use super::Cli;

    #[test]
    fn the_grammar_is_internally_consistent() {
        Cli::command().debug_assert();
        assert_eq!(Cli::command().get_name(), "iam");
    }

    #[test]
    fn updater_controls_are_reachable() {
        use clap::Parser as _;

        assert!(Cli::try_parse_from(["iam", "system", "update"]).is_ok());
        assert!(Cli::try_parse_from(["iam", "config", "set", "auto-update", "off"]).is_ok());
    }

    #[test]
    fn global_flags_are_accepted_after_the_command_too() {
        use clap::Parser as _;

        let parsed = Cli::try_parse_from(["iam", "tag", "list", "--org", "acme"]);
        assert!(parsed.is_ok(), "{parsed:?}");
        let Ok(cli) = parsed else { return };
        assert_eq!(cli.global.org.as_deref(), Some("acme"));
    }

    #[test]
    fn organization_scope_has_one_unambiguous_global_flag() {
        use clap::Parser as _;

        let parsed = Cli::try_parse_from([
            "iam",
            "app",
            "create",
            "billing",
            "--name",
            "Billing",
            "--webhook-url",
            "https://billing.example/hooks",
            "--webhook-secret",
            "caller-chosen-webhook-secret-0001",
            "--base-url",
            "https://billing.example",
            "--org",
            "acme",
        ]);
        assert!(parsed.is_ok(), "the global --org must work: {parsed:?}");
        let Ok(cli) = parsed else { return };
        assert_eq!(cli.global.org.as_deref(), Some("acme"));
        assert!(matches!(
            cli.command,
            super::Command::App(super::AppCommand::Create { org: None, .. })
        ));

        let member_id = "0198aa41-52e7-7f32-8ab3-bd42110a6e2c";
        let parsed = Cli::try_parse_from(["iam", "org", "transfer", member_id, "--org", "acme"]);
        assert!(parsed.is_ok(), "the global --org must work: {parsed:?}");
        let Ok(cli) = parsed else { return };
        assert_eq!(cli.global.org.as_deref(), Some("acme"));
        assert!(matches!(
            cli.command,
            super::Command::Org(super::OrgCommand::Transfer { org: None, .. })
        ));
    }

    #[test]
    fn webhook_urls_cannot_overwrite_the_service_url() {
        use clap::Parser as _;

        let parsed = Cli::try_parse_from([
            "iam",
            "--url",
            "http://127.0.0.1:8080",
            "silicon",
            "set-webhook",
            "builder",
            "--webhook-url",
            "https://hooks.example/events",
        ]);
        assert!(parsed.is_ok(), "{parsed:?}");
        let Ok(cli) = parsed else { return };
        assert_eq!(cli.global.url.as_deref(), Some("http://127.0.0.1:8080"));
        assert!(matches!(
            cli.command,
            super::Command::Silicon(super::SiliconCommand::SetWebhook {
                webhook_url,
                ..
            }) if webhook_url == "https://hooks.example/events"
        ));

        let parsed = Cli::try_parse_from([
            "iam",
            "app",
            "set-webhook",
            "acme>console",
            "--webhook-url",
            "https://hooks.example/app",
        ]);
        assert!(matches!(
            parsed,
            Ok(Cli {
                command: super::Command::App(super::AppCommand::SetWebhook {
                    webhook_url,
                    ..
                }),
                ..
            }) if webhook_url == "https://hooks.example/app"
        ));
    }

    #[test]
    fn login_admits_exactly_one_identity() {
        use clap::Parser as _;

        assert!(Cli::try_parse_from(["iam", "login", "--email", "a@b.test"]).is_ok());
        assert!(Cli::try_parse_from(["iam", "login"]).is_err());
        // Two identities is ambiguous, and the grammar says so rather than
        // silently preferring one.
        assert!(
            Cli::try_parse_from([
                "iam",
                "login",
                "--email",
                "a@b.test",
                "--carbon-id",
                "someone"
            ])
            .is_err()
        );
    }

    #[test]
    fn page_limits_are_bounded_by_the_service_contract() {
        use clap::Parser as _;

        assert!(Cli::try_parse_from(["iam", "org", "list", "--limit", "1"]).is_ok());
        assert!(Cli::try_parse_from(["iam", "org", "list", "--limit", "100"]).is_ok());
        assert!(Cli::try_parse_from(["iam", "org", "list", "--limit", "0"]).is_err());
        assert!(Cli::try_parse_from(["iam", "org", "list", "--limit", "101"]).is_err());
    }

    #[test]
    fn closed_contract_values_are_rejected_before_network_io() {
        use clap::Parser as _;

        let invalid = [
            vec!["iam", "org", "update", "--join-method", "password"],
            vec!["iam", "member", "list", "--principal-type", "robot"],
            vec!["iam", "member", "list", "--status", "pending"],
            vec!["iam", "invite", "list", "--status", "unknown"],
            vec![
                "iam",
                "trust",
                "set-default",
                "--boundary",
                "partner",
                "--level",
                "trusted",
            ],
            vec!["iam", "approval", "list", "--status", "cancelled"],
            vec!["iam", "approval", "list", "--kind", "role_change"],
            vec![
                "iam",
                "approval",
                "decide",
                "0198aa41-52e7-7f32-8ab3-bd42110a6e2c",
                "--decision",
                "abstain",
            ],
            vec!["iam", "app", "list", "--status", "active"],
            vec!["iam", "env", "list", "--status", "retired"],
            vec!["iam", "config", "set", "unknown", "value"],
            vec!["iam", "config", "unset", "url"],
            vec!["iam", "member", "directory", "--fields", "name,password"],
        ];
        for argv in invalid {
            assert!(
                Cli::try_parse_from(argv.clone()).is_err(),
                "unexpectedly accepted {argv:?}"
            );
        }

        assert!(Cli::try_parse_from(["iam", "org", "update", "--join-method", "sso"]).is_ok());
        assert!(
            Cli::try_parse_from(["iam", "member", "list", "--principal-type", "silicon"]).is_ok()
        );
        assert!(
            Cli::try_parse_from(["iam", "member", "directory", "--fields", "name, tags"]).is_ok()
        );
        assert!(Cli::try_parse_from(["iam", "app", "list", "--status", "verified"]).is_ok());
    }

    #[test]
    fn silicon_subscriptions_use_closed_modes_and_topics() {
        use clap::Parser as _;

        assert!(
            Cli::try_parse_from([
                "iam",
                "silicon",
                "set-subscription",
                "builder",
                "--mode",
                "all",
            ])
            .is_ok()
        );
        assert!(
            Cli::try_parse_from([
                "iam",
                "silicon",
                "set-subscription",
                "builder",
                "--mode",
                "selected",
                "--topic",
                "member_updates",
            ])
            .is_ok()
        );
        assert!(
            Cli::try_parse_from([
                "iam",
                "silicon",
                "set-subscription",
                "builder",
                "--mode",
                "selected",
            ])
            .is_err()
        );
        assert!(
            Cli::try_parse_from([
                "iam",
                "silicon",
                "set-subscription",
                "builder",
                "--mode",
                "some",
            ])
            .is_err()
        );
        assert!(
            Cli::try_parse_from([
                "iam",
                "silicon",
                "set-subscription",
                "builder",
                "--topic",
                "everything",
            ])
            .is_err()
        );
    }

    #[test]
    fn mutating_commands_reject_missing_payloads() {
        use clap::Parser as _;

        let id = "0198aa41-52e7-7f32-8ab3-bd42110a6e2c";
        let missing = [
            vec!["iam", "org", "update"],
            vec!["iam", "member", "update", id],
            vec!["iam", "invite", "create", "--job-role", "Engineer"],
            vec![
                "iam",
                "trust",
                "create",
                "--boundary",
                "internal",
                "--level",
                "trusted",
            ],
            vec!["iam", "approval", "request-tags", "--membership-id", id],
            vec!["iam", "silicon", "update", "builder"],
            vec!["iam", "app", "update", "billing"],
            vec!["iam", "env", "update", id],
            vec!["iam", "silicon", "replay", "builder"],
            vec!["iam", "app", "replay", "billing"],
        ];
        for argv in missing {
            assert!(
                Cli::try_parse_from(argv.clone()).is_err(),
                "unexpectedly accepted no-op mutation {argv:?}"
            );
        }
    }

    #[test]
    fn mutating_commands_accept_a_real_payload() {
        use clap::Parser as _;

        let id = "0198aa41-52e7-7f32-8ab3-bd42110a6e2c";

        assert!(Cli::try_parse_from(["iam", "org", "update", "--name", "Acme"]).is_ok());
        assert!(
            Cli::try_parse_from([
                "iam",
                "member",
                "update",
                id,
                "--profile-photo",
                "https://example.test/a.png"
            ])
            .is_ok()
        );
        assert!(
            Cli::try_parse_from([
                "iam",
                "invite",
                "create",
                "--email",
                "person@example.test",
                "--job-role",
                "Engineer",
            ])
            .is_ok()
        );
        assert!(
            Cli::try_parse_from([
                "iam",
                "trust",
                "create",
                "--subject-membership",
                id,
                "--target-membership",
                id,
                "--boundary",
                "internal",
                "--level",
                "trusted",
            ])
            .is_ok()
        );
        assert!(
            Cli::try_parse_from([
                "iam",
                "approval",
                "request-tags",
                "--membership-id",
                id,
                "--add",
                id,
            ])
            .is_ok()
        );
        assert!(
            Cli::try_parse_from([
                "iam",
                "silicon",
                "update",
                "builder",
                "--display-name",
                "Builder"
            ])
            .is_ok()
        );
        assert!(
            Cli::try_parse_from(["iam", "app", "update", "billing", "--name", "Billing"]).is_ok()
        );
        assert!(
            Cli::try_parse_from(["iam", "env", "update", id, "--description", "proof"]).is_ok()
        );
        assert!(
            Cli::try_parse_from(["iam", "silicon", "replay", "builder", "--delivery", id]).is_ok()
        );
        assert!(Cli::try_parse_from(["iam", "app", "replay", "billing", "--delivery", id]).is_ok());
    }

    #[test]
    fn privileged_help_names_the_step_up_contract() {
        fn command_at<'a>(root: &'a clap::Command, path: &[&str]) -> &'a clap::Command {
            path.iter().fold(root, |command, name| {
                command
                    .find_subcommand(name)
                    .unwrap_or_else(|| panic!("missing command {}", path.join(" ")))
            })
        }

        let command = Cli::command();
        let contracts = [
            (&["org", "transfer"][..], "organization.transfer_ownership"),
            (
                &["member", "remove"][..],
                "organization.authorization_change",
            ),
            (
                &["member", "promote"][..],
                "organization.authorization_change",
            ),
            (
                &["member", "demote"][..],
                "organization.authorization_change",
            ),
            (
                &["member", "capabilities"][..],
                "organization.authorization_change",
            ),
            (
                &["silicon", "remove"][..],
                "organization.authorization_change",
            ),
            (&["silicon", "rotate-request"][..], "silicon.rotate_token"),
            (&["silicon", "rotate-complete"][..], "silicon.rotate_token"),
            (
                &["silicon", "set-webhook"][..],
                "organization.silicon_webhook.redirect",
            ),
            (
                &["silicon", "delete-webhook"][..],
                "organization.silicon_webhook.redirect",
            ),
            (
                &["silicon", "set-subscription"][..],
                "organization.silicon_webhook.redirect",
            ),
            (
                &["silicon", "delete-subscription"][..],
                "organization.silicon_webhook.redirect",
            ),
            (
                &["app", "rotate-secret"][..],
                "application.client_secret.rotate",
            ),
            (
                &["app", "rotate-webhook-secret"][..],
                "application.webhook_secret.rotate",
            ),
            (&["session", "revoke"][..], "account.session_revoke"),
            (&["approval", "decide"][..], "silicon.rotate_token"),
        ];

        for (path, action) in contracts {
            let subcommand = command_at(&command, path);
            let about = subcommand
                .get_long_about()
                .or_else(|| subcommand.get_about())
                .map(ToString::to_string)
                .unwrap_or_default();
            assert!(
                about.to_ascii_lowercase().contains("needs --step-up"),
                "{} help omits the step-up requirement: {about}",
                path.join(" ")
            );
            assert!(
                about.contains(action) && about.contains("resource"),
                "{} help omits {action} or its resource: {about}",
                path.join(" ")
            );
        }
    }

    #[test]
    fn silicon_only_approval_requests_say_so_in_help() {
        let command = Cli::command();
        let Some(approval) = command.find_subcommand("approval") else {
            panic!("approval exists")
        };
        for name in ["request-role", "request-tags"] {
            let Some(subcommand) = approval.find_subcommand(name) else {
                panic!("approval {name} exists")
            };
            let about = subcommand
                .get_long_about()
                .or_else(|| subcommand.get_about())
                .map(ToString::to_string)
                .unwrap_or_default();
            assert!(
                about.contains("Silicon-only") && about.contains("Carbon callers are forbidden"),
                "approval {name} help must state its caller boundary: {about}"
            );
        }
    }

    #[test]
    fn test_context_accepts_an_environment_id_and_never_a_raw_key() {
        use clap::Parser as _;

        let id = "0198aa41-52e7-7f32-8ab3-bd42110a6e2c";
        let parsed = Cli::try_parse_from(["iam", "--test", id, "whoami"]);
        assert!(parsed.is_ok(), "{parsed:?}");
        let Ok(cli) = parsed else { return };
        assert_eq!(
            cli.global.test.map(|value| value.to_string()).as_deref(),
            Some(id)
        );

        assert!(
            Cli::try_parse_from(["iam", "--test", &"a".repeat(32), "whoami"]).is_err(),
            "a root key must never be accepted where the public test id belongs"
        );
    }

    #[test]
    fn application_creation_requires_every_contract_input() {
        use clap::Parser as _;

        // Missing base URL.
        assert!(
            Cli::try_parse_from([
                "iam",
                "app",
                "create",
                "billing",
                "--name",
                "Billing",
                "--webhook-url",
                "https://billing.example/hooks",
                "--webhook-secret",
                "caller-chosen-webhook-secret-0001",
            ])
            .is_err()
        );
        // Missing caller-chosen webhook secret.
        assert!(
            Cli::try_parse_from([
                "iam",
                "app",
                "create",
                "billing",
                "--name",
                "Billing",
                "--webhook-url",
                "https://billing.example/hooks",
                "--base-url",
                "https://billing.example",
            ])
            .is_err()
        );
        assert!(
            Cli::try_parse_from([
                "iam",
                "app",
                "create",
                "billing",
                "--name",
                "Billing",
                "--webhook-url",
                "https://billing.example/hooks",
                "--base-url",
                "https://billing.example",
                "--webhook-secret",
                "caller-chosen-webhook-secret-0001",
            ])
            .is_ok()
        );
    }

    #[test]
    fn application_help_surfaces_required_webhook_secrets() {
        let command = Cli::command();
        let Some(app) = command.find_subcommand("app") else {
            panic!("app must exist");
        };
        for name in ["create", "rotate-webhook-secret", "verify-webhook"] {
            let Some(subcommand) = app.find_subcommand(name) else {
                panic!("app {name} must exist");
            };
            let secret = subcommand
                .get_arguments()
                .find(|argument| argument.get_id() == "webhook_secret");
            assert!(
                secret.is_some_and(clap::Arg::is_required_set),
                "app {name} must require --webhook-secret in its generated usage"
            );
        }
        let Some(create) = app.find_subcommand("create") else {
            return;
        };
        let usage = create.clone().render_usage().to_string();
        assert!(usage.contains("--webhook-secret <WEBHOOK_SECRET>"));
    }

    #[test]
    fn every_user_supplied_argument_has_help() {
        fn walk(command: &clap::Command, path: &str) {
            for argument in command.get_arguments() {
                if matches!(argument.get_id().as_str(), "help" | "version") {
                    continue;
                }
                assert!(
                    argument.get_help().is_some(),
                    "{path} argument {} has no help",
                    argument.get_id()
                );
            }
            for subcommand in command.get_subcommands() {
                let child = format!("{path} {}", subcommand.get_name());
                walk(subcommand, &child);
            }
        }

        walk(&Cli::command(), "iam");
    }

    #[test]
    fn application_token_protocol_is_reachable_without_putting_secrets_in_argv() {
        use clap::Parser as _;

        assert!(Cli::try_parse_from(["iam", "app", "token", "exchange", "acme>checkout"]).is_ok());
        assert!(Cli::try_parse_from(["iam", "app", "token", "refresh", "acme>checkout"]).is_ok());
        assert!(
            Cli::try_parse_from([
                "iam",
                "app",
                "token",
                "revoke",
                "acme>checkout",
                "--token-type",
                "refresh-token",
            ])
            .is_ok()
        );
        assert!(
            Cli::try_parse_from([
                "iam",
                "app",
                "token",
                "introspect",
                "acme>checkout",
                "--token-type",
                "access-token",
            ])
            .is_ok()
        );
    }

    #[test]
    fn application_obo_protocol_has_all_three_steps_and_lossless_body_input() {
        use clap::Parser as _;

        assert!(
            Cli::try_parse_from([
                "iam",
                "app",
                "obo",
                "endpoints",
                "acme>billing",
                "--as-app-id",
                "acme>checkout",
            ])
            .is_ok()
        );
        assert!(
            Cli::try_parse_from([
                "iam",
                "app",
                "obo",
                "exchange",
                "acme>billing",
                "invoices.create",
                "--as-app-id",
                "acme>checkout",
                "--method",
                "post",
                "--body-file",
                "invoice.json",
            ])
            .is_ok()
        );
        assert!(
            Cli::try_parse_from([
                "iam",
                "app",
                "obo",
                "verify",
                "acme>billing",
                "--method",
                "POST",
                "--path",
                "/v1/invoices",
            ])
            .is_ok()
        );
        assert!(
            Cli::try_parse_from([
                "iam",
                "app",
                "obo",
                "exchange",
                "acme>billing",
                "invoices.create",
                "--as-app-id",
                "acme>checkout",
                "--method",
                "POST",
                "--body",
                "{}",
                "--body-file",
                "invoice.json",
            ])
            .is_err(),
            "inline and file bodies are mutually exclusive"
        );
    }

    #[test]
    fn offline_webhook_verification_requires_every_signed_header() {
        use clap::Parser as _;

        let complete = [
            "iam",
            "app",
            "verify-webhook",
            "delivery.json",
            "--event-id",
            "0198aa41-52e7-7f32-8ab3-bd42110a6e2c",
            "--timestamp",
            "1700000000",
            "--key-version",
            "1",
            "--signature",
            "v1=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
            "--webhook-secret",
            "caller-chosen-webhook-secret-0001",
        ];
        assert!(Cli::try_parse_from(complete).is_ok());
        assert!(
            Cli::try_parse_from([
                "iam",
                "app",
                "verify-webhook",
                "delivery.json",
                "--event-id",
                "0198aa41-52e7-7f32-8ab3-bd42110a6e2c",
                "--timestamp",
                "1700000000",
                "--key-version",
                "1",
                "--webhook-secret",
                "caller-chosen-webhook-secret-0001",
            ])
            .is_err()
        );
    }
}