weave-content 0.2.20

Content DSL parser, validator, and builder for OSINT case files
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
#![allow(clippy::module_name_repetitions)]

use std::fmt;

use serde::{Deserialize, Serialize};

/// Maximum length of a case NULID (26 chars Crockford Base32).
const MAX_CASE_ID_LEN: usize = 26;

/// Maximum number of sources in front matter.
const MAX_SOURCES: usize = 20;

/// Maximum length of the case title (H1).
const MAX_TITLE_LEN: usize = 200;

/// Maximum length of the case summary.
const MAX_SUMMARY_LEN: usize = 2000;

/// Known H2 section names for case files (case-insensitive match).
/// People and Organizations are no longer allowed in case files -- they
/// live in standalone entity files under `people/` and `organizations/`.
const KNOWN_CASE_SECTIONS: &[&str] = &[
    "Events",
    "Documents",
    "Assets",
    "Relationships",
    "Timeline",
    "Related Cases",
];

/// A parsed case file with front matter, title, summary, and raw sections.
#[derive(Debug)]
pub struct ParsedCase {
    /// NULID for the case node (None if not yet generated).
    pub id: Option<String>,
    pub sources: Vec<SourceEntry>,
    pub title: String,
    pub summary: String,
    pub sections: Vec<Section>,
    /// Case type from front matter (e.g. `corruption`, `fraud`).
    pub case_type: Option<String>,
    /// Case status from front matter (e.g. `open`, `trial`).
    pub status: Option<String>,
    /// Structured amounts DSL string (e.g. `660000 USD bribe | 250000000 IDR fine`).
    pub amounts: Option<String>,
    /// Tags from front matter for categorization.
    pub tags: Vec<String>,
    /// Related case entries from `## Related Cases` section.
    pub related_cases: Vec<RelatedCase>,
    /// Involved entity entries from `## Involved` section.
    pub involved: Vec<InvolvedEntry>,
}

/// A related case entry from `## Related Cases` section.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct RelatedCase {
    /// Case path relative to content root (e.g. `id/corruption/2002/blbi-liquidity-aid-scandal`).
    pub case_path: String,
    /// Description of the relationship between the cases.
    pub description: String,
    /// NULID for the `related_to` relationship (auto-generated on first build).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    /// Line number (1-indexed) where this entry appears in the original file.
    #[serde(skip)]
    pub line: usize,
}

/// An entity reference in the `## Involved` section.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InvolvedEntry {
    /// Entity name (must match a registry entity referenced in the case).
    pub entity_name: String,
    /// NULID for the `involved_in` relationship (auto-generated on first build).
    pub id: Option<String>,
    /// Line number (1-indexed) where this entry appears in the original file.
    pub line: usize,
}

/// A raw H2 section with its heading text and body content.
#[derive(Debug)]
pub struct Section {
    pub kind: SectionKind,
    pub body: String,
    /// Line number (1-indexed) where the H2 heading appears in the original file.
    pub line: usize,
}

/// The type of an H2 section, mapped from heading text.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SectionKind {
    People,
    Organizations,
    Events,
    Documents,
    Assets,
    Relationships,
    Timeline,
    RelatedCases,
    Involved,
}

impl SectionKind {
    fn from_heading(heading: &str) -> Option<Self> {
        match heading.trim() {
            s if s.eq_ignore_ascii_case("People") => Some(Self::People),
            s if s.eq_ignore_ascii_case("Organizations") => Some(Self::Organizations),
            s if s.eq_ignore_ascii_case("Events") => Some(Self::Events),
            s if s.eq_ignore_ascii_case("Documents") => Some(Self::Documents),
            s if s.eq_ignore_ascii_case("Assets") => Some(Self::Assets),
            s if s.eq_ignore_ascii_case("Relationships") => Some(Self::Relationships),
            s if s.eq_ignore_ascii_case("Timeline") => Some(Self::Timeline),
            s if s.eq_ignore_ascii_case("Related Cases") => Some(Self::RelatedCases),
            s if s.eq_ignore_ascii_case("Involved") => Some(Self::Involved),
            _ => None,
        }
    }

    /// Whether this section kind is valid in case files.
    /// People and Organizations are no longer allowed in case files.
    pub fn is_case_section(self) -> bool {
        matches!(
            self,
            Self::Events
                | Self::Documents
                | Self::Assets
                | Self::Relationships
                | Self::Timeline
                | Self::RelatedCases
                | Self::Involved
        )
    }
}

/// A parser error with file location.
#[derive(Debug)]
pub struct ParseError {
    pub line: usize,
    pub message: String,
}

impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "line {}: {}", self.line, self.message)
    }
}

/// Maximum number of tags per case file.
const MAX_CASE_TAGS: usize = 10;

/// Maximum number of tags per entity file.
const MAX_ENTITY_TAGS: usize = 5;

/// Maximum length of a single tag.
const MAX_TAG_LEN: usize = 50;

/// Maximum number of related case entries per case file.
const MAX_RELATED_CASES: usize = 10;

/// Maximum length of a related case description.
const MAX_RELATED_DESCRIPTION_LEN: usize = 500;

