sew-proto 0.11.6

Sew wire contracts: ids, addresses, errors, HTTP and gateway bodies, permissions, policies, audit.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
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
//! Management API bodies and act-as headers.
//!
//! HTTP over the Unix socket `/run/sew/mgmt.sock` with `Bearer sewsvc_…`. `Idempotency-Key` is
//! required on every mgmt `POST`.

use std::fmt;
use std::str::FromStr;

use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::addr::{Addr, Handle};
use crate::api::members::{AgentKind, MemberKind};
use crate::changes::NotificationReason;
use crate::error::{Validate, ValidationError, check_chars, check_known, check_opt_chars};
use crate::external_id::{ExternalId, is_lower_uuid};
use crate::http::Patch;
use crate::id::{Id, parse_id_str};
use crate::limits::{
    DELIVERIES_LONG_POLL_MAX_MS, DISPLAY_NAME_MAX_CHARS, EMAIL_MAX_BYTES, K2_SESSION_ID_MAX_LEN,
    PRONOUNS_MAX_CHARS, TITLE_MAX_CHARS,
};
use crate::time::Timestamp;

mod app_settings;
mod apps;
mod backups;
mod channels;
mod erase;
mod identity;
pub mod local;
mod meet;
mod member_channels;
mod notifications;
mod ops;
mod recordings;
mod spaces;
mod voice;

pub use app_settings::{
    APP_ORIGIN_MAX_BYTES, APP_ORIGINS_MAX, FEATURE_APP_ORIGIN, FEATURE_APP_ORIGINS,
    FEATURE_APP_SCOPED_SESSIONS, FEATURE_HOME_CLIENT, MgmtAppSettings, MgmtAppSettingsRequest,
    MgmtHomeClient, MgmtHomeClientRequest, canonical_origins, default_max_level, is_app_origin,
};
pub use apps::{
    APP_QUOTA_COUNT_MAX, APP_QUOTA_FILE_BYTES_MAX, APP_QUOTA_MESSAGES_PER_MIN_MAX, APP_QUOTAS,
    AppQuotaDetails, AppQuotaUsage, AppQuotas, AppQuotasResponse, QUOTA_CALL_PARTICIPANTS_MAX,
    QUOTA_CHANNELS_PER_SPACE_MAX, QUOTA_FILE_BYTES_MAX, QUOTA_MEMBERS_PER_SPACE_MAX,
    QUOTA_MESSAGES_PER_MIN, QUOTA_SPACES_MAX,
};
pub use backups::{BACKUP_JOB_KIND, BackupJobResult, MgmtBackup, MgmtBackupList};
pub use channels::{
    CHANNEL_BULK_OPS_MAX, CHANNEL_MEMBERS_ADD_MAX, FEATURE_CHANNEL_MEMBER_ADD_REMOVE,
    MgmtChannelBulkRequest, MgmtChannelBulkResponse, MgmtChannelCreateRequest,
    MgmtChannelCreateResponse, MgmtChannelMemberRequest, MgmtChannelMemberResponse,
    MgmtChannelNotAdded, MgmtChannelNotAddedReason, MgmtChannelOp, MgmtChannelOpKind,
    MgmtChannelOpResult, MgmtChannelTypeMismatchDetails, MgmtMemberRef,
};
pub use erase::{
    EraseKeptKind, EraseKeptReason, EraseMode, EraseState, FEATURE_PRINCIPAL_ERASE,
    MGMT_ERASURES_LIMIT_DEFAULT, MGMT_ERASURES_LIMIT_MAX, MgmtEraseAccepted, MgmtEraseCounts,
    MgmtEraseKept, MgmtEraseRequest, MgmtEraseResult, MgmtErasurePage, MgmtErasuresQuery,
    StatusErasures,
};
pub use identity::{
    FEATURE_PRINCIPAL_RELINK, IdentitySwitchRequest, IdentitySwitchResponse,
    PrincipalRelinkRequest, PrincipalRelinkResponse, is_relink_target,
};
pub use meet::{MEET_RETENTION_DAYS_MAX, MeetInitRequest, MeetInitResponse};
pub use member_channels::{
    FEATURE_APP_MEMBER_CHANNELS, FEATURE_MEMBER_CHANNELS_LEVEL_ONLY, MEMBER_CHANNELS_ITEMS_MAX,
    MEMBER_CHANNELS_MAX, MgmtMemberChannelsBulkRequest, MgmtMemberChannelsBulkResponse,
    MgmtMemberChannelsItem, MgmtMemberChannelsItemResult, MgmtMemberChannelsRequest,
    MgmtMemberChannelsResponse, MgmtMemberChannelsSkip,
};
pub use notifications::{
    CursorExpiredDetails, FEATURE_FEED_ALL_MESSAGES, FEATURE_FEED_ATTACHMENT_INFO,
    FEATURE_FEED_HEAD, FEATURE_PERSON_CHANNEL_NOTIFY, FEATURE_PERSON_NOTIFICATIONS,
    PERSON_NOTIFICATION_PREVIEW_MAX_CHARS, PERSON_NOTIFICATIONS_LIMIT_DEFAULT,
    PERSON_NOTIFICATIONS_LIMIT_MAX, PERSON_NOTIFICATIONS_WAIT_MAX_S, PersonNotification,
    PersonNotificationKind, PersonNotificationsHead, PersonNotificationsPage,
    PersonNotificationsQuery, preview_of,
};
pub use notifications::{FEATURE_VOICE_INVITES, FEATURE_VOICE_STARTED_FEED};
pub use ops::{
    AuditVerifyMode, AuditVerifyReport, AuditVerifyRequest, EXPORT_RECIPIENTS_MAX, ExportRequest,
};
pub use recordings::{
    FEATURE_APP_RECORDING, FEATURE_MGMT_RECORDING_READS, FEATURE_RECORDING_CONTROLS,
    FEATURE_RECORDING_REQUIRED, FEATURE_VOICE_RECORD_CAP, MGMT_RECORDINGS_LIMIT_DEFAULT,
    MGMT_RECORDINGS_LIMIT_MAX, MGMT_TRANSCRIPT_LIMIT_DEFAULT, MGMT_TRANSCRIPT_LIMIT_MAX,
    MgmtAppRecording, MgmtAppRecordingRequest, MgmtChannelRecording, MgmtChannelRecordingRequest,
    MgmtRecordingChange, MgmtRecordingChanges, MgmtRecordingChangesQuery, MgmtRecordingParticipant,
    MgmtRecordingQuery, MgmtRecordingStopRequest, MgmtRecordingSummary, MgmtRecordingsPage,
    MgmtRecordingsQuery, MgmtTranscriptPage, MgmtTranscriptQuery, MgmtTranscriptTurn,
    RECORDING_CHANGES_LIMIT_DEFAULT, RECORDING_CHANGES_LIMIT_MAX, RECORDING_CHANGES_WAIT_MAX_S,
    RECORDING_EVENTS_KEEP_DAYS,
};
pub use spaces::{
    ADMIN_SPACE_SLUG, APP_ID_MAX_BYTES, FEATURE_APP_GOVERNED_SPACES, FEATURE_APP_PARTICIPANT_LINKS,
    FEATURE_PRIVATE_SPACES, FEATURE_SERVER_GOVERNED_SPACE, FEATURE_SPACE_UNBIND,
    MGMT_APP_SPACES_LIMIT_DEFAULT, MGMT_APP_SPACES_LIMIT_MAX, MgmtAppSpace, MgmtAppSpaces,
    MgmtAppSpacesQuery, MgmtBoxUsersDetails, MgmtSpaceApp, MgmtSpaceAppLinkRequest,
    MgmtSpaceAppRole, MgmtSpaceApps, MgmtSpaceAppsChange, MgmtSpaceBindRequest,
    MgmtSpaceCreateRequest, MgmtSpaceMemberRef, MgmtSpaceMemberRemoveRequest,
    MgmtSpaceMemberRemoveResponse, MgmtSpaceMembersAddRequest, MgmtSpaceMembersAddResponse,
    MgmtSpaceNotAdded, MgmtSpaceNotAddedReason, MgmtSpaceOrphanResponse, MgmtSpaceReleaseRequest,
    MgmtSpaceResponse, MgmtSpaceSettings, MgmtSpaceSettingsRequest, MgmtSpaceUnbindRequest,
    SPACE_MANAGER_ROLE_NAME, SPACE_MANAGERS_MAX, SPACE_MEMBERS_ADD_MAX, is_app_id,
};
pub use voice::{
    FEATURE_MGMT_VOICE_STATES, FEATURE_VOICE_CHANNEL_CLOSING, FEATURE_VOICE_MEDIA_PATH,
    MEDIA_PATH_REASON_MAX_CHARS, MediaPathState, MgmtChannelClosingRequest,
    MgmtChannelClosingResponse, MgmtMediaPathRequest, MgmtMediaPathResponse, MgmtVoiceChange,
    MgmtVoiceChangeKind, MgmtVoiceChanges, MgmtVoiceChangesQuery, MgmtVoiceCursorExpiredDetails,
    MgmtVoiceState, MgmtVoiceStates, VOICE_CHANGES_LIMIT_DEFAULT, VOICE_CHANGES_LIMIT_MAX,
    VOICE_CHANGES_RING, VOICE_CHANGES_WAIT_MAX_S, VoiceMediaPathStatus,
};

/// Since Sew 0.7.1: the call-scoped credential. K2's backend asks for one
/// (`call_credential: true`) only when this flag is listed.
pub const FEATURE_CALL_CREDENTIAL: &str = "call_credential";
/// Since Sew 0.7.1: the call-stats beacon takes client `native` and the audio fields
/// (`stats.audio_fields` in join answers).
pub const FEATURE_CALL_STATS_NATIVE: &str = "call_stats_native";

// ---- act-as ----

/// The `Sew-Act-As` header: a member id (decimal) or an external id (contains `:`).
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ActAsTarget {
    /// A member id.
    Member(Id),
    /// An external id.
    External(ExternalId),
}

impl FromStr for ActAsTarget {
    type Err = String;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if s.contains(':') {
            return s
                .parse()
                .map(ActAsTarget::External)
                .map_err(|e: crate::external_id::ExternalIdError| e.to_string());
        }
        parse_id_str(s)
            .map(ActAsTarget::Member)
            .ok_or_else(|| format!("invalid Sew-Act-As {s:?}: expected a member id or external id"))
    }
}

impl fmt::Display for ActAsTarget {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ActAsTarget::Member(id) => fmt::Display::fmt(id, f),
            ActAsTarget::External(e) => fmt::Display::fmt(e, f),
        }
    }
}

