basecamp-sdk 0.38.0

Official client for Basecamp - https://basecamp.com
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
//! Recordings: the generated wire methods plus
//! [`RecordingsService::summarize`](RecordingsService::summarize), the SPEC §18 composite
//! that resolves an event pointer into a compact projection of the recording it names.
//!
//! [`RecordingSummary`] exists for consumers that must decide something about a recording
//! without paying for its full payload: an agent connector's admission step, an MCP tool
//! answering "what is this?".
//!
//! The SDK has no untyped recording read (BC3 has no such route), so the type is the
//! routing key: `comment.created` reads a comment, `card.created` reads a card, and so on —
//! one typed read per type. Chat lines are the exception, because their read needs the
//! Campfire id and the pointer does not carry it; `summarize` discovers the Campfire first
//! (see [`campfire_index`](crate::services::campfire_index)).
//!
//! This is hand-written composition over the generated services. It makes no wire request
//! of its own, and it mints no operation identity: hooks see the constituent reads under
//! their own names (SPEC §18 rule 3).

pub use crate::generated::services::recordings::{ListRecordingsParams, RecordingsService};

use std::fmt;

use serde::{Deserialize, Serialize};

use crate::client::AccountClient;
use crate::error::{Error, ErrorCode};
use crate::generated::types::{
    CampfireLine, Person, RecordingBucket, RecordingParent, TodoBucket, TodoParent,
};
use crate::mentions::mentioned_person_ids;
use crate::services::campfire_index::{MAX_CAMPFIRE_CANDIDATES, SourceRead, is_listing_overflow};
use crate::types::DateTime;

/// A pointer at one recording, the way an account event feed row carries it.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct RecordingRef {
    /// The project the recording lives in. Required: it scopes the Campfire discovery for
    /// chat lines, and the read is checked against it so a pointer from one project can
    /// never resolve to a recording in another.
    pub bucket_id: i64,

    /// The recording's id.
    pub recording_id: i64,

    /// The account event feed type that named the recording — `comment.created`,
    /// `card.assignment_changed`, `chat.line.created`. The segment before the action names
    /// the recording type. Used when `recording_type` is absent.
    pub event_type: Option<String>,

    /// The recording's own type as BC3 spells it — `Comment`, `Kanban::Card`,
    /// `Chat::Lines::Text`. When set it takes precedence over `event_type`, being the more
    /// exact of the two.
    pub recording_type: Option<String>,
}

impl RecordingRef {
    /// A pointer from an event feed type — `comment.created` and friends.
    pub fn from_event(bucket_id: i64, recording_id: i64, event_type: impl Into<String>) -> Self {
        RecordingRef {
            bucket_id,
            recording_id,
            event_type: Some(event_type.into()),
            recording_type: None,
        }
    }

    /// A pointer from a BC3 recording type — `Comment`, `Kanban::Card` and friends.
    pub fn from_recording_type(
        bucket_id: i64,
        recording_id: i64,
        recording_type: impl Into<String>,
    ) -> Self {
        RecordingRef {
            bucket_id,
            recording_id,
            event_type: None,
            recording_type: Some(recording_type.into()),
        }
    }

    /// The routing key the pointer is read by: the recording type when it carries one, the
    /// event type otherwise.
    fn key(&self) -> &str {
        let recording_type = self.recording_type.as_deref().unwrap_or_default().trim();
        if recording_type.is_empty() {
            self.event_type.as_deref().unwrap_or_default().trim()
        } else {
            recording_type
        }
    }
}

/// The projection [`RecordingsService::summarize`] returns. Fields a type does not have are
/// empty: a comment has no assignees, a vault no content.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct RecordingSummary {
    /// The recording's id.
    pub id: i64,
    /// `active`, `archived`, `trashed`.
    pub status: String,
    /// The recording type as BC3 spells it (`Comment`, `Kanban::Card`).
    pub r#type: String,
    /// The recording's title.
    pub title: String,
    /// Where a person reads it in Basecamp.
    pub app_url: String,
    /// The recording this one hangs off — the commented recording for a comment, the
    /// Campfire for a chat line, the column for a card.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub parent: Option<RecordingParent>,
    /// The project the recording lives in.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub bucket: Option<RecordingBucket>,
    /// Who created it.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub creator: Option<Person>,
    /// Set for the assignable types (to-dos, cards, card steps). Always serialized, so a
    /// server-sent `[]` reaches a JSON consumer as `[]` rather than a missing key; a type
    /// with no assignees serializes `[]` too, since a `Vec` can't tell absent from empty.
    #[serde(default)]
    pub assignees: Vec<Person>,
    /// The people [`content`](RecordingSummary::content) mentions, per
    /// [`mentioned_person_ids`]. Always serialized, so a JSON consumer reads `[]` rather
    /// than a missing key.
    pub mentioned_person_ids: Vec<i64>,
    /// The recording's rich text, in full: the comment body, the message body, a to-do's
    /// description, a card's content, the chat line.
    pub content: String,
    /// When it last changed.
    pub updated_at: DateTime,
    /// The Campfire a chat line was found under — the reply destination for a chat trigger.
    /// Absent for every other type.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub campfire_id: Option<i64>,
}

/// A chat line that was found under none of the Campfires the caller can currently see.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct UnresolvedRecording {
    /// The bucket the pointer named.
    pub bucket_id: i64,
    /// The line the pointer named.
    pub recording_id: i64,
    /// The candidates tried, in order; empty when the bucket has no visible Campfire at all.
    pub campfire_ids: Vec<i64>,
    /// Whether the cached discovery sources were re-read before concluding. `false` when
    /// every source had been read within the last
    /// [`CAMPFIRE_INDEX_MIN_REFRESH`](crate::services::campfire_index::CAMPFIRE_INDEX_MIN_REFRESH),
    /// so a Campfire created in that window was not seen: the conclusion stands on data up
    /// to that old, and a retry after the floor sees the current sources.
    pub refreshed: bool,
    /// Candidates from the cache that the refreshed sources no longer list — Campfires the
    /// caller could see when the cache filled and cannot now. Set only when `refreshed`.
    pub stale_campfire_ids: Vec<i64>,
}

/// Why [`RecordingsService::summarize`] could not answer, when the reason is the
/// composite's own rather than a read's.
///
/// It is chained as the cause of the [`Error`] the call returns, so a consumer matches on
/// the identity rather than parsing a message: `RecordingSummaryError::of(&error)`. Every
/// other failure — a 403 on a candidate, a 404 on the typed read, a transport error — is
/// returned as that read's own [`Error`], with no cause of this kind.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum RecordingSummaryError {
    /// The event type names no recording type — `boost.created`, whose recording is the
    /// boost's target and whose type the feed row does not carry. A consumer resolves those
    /// from its own record of what it posted, not through `summarize`.
    NoRecordingType {
        /// The routing key that was refused.
        key: String,
    },

    /// Neither the event type nor the recording type names a type in the routing table.
    UnknownRecordingType {
        /// The routing key that was refused.
        key: String,
    },

    /// A chat line was found under none of the Campfires the caller can currently see.
    ///
    /// It is distinct from a failed read (any non-404 answer is returned as itself) and
    /// from discovery that could not finish ([`Self::CampfireDiscoveryIncomplete`]): every
    /// candidate answered 404. It is *not* distinct from lost visibility — BC3 answers 404
    /// for a Campfire the caller may not see, too — so a consumer marks the record blocked
    /// and retries on its own schedule; see
    /// [`UnresolvedRecording::stale_campfire_ids`].
    Unresolved(UnresolvedRecording),

    /// Discovery could not be carried to a conclusion — the Campfire listing overflowed
    /// [`MAX_CAMPFIRE_LISTING`](crate::services::campfire_index::MAX_CAMPFIRE_LISTING), or
    /// a bucket has more visible Campfires than
    /// [`MAX_CAMPFIRE_CANDIDATES`]. Distinct from [`Self::Unresolved`]: candidates were
    /// left unsearched, so nothing can be reported absent.
    CampfireDiscoveryIncomplete {
        /// The bucket the pointer named.
        bucket_id: i64,
        /// The line the pointer named.
        recording_id: i64,
        /// Why discovery stopped short.
        reason: String,
    },

    /// The recording the read returned lives in a different bucket from the one the pointer
    /// named.
    BucketMismatch {
        /// The bucket the pointer named.
        bucket_id: i64,
        /// The recording the pointer named.
        recording_id: i64,
        /// The bucket the read returned.
        found_in: i64,
    },
}