/// Parse the body of a `## Related Cases` section into `RelatedCase` entries.
///
/// Each entry is a bullet `- <case_path>` followed by indented fields:
/// `description: <text>` (required) and `id: <NULID>` (optional, written back).
pub fn parse_related_cases(
    body: &str,
    section_start_line: usize,
    errors: &mut Vec<ParseError>,
) -> Vec<RelatedCase> {
    let mut entries: Vec<(String, String, Option<String>, usize)> = Vec::new(); // (path, desc, id, line)

    for (offset, line) in body.lines().enumerate() {
        let file_line = section_start_line + offset + 1;

        if let Some(rest) = line.strip_prefix("- ") {
            let case_path = rest.trim().to_string();
            entries.push((case_path, String::new(), None, file_line));
        } else if let Some(rest) = line.strip_prefix("  description: ") {
            if let Some(entry) = entries.last_mut() {
                entry.1 = rest.trim().to_string();
            } else {
                errors.push(ParseError {
                    line: file_line,
                    message: "description without a preceding case path".into(),
                });
            }
        } else if let Some(rest) = line.strip_prefix("  id: ") {
            if let Some(entry) = entries.last_mut() {
                entry.2 = Some(rest.trim().to_string());
            } else {
                errors.push(ParseError {
                    line: file_line,
                    message: "id without a preceding case path".into(),
                });
            }
        } else if !line.trim().is_empty() {
            errors.push(ParseError {
                line: file_line,
                message: format!("unexpected line in Related Cases: {line}"),
            });
        }
    }

    if entries.len() > MAX_RELATED_CASES {
        errors.push(ParseError {
            line: section_start_line,
            message: format!(
                "Related Cases exceeds {MAX_RELATED_CASES} entries (got {})",
                entries.len()
            ),
        });
    }

    let mut result = Vec::new();
    for (case_path, description, id, line) in entries {
        if case_path.is_empty() {
            errors.push(ParseError {
                line,
                message: "related case path must not be empty".into(),
            });
            continue;
        }
        if description.is_empty() {
            errors.push(ParseError {
                line,
                message: format!("related case {case_path:?} missing description"),
            });
            continue;
        }
        if description.len() > MAX_RELATED_DESCRIPTION_LEN {
            errors.push(ParseError {
                line,
                message: format!(
                    "related case description exceeds {MAX_RELATED_DESCRIPTION_LEN} chars (got {})",
                    description.len()
                ),
            });
            continue;
        }
        result.push(RelatedCase {
            case_path,
            description,
            id,
            line,
        });
    }

    result
}

/// Maximum number of entries in `## Involved` section.
const MAX_INVOLVED: usize = 50;

/// Parse the body of a `## Involved` section into `InvolvedEntry` items.
///
/// Format:
/// ```text
/// - Entity Name
///   id: 01ABC...
/// ```
pub fn parse_involved(
    body: &str,
    section_start_line: usize,
    errors: &mut Vec<ParseError>,
) -> Vec<InvolvedEntry> {
    let mut entries = Vec::new();
    let lines: Vec<&str> = body.lines().collect();

    let mut i = 0;
    while i < lines.len() {
        let file_line = section_start_line + 1 + i;
        let trimmed = lines[i].trim();

        if trimmed.is_empty() {
            i += 1;
            continue;
        }

        let Some(name) = trimmed.strip_prefix("- ") else {
            errors.push(ParseError {
                line: file_line,
                message: format!("expected involved entry `- Entity Name`, got {trimmed:?}"),
            });
            i += 1;
            continue;
        };

        let entity_name = name.trim().to_string();
        if entity_name.is_empty() {
            errors.push(ParseError {
                line: file_line,
                message: "involved entity name must not be empty".into(),
            });
            i += 1;
            continue;
        }

        // Look ahead for `id:` on the next line
        let mut id: Option<String> = None;
        if i + 1 < lines.len() {
            let next = lines[i + 1].trim();
            if let Some(id_val) = next.strip_prefix("id: ") {
                id = Some(id_val.trim().to_string());
                i += 1;
            }
        }

        entries.push(InvolvedEntry {
            entity_name,
            id,
            line: file_line,
        });

        i += 1;
    }

    if entries.len() > MAX_INVOLVED {
        errors.push(ParseError {
            line: section_start_line,
            message: format!(
                "Involved exceeds {MAX_INVOLVED} entries (got {})",
                entries.len()
            ),
        });
    }

    entries
}

/// YAML front matter schema.
#[derive(Deserialize)]
struct FrontMatter {
    /// NULID for the case node (auto-generated on first build).
    #[serde(default)]
    id: Option<String>,
    #[serde(default)]
    sources: Vec<SourceEntry>,
    #[serde(default)]
    case_type: Option<String>,
    #[serde(default)]
    status: Option<String>,
    #[serde(default)]
    amounts: Option<String>,
    #[serde(default)]
    tags: Vec<String>,
}

/// A source entry in front matter. Supports both bare URL strings and
/// structured objects with metadata.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(untagged)]
pub enum SourceEntry {
    /// Plain URL string (backward-compatible).
    Url(String),
    /// Structured source with metadata.
    Structured {
        url: String,
        #[serde(default)]
        title: Option<String>,
        #[serde(default)]
        published_at: Option<String>,
        #[serde(default)]
        language: Option<String>,
    },
}