/// Whether `s` is 1–128 chars of `[A-Za-z0-9_.-]` (a K2 session id).
pub fn is_k2_session_id(s: &str) -> bool {
    (1..=K2_SESSION_ID_MAX_LEN).contains(&s.len())
        && s.bytes()
            .all(|c| c.is_ascii_alphanumeric() || c == b'_' || c == b'.' || c == b'-')
}

/// The `Sew-Via` header, exactly `k2; agent=<uuid>; k2_session=<id>`. Only
/// `system = k2` exists in v1.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct SewVia {
    /// The agent's K2 workspace uuid.
    pub agent: String,
    /// The K2 session id, 1–128 chars of `[A-Za-z0-9_.-]`.
    pub k2_session: String,
}

impl FromStr for SewVia {
    type Err = String;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let err = || format!("invalid Sew-Via {s:?}: expected k2; agent=<uuid>; k2_session=<id>");
        let rest = s.strip_prefix("k2; agent=").ok_or_else(err)?;
        let (agent, session) = rest.split_once("; k2_session=").ok_or_else(err)?;
        if !is_lower_uuid(agent) || !is_k2_session_id(session) {
            return Err(err());
        }
        Ok(SewVia {
            agent: agent.to_owned(),
            k2_session: session.to_owned(),
        })
    }
}

impl fmt::Display for SewVia {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "k2; agent={}; k2_session={}",
            self.agent, self.k2_session
        )
    }
}

// ---- health and status ----

/// Response of `GET /mgmt/v1/health`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct Health {
    /// Whether the process is live.
    pub ok: bool,
}

unit_enum! {
    /// The install's identity provider.
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
    pub enum ProviderKind {
        /// K2 grants create accounts; K2 signs the pass.
        K2 = "k2",
        /// Local users and passwords.
        Standalone = "standalone",
        /// A meeting-link session (session provider 3, made by redeeming a
        /// meeting link). Never an install's provider.
        Link = "link",
    }
}

/// Database status.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[non_exhaustive]
pub struct StatusDb {
    /// Whether the database answers (`SELECT 1` within 1 s).
    pub ok: bool,
    /// Replica lag; absent in v1 (single node).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub lag_ms: Option<u64>,
    /// Round trip of the `SELECT 1` probe; absent when it failed (additive).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub ping_ms: Option<u64>,
}

/// Job queue status.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[non_exhaustive]
pub struct StatusJobs {
    /// Jobs waiting (not under a live lease).
    pub queued: u64,
    /// Jobs that exhausted their attempts.
    pub dead: u64,
    /// Jobs under a live lease (additive).
    #[serde(default)]
    pub running: u64,
    /// `now − min(run_at)` over due, unleased jobs; absent if none (additive).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub oldest_due_age_ms: Option<u64>,
}

unit_enum! {
    /// A status alarm. K2 shows each one verbatim.
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
    pub enum StatusAlarm {
        /// `db.ok = false`.
        DbDown = "db_down",
        /// `jobs.dead > 0`.
        JobsDead = "jobs_dead",
        /// `keys.provider_ok = false`.
        KeysProviderDown = "keys_provider_down",
        /// `keys.current_month_ready = false`.
        KeysCurrentMonthMissing = "keys_current_month_missing",
        /// The keyring mirror is stale (or kind `none`).
        KeysMirrorStale = "keys_mirror_stale",
        /// A crypto-shred started but did not finish.
        KeysShredPending = "keys_shred_pending",
        /// Old key files for shredded key ids were found and refused.
        KeysResurrected = "keys_resurrected",
        /// Keys wait for a re-wrap but no retiring KEK is loaded.
        KeysRewrapStalled = "keys_rewrap_stalled",
        /// Search indexing trails writes by more than 60 s.
        SearchLagging = "search_lagging",
        /// The search index is missing and being rebuilt; searches answer 503.
        SearchUnavailable = "search_unavailable",
        /// Three search index commits in a row failed.
        SearchCommitFailing = "search_commit_failing",
        /// Free disk under the search index is below its size plus 1 GiB.
        SearchDiskLow = "search_disk_low",
        /// The last good backup is older than 36 h (set by the backup engine).
        BackupStale = "backup_stale",
        /// The last backup failed (set by the backup engine).
        BackupFailed = "backup_failed",
        /// The last restore drill failed (set by the backup engine).
        DrillFailed = "drill_failed",
        /// The last good restore drill is older than 8 days (set by the backup engine).
        DrillStale = "drill_stale",
        /// GIF search is on but its provider key does not resolve or was refused.
        GifsUnconfigured = "gifs_unconfigured",
        /// A finite retention policy is set and no run finished `done` for 48 h.
        RetentionOverdue = "retention_overdue",
        /// Purged messages' search documents are still waiting after 24 h.
        RetentionSearchBacklog = "retention_search_backlog",
        /// Three or more mail sends in a row failed over at least 10 minutes.
        MailFailing = "mail_failing",
        /// `digest.enabled` is on but `[mail.smtp]` is not configured.
        DigestMailUnconfigured = "digest_mail_unconfigured",
        /// A running export wrote no progress for 10 minutes.
        ExportStuck = "export_stuck",
        /// A running account erasure made no progress for 10 minutes (its job is lost, overdue
        /// or has failed for that long; a wait for an active recording is not stuck).
        EraseStuck = "erase_stuck",
        /// Voice is enabled and the media engine has been unreachable for more than 60 s.
        VoiceMediaDown = "voice_media_down",
        /// The snapshot audit found voice state Sew did not expect.
        VoiceDrift = "voice_drift",
        /// The media engine reported a panic in the last hour.
        MediaPanic = "media_panic",
        /// More than 3 call key group resets install-wide in the last hour, or a call in reset
        /// back-off.
        E2eeGroupResets = "e2ee_group_resets",
        /// A key removal was not activated by its hard bound in the last hour.
        E2eeRemovalOverdue = "e2ee_removal_overdue",
        /// A key group rebuild ended in a new group or a reset in the last hour.
        E2eeRebuildFailed = "e2ee_rebuild_failed",
        /// A client reported a roster mismatch in the last hour.
        E2eeRosterMismatch = "e2ee_roster_mismatch",
        /// A stale-slot resync or a `peer_undecryptable` quorum in the last hour.
        E2eeSenderStale = "e2ee_sender_stale",
        /// A client's bridge reported a misbehaving engine in the last hour.
        E2eeEngineAnomaly = "e2ee_engine_anomaly",
        /// Voice is enabled and the call DS or CA key failed to load.
        E2eeKeysUnavailable = "e2ee_keys_unavailable",
        /// The media budget tier has been `critical` for more than 5 min;
        /// clears when the tier drops.
        MediaBudgetCritical = "media_budget_critical",
        /// The recorder failed health for 60 s.
        RecorderDown = "recorder_down",
        /// The transcriber failed health for 60 s.
        TranscriberDown = "transcriber_down",
        /// The oldest untranscribed segment is older than 30 min.
        TranscribeBacklog = "transcribe_backlog",
        /// A recording stopped on `storage_full` in the last 24 h.
        RecordingStorageFull = "recording_storage_full",
        /// Three auto-starts failed in 10 min for one channel.
        RecordingAutoFailed = "recording_auto_failed",
        /// A segment of an ungranted identity reached `sew` in the last 24 h.
        RecordingConsentViolation = "recording_consent_violation",
        /// A room that must record every call with two or more people could not: three
        /// required starts failed in 10 min for one channel, or recording is required where it
        /// cannot run (the recorder is not installed, `recording.enabled` is off, or the
        /// governing App has `uploads: false`) in the last hour.
        RecordingRequiredFailed = "recording_required_failed",
        /// The `dir` keyring mirror path is not mounted: it is missing, or a plain directory
        /// that holds no mirror of this install. Nothing is written there; key writes wait.
        KeysMirrorUnmounted = "keys_mirror_unmounted",
        /// The `dir` keyring mirror path is on the keyring's own filesystem. Nothing is
        /// written there; key writes wait.
        KeysMirrorSameDevice = "keys_mirror_same_device",
        /// No full keyring mirror reconcile has succeeded since the server started (it runs
        /// one at start), so the mirror is not known to hold every key; `keys.mirror_ok` is
        /// `false` meanwhile.
        KeysMirrorUnsynced = "keys_mirror_unsynced",
    }
}

/// Identity provider state in `status`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[non_exhaustive]
pub struct StatusIdentity {
    /// The provider.
    pub provider: ProviderKind,
    /// `identity.k2.jwks_url` (K2 only).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub jwks_url: Option<String>,
    /// Whether the JWKS cache holds keys (K2 only).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub jwks_ok: Option<bool>,
    /// The install's stored identity mode. It differs from `provider` only between
    /// `POST /mgmt/v1/identity/switch` and the restart that follows (additive).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub mode: Option<ProviderKind>,
    /// When the install switched from standalone to K2 (absent when it never did).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub switched_at: Option<Timestamp>,
    /// Active members still on a `local:` id. On a switched install they keep password
    /// sign-in until K2 relinks or deactivates them (the transition state); absent on an
    /// install that never switched.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub unlinked_local_members: Option<u64>,
    /// The mode was switched and this process still serves the old one; restart Sew.
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub restart_required: bool,
}

impl StatusIdentity {
    /// A `StatusIdentity` with its required fields: every flag `false` and every optional
    /// field unset.
    pub fn new(provider: ProviderKind) -> Self {
        StatusIdentity {
            provider,
            jwks_url: None,
            jwks_ok: None,
            mode: None,
            switched_at: None,
            unlinked_local_members: None,
            restart_required: false,
        }
    }
}

/// Backup state in `status`. The backup engine fills it; absent until then.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[non_exhaustive]
pub struct StatusBackup {
    /// The last good backup.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub last_ok_at: Option<Timestamp>,
    /// The last good backup's bundle id.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub last_id: Option<String>,
    /// Age of the last good backup.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub age_ms: Option<u64>,
    /// Why the last run failed, if it did (cleared by the next good one).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub last_error: Option<String>,
    /// The last good backup's kind (`scheduled`, `manual`, …; additive).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub last_kind: Option<String>,
    /// When the last failed run failed (additive).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub last_error_at: Option<Timestamp>,
}

/// Restore-drill state in `status`. The backup engine fills it; absent until then.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[non_exhaustive]
pub struct StatusDrill {
    /// The last good drill.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub last_ok_at: Option<Timestamp>,
    /// Whether the last drill passed.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub last_ok: Option<bool>,
    /// Why the last drill failed, if it did.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub last_error: Option<String>,
    /// Age of the last good drill (additive).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub age_ms: Option<u64>,
}