impl fmt::Display for RecordingSummaryError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            RecordingSummaryError::NoRecordingType { key } => {
                write!(f, "event type names no recording type: {key:?}")
            }
            RecordingSummaryError::UnknownRecordingType { key } => {
                write!(f, "no typed read for recording type: {key:?}")
            }
            RecordingSummaryError::Unresolved(unresolved) => write!(
                f,
                "chat line found under no visible campfire: line {} in bucket {} (tried {} campfires)",
                unresolved.recording_id,
                unresolved.bucket_id,
                unresolved.campfire_ids.len()
            ),
            RecordingSummaryError::CampfireDiscoveryIncomplete {
                bucket_id,
                recording_id,
                reason,
            } => write!(
                f,
                "campfire discovery incomplete: line {recording_id} in bucket {bucket_id}: {reason}"
            ),
            RecordingSummaryError::BucketMismatch {
                bucket_id,
                recording_id,
                found_in,
            } => write!(
                f,
                "recording is not in the requested bucket: recording {recording_id} is in bucket {found_in}, not {bucket_id}"
            ),
        }
    }
}

impl std::error::Error for RecordingSummaryError {}

impl RecordingSummaryError {
    /// The composite's own reason for an [`Error`], when it has one. A read that failed on
    /// its own terms — a 403, a 404, a transport error — answers `None`, and is classified
    /// by [`Error::code`] like any other SDK failure.
    ///
    /// The whole cause chain is searched, not just its first link, so the verdict survives
    /// being re-wrapped on its way out — which is what Go's `errors.Is` gives the reference
    /// implementation, and what makes this a contract a consumer can rely on rather than one
    /// a single [`Error::with_source`] elsewhere would quietly break.
    pub fn of(error: &Error) -> Option<&RecordingSummaryError> {
        error.find_source::<RecordingSummaryError>()
    }

    /// The taxonomy member this reason is reported under.
    ///
    /// Routing refusals are `usage`: the pointer names no read, and nothing was sent.
    /// "Unresolved" is `not_found`: every visible candidate answered 404, so the recording
    /// the pointer names is not where it was looked for.
    ///
    /// A bucket mismatch is `usage`, settled across every port on [card 41] after this port
    /// shipped `not_found` where Python, Ruby, Kotlin and TypeScript shipped `usage` — exit
    /// 2 against exit 1 for the same condition. `not_found` says the recording is not there,
    /// which is false: the read FOUND it, in another bucket, and returned it, so nothing is
    /// absent. What failed is the caller's pointer, which named a bucket the recording is
    /// not in, and that is what `usage` means everywhere else in the taxonomy. The majority
    /// agreed, but the argument is what settles it.
    ///
    /// [card 41]: https://app.basecamp.com/2914079/buckets/48699913/card_tables/cards/10308966794
    ///
    /// Incomplete discovery is `usage`, settled across every port on [card 40] after the
    /// merged ports shipped two different answers — this one said `api_error`, Kotlin said
    /// `usage`, and a caller got exit 7 from one SDK and exit 1 from another for the same
    /// condition. `usage` is one of only THREE coarse codes no HTTP response can produce:
    /// [`ErrorCode::from_status`] yields `auth_required`, `forbidden`, `not_found`,
    /// `rate_limit`, `validation`, `limit_exceeded` and `api_error`, and `network` and
    /// `ambiguous` are equally unreachable from a status. `usage` is the one of those three
    /// that also describes a call the SDK declined to complete, which is why it and not the
    /// other two. A verdict the composite reached on its own therefore can never be read
    /// back as a constituent read's own answer. It is explicitly not `not_found`, because nothing left unsearched
    /// may be reported absent. Retryability is a separate field and is unchanged: the
    /// [`Error`] built from this carries `retryable = false`, because both reasons are
    /// deterministic for the same account state and a retry loop would re-run the identical
    /// search forever.
    ///
    /// [card 40]: https://app.basecamp.com/2914079/buckets/48699913/card_tables/cards/10308122086
    fn code(&self) -> ErrorCode {
        match self {
            RecordingSummaryError::NoRecordingType { .. }
            | RecordingSummaryError::UnknownRecordingType { .. }
            | RecordingSummaryError::BucketMismatch { .. }
            | RecordingSummaryError::CampfireDiscoveryIncomplete { .. } => ErrorCode::Usage,
            RecordingSummaryError::Unresolved(_) => ErrorCode::NotFound,
        }
    }
}

impl From<RecordingSummaryError> for Error {
    fn from(reason: RecordingSummaryError) -> Error {
        Error::new(reason.code(), reason.to_string()).with_source(reason)
    }
}

/// Which typed read serves a [`RecordingRef`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Kind {
    Comment,
    Message,
    Todo,
    Card,
    ChatLine,
    Document,
    Upload,
    ScheduleEntry,
    Question,
    QuestionAnswer,
    Todolist,
    Vault,
    Forward,
    ClientApproval,
    ClientCorrespondence,
    GoogleDocument,
    CloudFile,
    CardStep,
    Questionnaire,
    Schedule,
    Todoset,
    MessageBoard,
    CardTable,
    CardColumn,
    Inbox,
    Campfire,
}

/// The subject of an account event feed type — everything before its final `.` — and the
/// read it names. This is the feed's catalog (bc3 `Event::EventType`) minus `boost`, which
/// names no recording type and is refused explicitly rather than left to fall through as
/// unknown.
const EVENT_SUBJECTS: &[(&str, Kind)] = &[
    ("comment", Kind::Comment),
    ("message", Kind::Message),
    ("todo", Kind::Todo),
    ("card", Kind::Card),
    ("chat.line", Kind::ChatLine),
];

/// BC3's recording type strings and the read each names. It is the routing contract, and it
/// is a DELIBERATE set, not an exhaustive one: the recording types the account event feed's
/// trigger matrix names (comment, message, to-do, card, chat line), plus the content and
/// tool recordings a consumer reasoning about those is likely to hold an id for. Chat lines
/// are matched by prefix (`Chat::Lines::Text`, `::RichText`, `::Code`, `::Upload`,
/// `::Integration` all read through the same route); everything else exactly.
///
/// A type outside this set is [`RecordingSummaryError::UnknownRecordingType`] by design,
/// whether or not the SDK has an id-only read for it — `Timesheet::Entry` and
/// `Gauge::Needle` do, and are not routed; `Client::Reply` and `Forward::Reply` cannot be,
/// since their reads need a parent id the pointer does not carry. Widening the set is a
/// product decision, not a gap: add the type here, its projection in `read_summary`, a
/// routing row in this module's tests, and a case in
/// `conformance/tests/recording_summary.json`, the fixture every SDK implements.
const RECORDING_TYPES: &[(&str, Kind)] = &[
    ("Chat::Transcript", Kind::Campfire),
    ("Client::Approval", Kind::ClientApproval),
    ("Client::Correspondence", Kind::ClientCorrespondence),
    ("CloudFile", Kind::CloudFile),
    ("Comment", Kind::Comment),
    ("Document", Kind::Document),
    ("GoogleDocument", Kind::GoogleDocument),
    ("Inbox", Kind::Inbox),
    ("Inbox::Forward", Kind::Forward),
    ("Kanban::Board", Kind::CardTable),
    ("Kanban::Card", Kind::Card),
    ("Kanban::Column", Kind::CardColumn),
    ("Kanban::Step", Kind::CardStep),
    ("Message", Kind::Message),
    ("Message::Board", Kind::MessageBoard),
    ("Question", Kind::Question),
    ("Question::Answer", Kind::QuestionAnswer),
    ("Questionnaire", Kind::Questionnaire),
    ("Schedule", Kind::Schedule),
    ("Schedule::Entry", Kind::ScheduleEntry),
    ("Todo", Kind::Todo),
    ("Todolist", Kind::Todolist),
    ("Todoset", Kind::Todoset),
    ("Upload", Kind::Upload),
    ("Vault", Kind::Vault),
];