impl SourceEntry {
    /// Get the URL from any source entry variant.
    pub fn url(&self) -> &str {
        match self {
            Self::Url(u) => u,
            Self::Structured { url, .. } => url,
        }
    }
}

/// YAML front matter schema for standalone entity files.
/// Only contains an optional `id` field (NULID, generated on first build).
#[derive(Deserialize)]
struct EntityFrontMatter {
    #[serde(default)]
    id: Option<String>,
    #[serde(default)]
    tags: Vec<String>,
}

/// A parsed standalone entity file (actor or institution).
#[derive(Debug)]
pub struct ParsedEntityFile {
    /// Stored NULID from front matter (None if not yet generated).
    pub id: Option<String>,
    /// Entity name from H1 heading.
    pub name: String,
    /// Raw bullet field lines (body after H1, no sections).
    pub body: String,
    /// Line number of the H1 heading in the original file.
    pub title_line: usize,
    /// Tags from front matter.
    pub tags: Vec<String>,
}

/// Parse a Markdown case file into a `ParsedCase`.
///
/// Extracts YAML front matter, H1 title, summary, and H2 sections.
/// Returns errors for malformed structure or boundary violations.
pub fn parse(input: &str) -> Result<ParsedCase, Vec<ParseError>> {
    let mut errors = Vec::new();

    // Extract front matter
    let (front_matter, body_start_line, body) = extract_front_matter(input, &mut errors);

    let Some(front_matter) = front_matter else {
        if errors.is_empty() {
            errors.push(ParseError {
                line: 1,
                message: "missing YAML front matter (expected `---` delimiter)".into(),
            });
        }
        return Err(errors);
    };

    // Validate front matter fields
    validate_front_matter(&front_matter, &mut errors);

    // Extract title, summary, and sections from body
    let (title, summary, mut sections) = extract_body(&body, body_start_line, &mut errors);

    // Parse Related Cases sections
    let mut related_cases = Vec::new();
    for section in &sections {
        if section.kind == SectionKind::RelatedCases {
            let entries = parse_related_cases(&section.body, section.line, &mut errors);
            related_cases.extend(entries);
        }
    }
    // Remove RelatedCases from sections list (consumed)
    sections.retain(|s| s.kind != SectionKind::RelatedCases);

    // Parse Involved sections
    let mut involved = Vec::new();
    for section in &sections {
        if section.kind == SectionKind::Involved {
            let entries = parse_involved(&section.body, section.line, &mut errors);
            involved.extend(entries);
        }
    }
    // Remove Involved from sections list (consumed)
    sections.retain(|s| s.kind != SectionKind::Involved);

    if !errors.is_empty() {
        return Err(errors);
    }

    Ok(ParsedCase {
        id: front_matter.id,
        sources: front_matter.sources,
        title,
        summary,
        sections,
        case_type: front_matter.case_type,
        status: front_matter.status,
        amounts: front_matter.amounts,
        tags: front_matter.tags,
        related_cases,
        involved,
    })
}

/// Parse a standalone entity file (actor or institution).
///
/// Entity files have YAML front matter with optional `id:`, an H1 name,
/// and bullet fields directly in the body. No H2 sections are allowed.
pub fn parse_entity_file(input: &str) -> Result<ParsedEntityFile, Vec<ParseError>> {
    let mut errors = Vec::new();

    let (front_matter, body_start_line, body) = extract_entity_front_matter(input, &mut errors);

    let id = front_matter.as_ref().and_then(|fm| fm.id.clone());
    let tags = front_matter.map_or_else(Vec::new, |fm| fm.tags);

    // Validate entity tags
    if tags.len() > MAX_ENTITY_TAGS {
        errors.push(ParseError {
            line: 2,
            message: format!(
                "front matter `tags` exceeds {MAX_ENTITY_TAGS} entries (got {})",
                tags.len()
            ),
        });
    }
    for (i, tag) in tags.iter().enumerate() {
        if tag.len() > MAX_TAG_LEN {
            errors.push(ParseError {
                line: 2,
                message: format!("front matter tag #{} exceeds {MAX_TAG_LEN} chars", i + 1),
            });
        }
        if tag.is_empty() {
            errors.push(ParseError {
                line: 2,
                message: format!("front matter tag #{} is empty", i + 1),
            });
        }
    }

    // Extract H1 title and body content (no sections allowed)
    let (name, title_line, field_body) = extract_entity_body(&body, body_start_line, &mut errors);

    if !errors.is_empty() {
        return Err(errors);
    }

    Ok(ParsedEntityFile {
        id,
        name,
        body: field_body,
        title_line,
        tags,
    })
}