/// Search index status.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[non_exhaustive]
pub struct StatusSearch {
    /// How far indexing trails writes: `now − min(created_at)` over pending or running
    /// `search_index` / `search_delete` jobs; 0 when none.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub lag_ms: Option<u64>,
    /// `ready` or `missing` (rebuilding; searches answer 503).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub state: Option<SearchState>,
    /// Live documents in the index.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub docs: Option<StatusSearchDocs>,
    /// Bytes of the live index on disk.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub index_bytes: Option<u64>,
    /// The live index generation (`gen-<n>`).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub generation: Option<u64>,
    /// When the indexer last committed.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub last_commit_at: Option<Timestamp>,
    /// Present only while a rebuild runs.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub rebuild: Option<StatusSearchRebuild>,
}

unit_enum! {
    /// Search index state.
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
    pub enum SearchState {
        /// Queries use the live index.
        Ready = "ready",
        /// No usable index; a rebuild runs and searches answer `503 search_rebuilding`.
        Missing = "missing",
    }
}

/// Live search documents by type.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[non_exhaustive]
pub struct StatusSearchDocs {
    /// Message documents.
    pub messages: u64,
    /// File-name documents.
    pub files: u64,
}

/// A running search rebuild.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[non_exhaustive]
pub struct StatusSearchRebuild {
    /// Why: `marker`, `missing`, `corrupt`, `meta_mismatch` or `admin`.
    pub reason: String,
    /// The generation being built.
    pub generation: u64,
    /// 0–99 until the swap.
    pub progress_pct: u8,
    /// Documents in the building index so far.
    pub docs: u64,
    /// When it started.
    pub started_at: Timestamp,
}

impl StatusSearchRebuild {
    /// A `StatusSearchRebuild` with its required fields: every count 0.
    pub fn new(reason: impl Into<String>, started_at: Timestamp) -> Self {
        StatusSearchRebuild {
            reason: reason.into(),
            generation: 0,
            progress_pct: 0,
            docs: 0,
            started_at,
        }
    }
}

/// Body of `POST /mgmt/v1/search/rebuild`.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct SearchRebuildRequest {
    /// Free text for the operator, at most 200 bytes (never audited as content).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub reason: Option<String>,
}

impl Validate for SearchRebuildRequest {
    fn validate(&self) -> Result<(), ValidationError> {
        if let Some(r) = &self.reason
            && r.len() > crate::limits::SEARCH_REBUILD_REASON_MAX_BYTES
        {
            return Err(ValidationError::invalid(
                "reason",
                format!(
                    "reason must be at most {} bytes",
                    crate::limits::SEARCH_REBUILD_REASON_MAX_BYTES
                ),
            ));
        }
        Ok(())
    }
}

/// Response of `POST /mgmt/v1/search/rebuild`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct SearchRebuildResponse {
    /// The `search_rebuild` job (poll `GET /mgmt/v1/jobs/{id}`).
    pub job_id: Id,
}

/// Key provider and keyring status.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[non_exhaustive]
pub struct StatusKeys {
    /// Whether the master key provider works.
    pub provider_ok: bool,
    /// Whether this month's keys exist.
    pub current_month_ready: bool,
    /// Whether the off-box keyring mirror is current.
    pub mirror_ok: bool,
    /// Age of the last good mirror write; absent if never ok.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub mirror_age_ms: Option<u64>,
    /// The key provider kind: `systemd-credential`, `file` (additive).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub provider: Option<String>,
    /// Current KEK id, 16 hex.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub kek_id: Option<String>,
    /// Retiring KEK ids, 16 hex each.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub retiring_kek_ids: Vec<String>,
    /// Keyring entries still wrapped under a retiring KEK.
    #[serde(default)]
    pub rewrap_pending: u64,
    /// Whether next month's install key exists.
    #[serde(default)]
    pub next_month_prepared: bool,
    /// Keys created lazily at write time this month.
    #[serde(default)]
    pub lazy_creates_this_month: u64,
    /// Shreds started but not finished.
    #[serde(default)]
    pub shred_pending: u64,
    /// Old key files for shredded kids, refused at open.
    #[serde(default)]
    pub resurrected_keys: u64,
    /// The keyring mirror block; absent from servers that predate it.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub mirror: Option<StatusKeysMirror>,
}

/// `status.keys.mirror`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[non_exhaustive]
pub struct StatusKeysMirror {
    /// Mirror kind: `dir`, `none` (`k2` reserved).
    pub kind: String,
    /// Alarm: a pending write is older than the stale threshold, or kind `none`.
    pub stale: bool,
    /// Names waiting to be pushed.
    pub pending: u64,
    /// Age of the oldest pending name, seconds.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub oldest_pending_age_s: Option<u64>,
    /// Last successful mirror operation.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub last_ok_at: Option<Timestamp>,
    /// Mirror entries that are neither local nor on the ledger.
    pub unknown_keys: u64,
}

impl StatusKeysMirror {
    /// A `StatusKeysMirror` with its required fields: every count 0, every flag `false` and
    /// every optional field unset.
    pub fn new(kind: impl Into<String>) -> Self {
        StatusKeysMirror {
            kind: kind.into(),
            stale: false,
            pending: 0,
            oldest_pending_age_s: None,
            last_ok_at: None,
            unknown_keys: 0,
        }
    }
}

/// Response of `GET /mgmt/v1/status`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[non_exhaustive]
pub struct Status {
    /// The Sew version.
    pub version: String,
    /// The applied schema version.
    pub schema_version: u64,
    /// The install id.
    pub install_id: Id,
    /// The identity provider.
    pub provider: ProviderKind,
    /// Database status.
    pub db: StatusDb,
    /// Job queue status.
    pub jobs: StatusJobs,
    /// Search status.
    pub search: StatusSearch,
    /// Key status.
    pub keys: StatusKeys,
    /// Live login sessions.
    pub sessions: u64,
    /// Open gateway sockets.
    pub sockets: u64,
    /// When this process started (additive).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub started_at: Option<Timestamp>,
    /// Time since this process started (additive).
    #[serde(default)]
    pub uptime_ms: u64,
    /// Every active alarm, sorted (additive). K2 shows them verbatim.
    #[serde(default)]
    pub alarms: Vec<StatusAlarm>,
    /// The identity provider's state (additive).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub identity: Option<StatusIdentity>,
    /// The client listener as `http://<bound address>` (additive).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub client_listen: Option<String>,
    /// Backups (the backup engine fills it).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub backup: Option<StatusBackup>,
    /// Restore drills (the backup engine fills it).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub drill: Option<StatusDrill>,
    /// Files (additive).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub files: Option<StatusFiles>,
    /// Retention runs, pending policies, holds and shred candidates (additive).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub retention: Option<crate::api::compliance::StatusRetention>,
    /// Outgoing mail (additive).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub mail: Option<StatusMail>,
    /// The email digest (additive).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub digest: Option<StatusDigest>,
    /// Exports running, queued and on disk (additive).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub exports: Option<crate::api::exports::StatusExports>,
    /// Account erasures running and waiting (additive).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub erasures: Option<StatusErasures>,
    /// Voice, the media engine and call keys (additive). Absent while
    /// voice was never configured.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub voice: Option<crate::voice::VoiceStatus>,
    /// Recording and transcription (additive). Absent while recording is off.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub recording: Option<crate::recording::StatusRecording>,
    /// The features this build has, sorted ([`FEATURES`]; additive). K2 tests a feature, not a
    /// version. A flag is never removed or renamed once shipped.
    #[serde(default)]
    pub features: Vec<String>,
    /// The gateway's counters since this process started (0.7.1, additive): IDENTIFY and RESUME
    /// outcomes and server-sent closes by code. Absent from servers that predate it.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub gateway: Option<StatusGateway>,
}

impl Status {
    /// A status with its required parts: no sessions or sockets, empty search and keys blocks,
    /// no alarms or features, every optional block unset.
    pub fn new(
        version: String,
        schema_version: u64,
        install_id: Id,
        provider: ProviderKind,
        db: StatusDb,
        jobs: StatusJobs,
    ) -> Status {
        Status {
            version,
            schema_version,
            install_id,
            provider,
            db,
            jobs,
            search: StatusSearch::default(),
            keys: StatusKeys::default(),
            sessions: 0,
            sockets: 0,
            started_at: None,
            uptime_ms: 0,
            alarms: Vec::new(),
            identity: None,
            client_listen: None,
            backup: None,
            drill: None,
            files: None,
            retention: None,
            mail: None,
            digest: None,
            exports: None,
            erasures: None,
            voice: None,
            recording: None,
            features: Vec::new(),
            gateway: None,
        }
    }
}

/// `status.gateway` (0.7.1): the gateway's counters since this process started, the same
/// numbers as the `sew_gateway_*` metric families.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[non_exhaustive]
pub struct StatusGateway {
    /// Sockets with READY or RESUMED queued.
    pub sockets_live: u64,
    /// Upgraded sockets not live yet (HELLO sent, no READY).
    pub sockets_pending: u64,
    /// IDENTIFY outcomes.
    pub identify: StatusGatewayIdentify,
    /// RESUME outcomes.
    pub resume: StatusGatewayResume,
    /// REAUTH outcomes (0.7.2, additive; zeros when an older server left it out).
    #[serde(default)]
    pub reauth: StatusGatewayReauth,
    /// Server-sent closes by code (`"4000"`..`"4014"`, and `"stall"` for a writer that gave
    /// up); every code is listed, zeros included. Clients must accept a key they do not know.
    pub closes: std::collections::BTreeMap<String, u64>,
}

impl StatusGateway {
    /// A `StatusGateway` with its required fields: every count 0, every list empty and the
    /// other fields at their serde defaults.
    pub fn new(identify: StatusGatewayIdentify, resume: StatusGatewayResume) -> Self {
        StatusGateway {
            sockets_live: 0,
            sockets_pending: 0,
            identify,
            resume,
            reauth: Default::default(),
            closes: std::collections::BTreeMap::new(),
        }
    }
}

/// `status.gateway.identify`: IDENTIFY outcomes since start.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[non_exhaustive]
pub struct StatusGatewayIdentify {
    /// READY sent.
    pub ok: u64,
    /// Closed `4008` (the install or the per-login limit).
    pub rate_limited: u64,
    /// Closed `4004`.
    pub auth_failed: u64,
    /// Closed `4003 identify_timeout`: no IDENTIFY in time, or its ticket or token was not
    /// redeemed in time; or the READY build timed out (`4000`).
    pub timeout: u64,
    /// Closed `4000` for any other reason.
    pub error: u64,
}