/// Every `Chat::Lines::*` subtype reads through the one Campfire-line route.
const CHAT_LINE_TYPE_PREFIX: &str = "Chat::Lines::";

/// The recording types [`RecordingsService::summarize`] routes by
/// [`RecordingRef::recording_type`], sorted, with the `Chat::Lines` subtypes represented by
/// their shared prefix (`Chat::Lines::*`). The set is deliberate rather than exhaustive —
/// see the routing table — and any other type is
/// [`RecordingSummaryError::UnknownRecordingType`] by design.
pub fn summarizable_recording_types() -> Vec<String> {
    let mut types: Vec<String> = RECORDING_TYPES
        .iter()
        .map(|(name, _)| (*name).to_string())
        .chain(std::iter::once(format!("{CHAT_LINE_TYPE_PREFIX}*")))
        .collect();
    types.sort();
    types
}

/// The account event feed subjects [`RecordingsService::summarize`] routes by
/// [`RecordingRef::event_type`] — an event type is `<subject>.<action>`, and any action on a
/// listed subject routes to that subject's read — sorted. `boost` is absent on purpose:
/// [`RecordingSummaryError::NoRecordingType`].
pub fn summarizable_event_types() -> Vec<String> {
    let mut subjects: Vec<String> = EVENT_SUBJECTS
        .iter()
        .map(|(subject, _)| format!("{subject}.*"))
        .collect();
    subjects.sort();
    subjects
}

/// Whether a chat line subtype carries rich text — the two that declare
/// `rich_text_attribute :content` in BC3, and so the only two whose content can hold a
/// mention. A `Text` line's content is HTML-escaped on the way out
/// (`content_helper.rb`, `format_chat_line_with`), a `Code` line's is served verbatim — a
/// snippet that happens to contain a `bc-attachment` tag — and an `Upload` line has no
/// content.
fn chat_line_is_rich_text(line_type: &str) -> bool {
    matches!(
        line_type,
        "Chat::Lines::RichText" | "Chat::Lines::Integration"
    )
}

/// Picks the read for a pointer. The recording type wins when set.
fn route(reference: &RecordingRef) -> Result<Kind, RecordingSummaryError> {
    let unknown = || RecordingSummaryError::UnknownRecordingType {
        key: reference.key().to_string(),
    };
    let recording_type = reference
        .recording_type
        .as_deref()
        .unwrap_or_default()
        .trim();
    if !recording_type.is_empty() {
        if recording_type.starts_with(CHAT_LINE_TYPE_PREFIX) {
            return Ok(Kind::ChatLine);
        }
        return lookup(RECORDING_TYPES, recording_type).ok_or_else(unknown);
    }
    let event_type = reference.event_type.as_deref().unwrap_or_default().trim();
    // A feed type is "<subject>.<action>"; the subject names the recording type. A string
    // with no action is not a feed type and is not routed.
    let Some(index) = event_type.rfind('.') else {
        return Err(unknown());
    };
    if index == 0 || index == event_type.len() - 1 {
        return Err(unknown());
    }
    let subject = &event_type[..index];
    if subject == "boost" {
        return Err(RecordingSummaryError::NoRecordingType {
            key: event_type.to_string(),
        });
    }
    lookup(EVENT_SUBJECTS, subject).ok_or_else(unknown)
}

fn lookup(table: &[(&str, Kind)], key: &str) -> Option<Kind> {
    table
        .iter()
        .find(|(name, _)| *name == key)
        .map(|(_, kind)| *kind)
}

impl RecordingsService<'_> {
    /// Resolves a recording pointer into a [`RecordingSummary`] through the typed read its
    /// type names. See [`RecordingRef`] for the routing inputs and the module docs for the
    /// design.
    ///
    /// # Errors
    ///
    /// A routing failure before any request; the read's own [`Error`] otherwise — a 404 is
    /// `not_found`, as from the typed read itself; for chat lines,
    /// [`RecordingSummaryError::Unresolved`] when every visible Campfire answered 404, which
    /// is distinct from a read that failed (any non-404 from a candidate is returned as that
    /// error, and the loop stops there) and from
    /// [`RecordingSummaryError::CampfireDiscoveryIncomplete`] (candidates were left
    /// unsearched); [`RecordingSummaryError::BucketMismatch`] when the read returned a
    /// recording from another bucket. Match the composite's own reasons with
    /// [`RecordingSummaryError::of`].
    pub async fn summarize(&self, reference: &RecordingRef) -> Result<RecordingSummary, Error> {
        if reference.bucket_id <= 0 || reference.recording_id <= 0 {
            return Err(Error::usage("bucket id and recording id are required"));
        }
        let kind = route(reference)?;
        let summary = read_summary(self.client(), reference, kind).await?;
        if let Some(bucket) = &summary.bucket
            && bucket.id != 0
            && bucket.id != reference.bucket_id
        {
            return Err(RecordingSummaryError::BucketMismatch {
                bucket_id: reference.bucket_id,
                recording_id: reference.recording_id,
                found_in: bucket.id,
            }
            .into());
        }
        Ok(summary)
    }
}

/// The recording fields every read shares, plus what its own shape adds, before the
/// mentions are read off the content.
struct Projected {
    id: i64,
    status: String,
    r#type: String,
    title: String,
    app_url: String,
    updated_at: DateTime,
    parent: Option<RecordingParent>,
    bucket: Option<RecordingBucket>,
    creator: Option<Person>,
    assignees: Vec<Person>,
    content: String,
}

impl Projected {
    fn new(
        id: i64,
        status: String,
        r#type: String,
        title: String,
        app_url: String,
        updated_at: DateTime,
    ) -> Projected {
        Projected {
            id,
            status,
            r#type,
            title,
            app_url,
            updated_at,
            parent: None,
            bucket: None,
            creator: None,
            assignees: Vec::new(),
            content: String::new(),
        }
    }

    fn parent(mut self, parent: RecordingParent) -> Projected {
        self.parent = Some(parent);
        self
    }

    fn maybe_parent(mut self, parent: Option<RecordingParent>) -> Projected {
        self.parent = parent;
        self
    }

    fn todo_parent(self, parent: TodoParent) -> Projected {
        self.parent(RecordingParent {
            id: parent.id,
            title: parent.title,
            r#type: parent.r#type,
            url: parent.url,
            app_url: parent.app_url,
            bucket: None,
        })
    }

    fn bucket(mut self, bucket: RecordingBucket) -> Projected {
        self.bucket = Some(bucket);
        self
    }

    fn todo_bucket(self, bucket: TodoBucket) -> Projected {
        self.bucket(RecordingBucket {
            id: bucket.id,
            name: bucket.name,
            r#type: bucket.r#type,
        })
    }

    fn creator(mut self, creator: Person) -> Projected {
        self.creator = Some(creator);
        self
    }

    fn assignees(mut self, assignees: Option<Vec<Person>>) -> Projected {
        self.assignees = assignees.unwrap_or_default();
        self
    }

    fn content(mut self, content: impl Into<String>) -> Projected {
        self.content = content.into();
        self
    }
}

impl From<Projected> for RecordingSummary {
    fn from(projected: Projected) -> RecordingSummary {
        RecordingSummary {
            id: projected.id,
            status: projected.status,
            r#type: projected.r#type,
            title: projected.title,
            app_url: projected.app_url,
            parent: projected.parent,
            bucket: projected.bucket,
            creator: projected.creator,
            assignees: projected.assignees,
            mentioned_person_ids: mentioned_person_ids(&projected.content),
            content: projected.content,
            updated_at: projected.updated_at,
            campfire_id: None,
        }
    }
}