/// Extract YAML front matter for entity files.
/// Front matter is optional for entity files -- if absent, returns None with no error.
fn extract_entity_front_matter(
    input: &str,
    errors: &mut Vec<ParseError>,
) -> (Option<EntityFrontMatter>, usize, String) {
    let lines: Vec<&str> = input.lines().collect();

    let first_delim = lines.iter().position(|l| l.trim() == "---");
    if first_delim != Some(0) {
        // No front matter -- entire file is body, starting at line 1
        return (None, 1, input.to_string());
    }

    let close_delim = lines[1..].iter().position(|l| l.trim() == "---");
    let Some(close_offset) = close_delim else {
        errors.push(ParseError {
            line: 1,
            message: "unclosed YAML front matter (missing closing `---`)".into(),
        });
        return (None, 1, String::new());
    };

    let close_line = close_offset + 1;
    let yaml_str: String = lines[1..close_line].join("\n");
    let body_start_line = close_line + 2; // 1-indexed line number after closing `---`
    let body = lines[close_line + 1..].join("\n");

    match serde_yaml::from_str::<EntityFrontMatter>(&yaml_str) {
        Ok(fm) => (Some(fm), body_start_line, body),
        Err(e) => {
            errors.push(ParseError {
                line: 2,
                message: format!("invalid YAML front matter: {e}"),
            });
            (None, body_start_line, body)
        }
    }
}

/// Extract H1 name and field body from an entity file.
/// Rejects any H2 sections.
fn extract_entity_body(
    body: &str,
    body_start_line: usize,
    errors: &mut Vec<ParseError>,
) -> (String, usize, String) {
    let lines: Vec<&str> = body.lines().collect();
    let mut name = String::new();
    let mut title_found = false;
    let mut title_line = body_start_line;
    let mut field_lines: Vec<&str> = Vec::new();

    for (i, line) in lines.iter().enumerate() {
        let file_line = body_start_line + i;

        if let Some(heading) = strip_heading(line, 1) {
            if title_found {
                errors.push(ParseError {
                    line: file_line,
                    message: "multiple H1 headings found (expected exactly one)".into(),
                });
                continue;
            }
            name = heading.to_string();
            title_found = true;
            title_line = file_line;
            continue;
        }

        // Reject H2 sections in entity files
        if strip_heading(line, 2).is_some() {
            errors.push(ParseError {
                line: file_line,
                message: "H2 sections are not allowed in entity files".into(),
            });
            continue;
        }

        if title_found {
            field_lines.push(line);
        } else if !line.trim().is_empty() {
            errors.push(ParseError {
                line: file_line,
                message: "expected H1 heading (# Name)".into(),
            });
        }
    }

    if !title_found {
        errors.push(ParseError {
            line: body_start_line,
            message: "missing H1 heading".into(),
        });
    } else if name.len() > MAX_TITLE_LEN {
        errors.push(ParseError {
            line: title_line,
            message: format!("H1 name exceeds {MAX_TITLE_LEN} chars (got {})", name.len()),
        });
    }

    (name, title_line, field_lines.join("\n"))
}

/// Extract YAML front matter delimited by `---` lines.
/// Returns the parsed front matter, the line number where the body starts,
/// and the body text.
fn extract_front_matter(
    input: &str,
    errors: &mut Vec<ParseError>,
) -> (Option<FrontMatter>, usize, String) {
    let lines: Vec<&str> = input.lines().collect();

    // First non-empty line must be `---`
    let first_delim = lines.iter().position(|l| l.trim() == "---");
    if first_delim != Some(0) {
        errors.push(ParseError {
            line: 1,
            message: "missing YAML front matter (expected `---` on first line)".into(),
        });
        return (None, 1, input.to_string());
    }

    // Find closing `---`
    let close_delim = lines[1..].iter().position(|l| l.trim() == "---");
    let Some(close_offset) = close_delim else {
        errors.push(ParseError {
            line: 1,
            message: "unclosed YAML front matter (missing closing `---`)".into(),
        });
        return (None, 1, String::new());
    };

    let close_line = close_offset + 1; // index in `lines`
    let yaml_str: String = lines[1..close_line].join("\n");
    let body_start_line = close_line + 2; // 1-indexed line number after closing `---`
    let body = lines[close_line + 1..].join("\n");

    match serde_yaml::from_str::<FrontMatter>(&yaml_str) {
        Ok(fm) => (Some(fm), body_start_line, body),
        Err(e) => {
            errors.push(ParseError {
                line: 2,
                message: format!("invalid YAML front matter: {e}"),
            });
            (None, body_start_line, body)
        }
    }
}