/// `status.gateway.resume`: RESUME outcomes since start.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[non_exhaustive]
pub struct StatusGatewayResume {
    /// RESUMED sent.
    pub resumed: u64,
    /// INVALID_SESSION sent.
    pub invalid: u64,
    /// Closed `4007`.
    pub bad_seq: u64,
    /// Closed `4008`.
    pub rate_limited: u64,
    /// Closed `4004`.
    pub auth_failed: u64,
    /// Closed `4003 identify_timeout`: RESUME's ticket or token was not redeemed in time.
    pub timeout: u64,
}

/// `status.gateway.reauth` (0.7.2): REAUTH outcomes since start.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[non_exhaustive]
pub struct StatusGatewayReauth {
    /// The socket's login was swapped.
    pub ok: u64,
    /// Closed `4004`: the token was refused, or names another member or credential.
    pub auth_failed: u64,
    /// Closed `4003 identify_timeout`: REAUTH's token was not redeemed within
    /// `identify_timeout_ms` of the REAUTH (the session left resumable).
    pub timeout: u64,
}

/// Every feature flag of this build, sorted: `features` in `GET /mgmt/v1/status` and
/// `sew version --json`. A flag is never removed or renamed once shipped.
pub const FEATURES: &[&str] = &[
    // Since 0.10.0: the policy `agents.dm_with_box_people`.
    crate::policy::FEATURE_AGENTS_DM_WITH_BOX_PEOPLE,
    // `POST /mgmt/v1/channels/create`.
    "app_channel_create",
    "app_controls", // channels/bulk, sessions/revoke app_id, uploads off.
    // Since 0.6.0: App-governed Spaces (`app:` ids, the governance switch, the links, the App
    // levels and uploads settings).
    FEATURE_APP_GOVERNED_SPACES,
    FEATURE_APP_MEMBER_CHANNELS, // The member-channels sync.
    "app_mode_gate", // App mode's `governed_by_app` gate, `role_managed` and masked perms.
    // `origin` in the App settings is the join answers' signalling URL.
    FEATURE_APP_ORIGIN,
    // Since 0.7.2: `origins` in the App settings; a voice or call join may name its `origin`.
    FEATURE_APP_ORIGINS,
    // Since 0.8.0: participant links (`spaces/{space_id}/apps/link`, `participant_app_ids`).
    FEATURE_APP_PARTICIPANT_LINKS,
    // `GET`/`POST /mgmt/v1/apps/{app_id}/quotas` and their enforcement.
    "app_quotas",
    // Since 0.9.0: App, Space and channel recording settings (`recording` in the App settings,
    // the Space overrides, `channels/{channel_id}/recording`), `start_level`, the mgmt stop.
    FEATURE_APP_RECORDING,
    // The pass's `app` claim scopes the session.
    FEATURE_APP_SCOPED_SESSIONS,
    "app_session_member_role", // An App session acts with install role at most `member`.
    // `type: voice` and `user_limit` on `POST /mgmt/v1/channels/create`.
    "app_voice_channel_create",
    // A DM call's starter cancels it before anyone else joined (`call/cancel`, `cancelled`).
    crate::calls::FEATURE_CALL_CANCEL,
    // Since 0.7.1: the call-scoped credential (`call_credential: true` on voice and DM
    // call joins, `sewcc_`, the call socket, `POST /api/v1/me/voice/credential`).
    FEATURE_CALL_CREDENTIAL,
    // Since 0.7.1: the beacon's client `native` and audio fields (`stats.audio_fields`).
    FEATURE_CALL_STATS_NATIVE,
    // Since 0.10.0: one channel member in or out over mgmt
    // (`channels/{channel_id}/members/add`, `…/remove`).
    FEATURE_CHANNEL_MEMBER_ADD_REMOVE,
    // Since 0.10.0: `Channel.members_can_leave` (mgmt create, the bulk `ensure` and `update`)
    // and the leave it allows in a governed Space.
    crate::api::channels::FEATURE_CHANNEL_MEMBERS_CAN_LEAVE,
    // Since 0.9.2: the DMs by latest activity (`GET /api/v1/me/channels?order=activity`).
    crate::api::channels::FEATURE_DM_ORDER_ACTIVITY,
    // Read receipts in DMs: the seen positions, `share_read_receipts`, the policy
    // `dm.read_receipts` and their events.
    crate::api::read_receipts::FEATURE_DM_READ_RECEIPTS,
    // The person feed's `message`, `keyword`, `read` and `call_ring_stopped` kinds, inline
    // replies, `from_external_id`, `thread_root_id` and the page's `head`.
    FEATURE_FEED_ALL_MESSAGES,
    // Since 0.11.5: `attachment_count` and `attachment_kinds` on the person feed's message
    // items.
    FEATURE_FEED_ATTACHMENT_INFO,
    // `GET /mgmt/v1/people/{external_id}/notifications/head` (the person feed's start).
    FEATURE_FEED_HEAD,
    // Since 0.9.2: group DM names (the update's `name` on a group DM, the rename notice).
    crate::api::channels::FEATURE_GROUP_DM_NAMES,
    // `/mgmt/v1/home-client` and `hidden_from_home`.
    FEATURE_HOME_CLIENT,
    "home_client_owner_approval", // `POST /mgmt/v1/home-client` needs the owner's approval.
    // Since 0.10.0: `POST /api/v1/channels/{id}/typing` (typing over HTTP, act-as included).
    crate::api::channels::FEATURE_HTTP_TYPING,
    // `sew import --to-standalone [--identity-map]` (moving off K2).
    "import_identity_map",
    // Policy `identity.managed_fields` (an avatar K2 owns: client uploads refused).
    "k2_managed_avatar",
    "k2_managed_guard", // `--i-am-k2` and `sew install --managed-by` (the K2-managed guard).
    // Since 0.10.0: the member-channels sync without `channel_ids` changes only the level.
    FEATURE_MEMBER_CHANNELS_LEVEL_ONLY,
    // `external_id` on the client `Member` (K2 installs, shared Spaces and DMs).
    crate::api::members::FEATURE_MEMBER_EXTERNAL_ID,
    // Since 0.11.5: `GET /api/v1/messages/{id}` (one message, under the history read rule).
    crate::api::messages::FEATURE_MESSAGE_GET,
    // Since 0.9.0: the mgmt recording reads, the transcript route and an App's recording feed
    // (`recordings/{id}`, `spaces/{space_id}/recordings`, `apps/{app_id}/recordings/changes`).
    FEATURE_MGMT_RECORDING_READS,
    // `voice/states` and the `voice/changes` feed over mgmt.
    FEATURE_MGMT_VOICE_STATES,
    // `GET`/`POST /mgmt/v1/people/{external_id}/channels/{id}/notify[/set]`: a person's level
    // and mute per channel or DM over mgmt.
    FEATURE_PERSON_CHANNEL_NOTIFY,
    // `GET /mgmt/v1/people/{external_id}/notifications`.
    FEATURE_PERSON_NOTIFICATIONS,
    // `principals/erase`, its status and list routes, and the `erased:` tombstone id.
    FEATURE_PRINCIPAL_ERASE,
    // `principals/relink`, `identity/switch` and the transition state.
    FEATURE_PRINCIPAL_RELINK,
    // Private Spaces (`Space.private`, `/mgmt/v1/spaces/{space_id}/settings`).
    FEATURE_PRIVATE_SPACES,
    // Since 0.10.0: `ReadState.unread_count` (unread timeline messages, capped at 99).
    crate::api::read_state::FEATURE_READ_STATE_UNREAD_COUNT,
    // Each person's Record and Stop, computed by the server (`recording_controls_updated`,
    // `Recording.my_controls`, `CallReady.recording_controls`).
    FEATURE_RECORDING_CONTROLS,
    // Since 0.9.0: rooms that record every call with more than one person (policy
    // `recording.required`, the App, Space and channel `required`, `Channel.recording_rule`).
    FEATURE_RECORDING_REQUIRED,
    // The server-governed Space (governance `server`, the `k2:server` owner).
    FEATURE_SERVER_GOVERNED_SPACE,
    // `spaces/unbind`, `spaces/release` and read-only orphan Spaces.
    FEATURE_SPACE_UNBIND,
    // Relay-only through the edge (`voice.ops.ice_relay_only`,
    // `MediaPolicy.relay_only`, no node IP in tunnel mode).
    "turn_relay_only",
    // Closing a voice room (`channels/{channel_id}/closing`, `Channel.closing`).
    FEATURE_VOICE_CHANNEL_CLOSING,
    // Voice invites (`channels/{id}/voice/invite`, decline, cancel, `me/voice/invites`, the
    // `voice_invite` event, `ReadyChannel.voice_invites`, the feed's `voice_invite` and
    // `voice_invite_stopped`).
    FEATURE_VOICE_INVITES,
    // `POST /mgmt/v1/voice/media-path`, `status.voice.media_path`.
    FEATURE_VOICE_MEDIA_PATH,
    // Since 0.9.0: the pass cap `voice:record` (a ceiling on recording start and stop where the
    // session's App sets `recording.cap_required`).
    FEATURE_VOICE_RECORD_CAP,
    // A voice room's start on the person feed (`voice_started`, `voice_ended`) and the
    // `voice_started` event.
    FEATURE_VOICE_STARTED_FEED,
    // The pass cap `voice:video` (camera and screen leases).
    "voice_video_cap",
];

/// [`FEATURES`] as owned strings.
pub fn features() -> Vec<String> {
    FEATURES.iter().map(|f| (*f).to_owned()).collect()
}

unit_enum! {
    /// How the SMTP connection is secured (`[mail.smtp] tls`).
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
    pub enum MailTls {
        /// Plain SMTP; loopback relays only.
        None = "none",
        /// STARTTLS, required.
        Starttls = "starttls",
        /// Implicit TLS.
        Tls = "tls",
    }
}

/// Outgoing mail in `status`.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[non_exhaustive]
pub struct StatusMail {
    /// `[mail.smtp]` is set.
    pub configured: bool,
    /// The connection security (absent when not configured).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub tls: Option<MailTls>,
    /// The last accepted send.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub last_ok_at: Option<Timestamp>,
    /// The last failure (at most 200 characters, never an address).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub last_error: Option<String>,
    /// When the last failure happened.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub last_error_at: Option<Timestamp>,
    /// Sends accepted in the last 24 h.
    pub sent_24h: u64,
    /// Sends that failed in the last 24 h.
    pub failed_24h: u64,
}

/// The email digest in `status`.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[non_exhaustive]
pub struct StatusDigest {
    /// Org policy `digest.enabled`.
    pub enabled: bool,
    /// Queued `email_digest` jobs.
    pub pending_jobs: u64,
}

/// Body of `POST /mgmt/v1/mail/test`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct MailTestRequest {
    /// The recipient (never audited).
    pub to: String,
}