/// The first of the candidates that is not empty, as a `String`.
fn first_non_empty(primary: String, fallback: Option<String>) -> String {
    if primary.is_empty() {
        fallback.unwrap_or_default()
    } else {
        primary
    }
}

/// Performs the one typed read a kind names and projects it.
///
/// One arm per routed type, spelled out: the projection differs per shape — which member
/// carries the rich text, which carries the human title — and a table could not say that.
#[allow(clippy::too_many_lines)]
async fn read_summary(
    account: &AccountClient,
    reference: &RecordingRef,
    kind: Kind,
) -> Result<RecordingSummary, Error> {
    let id = reference.recording_id;
    let projected = match kind {
        Kind::Comment => {
            let comment = account.comments().get(id).await?;
            Projected::new(
                comment.id,
                comment.status,
                comment.r#type,
                comment.title,
                comment.app_url,
                comment.updated_at,
            )
            .parent(comment.parent)
            .todo_bucket(comment.bucket)
            .creator(comment.creator)
            .content(comment.content)
        }
        Kind::Message => {
            let message = account.messages().get(id).await?;
            Projected::new(
                message.id,
                message.status,
                message.r#type,
                first_non_empty(message.title, Some(message.subject)),
                message.app_url,
                message.updated_at,
            )
            .parent(message.parent)
            .todo_bucket(message.bucket)
            .creator(message.creator)
            .content(message.content)
        }
        Kind::Todo => {
            // A to-do's `content` is its plain title; the rich text — where mentions live —
            // is the description.
            let todo = account.todos().get(id).await?;
            Projected::new(
                todo.id,
                todo.status,
                todo.r#type,
                first_non_empty(todo.title, Some(todo.content)),
                todo.app_url,
                todo.updated_at,
            )
            .todo_parent(todo.parent)
            .todo_bucket(todo.bucket)
            .creator(todo.creator)
            .assignees(todo.assignees)
            .content(todo.description.unwrap_or_default())
        }
        Kind::Card => {
            let card = account.cards().get(id).await?;
            Projected::new(
                card.id,
                card.status,
                card.r#type,
                card.title,
                card.app_url,
                card.updated_at,
            )
            .parent(card.parent)
            .todo_bucket(card.bucket)
            .creator(card.creator)
            .assignees(card.assignees)
            .content(first_non_empty(
                card.content.unwrap_or_default(),
                card.description,
            ))
        }
        Kind::ChatLine => return chat_line_summary(account, reference).await,
        Kind::Document => {
            let document = account.documents().get(id).await?;
            Projected::new(
                document.id,
                document.status,
                document.r#type,
                document.title,
                document.app_url,
                document.updated_at,
            )
            .parent(document.parent)
            .todo_bucket(document.bucket)
            .creator(document.creator)
            .content(document.content.unwrap_or_default())
        }
        Kind::Upload => {
            let upload = account.uploads().get(id).await?;
            Projected::new(
                upload.id,
                upload.status,
                upload.r#type,
                first_non_empty(upload.title, upload.filename),
                upload.app_url,
                upload.updated_at,
            )
            .parent(upload.parent)
            .todo_bucket(upload.bucket)
            .creator(upload.creator)
            .content(upload.description.unwrap_or_default())
        }
        Kind::ScheduleEntry => {
            let entry = account.schedules().get_entry(id).await?;
            Projected::new(
                entry.id,
                entry.status,
                entry.r#type,
                first_non_empty(entry.title, Some(entry.summary)),
                entry.app_url,
                entry.updated_at,
            )
            .parent(entry.parent)
            .todo_bucket(entry.bucket)
            .creator(entry.creator)
            .content(entry.description.unwrap_or_default())
        }
        Kind::Question => {
            let question = account.checkins().get_question(id).await?;
            Projected::new(
                question.id,
                question.status,
                question.r#type,
                question.title,
                question.app_url,
                question.updated_at,
            )
            .parent(question.parent)
            .bucket(question.bucket)
            .creator(question.creator)
        }
        Kind::QuestionAnswer => {
            let answer = account.checkins().get_answer(id).await?;
            Projected::new(
                answer.id,
                answer.status,
                answer.r#type,
                answer.title,
                answer.app_url,
                answer.updated_at,
            )
            .parent(answer.parent)
            .bucket(answer.bucket)
            .creator(answer.creator)
            .content(answer.content)
        }
        Kind::Todolist => {
            let todolist = account.todolists().get(id).await?;
            Projected::new(
                todolist.id,
                todolist.status,
                todolist.r#type,
                first_non_empty(todolist.title, Some(todolist.name)),
                todolist.app_url,
                todolist.updated_at,
            )
            .todo_parent(todolist.parent)
            .todo_bucket(todolist.bucket)
            .creator(todolist.creator)
            .content(todolist.description)
        }
        Kind::Vault => {
            let vault = account.vaults().get(id).await?;
            Projected::new(
                vault.id,
                vault.status,
                vault.r#type,
                vault.title,
                vault.app_url,
                vault.updated_at,
            )
            .maybe_parent(vault.parent)
            .todo_bucket(vault.bucket)
            .creator(vault.creator)
        }
        Kind::Forward => {
            let forward = account.forwards().get(id).await?;
            Projected::new(
                forward.id,
                forward.status,
                forward.r#type,
                first_non_empty(forward.title, Some(forward.subject)),
                forward.app_url,
                forward.updated_at,
            )
            .parent(forward.parent)
            .todo_bucket(forward.bucket)
            .creator(forward.creator)
            .content(forward.content.unwrap_or_default())
        }
        Kind::ClientApproval => {
            let approval = account.client_approvals().get(id).await?;
            Projected::new(
                approval.id,
                approval.status,
                approval.r#type,
                first_non_empty(approval.title, approval.subject),
                approval.app_url,
                approval.updated_at,
            )
            .parent(approval.parent)
            .bucket(approval.bucket)
            .creator(approval.creator)
            .content(approval.content.unwrap_or_default())
        }
        Kind::ClientCorrespondence => {
            let correspondence = account.client_correspondences().get(id).await?;
            Projected::new(
                correspondence.id,
                correspondence.status,
                correspondence.r#type,
                first_non_empty(correspondence.title, Some(correspondence.subject)),
                correspondence.app_url,
                correspondence.updated_at,
            )
            .parent(correspondence.parent)
            .bucket(correspondence.bucket)
            .creator(correspondence.creator)
            .content(correspondence.content.unwrap_or_default())
        }
        Kind::GoogleDocument => {
            let document = account.google_documents().google_document(id).await?;
            Projected::new(
                document.id,
                document.status,
                document.r#type,
                document.title,
                document.app_url,
                document.updated_at,
            )
            .parent(document.parent)
            .todo_bucket(document.bucket)
            .creator(document.creator)
            .content(document.description.unwrap_or_default())
        }
        Kind::CloudFile => {
            let file = account.cloud_files().cloud_file(id).await?;
            Projected::new(
                file.id,
                file.status,
                file.r#type,
                file.title,
                file.app_url,
                file.updated_at,
            )
            .parent(file.parent)
            .todo_bucket(file.bucket)
            .creator(file.creator)
            .content(file.description.unwrap_or_default())
        }
        Kind::CardStep => {
            let step = account.card_steps().get(id).await?;
            Projected::new(
                step.id,
                step.status,
                step.r#type,
                step.title,
                step.app_url,
                step.updated_at,
            )
            .parent(step.parent)
            .todo_bucket(step.bucket)
            .creator(step.creator)
            .assignees(step.assignees)
        }
        Kind::Questionnaire => {
            let questionnaire = account.checkins().get_questionnaire(id).await?;
            Projected::new(
                questionnaire.id,
                questionnaire.status,
                questionnaire.r#type,
                first_non_empty(questionnaire.title, Some(questionnaire.name)),
                questionnaire.app_url,
                questionnaire.updated_at,
            )
            .bucket(questionnaire.bucket)
            .creator(questionnaire.creator)
        }
        Kind::Schedule => {
            let schedule = account.schedules().get(id).await?;
            Projected::new(
                schedule.id,
                schedule.status,
                schedule.r#type,
                schedule.title,
                schedule.app_url,
                schedule.updated_at,
            )
            .todo_bucket(schedule.bucket)
            .creator(schedule.creator)
        }
        Kind::Todoset => {
            let todoset = account.todosets().get(id).await?;
            Projected::new(
                todoset.id,
                todoset.status,
                todoset.r#type,
                first_non_empty(todoset.title, Some(todoset.name)),
                todoset.app_url,
                todoset.updated_at,
            )
            .todo_bucket(todoset.bucket)
            .creator(todoset.creator)
        }
        Kind::MessageBoard => {
            let board = account.message_boards().get(id).await?;
            Projected::new(
                board.id,
                board.status,
                board.r#type,
                board.title,
                board.app_url,
                board.updated_at,
            )
            .todo_bucket(board.bucket)
            .creator(board.creator)
        }
        Kind::CardTable => {
            let table = account.card_tables().get(id).await?;
            Projected::new(
                table.id,
                table.status,
                table.r#type,
                table.title,
                table.app_url,
                table.updated_at,
            )
            .todo_bucket(table.bucket)
            .creator(table.creator)
        }
        Kind::CardColumn => {
            let column = account.card_columns().get(id).await?;
            Projected::new(
                column.id,
                column.status,
                column.r#type,
                column.title,
                column.app_url,
                column.updated_at,
            )
            .parent(column.parent)
            .todo_bucket(column.bucket)
            .creator(column.creator)
            .content(column.description.unwrap_or_default())
        }
        Kind::Inbox => {
            let inbox = account.forwards().get_inbox(id).await?;
            Projected::new(
                inbox.id,
                inbox.status,
                inbox.r#type,
                inbox.title,
                inbox.app_url,
                inbox.updated_at,
            )
            .todo_bucket(inbox.bucket)
            .creator(inbox.creator)
        }
        Kind::Campfire => {
            let campfire = account.campfires().get(id).await?;
            Projected::new(
                campfire.id,
                campfire.status,
                campfire.r#type,
                campfire.title,
                campfire.app_url,
                campfire.updated_at,
            )
            .todo_bucket(campfire.bucket)
            .creator(campfire.creator)
        }
    };
    Ok(projected.into())
}