fn validate_front_matter(fm: &FrontMatter, errors: &mut Vec<ParseError>) {
    // Validate case ID (NULID) if present
    if let Some(id) = &fm.id
        && id.len() != MAX_CASE_ID_LEN
    {
        errors.push(ParseError {
            line: 2,
            message: format!(
                "front matter `id` must be a {MAX_CASE_ID_LEN}-char NULID, got {} chars",
                id.len()
            ),
        });
    }

    // Validate sources count
    if fm.sources.len() > MAX_SOURCES {
        errors.push(ParseError {
            line: 2,
            message: format!(
                "front matter `sources` exceeds {MAX_SOURCES} entries (got {})",
                fm.sources.len()
            ),
        });
    }

    // Validate each source URL is HTTPS
    for (i, source) in fm.sources.iter().enumerate() {
        if !source.url().starts_with("https://") {
            errors.push(ParseError {
                line: 2,
                message: format!("source[{i}] must be HTTPS, got {:?}", source.url()),
            });
        }
    }

    // Validate case_type
    if let Some(ct) = &fm.case_type {
        use crate::domain::CaseType;
        let normalized = ct.to_lowercase().replace(' ', "_");
        if !CaseType::KNOWN.contains(&normalized.as_str())
            && crate::domain::parse_custom(ct).is_none()
        {
            errors.push(ParseError {
                line: 2,
                message: format!(
                    "invalid case_type {:?} (known: {}; use \"custom:Value\" for custom)",
                    ct,
                    CaseType::KNOWN.join(", ")
                ),
            });
        }
    }

    // Validate status
    if let Some(st) = &fm.status {
        use crate::domain::CaseStatus;
        let normalized = st.to_lowercase().replace(' ', "_");
        if !CaseStatus::KNOWN.contains(&normalized.as_str()) {
            errors.push(ParseError {
                line: 2,
                message: format!(
                    "invalid status {:?} (known: {})",
                    st,
                    CaseStatus::KNOWN.join(", ")
                ),
            });
        }
    }

    // Validate tags
    if fm.tags.len() > MAX_CASE_TAGS {
        errors.push(ParseError {
            line: 2,
            message: format!(
                "front matter `tags` exceeds {MAX_CASE_TAGS} entries (got {})",
                fm.tags.len()
            ),
        });
    }
    for (i, tag) in fm.tags.iter().enumerate() {
        if tag.len() > MAX_TAG_LEN {
            errors.push(ParseError {
                line: 2,
                message: format!("tag[{i}] exceeds {MAX_TAG_LEN} chars (got {})", tag.len()),
            });
        }
        if tag.is_empty() {
            errors.push(ParseError {
                line: 2,
                message: format!("tag[{i}] must not be empty"),
            });
        }
    }
}

/// Extract the H1 title, summary text, and H2 sections from the body.
#[allow(clippy::too_many_lines)]
fn extract_body(
    body: &str,
    body_start_line: usize,
    errors: &mut Vec<ParseError>,
) -> (String, String, Vec<Section>) {
    let lines: Vec<&str> = body.lines().collect();
    let mut title = String::new();
    let mut title_found = false;
    let mut summary_lines: Vec<&str> = Vec::new();
    let mut sections: Vec<Section> = Vec::new();

    // Track current H2 section being built
    let mut current_section_kind: Option<SectionKind> = None;
    let mut current_section_line: usize = 0;
    let mut current_section_body: Vec<&str> = Vec::new();

    // State: before H1, after H1 (summary), in sections
    let mut state = State::BeforeTitle;

    for (i, line) in lines.iter().enumerate() {
        let file_line = body_start_line + i; // 1-indexed line in original file

        if let Some(heading) = strip_heading(line, 1) {
            if title_found {
                errors.push(ParseError {
                    line: file_line,
                    message: "multiple H1 headings found (expected exactly one)".into(),
                });
                continue;
            }
            title = heading.to_string();
            title_found = true;
            state = State::Summary;
            continue;
        }

        if let Some(heading) = strip_heading(line, 2) {
            // Flush previous section
            if let Some(kind) = current_section_kind.take() {
                sections.push(Section {
                    kind,
                    body: current_section_body.join("\n"),
                    line: current_section_line,
                });
                current_section_body.clear();
            }

            match SectionKind::from_heading(heading) {
                Some(kind) if kind.is_case_section() => {
                    // Check for duplicate sections
                    if sections.iter().any(|s| s.kind == kind) {
                        errors.push(ParseError {
                            line: file_line,
                            message: format!("duplicate section: ## {heading}"),
                        });
                    }
                    current_section_kind = Some(kind);
                    current_section_line = file_line;
                    state = State::InSection;
                }
                Some(_) => {
                    // Legacy section (People/Organizations) -- not allowed in case files
                    errors.push(ParseError {
                        line: file_line,
                        message: format!(
                            "## {heading} is not allowed in case files (use standalone entity files in people/ or organizations/ instead)"
                        ),
                    });
                }
                None => {
                    errors.push(ParseError {
                        line: file_line,
                        message: format!(
                            "unknown section: ## {heading} (expected one of: {})",
                            KNOWN_CASE_SECTIONS.join(", ")
                        ),
                    });
                }
            }
            continue;
        }

        match state {
            State::BeforeTitle => {
                // Skip blank lines before title
                if !line.trim().is_empty() {
                    errors.push(ParseError {
                        line: file_line,
                        message: "expected H1 title (# Title)".into(),
                    });
                }
            }
            State::Summary => {
                summary_lines.push(line);
            }
            State::InSection => {
                current_section_body.push(line);
            }
        }
    }

    // Flush last section
    if let Some(kind) = current_section_kind.take() {
        sections.push(Section {
            kind,
            body: current_section_body.join("\n"),
            line: current_section_line,
        });
    }

    // Validate title
    if !title_found {
        errors.push(ParseError {
            line: body_start_line,
            message: "missing H1 title".into(),
        });
    } else if title.len() > MAX_TITLE_LEN {
        errors.push(ParseError {
            line: body_start_line,
            message: format!(
                "H1 title exceeds {MAX_TITLE_LEN} chars (got {})",
                title.len()
            ),
        });
    }

    // Build summary (trim leading/trailing blank lines)
    let summary = summary_lines.clone().join("\n").trim().to_string();

    if summary.len() > MAX_SUMMARY_LEN {
        errors.push(ParseError {
            line: body_start_line,
            message: format!(
                "summary exceeds {MAX_SUMMARY_LEN} chars (got {})",
                summary.len()
            ),
        });
    }

    (title, summary, sections)
}