impl Validate for MailTestRequest {
    fn validate(&self) -> Result<(), ValidationError> {
        check_email("to", &self.to)
    }
}

/// Response of `POST /mgmt/v1/mail/test`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct MailTestResponse {
    /// The relay accepted the mail.
    pub ok: bool,
    /// The relay's reply code, when there was one.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub smtp_code: Option<u16>,
    /// Why it failed (at most 200 characters, never the address).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub error: Option<String>,
}

/// File state in `status`.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[non_exhaustive]
pub struct StatusFiles {
    /// Whether `ffmpeg` answered `-version` at boot (video posters need it).
    pub ffmpeg_available: bool,
}

// ---- provisioning ----

fn check_email(field: &str, email: &str) -> Result<(), ValidationError> {
    if email.is_empty() || email.len() > EMAIL_MAX_BYTES || !email.contains('@') {
        return Err(ValidationError::invalid(
            field,
            format!("{field} must be an address of at most {EMAIL_MAX_BYTES} bytes"),
        ));
    }
    Ok(())
}

/// A profile at provisioning.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct ProfileInput {
    /// Display name, 1–100 characters.
    pub display_name: String,
    /// Handle.
    pub username: Handle,
    /// Email address.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub email: Option<String>,
    /// Title, at most 100 characters.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,
    /// Pronouns, at most 40 characters.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub pronouns: Option<String>,
    /// Provider custom fields: declarative, unlisted provider fields are cleared.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub custom_fields: Vec<crate::api::people::MgmtCustomFieldInput>,
}

impl Validate for ProfileInput {
    fn validate(&self) -> Result<(), ValidationError> {
        check_chars(
            "display_name",
            &self.display_name,
            1,
            DISPLAY_NAME_MAX_CHARS,
        )?;
        if let Some(e) = &self.email {
            check_email("email", e)?;
        }
        check_opt_chars("title", self.title.as_deref(), 0, TITLE_MAX_CHARS)?;
        check_opt_chars("pronouns", self.pronouns.as_deref(), 0, PRONOUNS_MAX_CHARS)
    }
}

/// Body of `POST /mgmt/v1/principals/provision`. Idempotent by `external_id`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct ProvisionRequest {
    /// Who the member is outside Sew.
    pub external_id: ExternalId,
    /// Person or agent.
    pub kind: MemberKind,
    /// Required iff `kind = agent`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub agent_kind: Option<AgentKind>,
    /// The assistant's person; required iff `agent_kind = assistant`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub assistant_of: Option<ExternalId>,
    /// Provision as a guest; only for persons.
    #[serde(default)]
    pub guest: bool,
    /// The profile.
    pub profile: ProfileInput,
}

impl Validate for ProvisionRequest {
    fn validate(&self) -> Result<(), ValidationError> {
        check_known("kind", self.kind)?;
        if let Some(k) = self.agent_kind {
            check_known("agent_kind", k)?;
        }
        match self.kind {
            MemberKind::Agent => {
                let Some(agent_kind) = self.agent_kind else {
                    return Err(ValidationError::invalid(
                        "agent_kind",
                        "agent_kind is required for agents",
                    ));
                };
                if !self.external_id.is_agent() {
                    return Err(ValidationError::invalid(
                        "external_id",
                        "an agent's external_id must be k2:workspace:<uuid>",
                    ));
                }
                if self.guest {
                    return Err(ValidationError::invalid("guest", "agents cannot be guests"));
                }
                match (agent_kind, &self.assistant_of) {
                    (AgentKind::Assistant, None) => {
                        return Err(ValidationError::invalid(
                            "assistant_of",
                            "assistant_of is required for assistants",
                        ));
                    }
                    (AgentKind::Coworker, Some(_)) => {
                        return Err(ValidationError::invalid(
                            "assistant_of",
                            "assistant_of is only for assistants",
                        ));
                    }
                    _ => {}
                }
                if let Some(person) = &self.assistant_of
                    && person.is_agent()
                {
                    return Err(ValidationError::invalid(
                        "assistant_of",
                        "an assistant belongs to a person",
                    ));
                }
            }
            _ => {
                if self.agent_kind.is_some() {
                    return Err(ValidationError::invalid(
                        "agent_kind",
                        "agent_kind is only for agents",
                    ));
                }
                if self.assistant_of.is_some() {
                    return Err(ValidationError::invalid(
                        "assistant_of",
                        "assistant_of is only for assistants",
                    ));
                }
                if self.external_id.is_agent() {
                    return Err(ValidationError::invalid(
                        "external_id",
                        "a person's external_id must not be k2:workspace:*",
                    ));
                }
            }
        }
        self.profile.validate().map_err(|e| e.nested("profile"))
    }
}

/// Response of `POST /mgmt/v1/principals/provision`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct ProvisionResponse {
    /// The member id.
    pub member_id: Id,
    /// Whether this call created the member.
    pub created: bool,
    /// Whether this call reactivated a deactivated member (additive).
    #[serde(default)]
    pub reactivated: bool,
    /// Whether an existing member's profile, guest flag, agent kind or pin changed
    /// (additive).
    #[serde(default)]
    pub changed: bool,
}

/// A partial profile update from K2.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct ProfilePatch {
    /// A new display name, 1–100 characters.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub display_name: Option<String>,
    /// A new handle.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub username: Option<Handle>,
    /// Set or clear the email.
    #[serde(default, skip_serializing_if = "Patch::is_keep")]
    pub email: Patch<String>,
    /// Set or clear the title (≤ 100).
    #[serde(default, skip_serializing_if = "Patch::is_keep")]
    pub title: Patch<String>,
    /// Set or clear the pronouns (≤ 40).
    #[serde(default, skip_serializing_if = "Patch::is_keep")]
    pub pronouns: Patch<String>,
    /// Provider custom fields to set or clear; unlisted keys are kept.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub custom_fields: Vec<crate::api::people::MgmtCustomFieldInput>,
}

impl Validate for ProfilePatch {
    fn validate(&self) -> Result<(), ValidationError> {
        check_opt_chars(
            "display_name",
            self.display_name.as_deref(),
            1,
            DISPLAY_NAME_MAX_CHARS,
        )?;
        if let Some(e) = self.email.as_set() {
            check_email("email", e)?;
        }
        check_opt_chars(
            "title",
            self.title.as_set().map(String::as_str),
            0,
            TITLE_MAX_CHARS,
        )?;
        check_opt_chars(
            "pronouns",
            self.pronouns.as_set().map(String::as_str),
            0,
            PRONOUNS_MAX_CHARS,
        )
    }
}

/// Body of `POST /mgmt/v1/principals/update`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct UpdatePrincipalRequest {
    /// Who to update.
    pub external_id: ExternalId,
    /// What to change.
    pub profile: ProfilePatch,
}

impl Validate for UpdatePrincipalRequest {
    fn validate(&self) -> Result<(), ValidationError> {
        not_erased("external_id", &self.external_id)?;
        self.profile.validate().map_err(|e| e.nested("profile"))
    }
}

/// Refuses an erased member's tombstone id (`erased:<member_id>`) as a mgmt input: it is
/// Sew's own name for an erased account, never an id a caller addresses.
fn not_erased(field: &str, e: &ExternalId) -> Result<(), ValidationError> {
    if e.is_erased() {
        return Err(ValidationError::invalid(
            field,
            "erased:<member_id> names an erased account and is never a mgmt input",
        ));
    }
    Ok(())
}

/// Body of `POST /mgmt/v1/principals/deprovision`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct DeprovisionRequest {
    /// Who to deactivate.
    pub external_id: ExternalId,
}

impl Validate for DeprovisionRequest {
    fn validate(&self) -> Result<(), ValidationError> {
        not_erased("external_id", &self.external_id)
    }
}

/// Response of `POST /mgmt/v1/principals/deprovision`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct DeprovisionResponse {
    /// The deactivated member.
    pub member_id: Id,
    /// Sessions revoked.
    pub sessions_revoked: u64,
}

/// Body of `POST /mgmt/v1/sessions/revoke`: at least one of `external_id`, `k2_sid` and
/// `app_id`. `app_id` goes alone or with `external_id`, never with `k2_sid`.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct RevokeSessionsRequest {
    /// Revoke every session of this principal.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub external_id: Option<ExternalId>,
    /// Revoke sessions bound to this K2 login session.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub k2_sid: Option<String>,
    /// Revoke every session whose pass `app` claim is this App (with `external_id`:
    /// only that principal's). K2 sends it when it revokes an App's key or uninstalls the App,
    /// never on a key rotation.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub app_id: Option<String>,
}

impl Validate for RevokeSessionsRequest {
    fn validate(&self) -> Result<(), ValidationError> {
        if let Some(e) = &self.external_id {
            not_erased("external_id", e)?;
        }
        if self.external_id.is_none() && self.k2_sid.is_none() && self.app_id.is_none() {
            return Err(ValidationError::invalid(
                "external_id",
                "send external_id, k2_sid, app_id, or external_id with one of the other two",
            ));
        }
        if let Some(app) = &self.app_id {
            spaces::check_app_id("app_id", app)?;
            if self.k2_sid.is_some() {
                return Err(ValidationError::invalid(
                    "app_id",
                    "app_id goes alone or with external_id, never with k2_sid",
                ));
            }
        }
        if let Some(sid) = &self.k2_sid
            && !is_k2_session_id(sid)
        {
            return Err(ValidationError::invalid(
                "k2_sid",
                "k2_sid must be 1–128 chars of [A-Za-z0-9_.-]",
            ));
        }
        Ok(())
    }
}

/// Response of `POST /mgmt/v1/sessions/revoke`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct RevokeSessionsResponse {
    /// Sessions revoked.
    pub revoked: u64,
}

/// The current owner's inline approval of an owner transfer. `Debug` redacts the
/// secret.
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
pub enum OwnerApproval {
    /// K2 mode: a chat pass whose `sub` is the current owner.
    OwnerPass {
        /// The pass.
        pass: String,
    },
    /// Standalone: the current owner's password.
    OwnerPassword {
        /// The password.
        password: String,
    },
}

impl OwnerApproval {
    /// The wire `kind`.
    pub fn kind_str(&self) -> &'static str {
        match self {
            OwnerApproval::OwnerPass { .. } => "owner_pass",
            OwnerApproval::OwnerPassword { .. } => "owner_password",
        }
    }
}

impl fmt::Debug for OwnerApproval {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            OwnerApproval::OwnerPass { .. } => f
                .debug_struct("OwnerPass")
                .field("pass", &"<redacted>")
                .finish(),
            OwnerApproval::OwnerPassword { .. } => f
                .debug_struct("OwnerPassword")
                .field("password", &"<redacted>")
                .finish(),
        }
    }
}