/// Discovers the Campfire a chat line lives in, reads the line under it, and projects it.
async fn chat_line_summary(
    account: &AccountClient,
    reference: &RecordingRef,
) -> Result<RecordingSummary, Error> {
    let (line, campfire_id) =
        resolve_chat_line(account, reference.bucket_id, reference.recording_id).await?;
    let rich_text = chat_line_is_rich_text(&line.r#type);
    let mut summary: RecordingSummary = Projected::new(
        line.id,
        line.status,
        line.r#type,
        line.title,
        line.app_url,
        line.updated_at,
    )
    .parent(line.parent)
    .todo_bucket(line.bucket)
    .creator(line.creator)
    .content(line.content.unwrap_or_default())
    .into();
    if !rich_text {
        // A plain-text or code line's content is text BC3 never read as markup, so a
        // literal "<bc-attachment>" in it mentions nobody.
        summary.mentioned_person_ids.clear();
    }
    summary.campfire_id = Some(campfire_id);
    Ok(summary)
}

/// One `summarize` call's discovery state: which candidates have answered "not here", how
/// many more this call may try, and whether any were left untried for want of budget.
struct ChatLineSearch<'a> {
    account: &'a AccountClient,
    line_id: i64,
    tried: Vec<i64>,
    budget: usize,
    skipped: bool,
}

impl ChatLineSearch<'_> {
    /// Reads the line under each candidate not yet tried. Answers the line and its Campfire
    /// on a hit; on a miss it answers `None` with no error and records the candidates in
    /// `tried`. Any answer but 404 is returned as is.
    async fn try_candidates(
        &mut self,
        candidates: &[i64],
    ) -> Result<Option<(CampfireLine, i64)>, Error> {
        for campfire_id in candidates {
            if self.tried.contains(campfire_id) {
                continue;
            }
            if self.budget == 0 {
                self.skipped = true;
                return Ok(None);
            }
            self.budget -= 1;
            match self
                .account
                .campfires()
                .get_line(*campfire_id, self.line_id)
                .await
            {
                Ok(line) => return Ok(Some((line, *campfire_id))),
                Err(error) if error.code() == ErrorCode::NotFound => self.tried.push(*campfire_id),
                Err(error) => return Err(error),
            }
        }
        Ok(None)
    }
}

/// Finds the Campfire a line lives in and reads it.
///
/// Two failure shapes are kept apart on purpose. A candidate that answers anything but 404 —
/// 401, 403, 5xx, a transport error — stops the loop and is returned as that error: the read
/// failed, and trying the next Campfire would only hide it. A 404 means "not here", so the
/// loop moves on. Only when every candidate said "not here" is the line
/// [`RecordingSummaryError::Unresolved`] — and before concluding that, the cached sources
/// are refreshed (subject to a floor) so a Campfire created after the cache filled is tried
/// too. Discovery that could not be completed — a listing cut off at its cap, a bucket with
/// more candidates than the budget — is
/// [`RecordingSummaryError::CampfireDiscoveryIncomplete`], never "unresolved": nothing
/// unsearched is ever reported absent.
///
/// What HTTP cannot tell apart: BC3 answers 404 both for a line that is not in a Campfire
/// and for a Campfire the caller may no longer see. "Unresolved" therefore means "under no
/// Campfire the caller can currently see", and the error reports the cached candidates that
/// the refreshed sources no longer list
/// ([`UnresolvedRecording::stale_campfire_ids`]) so a consumer can see when visibility, not
/// existence, is what changed.
async fn resolve_chat_line(
    account: &AccountClient,
    bucket_id: i64,
    line_id: i64,
) -> Result<(CampfireLine, i64), Error> {
    let index = account.campfire_index();
    let mut search = ChatLineSearch {
        account,
        line_id,
        tried: Vec::new(),
        budget: MAX_CAMPFIRE_CANDIDATES,
        skipped: false,
    };
    let incomplete = |reason: String| -> Error {
        RecordingSummaryError::CampfireDiscoveryIncomplete {
            bucket_id,
            recording_id: line_id,
            reason,
        }
        .into()
    };
    let over_budget =
        || format!("more than {MAX_CAMPFIRE_CANDIDATES} visible campfires in the bucket");

    // Pass 1: what the sources already hold — the dock (read if it must be), then the
    // listing only if it is cached. A listing fetch is the expensive, slow request, and it
    // is not made until the dock — including its refresh — has had its say, so a listing
    // that is down, over its cap, or stalled on a deadline never stands between a project's
    // line and the one project read that finds it.
    let mut dock = index.dock_campfires(account, bucket_id, false).await?;
    if let Some(found) = search.try_candidates(&dock.ids).await? {
        return Ok(found);
    }
    let listed = index.cached_listed_campfires(account.account_id(), bucket_id);
    if let Some(listed) = &listed
        && let Some(found) = search.try_candidates(&listed.ids).await?
    {
        return Ok(found);
    }

    // Pass 2: re-read the dock if it was served from cache (the floor may decline), then
    // fetch or refresh the listing. Whatever comes back is the current snapshot of that
    // source, whoever loaded it — another caller may have populated or refreshed it in the
    // meantime — so it always replaces the pass-1 one; "refreshed" is whether a source the
    // conclusion had consulted is now newer than when it was consulted.
    //
    // A spent budget stops both, and the two sources part company on WHY. A source already
    // consulted cannot hand this call a candidate it may try, so its refresh is skipped and
    // the conclusion stands on what was seen (`refreshed` stays false) — paying for that
    // request would buy nothing, and a failure on it would replace a settled `incomplete`
    // with a transient error a consumer retries forever. A source NEVER consulted is a
    // different verdict: candidates may exist there unsearched, so running out of budget
    // before reaching it makes the answer incomplete rather than unresolved.
    //
    // `skipped` alone cannot express that, which is what the earlier shape got wrong. It is
    // set only when a candidate is OBSERVED and cannot be tried, so a source holding
    // exactly `MAX_CAMPFIRE_CANDIDATES` candidates that all answer 404 leaves the budget at
    // zero with `skipped` still false. And concluding `incomplete` on a spent budget alone
    // is equally wrong in the other direction: a bucket whose candidates were all searched
    // is not incomplete. The budget gates the re-reads; `skipped` still decides the final
    // verdict.
    let mut refreshed = false;
    if search.budget > 0 && dock.cached {
        let again = index.dock_campfires(account, bucket_id, true).await?;
        if newer_than(&again, &dock) {
            refreshed = true;
        }
        dock = again;
        if let Some(found) = search.try_candidates(&dock.ids).await? {
            return Ok(found);
        }
    }
    // Seeded from what pass 1 consulted, not empty: on the budget-spent path the listing is
    // not re-read, and the stale filter below has to compare against the snapshot the
    // conclusion actually used. Starting empty reports every listing-only candidate as one
    // the caller has LOST VISIBILITY of, when nothing changed.
    let mut listed_ids = listed
        .as_ref()
        .map(|listed| listed.ids.clone())
        .unwrap_or_default();
    if search.budget == 0 {
        if listed.is_none() {
            return Err(incomplete(format!(
                "the candidate budget of {MAX_CAMPFIRE_CANDIDATES} was spent before the account listing was consulted"
            )));
        }
    } else {
        let again = index
            .listed_campfires(account, bucket_id, listed.is_some())
            .await
            .map_err(|error| {
                if is_listing_overflow(&error) {
                    incomplete(error.to_string())
                } else {
                    error
                }
            })?;
        if let Some(previous) = &listed
            && newer_than(&again, previous)
        {
            refreshed = true;
        }
        listed_ids = again.ids;
        if let Some(found) = search.try_candidates(&listed_ids).await? {
            return Ok(found);
        }
    }
    if search.skipped {
        return Err(incomplete(over_budget()));
    }

    let mut unresolved = UnresolvedRecording {
        bucket_id,
        recording_id: line_id,
        campfire_ids: search.tried.clone(),
        refreshed,
        stale_campfire_ids: Vec::new(),
    };
    if refreshed {
        unresolved.stale_campfire_ids = search
            .tried
            .iter()
            .copied()
            .filter(|id| !dock.ids.contains(id) && !listed_ids.contains(id))
            .collect();
    }
    Err(RecordingSummaryError::Unresolved(unresolved).into())
}