#[derive(Clone, Copy)]
enum State {
    BeforeTitle,
    Summary,
    InSection,
}

/// Strip an ATX heading prefix of the given level. Returns the heading text.
/// E.g., `strip_heading("## Foo", 2)` returns `Some("Foo")`.
fn strip_heading(line: &str, level: usize) -> Option<&str> {
    let prefix = "#".repeat(level);
    let trimmed = line.trim_start();
    if trimmed.starts_with(&prefix) {
        let after = &trimmed[prefix.len()..];
        // Must be followed by space or end of line, and NOT more `#` chars
        if after.is_empty() {
            return Some("");
        }
        if after.starts_with(' ') && !after.starts_with(" #") {
            // Actually, need to exclude `### Foo` when looking for `## Foo`
            return Some(after[1..].trim());
        }
        // Check: `###` should not match `##`
        if after.starts_with('#') {
            return None;
        }
    }
    None
}

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

    fn minimal_case() -> String {
        [
            "---",
            "id: 01H9XT7H1J3929RK32FWSRKV88",
            "sources:",
            "  - https://example.com/source",
            "---",
            "",
            "# Test Case Title",
            "",
            "This is the summary.",
            "",
            "## Events",
            "",
            "### Something happened",
            "- occurred_at: 2025-01-01",
            "",
            "## Relationships",
            "",
            "- Something happened -> Something happened: associate_of",
        ]
        .join("\n")
    }

    #[test]
    fn parse_minimal_case() {
        let result = parse(&minimal_case());
        let case = result.unwrap_or_else(|errs| {
            panic!(
                "parse failed: {}",
                errs.iter()
                    .map(ToString::to_string)
                    .collect::<Vec<_>>()
                    .join("; ")
            );
        });

        assert_eq!(case.id.as_deref(), Some("01H9XT7H1J3929RK32FWSRKV88"));
        assert_eq!(case.sources.len(), 1);
        assert_eq!(case.sources[0].url(), "https://example.com/source");
        assert_eq!(case.title, "Test Case Title");
        assert_eq!(case.summary, "This is the summary.");
        assert_eq!(case.sections.len(), 2);
        assert_eq!(case.sections[0].kind, SectionKind::Events);
        assert_eq!(case.sections[1].kind, SectionKind::Relationships);
    }

    #[test]
    fn parse_missing_front_matter() {
        let input = "# Title\n\nSummary.\n";
        let errs = parse(input).unwrap_err();
        assert!(errs.iter().any(|e| e.message.contains("front matter")));
    }

    #[test]
    fn parse_unclosed_front_matter() {
        let input = "---\nsources: []\n# Title\n";
        let errs = parse(input).unwrap_err();
        assert!(errs.iter().any(|e| e.message.contains("unclosed")));
    }

    #[test]
    fn parse_invalid_case_id_wrong_length() {
        let input = "---\nid: short\nsources: []\n---\n\n# Title\n";
        let errs = parse(input).unwrap_err();
        assert!(errs.iter().any(|e| e.message.contains("NULID")));
    }

    #[test]
    fn parse_case_id_absent_is_ok() {
        let input = "---\nsources:\n  - https://example.com\n---\n\n# Title\n\nSummary.\n";
        let case = parse(input).unwrap();
        assert!(case.id.is_none());
    }

    #[test]
    fn parse_non_https_source() {
        let input = "---\nsources:\n  - http://example.com\n---\n\n# Title\n";
        let errs = parse(input).unwrap_err();
        assert!(errs.iter().any(|e| e.message.contains("HTTPS")));
    }

    #[test]
    fn parse_too_many_sources() {
        let sources: Vec<String> = (0..21)
            .map(|i| format!("  - https://example.com/{i}"))
            .collect();
        let input = format!("---\nsources:\n{}\n---\n\n# Title\n", sources.join("\n"));
        let errs = parse(&input).unwrap_err();
        assert!(errs.iter().any(|e| e.message.contains("exceeds 20")));
    }

    #[test]
    fn parse_unknown_section() {
        let input = [
            "---",
            "sources: []",
            "---",
            "",
            "# Title",
            "",
            "## Unknown Section",
            "",
        ]
        .join("\n");
        let errs = parse(&input).unwrap_err();
        assert!(errs.iter().any(|e| e.message.contains("unknown section")));
    }

    #[test]
    fn parse_duplicate_section() {
        let input = [
            "---",
            "sources: []",
            "---",
            "",
            "# Title",
            "",
            "## Events",
            "",
            "## Events",
            "",
        ]
        .join("\n");
        let errs = parse(&input).unwrap_err();
        assert!(errs.iter().any(|e| e.message.contains("duplicate")));
    }

    #[test]
    fn parse_multiple_h1() {
        let input = [
            "---",
            "sources: []",
            "---",
            "",
            "# First Title",
            "",
            "# Second Title",
            "",
        ]
        .join("\n");
        let errs = parse(&input).unwrap_err();
        assert!(errs.iter().any(|e| e.message.contains("multiple H1")));
    }

    #[test]
    fn parse_all_sections() {
        let input = [
            "---",
            "id: 01H9XT7H1KRQ9SJ7SD9ETB5CVQ",
            "sources:",
            "  - https://example.com/a",
            "---",
            "",
            "# Full Case",
            "",
            "Summary text here.",
            "",
            "## Events",
            "",
            "### Something happened",
            "- occurred_at: 2025-01-01",
            "",
            "## Relationships",
            "",
            "- Alice -> Corp Inc: employed_by",
            "",
            "## Timeline",
            "",
            "Something happened",
        ]
        .join("\n");

        let case = parse(&input).unwrap_or_else(|errs| {
            panic!(
                "parse failed: {}",
                errs.iter()
                    .map(ToString::to_string)
                    .collect::<Vec<_>>()
                    .join("; ")
            );
        });

        assert_eq!(case.id.as_deref(), Some("01H9XT7H1KRQ9SJ7SD9ETB5CVQ"));
        assert_eq!(case.title, "Full Case");
        assert_eq!(case.summary, "Summary text here.");
        assert_eq!(case.sections.len(), 3);
        assert_eq!(case.sections[0].kind, SectionKind::Events);
        assert_eq!(case.sections[1].kind, SectionKind::Relationships);
        assert_eq!(case.sections[2].kind, SectionKind::Timeline);
    }

    #[test]
    fn parse_empty_summary() {
        let input = [
            "---",
            "sources: []",
            "---",
            "",
            "# Title",
            "",
            "## Events",
            "",
        ]
        .join("\n");

        let case = parse(&input).unwrap_or_else(|errs| {
            panic!(
                "parse failed: {}",
                errs.iter()
                    .map(ToString::to_string)
                    .collect::<Vec<_>>()
                    .join("; ")
            );
        });
        assert_eq!(case.summary, "");
    }

    #[test]
    fn parse_multiline_summary() {
        let input = [
            "---",
            "sources: []",
            "---",
            "",
            "# Title",
            "",
            "First line of summary.",
            "Second line of summary.",
            "",
            "## Events",
            "",
        ]
        .join("\n");

        let case = parse(&input).unwrap_or_else(|errs| {
            panic!(
                "parse failed: {}",
                errs.iter()
                    .map(ToString::to_string)
                    .collect::<Vec<_>>()
                    .join("; ")
            );
        });
        assert_eq!(
            case.summary,
            "First line of summary.\nSecond line of summary."
        );
    }

    #[test]
    fn strip_heading_levels() {
        assert_eq!(strip_heading("# Title", 1), Some("Title"));
        assert_eq!(strip_heading("## Section", 2), Some("Section"));
        assert_eq!(strip_heading("### Entity", 3), Some("Entity"));
        // H3 should not match H2
        assert_eq!(strip_heading("### Entity", 2), None);
        // H2 should not match H1
        assert_eq!(strip_heading("## Section", 1), None);
        // Not a heading
        assert_eq!(strip_heading("Normal text", 1), None);
    }

    #[test]
    fn section_body_content() {
        let input = [
            "---",
            "sources: []",
            "---",
            "",
            "# Title",
            "",
            "## Events",
            "",
            "### Bonnick dismissal",
            "- occurred_at: 2024-12-24",
            "- type: termination",
            "",
        ]
        .join("\n");

        let case = parse(&input).unwrap_or_else(|errs| {
            panic!(
                "parse failed: {}",
                errs.iter()
                    .map(ToString::to_string)
                    .collect::<Vec<_>>()
                    .join("; ")
            );
        });

        assert_eq!(case.sections.len(), 1);
        let body = &case.sections[0].body;
        assert!(body.contains("### Bonnick dismissal"));
        assert!(body.contains("- occurred_at: 2024-12-24"));
    }

    #[test]
    fn parse_rejects_people_section_in_case_file() {
        let input = [
            "---",
            "sources: []",
            "---",
            "",
            "# Title",
            "",
            "## People",
            "",
        ]
        .join("\n");
        let errs = parse(&input).unwrap_err();
        assert!(
            errs.iter()
                .any(|e| e.message.contains("not allowed in case files"))
        );
    }

    #[test]
    fn parse_rejects_organizations_section_in_case_file() {
        let input = [
            "---",
            "sources: []",
            "---",
            "",
            "# Title",
            "",
            "## Organizations",
            "",
        ]
        .join("\n");
        let errs = parse(&input).unwrap_err();
        assert!(
            errs.iter()
                .any(|e| e.message.contains("not allowed in case files"))
        );
    }

    #[test]
    fn parse_entity_file_with_id() {
        let input = [
            "---",
            "id: 01JXYZ123456789ABCDEFGHIJK",
            "---",
            "",
            "# Mark Bonnick",
            "",
            "- qualifier: Arsenal Kit Manager",
            "- nationality: British",
            "",
        ]
        .join("\n");

        let result = parse_entity_file(&input).unwrap();
        assert_eq!(result.id.as_deref(), Some("01JXYZ123456789ABCDEFGHIJK"));
        assert_eq!(result.name, "Mark Bonnick");
        assert!(result.body.contains("- qualifier: Arsenal Kit Manager"));
        assert!(result.body.contains("- nationality: British"));
    }

    #[test]
    fn parse_entity_file_without_id() {
        let input = [
            "---",
            "---",
            "",
            "# Arsenal FC",
            "",
            "- qualifier: English Football Club",
            "- org_type: sports_club",
            "",
        ]
        .join("\n");

        let result = parse_entity_file(&input).unwrap();
        assert!(result.id.is_none());
        assert_eq!(result.name, "Arsenal FC");
    }

    #[test]
    fn parse_entity_file_no_front_matter() {
        let input = ["# Bob Smith", "", "- nationality: Dutch", ""].join("\n");

        let result = parse_entity_file(&input).unwrap();
        assert!(result.id.is_none());
        assert_eq!(result.name, "Bob Smith");
        assert!(result.body.contains("- nationality: Dutch"));
    }

    #[test]
    fn parse_entity_file_rejects_h2_sections() {
        let input = [
            "---",
            "---",
            "",
            "# Test Entity",
            "",
            "## Relationships",
            "",
        ]
        .join("\n");

        let errs = parse_entity_file(&input).unwrap_err();
        assert!(errs.iter().any(|e| e.message.contains("H2 sections")));
    }

    #[test]
    fn parse_entity_file_missing_h1() {
        let input = ["---", "---", "", "- nationality: Dutch", ""].join("\n");

        let errs = parse_entity_file(&input).unwrap_err();
        assert!(errs.iter().any(|e| e.message.contains("missing H1")));
    }

    #[test]
    fn parse_related_cases_section() {
        let input = [
            "---",
            "tags: [bribery]",
            "sources:",
            "  - https://example.com",
            "---",
            "",
            "# Test Case",
            "",
            "Summary text.",
            "",
            "## Related Cases",
            "",
            "- id/corruption/2002/blbi-liquidity-aid-scandal",
            "  description: Artalyta bribed Urip to influence the BLBI investigation",
            "- id/corruption/2008/another-case",
            "  description: A second related case",
        ]
        .join("\n");

        let case = parse(&input).unwrap_or_else(|errs| {
            panic!(
                "parse failed: {}",
                errs.iter()
                    .map(ToString::to_string)
                    .collect::<Vec<_>>()
                    .join("; ")
            );
        });

        assert_eq!(case.related_cases.len(), 2);
        assert_eq!(
            case.related_cases[0].case_path,
            "id/corruption/2002/blbi-liquidity-aid-scandal"
        );
        assert_eq!(
            case.related_cases[0].description,
            "Artalyta bribed Urip to influence the BLBI investigation"
        );
        assert_eq!(
            case.related_cases[1].case_path,
            "id/corruption/2008/another-case"
        );
        assert_eq!(case.related_cases[1].description, "A second related case");
        // RelatedCases should be consumed and NOT appear in sections
        assert!(
            !case
                .sections
                .iter()
                .any(|s| s.kind == SectionKind::RelatedCases)
        );
    }

    #[test]
    fn parse_related_cases_empty_path() {
        let input = [
            "---",
            "sources: []",
            "---",
            "",
            "# Title",
            "",
            "## Related Cases",
            "",
            "- ",
            "  description: Some description",
        ]
        .join("\n");

        let errs = parse(&input).unwrap_err();
        assert!(
            errs.iter()
                .any(|e| e.message.contains("case path must not be empty"))
        );
    }

    #[test]
    fn parse_related_cases_missing_description() {
        let input = [
            "---",
            "sources: []",
            "---",
            "",
            "# Title",
            "",
            "## Related Cases",
            "",
            "- id/corruption/2002/some-case",
        ]
        .join("\n");

        let errs = parse(&input).unwrap_err();
        assert!(errs.iter().any(|e| e.message.contains("description")));
    }

    #[test]
    fn parse_related_cases_description_too_long() {
        let long_desc = "x".repeat(501);
        let input = [
            "---",
            "sources: []",
            "---",
            "",
            "# Title",
            "",
            "## Related Cases",
            "",
            "- id/corruption/2002/some-case",
            &format!("  description: {long_desc}"),
        ]
        .join("\n");

        let errs = parse(&input).unwrap_err();
        assert!(errs.iter().any(|e| e.message.contains("exceeds 500")));
    }

    #[test]
    fn parse_related_cases_too_many() {
        let mut lines = vec![
            "---".to_string(),
            "sources: []".to_string(),
            "---".to_string(),
            String::new(),
            "# Title".to_string(),
            String::new(),
            "## Related Cases".to_string(),
            String::new(),
        ];
        for i in 0..11 {
            lines.push(format!("- id/corruption/2002/case-{i}"));
            lines.push(format!("  description: Description {i}"));
        }
        let input = lines.join("\n");

        let errs = parse(&input).unwrap_err();
        assert!(errs.iter().any(|e| e.message.contains("exceeds 10")));
    }
}