/// Body of `POST /mgmt/v1/owner/set`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct OwnerSetRequest {
    /// The new install owner.
    pub external_id: ExternalId,
    /// The current owner's approval; not needed while no owner exists (additive).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub approval: Option<OwnerApproval>,
    /// Break-glass transfer: standalone only, and only for peer uid 0 (additive).
    #[serde(default)]
    pub force: bool,
}

impl Validate for OwnerSetRequest {
    fn validate(&self) -> Result<(), ValidationError> {
        if self.external_id.is_agent() {
            return Err(ValidationError::invalid(
                "external_id",
                "the install owner must be a person",
            ));
        }
        match &self.approval {
            Some(OwnerApproval::OwnerPass { pass }) if pass.is_empty() || pass.len() > 4_096 => {
                Err(ValidationError::invalid(
                    "approval.pass",
                    "approval.pass must be 1–4096 bytes",
                ))
            }
            Some(OwnerApproval::OwnerPassword { password })
                if password.is_empty() || password.len() > crate::limits::PASSWORD_MAX_BYTES =>
            {
                Err(ValidationError::invalid(
                    "approval.password",
                    "approval.password must be 1–1024 bytes",
                ))
            }
            _ => Ok(()),
        }
    }
}

/// Response of `POST /mgmt/v1/owner/set`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct OwnerSetResponse {
    /// The install owner after the call.
    pub owner_id: Id,
    /// The owner before the call, now an admin; absent on the first call or when unchanged.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub previous_owner_id: Option<Id>,
    /// Whether the owner changed.
    pub changed: bool,
}

/// Body of `POST /mgmt/v1/agents/pin` and `unpin` (external ids).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct PinRequest {
    /// The assistant, `k2:workspace:<uuid>`.
    pub agent: ExternalId,
    /// The person.
    pub person: ExternalId,
}

impl Validate for PinRequest {
    fn validate(&self) -> Result<(), ValidationError> {
        if !self.agent.is_agent() {
            return Err(ValidationError::invalid(
                "agent",
                "agent must be k2:workspace:<uuid>",
            ));
        }
        if self.person.is_agent() {
            return Err(ValidationError::invalid(
                "person",
                "person must be a person",
            ));
        }
        Ok(())
    }
}

/// Response of `POST /mgmt/v1/agents/pin` and `unpin`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct PinResponse {
    /// The assistant.
    pub agent_id: Id,
    /// The person it is pinned to; absent after unpin.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub person_id: Option<Id>,
    /// Whether the pin changed.
    pub changed: bool,
}

// ---- principals list ----

fn default_true() -> bool {
    true
}

fn is_true(v: &bool) -> bool {
    *v
}

/// Query of `GET /mgmt/v1/principals`: pages by member id,
/// ascending.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct PrincipalsQuery {
    /// Members with an id above this one.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub after: Option<Id>,
    /// Page size, 1–500 (default 100).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub limit: Option<u32>,
    /// Only persons or only agents.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub kind: Option<MemberKind>,
    /// Only this external id.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub external_id: Option<ExternalId>,
    /// Only this handle (exact match).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub username: Option<Handle>,
    /// Include deactivated members (default `true`).
    #[serde(default = "default_true", skip_serializing_if = "is_true")]
    pub include_deactivated: bool,
    /// Only compliance-flag holders (`true`) or only non-holders (`false`) (additive).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub compliance: Option<bool>,
    /// `link_guests` also lists meeting link guests (`link:*` members), which are left out by
    /// default so K2's reconcile never sees them.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub include: Option<PrincipalsInclude>,
}

unit_enum! {
    /// `GET /mgmt/v1/principals?include=`.
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
    pub enum PrincipalsInclude {
        /// Meeting link guests (`link:*`, install role `link_guest`) too.
        LinkGuests = "link_guests",
    }
}

/// Body of `POST /mgmt/v1/principals/compliance`: grant or revoke the compliance
/// flag. Returns `ComplianceFlagResponse`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct PrincipalComplianceRequest {
    /// The member.
    pub external_id: ExternalId,
    /// Grant (`true`) or revoke (`false`).
    pub compliance: bool,
}

impl Validate for PrincipalComplianceRequest {
    fn validate(&self) -> Result<(), ValidationError> {
        Ok(())
    }
}

/// Largest `GET /mgmt/v1/principals` page.
pub const PRINCIPALS_LIMIT_MAX: u32 = 500;
/// Default `GET /mgmt/v1/principals` page.
pub const PRINCIPALS_LIMIT_DEFAULT: u32 = 100;

impl PrincipalsQuery {
    /// The page size, default 100.
    pub fn limit(&self) -> u32 {
        self.limit.unwrap_or(PRINCIPALS_LIMIT_DEFAULT)
    }
}

impl Validate for PrincipalsQuery {
    fn validate(&self) -> Result<(), ValidationError> {
        if let Some(l) = self.limit
            && !(1..=PRINCIPALS_LIMIT_MAX).contains(&l)
        {
            return Err(ValidationError::invalid(
                "limit",
                format!("limit must be 1–{PRINCIPALS_LIMIT_MAX}"),
            ));
        }
        if let Some(k) = self.kind {
            check_known("kind", k)?;
        }
        if let Some(i) = self.include {
            check_known("include", i)?;
        }
        Ok(())
    }
}

/// One principal in `GET /mgmt/v1/principals` (directory metadata only).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[non_exhaustive]
pub struct MgmtPrincipal {
    /// The member id.
    pub member_id: Id,
    /// Who the member is outside Sew.
    pub external_id: ExternalId,
    /// Person or agent.
    pub kind: MemberKind,
    /// For agents.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub agent_kind: Option<AgentKind>,
    /// For pinned assistants, the person's external id.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub assistant_of: Option<ExternalId>,
    /// Install role.
    pub install_role: crate::api::members::InstallRole,
    /// The handle.
    pub username: Handle,
    /// The display name.
    pub display_name: String,
    /// The email, as provisioned (absent when unset; additive).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub email: Option<String>,
    /// The title, as provisioned (absent when unset; additive).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,
    /// The pronouns, as provisioned (absent when unset; additive).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub pronouns: Option<String>,
    /// Whether the member is deactivated.
    pub deactivated: bool,
    /// When the member was created.
    pub created_at: Timestamp,
    /// When the member was deactivated.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub deactivated_at: Option<Timestamp>,
}

impl MgmtPrincipal {
    /// A `MgmtPrincipal` with its required fields: every flag `false` and every optional
    /// field unset.
    pub fn new(
        member_id: Id,
        external_id: ExternalId,
        kind: MemberKind,
        install_role: crate::api::members::InstallRole,
        username: Handle,
        display_name: impl Into<String>,
        created_at: Timestamp,
    ) -> Self {
        MgmtPrincipal {
            member_id,
            external_id,
            kind,
            agent_kind: None,
            assistant_of: None,
            install_role,
            username,
            display_name: display_name.into(),
            email: None,
            title: None,
            pronouns: None,
            deactivated: false,
            created_at,
            deactivated_at: None,
        }
    }
}

/// Response of `GET /mgmt/v1/principals`.
pub type MgmtPrincipalPage = crate::http::Page<MgmtPrincipal>;

// ---- act-as and mgmt reasons ----

/// Whether `s` is a valid `Sew-Actor` header: 1–128 chars of `[A-Za-z0-9_.:@-]`.
/// It names the K2 person behind a mgmt call, for audit only.
pub fn is_sew_actor(s: &str) -> bool {
    (1..=128).contains(&s.len())
        && s.bytes().all(|c| {
            c.is_ascii_alphanumeric()
                || c == b'_'
                || c == b'.'
                || c == b':'
                || c == b'@'
                || c == b'-'
        })
}