/// Whether a re-read of a source answered something newer than the snapshot the conclusion
/// had already consulted — a fresh load, or a snapshot another caller published in the
/// meantime.
fn newer_than(again: &SourceRead, previous: &SourceRead) -> bool {
    !again.cached || again.fetched > previous.fetched
}

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

    fn kind_of(reference: &RecordingRef) -> Result<Kind, RecordingSummaryError> {
        route(reference)
    }

    #[test]
    fn a_server_sent_empty_assignee_list_serializes_as_an_empty_list() {
        let summary = RecordingSummary::default();
        assert!(summary.assignees.is_empty());
        let json = serde_json::to_value(&summary).unwrap();
        assert_eq!(json["assignees"], serde_json::json!([]), "{json}");
    }

    #[test]
    fn a_recording_type_routes_and_wins_over_the_event_type() {
        let reference = RecordingRef {
            bucket_id: 1,
            recording_id: 2,
            event_type: Some("comment.created".to_string()),
            recording_type: Some("Kanban::Card".to_string()),
        };
        assert_eq!(kind_of(&reference), Ok(Kind::Card));
    }

    #[test]
    fn every_chat_line_subtype_reads_through_the_one_route() {
        for subtype in [
            "Chat::Lines::Text",
            "Chat::Lines::RichText",
            "Chat::Lines::Code",
            "Chat::Lines::Upload",
            "Chat::Lines::Integration",
        ] {
            assert_eq!(
                kind_of(&RecordingRef::from_recording_type(1, 2, subtype)),
                Ok(Kind::ChatLine),
                "{subtype}"
            );
        }
    }

    #[test]
    fn every_feed_subject_routes_whatever_the_action_is() {
        for event in [
            "comment.created",
            "message.updated",
            "todo.assignment_changed",
            "card.moved",
            "chat.line.created",
        ] {
            assert!(
                kind_of(&RecordingRef::from_event(1, 2, event)).is_ok(),
                "{event}"
            );
        }
    }

    #[test]
    fn boost_is_refused_as_naming_no_recording_type() {
        let refused = kind_of(&RecordingRef::from_event(1, 2, "boost.created"));
        assert!(matches!(
            refused,
            Err(RecordingSummaryError::NoRecordingType { .. })
        ));
        let error: Error = refused.unwrap_err().into();
        assert_eq!(error.code(), ErrorCode::Usage);
        assert!(error.to_string().contains("names no recording type"));
        assert!(matches!(
            RecordingSummaryError::of(&error),
            Some(RecordingSummaryError::NoRecordingType { .. })
        ));
    }

    #[test]
    fn an_unrouted_type_or_a_malformed_event_type_is_unknown() {
        for reference in [
            RecordingRef::from_recording_type(1, 2, "Timesheet::Entry"),
            RecordingRef::from_recording_type(1, 2, "Client::Reply"),
            RecordingRef::from_event(1, 2, "comment"),
            RecordingRef::from_event(1, 2, "comment."),
            RecordingRef::from_event(1, 2, ".created"),
            RecordingRef::from_event(1, 2, "unheard_of.created"),
            RecordingRef::default(),
        ] {
            assert!(
                matches!(
                    kind_of(&reference),
                    Err(RecordingSummaryError::UnknownRecordingType { .. })
                ),
                "{reference:?}"
            );
        }
    }

    #[test]
    fn the_documented_sets_are_the_routed_ones() {
        let types = summarizable_recording_types();
        assert_eq!(types.len(), RECORDING_TYPES.len() + 1);
        assert!(types.contains(&"Chat::Lines::*".to_string()));
        assert!(types.windows(2).all(|pair| pair[0] <= pair[1]), "sorted");
        for (name, _) in RECORDING_TYPES {
            assert!(types.contains(&(*name).to_string()), "{name}");
            assert!(
                kind_of(&RecordingRef::from_recording_type(1, 2, *name)).is_ok(),
                "{name}"
            );
        }
        let events = summarizable_event_types();
        assert_eq!(events.len(), EVENT_SUBJECTS.len());
        assert!(!events.iter().any(|subject| subject.starts_with("boost")));
        assert!(events.windows(2).all(|pair| pair[0] <= pair[1]), "sorted");
    }

    #[test]
    fn only_the_two_rich_text_line_subtypes_can_carry_a_mention() {
        assert!(chat_line_is_rich_text("Chat::Lines::RichText"));
        assert!(chat_line_is_rich_text("Chat::Lines::Integration"));
        assert!(!chat_line_is_rich_text("Chat::Lines::Text"));
        assert!(!chat_line_is_rich_text("Chat::Lines::Code"));
        assert!(!chat_line_is_rich_text("Chat::Lines::Upload"));
    }

    #[test]
    fn the_composites_own_reasons_carry_their_taxonomy_member() {
        let unresolved: Error = RecordingSummaryError::Unresolved(UnresolvedRecording {
            bucket_id: 1,
            recording_id: 2,
            campfire_ids: vec![3, 4],
            refreshed: true,
            stale_campfire_ids: vec![3],
        })
        .into();
        assert_eq!(unresolved.code(), ErrorCode::NotFound);
        assert!(
            unresolved
                .to_string()
                .contains("found under no visible campfire")
        );

        let incomplete: Error = RecordingSummaryError::CampfireDiscoveryIncomplete {
            bucket_id: 1,
            recording_id: 2,
            reason: "too many".to_string(),
        }
        .into();
        // `usage`, settled on card 40: the one coarse code no HTTP response can
        // produce, so this verdict can never be read back as a constituent read's
        // own answer. Non-retryable, which is the other half of that decision and
        // the half a code change could otherwise carry away with it.
        assert_eq!(incomplete.code(), ErrorCode::Usage);
        assert!(!incomplete.is_retryable());
        // Never not_found: nothing left unsearched may be reported absent.
        assert_ne!(incomplete.code(), unresolved.code());

        let mismatch: Error = RecordingSummaryError::BucketMismatch {
            bucket_id: 1,
            recording_id: 2,
            found_in: 9,
        }
        .into();
        // `usage`, settled on card 41: the read found the recording, in another
        // bucket, and returned it -- what failed is the caller's pointer.
        assert_eq!(mismatch.code(), ErrorCode::Usage);
        assert!(!mismatch.is_retryable());
        assert!(matches!(
            RecordingSummaryError::of(&mismatch),
            Some(RecordingSummaryError::BucketMismatch { found_in: 9, .. })
        ));
    }

    #[test]
    fn a_plain_read_failure_carries_no_composite_reason() {
        let forbidden = Error::new(ErrorCode::Forbidden, "nope").with_status(403);
        assert!(RecordingSummaryError::of(&forbidden).is_none());
    }

    #[test]
    fn a_verdict_survives_being_wrapped_on_its_way_out() {
        let verdict: Error = RecordingSummaryError::BucketMismatch {
            bucket_id: 1,
            recording_id: 2,
            found_in: 9,
        }
        .into();
        let wrapped = Error::new(ErrorCode::ApiError, "while admitting an event")
            .with_source(std::sync::Arc::new(verdict));
        assert!(matches!(
            RecordingSummaryError::of(&wrapped),
            Some(RecordingSummaryError::BucketMismatch { found_in: 9, .. })
        ));
    }

    #[test]
    fn a_title_falls_back_only_when_the_primary_is_empty() {
        assert_eq!(
            first_non_empty("title".to_string(), Some("subject".to_string())),
            "title"
        );
        assert_eq!(
            first_non_empty(String::new(), Some("subject".to_string())),
            "subject"
        );
        assert_eq!(first_non_empty(String::new(), None), "");
    }
}

/// The refresh path, which needs the floor crossed and so needs a clock a test can move.
/// Everything here turns on entries ageing: the rest of the discovery behaviour is driven
/// end-to-end in `tests/composites_recording_summary.rs`, where real time is enough.
///
/// These drive wiremock and so need the shipped transport, exactly as the OAuth unit tests
/// do. The no-default-features lane builds the crate WITHOUT one — that is the point of the
/// lane — so it skips this module rather than failing in it.
#[cfg(all(test, feature = "reqwest"))]
mod refresh_tests {
    use super::*;
    use crate::Config;
    use crate::client::Client;
    use crate::services::campfire_index::{
        CAMPFIRE_INDEX_MIN_REFRESH, MAX_CAMPFIRE_CANDIDATES, TestClock,
    };
    use serde_json::{Value, json};
    use std::sync::Arc;
    use std::time::Duration;
    use wiremock::matchers::{method, path};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    const BUCKET: i64 = 1_069_479_338;
    const LINE: i64 = 1_069_479_351;
    const CACHED_A: i64 = 1_069_479_340;
    const CACHED_B: i64 = 1_069_479_345;
    const APPEARED: i64 = 1_069_479_399;
    const OTHER_BUCKET: i64 = 2_085_958_500;

    fn campfire(id: i64, bucket: i64) -> Value {
        json!({
            "id": id,
            "status": "active",
            "created_at": "2022-10-28T15:25:00.000Z",
            "updated_at": "2022-10-28T15:25:00.000Z",
            "title": "Campfire",
            "visible_to_clients": false,
            "inherits_status": true,
            "type": "Chat::Transcript",
            "url": "https://3.basecampapi.com/999/buckets/x/chats/x.json",
            "app_url": "https://3.basecamp.com/999/buckets/x/chats/x",
            "bucket": { "id": bucket, "name": "A project", "type": "Project" },
            "creator": { "id": 1, "name": "Victor Cooper" },
        })
    }

    fn project_docking(campfire_ids: &[i64]) -> Value {
        let dock: Vec<Value> = campfire_ids
            .iter()
            .map(|id| {
                json!({ "id": id, "title": "Campfire", "name": "chat", "enabled": true, "url": "u", "app_url": "a" })
            })
            .collect();
        json!({
            "id": BUCKET,
            "status": "active",
            "created_at": "2022-10-28T15:25:00.000Z",
            "updated_at": "2022-10-28T15:25:00.000Z",
            "name": "A project",
            "url": "https://3.basecampapi.com/999/projects/x.json",
            "app_url": "https://3.basecamp.com/999/projects/x",
            "dock": dock,
        })
    }

    async fn mount(server: &MockServer, route: &str, status: u16, body: &Value) {
        Mock::given(method("GET"))
            .and(path(route))
            .respond_with(ResponseTemplate::new(status).set_body_json(body.clone()))
            .mount(server)
            .await;
    }

    /// The dock shows `docked`, the account listing shows `listed`, and every line read
    /// answers 404 — so the search always runs to the end and reports what it tried. The
    /// line routes are mounted one per candidate rather than as a catch-all, which could
    /// shadow the dock or the listing and decide the test by mount order.
    async fn scripted(server: &MockServer, docked: &[i64], listed: &[i64]) {
        server.reset().await;
        mount(
            server,
            &format!("/999/projects/{BUCKET}"),
            200,
            &project_docking(docked),
        )
        .await;
        let listing: Vec<Value> = listed.iter().map(|id| campfire(*id, BUCKET)).collect();
        mount(server, "/999/chats.json", 200, &json!(listing)).await;
        for id in docked.iter().chain(listed) {
            mount(
                server,
                &format!("/999/chats/{id}/lines/{LINE}"),
                404,
                &json!({ "error": "Record not found" }),
            )
            .await;
        }
    }

    async fn paths(server: &MockServer) -> Vec<String> {
        server
            .received_requests()
            .await
            .unwrap_or_default()
            .iter()
            .map(|request| request.url.path().to_string())
            .collect()
    }

    fn account_on(server: &MockServer, clock: &Arc<TestClock>) -> AccountClient {
        Client::builder(
            Config::default()
                .with_base_url(server.uri())
                .with_timeout(Duration::from_secs(86_400)),
        )
        .access_token("test-token")
        .campfire_clock(clock.clock())
        .build()
        .expect("client")
        .for_account("999")
    }

    fn unresolved_from(error: &Error) -> UnresolvedRecording {
        match RecordingSummaryError::of(error) {
            Some(RecordingSummaryError::Unresolved(unresolved)) => unresolved.clone(),
            _ => panic!("expected an unresolved line, got {error}"),
        }
    }

    /// Past the floor a miss re-reads both sources, and the conclusion says so: `refreshed`
    /// is set, and the Campfires the cache had that neither source lists any more are named
    /// as stale. That is the only signal a consumer has that the line went missing because
    /// visibility changed rather than because it never existed — a 404 per candidate cannot
    /// tell the two apart. Mirrors Go's `TestSummarize_ChatLineDiscovery`.
    #[tokio::test]
    async fn a_refreshed_miss_names_the_campfires_the_caller_can_no_longer_see() {
        let clock = TestClock::new();
        let server = MockServer::start().await;
        scripted(&server, &[CACHED_A], &[CACHED_A, CACHED_B]).await;
        let account = account_on(&server, &clock);
        let reference = RecordingRef::from_event(BUCKET, LINE, "chat.line.created");

        // First call: both sources are read during the call, so nothing was refreshed.
        let first = unresolved_from(
            &account
                .recordings()
                .summarize(&reference)
                .await
                .unwrap_err(),
        );
        assert_eq!(first.campfire_ids, vec![CACHED_A, CACHED_B]);
        assert!(!first.refreshed, "sources read this very call");
        assert!(first.stale_campfire_ids.is_empty());

        // Past the floor, with both sources now showing a Campfire that did not exist when
        // the cache filled and no longer showing either that did.
        clock.advance(CAMPFIRE_INDEX_MIN_REFRESH + Duration::from_secs(1));
        scripted(&server, &[APPEARED], &[APPEARED]).await;

        let second = unresolved_from(
            &account
                .recordings()
                .summarize(&reference)
                .await
                .unwrap_err(),
        );
        assert!(
            second.refreshed,
            "the floor was past and both sources answered newer than the cache"
        );
        assert_eq!(
            second.campfire_ids,
            vec![CACHED_A, CACHED_B, APPEARED],
            "the cached candidates were tried first, then the one the refresh found"
        );
        assert_eq!(
            second.stale_campfire_ids,
            vec![CACHED_A, CACHED_B],
            "both cached Campfires are gone from the dock and the listing"
        );
    }