/// `details.reason` values of mgmt and act-as errors.
/// Stable strings; no new error codes.
pub mod reasons {
    /// The sync names no provisioned member (422 `invalid_state`; nothing changes).
    pub const MEMBER_NOT_PROVISIONED: &str = "member_not_provisioned";
    /// Box users (`k2:owner`, `k2:connect:`) are never synced (422 `invalid_state`).
    pub const BOX_USER_NOT_ALLOWED: &str = "box_user_not_allowed";
    /// An agent is synced at level `member` only (422 `invalid_state`).
    pub const AGENT_LEVEL: &str = "agent_level";
    /// A level above the App's `max_level` (422 `invalid_state`).
    pub const LEVEL_ABOVE_CEILING: &str = "level_above_ceiling";
    /// The sync runs only in an App-governed Space (409). Never
    /// answered for the server-governed Space (governance `server`).
    pub const NOT_APP_GOVERNED: &str = "not_app_governed";
    /// App users (`k2:principal:`) and App service owners (`app:`) are never
    /// members of the server-governed Space (422 `invalid_state`; the member-channels sync and
    /// `spaces/members/add`).
    pub const APP_USER_NOT_ALLOWED: &str = "app_user_not_allowed";
    /// The install has its server-governed Space already, under another slug (409;
    /// `details.space_id` names it).
    pub const SERVER_SPACE_EXISTS: &str = "server_space_exists";
    /// `governance` never changes to or from `server`, and the server-governed Space is
    /// never bound to an App (409).
    pub const SERVER_GOVERNANCE_FIXED: &str = "server_governance_fixed";
    /// The slug `k5-admin` belongs to the server-governed Space (400
    /// `validation_failed`).
    pub const SLUG_RESERVED: &str = "slug_reserved";
    /// `governance: app` on a Space with no governing App (409).
    pub const NO_GOVERNING_APP: &str = "no_governing_app";
    /// The home client never governs a Space: a governing link for it, `max_level`
    /// on it, or a home client that governs a Space (409).
    pub const HOME_CLIENT_CANNOT_GOVERN: &str = "home_client_cannot_govern";
    /// Sew 0.8.0: an App id on a Space write names a participant App of the Space, not
    /// its governing App (409; a participant App has no say over the Space).
    pub const APP_NOT_GOVERNING: &str = "app_not_governing";
    /// Sew 0.8.0: a participant link (or unlink) names the Space's governing App
    /// (409; use `spaces/unbind`).
    pub const APP_IS_GOVERNING: &str = "app_is_governing";
    /// Sew 0.8.0: the Space has 8 participant Apps already (409).
    pub const PARTICIPANT_APPS_MAX: &str = "participant_apps_max";
    /// Sew 0.8.0: the home client is never a participant App: a link naming it, or a home
    /// client that is a participant somewhere (409).
    pub const HOME_CLIENT_CANNOT_LINK: &str = "home_client_cannot_link";
    /// An erase asks for `anonymise` after the account was erased with `delete_content` (409;
    /// the content is gone).
    pub const ERASE_MODE_DOWNGRADE: &str = "erase_mode_downgrade";
    /// The transcript route, for an App whose `recording.server_read` setting is off (403
    /// `policy_denied`).
    pub const RECORDING_SERVER_READ_OFF: &str = "recording_server_read_off";
    /// A mgmt call that only the install's Owner or an Admin may make: the stop of a
    /// recording, a recording read without `app_id`, an audio download. `Sew-Actor` is
    /// absent or does not name an active person (by external id) whose install role is
    /// owner or admin (403 `missing_permission`; audited denied).
    pub const SEW_ACTOR_ADMIN_REQUIRED: &str = "sew_actor_admin_required";
    /// `governance: app` while box users (`k2:owner`, `k2:connect:`) other than
    /// the owner are members of the Space (409, `MgmtBoxUsersDetails`).
    pub const BOX_USERS_PRESENT: &str = "box_users_present";
    /// A peer uid outside `[mgmt] allowed_uids` (403).
    pub const PEER_UID_NOT_ALLOWED: &str = "peer_uid_not_allowed";
    /// Act-as headers on a `/mgmt/v1/*` route (400).
    pub const ACT_AS_ON_MGMT_ROUTE: &str = "act_as_on_mgmt_route";
    /// `/api/v1/*` on the mgmt socket without both act-as headers (400).
    pub const ACT_AS_HEADERS_REQUIRED: &str = "act_as_headers_required";
    /// An act-as header does not parse (400).
    pub const ACT_AS_HEADER_INVALID: &str = "act_as_header_invalid";
    /// Act-as headers on the TCP client listener (400).
    pub const ACT_AS_ONLY_ON_MGMT_SOCKET: &str = "act_as_only_on_mgmt_socket";
    /// A route act-as may never reach (403).
    pub const ACT_AS_ROUTE_FORBIDDEN: &str = "act_as_route_forbidden";
    /// `Sew-Via` names another agent than `Sew-Act-As` (403).
    pub const VIA_AGENT_MISMATCH: &str = "via_agent_mismatch";
    /// The handle is held by another active member (409).
    pub const USERNAME_TAKEN: &str = "username_taken";
    /// The install owner cannot be deprovisioned or made a guest (422).
    pub const IS_OWNER: &str = "is_owner";
    /// The person is deactivated (422).
    pub const PERSON_DEACTIVATED: &str = "person_deactivated";
    /// The person is a guest (422).
    pub const PERSON_IS_GUEST: &str = "person_is_guest";
    /// The person is an agent (422).
    pub const PERSON_IS_AGENT: &str = "person_is_agent";
    /// Provisioning an assistant whose person is not provisioned yet (422;
    /// K2 retries for up to 1 h).
    pub const ASSISTANT_PERSON_UNKNOWN: &str = "assistant_person_unknown";
    /// Provisioning an assistant whose person is deactivated (422; K2 stops
    /// retrying).
    pub const ASSISTANT_PERSON_INACTIVE: &str = "assistant_person_inactive";
    /// The agent is deactivated (422).
    pub const AGENT_DEACTIVATED: &str = "agent_deactivated";
    /// Pinning a coworker; convert it through provision (422).
    pub const AGENT_IS_COWORKER: &str = "agent_is_coworker";
    /// Unpin names another person than the current pin (409).
    pub const PIN_MISMATCH: &str = "pin_mismatch";
    /// The owner must be an active, non-guest person (422).
    pub const TARGET_NOT_ELIGIBLE: &str = "target_not_eligible";
    /// This server has no backup engine (503).
    pub const BACKUPS_UNAVAILABLE: &str = "backups_unavailable";
    /// Another backup holds the backup lock (409).
    pub const BACKUP_RUNNING: &str = "backup_running";
    /// An owner, manager or member names no provisioned principal (422).
    pub const PRINCIPAL_UNKNOWN: &str = "principal_unknown";
    /// The slug is held by a Space of another owner or App (409).
    pub const SLUG_TAKEN: &str = "slug_taken";
    /// `app_id` does not name the Space's App (409).
    pub const APP_MISMATCH: &str = "app_mismatch";
    /// The Space is bound to another App already (409).
    pub const SPACE_BOUND: &str = "space_bound";
    /// The Space was unbound from another App; only that App may bind it until
    /// `spaces/release` (409).
    pub const SPACE_BOUND_ELSEWHERE: &str = "space_bound_elsewhere";
    /// Channels over mgmt need a Space bound to an App (409).
    pub const SPACE_UNBOUND: &str = "space_unbound";
    /// The slug is held by a channel of the other type (409; details
    /// `MgmtChannelTypeMismatchDetails`).
    pub const CHANNEL_TYPE_MISMATCH: &str = "channel_type_mismatch";
    /// The owner approval did not verify (403).
    pub const APPROVAL_INVALID: &str = "approval_invalid";
    /// A transfer needs the current owner's approval (403).
    pub const OWNER_APPROVAL_REQUIRED: &str = "owner_approval_required";
    /// `force` needs a standalone install and peer uid 0 (403).
    pub const FORCE_NOT_ALLOWED: &str = "force_not_allowed";
    /// A live job holds the dead job's dedupe key (409).
    pub const DEDUPE_KEY_TAKEN: &str = "dedupe_key_taken";
    /// Only dead jobs can be retried (422).
    pub const NOT_DEAD: &str = "not_dead";
    /// Master rotation needs `sew-master-key-prev` loaded (422).
    pub const NO_RETIRING_MASTER_KEY: &str = "no_retiring_master_key";
    /// An assistant may not open DMs (403, stage B).
    pub const ASSISTANT_CANNOT_OPEN_DM: &str = "assistant_cannot_open_dm";
    /// Assistants hold no space or channel memberships (422, stage B).
    pub const ASSISTANT_NOT_JOINABLE: &str = "assistant_not_joinable";
    /// A 1:1 DM with a deactivated partner is read-only (422, stage B).
    pub const DM_PARTNER_DEACTIVATED: &str = "dm_partner_deactivated";
    /// `sew init` when an owner exists (409).
    pub const OWNER_EXISTS: &str = "owner_exists";
    /// The local user is deactivated (409/422).
    pub const USER_DEACTIVATED: &str = "user_deactivated";
    /// The member is not a local person (422).
    pub const NOT_A_LOCAL_PERSON: &str = "not_a_local_person";
    /// A local user's handle is part of its external id and cannot change (422).
    pub const LOCAL_USERNAME_FIXED: &str = "local_username_fixed";
    /// No mgmt or act-as route matches (404).
    pub const UNKNOWN_ROUTE: &str = "unknown_route";
    /// The install owner's role changes only through `owner/set` (422).
    pub const OWNER_ROLE_FIXED: &str = "owner_role_fixed";
    /// A guest's role changes only through guest conversion (422).
    pub const GUEST_ROLE_FIXED: &str = "guest_role_fixed";
    /// Agents are always `member` (422).
    pub const AGENT_ROLE_FIXED: &str = "agent_role_fixed";
    /// A meeting link guest is never converted or given another role (422).
    pub const LINK_GUEST_FIXED: &str = "link_guest_fixed";
    /// A person with an active pinned assistant cannot become a guest (422; matches
    /// K2's own refusal). `details.assistant_ids` names them.
    pub const PERSON_HAS_ASSISTANTS: &str = "person_has_assistants";
    /// A guest login while `guests.enabled` is off (audit `details.reason`).
    pub const GUESTS_DISABLED: &str = "guests_disabled";
    /// The compliance flag is never on a guest (422).
    pub const TARGET_IS_GUEST: &str = "target_is_guest";
    /// The relink target already belongs to a member (409; merging is not in v1).
    pub const EXTERNAL_ID_TAKEN: &str = "external_id_taken";
    /// A relink before the install's identity mode is K2 (422; switch first).
    /// The server-governed Space (governance `server`) on a standalone install (409).
    pub const IDENTITY_NOT_K2: &str = "identity_not_k2";
    /// The identity switch needs `[identity.k2] jwks_url` in the running config (422).
    pub const K2_IDENTITY_UNCONFIGURED: &str = "k2_identity_unconfigured";
    /// The identity switch is refused while an upgrade or rollback runs (409).
    pub const UPGRADE_IN_PROGRESS: &str = "upgrade_in_progress";

    /// Every reason above.
    pub const ALL: &[&str] = &[
        PEER_UID_NOT_ALLOWED,
        ACT_AS_ON_MGMT_ROUTE,
        ACT_AS_HEADERS_REQUIRED,
        ACT_AS_HEADER_INVALID,
        ACT_AS_ONLY_ON_MGMT_SOCKET,
        ACT_AS_ROUTE_FORBIDDEN,
        VIA_AGENT_MISMATCH,
        USERNAME_TAKEN,
        IS_OWNER,
        PERSON_DEACTIVATED,
        PERSON_IS_GUEST,
        PERSON_IS_AGENT,
        ASSISTANT_PERSON_UNKNOWN,
        ASSISTANT_PERSON_INACTIVE,
        AGENT_DEACTIVATED,
        AGENT_IS_COWORKER,
        PIN_MISMATCH,
        TARGET_NOT_ELIGIBLE,
        APPROVAL_INVALID,
        OWNER_APPROVAL_REQUIRED,
        FORCE_NOT_ALLOWED,
        DEDUPE_KEY_TAKEN,
        NOT_DEAD,
        NO_RETIRING_MASTER_KEY,
        ASSISTANT_CANNOT_OPEN_DM,
        ASSISTANT_NOT_JOINABLE,
        DM_PARTNER_DEACTIVATED,
        OWNER_EXISTS,
        USER_DEACTIVATED,
        NOT_A_LOCAL_PERSON,
        LOCAL_USERNAME_FIXED,
        UNKNOWN_ROUTE,
        OWNER_ROLE_FIXED,
        GUEST_ROLE_FIXED,
        AGENT_ROLE_FIXED,
        LINK_GUEST_FIXED,
        PERSON_HAS_ASSISTANTS,
        GUESTS_DISABLED,
        TARGET_IS_GUEST,
        EXTERNAL_ID_TAKEN,
        IDENTITY_NOT_K2,
        K2_IDENTITY_UNCONFIGURED,
        UPGRADE_IN_PROGRESS,
    ];
}

// ---- agent deliveries ----

/// Largest `limit` of a deliveries page.
pub const DELIVERIES_LIMIT_MAX: u32 = 500;
/// Default `limit` of a deliveries page.
pub const DELIVERIES_LIMIT_DEFAULT: u32 = 100;
/// A delivery's `body` is cut to at most this many bytes, at a char boundary.
pub const DELIVERY_BODY_MAX_BYTES: usize = 4_000;