    /// Refreshed by the LISTING alone. The account listing is cached per account, not per
    /// bucket, so the first bucket looked at pays for it and every other bucket meets it
    /// already warm — while its own dock is read fresh this call and so is never re-read.
    /// That is the one path where the listing refresh is what makes the conclusion a
    /// refreshed one, and the only reason to test it separately is that on every other path
    /// the dock re-read has already said so.
    ///
    /// Written after mutation testing: with the listing branch's `refreshed = true` removed,
    /// every other test here still passed.
    #[tokio::test]
    async fn a_second_bucket_meets_a_warm_listing_and_is_refreshed_by_it_alone() {
        let clock = TestClock::new();
        let server = MockServer::start().await;
        // Neither bucket is a project, so the dock answers "no dock" and the listing is the
        // only source. It shows one Campfire in each bucket.
        for bucket in [BUCKET, OTHER_BUCKET] {
            mount(
                &server,
                &format!("/999/projects/{bucket}"),
                404,
                &json!({ "error": "Record not found" }),
            )
            .await;
        }
        for id in [CACHED_A, CACHED_B] {
            mount(
                &server,
                &format!("/999/chats/{id}/lines/{LINE}"),
                404,
                &json!({ "error": "Record not found" }),
            )
            .await;
        }
        mount(
            &server,
            "/999/chats.json",
            200,
            &json!([campfire(CACHED_A, BUCKET), campfire(CACHED_B, OTHER_BUCKET)]),
        )
        .await;
        let account = account_on(&server, &clock);

        // The first bucket pays for the listing.
        let first = unresolved_from(
            &account
                .recordings()
                .summarize(&RecordingRef::from_event(BUCKET, LINE, "chat.line.created"))
                .await
                .unwrap_err(),
        );
        assert_eq!(first.campfire_ids, vec![CACHED_A]);
        assert!(!first.refreshed, "the listing was read during that call");

        // Past the floor, and the listing no longer shows the second bucket's Campfire.
        clock.advance(CAMPFIRE_INDEX_MIN_REFRESH + Duration::from_secs(1));
        server.reset().await;
        for bucket in [BUCKET, OTHER_BUCKET] {
            mount(
                &server,
                &format!("/999/projects/{bucket}"),
                404,
                &json!({ "error": "Record not found" }),
            )
            .await;
        }
        mount(
            &server,
            &format!("/999/chats/{CACHED_B}/lines/{LINE}"),
            404,
            &json!({ "error": "Record not found" }),
        )
        .await;
        mount(
            &server,
            "/999/chats.json",
            200,
            &json!([campfire(CACHED_A, BUCKET)]),
        )
        .await;

        let second = unresolved_from(
            &account
                .recordings()
                .summarize(&RecordingRef::from_event(
                    OTHER_BUCKET,
                    LINE,
                    "chat.line.created",
                ))
                .await
                .unwrap_err(),
        );
        assert_eq!(second.bucket_id, OTHER_BUCKET);
        assert_eq!(
            second.campfire_ids,
            vec![CACHED_B],
            "the warm listing supplied the candidate"
        );
        assert!(
            second.refreshed,
            "this bucket's dock was read fresh and never re-read, so only the listing \
             refresh can have said the conclusion rests on newer data"
        );
        assert_eq!(
            second.stale_campfire_ids,
            vec![CACHED_B],
            "the refreshed listing no longer shows it: visibility changed"
        );
    }

    /// The stale list is measured against what the conclusion ACTUALLY consulted, and on
    /// this path the listing is never re-read: the dock refresh spends the last of the
    /// budget, so refreshing the listing could hand this call no candidate it may try and
    /// is skipped. The cached listing's Campfires are therefore still visible as far as
    /// this call knows, and naming them stale would tell a consumer visibility changed when
    /// nothing did. That is the whole reason the listing snapshot is carried forward
    /// instead of starting empty.
    ///
    /// Written after mutation testing showed the other two tests here pass with the
    /// snapshot dropped: they take the branch that overwrites it.
    #[tokio::test]
    async fn a_refresh_that_spends_the_budget_measures_stale_against_the_listing_it_read() {
        let clock = TestClock::new();
        let server = MockServer::start().await;
        // Pass 1 costs two candidates, leaving the rest of the budget for the dock refresh.
        let appeared: Vec<i64> = (0..i64::try_from(MAX_CAMPFIRE_CANDIDATES).unwrap() - 2)
            .map(|index| 9_000_000 + index)
            .collect();
        scripted(&server, &[CACHED_A], &[CACHED_A, CACHED_B]).await;
        let account = account_on(&server, &clock);
        let reference = RecordingRef::from_event(BUCKET, LINE, "chat.line.created");
        account
            .recordings()
            .summarize(&reference)
            .await
            .unwrap_err();

        clock.advance(CAMPFIRE_INDEX_MIN_REFRESH + Duration::from_secs(1));
        // The dock now holds exactly the remaining budget, and the listing still holds what
        // pass 1 saw. Only the dock is re-read; the listing snapshot is the cached one.
        scripted(&server, &appeared, &[CACHED_A, CACHED_B]).await;
        let before = paths(&server).await.len();

        let unresolved = unresolved_from(
            &account
                .recordings()
                .summarize(&reference)
                .await
                .unwrap_err(),
        );
        assert!(
            unresolved.refreshed,
            "the dock re-read answered newer than the cache"
        );
        let mut expected = vec![CACHED_A, CACHED_B];
        expected.extend(&appeared);
        assert_eq!(unresolved.campfire_ids, expected);
        assert!(
            unresolved.stale_campfire_ids.is_empty(),
            "the cached listing still lists both, so neither has been lost from view: {:?}",
            unresolved.stale_campfire_ids
        );
        let after = paths(&server).await;
        assert!(
            !after[before..].iter().any(|path| path == "/999/chats.json"),
            "a listing refresh that could admit no candidate was not paid for"
        );
    }

    /// The stale list is what the conclusion ACTUALLY consulted minus what the refresh
    /// shows, so a candidate the refreshed listing still lists is not stale — even though
    /// the dock (which never had it) does not name it either.
    #[tokio::test]
    async fn a_candidate_the_refresh_still_lists_is_not_stale() {
        let clock = TestClock::new();
        let server = MockServer::start().await;
        scripted(&server, &[CACHED_A], &[CACHED_A, CACHED_B]).await;
        let account = account_on(&server, &clock);
        let reference = RecordingRef::from_event(BUCKET, LINE, "chat.line.created");
        account
            .recordings()
            .summarize(&reference)
            .await
            .unwrap_err();

        clock.advance(CAMPFIRE_INDEX_MIN_REFRESH + Duration::from_secs(1));
        scripted(&server, &[APPEARED], &[CACHED_B]).await;

        let second = unresolved_from(
            &account
                .recordings()
                .summarize(&reference)
                .await
                .unwrap_err(),
        );
        assert!(second.refreshed);
        assert_eq!(
            second.stale_campfire_ids,
            vec![CACHED_A],
            "CACHED_B is still listed, so only the dropped dock Campfire is stale"
        );
    }
}