/// Query of `GET /mgmt/v1/agents/{member_id}/deliveries`: long-poll after a member seq.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct DeliveriesQuery {
    /// Return entries with `seq > after`. K2 sends the previous page's `next_after`.
    pub after: u64,
    /// Long-poll up to this long (≤ 25000, default 25000).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub timeout_ms: Option<u64>,
    /// At most this many entries (1–500, default 100; additive).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub limit: Option<u32>,
}

impl DeliveriesQuery {
    /// The long-poll timeout, default 25 s.
    pub fn timeout_ms(&self) -> u64 {
        self.timeout_ms.unwrap_or(DELIVERIES_LONG_POLL_MAX_MS)
    }

    /// The page size, default 100.
    pub fn limit(&self) -> u32 {
        self.limit.unwrap_or(DELIVERIES_LIMIT_DEFAULT)
    }
}

impl Validate for DeliveriesQuery {
    fn validate(&self) -> Result<(), ValidationError> {
        if let Some(t) = self.timeout_ms
            && t > DELIVERIES_LONG_POLL_MAX_MS
        {
            return Err(ValidationError::invalid(
                "timeout_ms",
                format!("timeout_ms must be at most {DELIVERIES_LONG_POLL_MAX_MS}"),
            ));
        }
        if let Some(l) = self.limit
            && !(1..=DELIVERIES_LIMIT_MAX).contains(&l)
        {
            return Err(ValidationError::invalid(
                "limit",
                format!("limit must be 1–{DELIVERIES_LIMIT_MAX}"),
            ));
        }
        Ok(())
    }
}

unit_enum! {
    /// What kind of message a delivery carries. v1 sends `text` only;
    /// `Unknown` renders as text.
    #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
    pub enum DeliveryKind {
        /// A chat message.
        #[default]
        Text = "text",
        /// A DM call record.
        CallRecord = "call_record",
        /// A poll.
        Poll = "poll",
        /// A call transcript.
        Transcript = "transcript",
    }
}

unit_enum! {
    /// Where a delivered message lives, from the agent's side.
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
    pub enum DeliveryPlace {
        /// A space channel.
        SpaceChannel = "space_channel",
        /// A 1:1 DM the agent is in.
        Dm = "dm",
        /// A group DM the agent is in.
        GroupDm = "group_dm",
        /// A DM or group DM of an assistant's person, read (never posted to) by the assistant.
        PersonDm = "person_dm",
    }
}

/// A call record's summary in a delivery (`kind = call_record`).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct DeliveryCall {
    /// The call was missed.
    pub missed: bool,
    /// How long it lasted.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub duration_ms: Option<u64>,
}

/// A message summary in an agent delivery.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[non_exhaustive]
pub struct DeliveryMessage {
    /// The message.
    pub id: Id,
    /// Its channel.
    pub channel_id: Id,
    /// Its space; absent for DMs.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub space_id: Option<Id>,
    /// Its thread root, for replies.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub thread_root_id: Option<Id>,
    /// The author.
    pub author_id: Id,
    /// The author's current display name (also for deactivated authors).
    pub author_display_name: String,
    /// The author's current username.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub author_username: Option<Handle>,
    /// Person or agent.
    pub author_kind: MemberKind,
    /// The message's conversation address (the thread address for replies).
    pub addr: Addr,
    /// Where a reply goes: `<channel addr>/<message id>` for a top-level
    /// space message, the thread address for replies, the DM address for DMs. Absent for
    /// `place = person_dm` (the assistant may not post there).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub reply_addr: Option<Addr>,
    /// Where the message lives from the agent's side.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub place: Option<DeliveryPlace>,
    /// What kind of message it is.
    #[serde(default)]
    pub kind: DeliveryKind,
    /// A call record's summary (`kind = call_record` only).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub call: Option<DeliveryCall>,
    /// A recording record's summary (`kind = transcript` only). Never audio.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub recording: Option<crate::recording::RecordingSummary>,
    /// The current body (after edits), cut to 4,000 bytes at a char boundary.
    pub body: String,
    /// Whether `body` was cut.
    #[serde(default)]
    pub body_truncated: bool,
    /// How many attachments the message has.
    #[serde(default)]
    pub attachment_count: u32,
    /// For assistant-authored messages: the person the assistant posted for.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub on_behalf_of: Option<Id>,
    /// That person's current display name.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub on_behalf_of_display_name: Option<String>,
    /// When it was posted.
    pub created_at: Timestamp,
}

impl DeliveryMessage {
    /// A `DeliveryMessage` with its required fields: every count 0, every flag `false`, the
    /// other fields at their serde defaults and every optional field unset.
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        id: Id,
        channel_id: Id,
        author_id: Id,
        author_display_name: impl Into<String>,
        author_kind: MemberKind,
        addr: Addr,
        body: impl Into<String>,
        created_at: Timestamp,
    ) -> Self {
        DeliveryMessage {
            id,
            channel_id,
            space_id: None,
            thread_root_id: None,
            author_id,
            author_display_name: author_display_name.into(),
            author_username: None,
            author_kind,
            addr,
            reply_addr: None,
            place: None,
            kind: Default::default(),
            call: None,
            recording: None,
            body: body.into(),
            body_truncated: false,
            attachment_count: 0,
            on_behalf_of: None,
            on_behalf_of_display_name: None,
            created_at,
        }
    }
}

/// One agent delivery: a `notification` entry of the agent's member log.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct Delivery {
    /// The member seq.
    pub seq: u64,
    /// When it was written.
    pub at: Timestamp,
    /// Why the agent was notified.
    pub reason: NotificationReason,
    /// The message.
    pub message: DeliveryMessage,
}

/// Response of `GET /mgmt/v1/agents/{member_id}/deliveries`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct DeliveriesPage {
    /// Deliveries with `seq > after`, oldest first. Entries the agent cannot read now are
    /// skipped without a trace.
    pub entries: Vec<Delivery>,
    /// The member log head.
    pub head: u64,
    /// The next `after`: the seq of the last row scanned (delivered or skipped), `after` when
    /// nothing was scanned, `head` after an empty long-poll (additive).
    #[serde(default)]
    pub next_after: u64,
    /// Whether more entries follow `next_after`.
    pub has_more: bool,
    /// The install's `sync_epoch` (opaque; changes on restore).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub sync_epoch: Option<String>,
}

/// Response of `POST /mgmt/v1/keys/mirror-sync`: one full keyring mirror reconcile, run now.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[non_exhaustive]
pub struct KeysMirrorSyncResponse {
    /// The mirror kind: `dir`, `none`.
    pub kind: String,
    /// The full listing succeeded and every push and repair went through.
    pub ok: bool,
    /// Pending writes pushed.
    pub pushed: u64,
    /// Pending writes that failed again (they stay pending).
    pub failed: u64,
    /// Entries missing in the mirror, written there (keys, `FORMAT`, the ledgers).
    pub repaired: u64,
    /// Mirror entries of shredded keys, deleted.
    pub deleted_shredded: u64,
    /// Mirror entries that are neither local nor shredded (never deleted).
    pub unknown: u64,
    /// Why the mirror could not be listed; nothing was backfilled.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub error: Option<String>,
    /// A refused `dir` mirror path: `unmounted` or `same_device`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub placement: Option<String>,
    /// The last successful full reconcile since the server started.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub last_full_reconcile_ok_at: Option<Timestamp>,
}

impl KeysMirrorSyncResponse {
    /// A reconcile of a mirror of `kind` that did nothing: every count 0, no error, placement
    /// or last reconcile; set the other fields.
    pub fn new(kind: impl Into<String>, ok: bool) -> KeysMirrorSyncResponse {
        KeysMirrorSyncResponse {
            kind: kind.into(),
            ok,
            pushed: 0,
            failed: 0,
            repaired: 0,
            deleted_shredded: 0,
            unknown: 0,
            error: None,
            placement: None,
            last_full_reconcile_ok_at: None,
        }
    }
}

// ---- jobs ----

/// Response of the job-starting routes (backup, export, rotate-master).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct StartJobResponse {
    /// The job.
    pub job_id: Id,
}

unit_enum! {
    /// A job's state.
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
    pub enum JobState {
        /// Waiting to run.
        Queued = "queued",
        /// Running.
        Running = "running",
        /// Finished.
        Succeeded = "succeeded",
        /// Failed; may retry.
        Failed = "failed",
        /// Exhausted its attempts.
        Dead = "dead",
    }
}

/// Response of `GET /mgmt/v1/jobs/{id}`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
pub struct JobStatus {
    /// The job.
    pub id: Id,
    /// The job kind (for example `backup`).
    pub kind: String,
    /// The state.
    pub state: JobState,
    /// Attempts so far.
    pub attempts: u32,
    /// The last error kind.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub last_error: Option<String>,
    /// When it was queued.
    pub created_at: Timestamp,
    /// When it finished.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub finished_at: Option<Timestamp>,
    /// What a `succeeded` job recorded (`jobs_done.result`; additive).
    /// Absent otherwise. Ids and counts only, never content.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub result: Option<serde_json::Value>,
}

/// Query of `GET /mgmt/v1/jobs`: newest first.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct JobsQuery {
    /// Only jobs in this state.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub state: Option<JobState>,
    /// Only jobs of this kind.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub kind: Option<String>,
    /// Jobs with an id below this one.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub before: Option<Id>,
    /// Page size, 1–100 (default 50).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub limit: Option<u32>,
}

impl JobsQuery {
    /// The page size, default 50.
    pub fn limit(&self) -> u32 {
        self.limit.unwrap_or(crate::limits::PAGE_LIMIT_DEFAULT)
    }
}

impl Validate for JobsQuery {
    fn validate(&self) -> Result<(), ValidationError> {
        if let Some(s) = self.state {
            check_known("state", s)?;
        }
        if let Some(k) = &self.kind
            && (k.is_empty()
                || k.len() > 64
                || !k
                    .bytes()
                    .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'_'))
        {
            return Err(ValidationError::invalid(
                "kind",
                "kind must be 1–64 chars of [a-z0-9_]",
            ));
        }
        if let Some(l) = self.limit
            && !(1..=crate::limits::PAGE_LIMIT_MAX).contains(&l)
        {
            return Err(ValidationError::invalid(
                "limit",
                format!("limit must be 1–{}", crate::limits::PAGE_LIMIT_MAX),
            ));
        }
        Ok(())
    }
}

/// Response of `GET /mgmt/v1/jobs`.
pub type JobStatusPage = crate::http::Page<JobStatus>;