oxur-odm 0.3.2

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

use crate::doc::{DesignDoc, DocState};
use anyhow::Result;
use regex::Regex;
use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};

/// Represents an entry in the index table
#[derive(Debug, Clone)]
pub struct IndexEntry {
    pub number: String,
    pub title: String,
    pub state: String,
    pub updated: String,
}

/// Represents a parsed index file
#[derive(Debug)]
pub struct ParsedIndex {
    pub table_entries: HashMap<String, IndexEntry>,
    pub state_sections: HashMap<String, Vec<String>>, // state -> list of doc paths
}

impl ParsedIndex {
    /// Parse an index file from its content
    pub fn parse(content: &str) -> Result<Self> {
        let table_entries = parse_table(content);
        let state_sections = parse_state_sections(content);

        Ok(ParsedIndex { table_entries, state_sections })
    }
}

/// Parse the "All Documents by Number" table
fn parse_table(content: &str) -> HashMap<String, IndexEntry> {
    let mut entries = HashMap::new();
    let lines: Vec<&str> = content.lines().collect();

    let mut in_table = false;
    let mut passed_separator = false;

    for line in lines {
        // Detect table start
        if line.starts_with("| Number | Title") {
            in_table = true;
            continue;
        }

        // Detect separator line
        if in_table && line.contains("---|") {
            passed_separator = true;
            continue;
        }

        // End of table
        if in_table && !line.starts_with('|') {
            break;
        }

        // Parse data rows
        if in_table && passed_separator && line.starts_with('|') {
            let parts: Vec<&str> = line.split('|').collect();
            if parts.len() >= 5 {
                let number = parts[1].trim();
                let title = parts[2].trim();
                let state = parts[3].trim();
                let updated = parts[4].trim();

                if !number.is_empty() && number != "Number" {
                    entries.insert(
                        number.to_string(),
                        IndexEntry {
                            number: number.to_string(),
                            title: title.to_string(),
                            state: state.to_string(),
                            updated: updated.to_string(),
                        },
                    );
                }
            }
        }
    }

    entries
}

/// Parse state sections to extract document paths
fn parse_state_sections(content: &str) -> HashMap<String, Vec<String>> {
    let mut sections = HashMap::new();
    let lines: Vec<&str> = content.lines().collect();

    let mut current_state: Option<String> = None;
    let re = Regex::new(r"\]\(([^)]+)\)").unwrap();

    for line in lines {
        // Detect state section header (### State Name)
        if let Some(state_name) = line.strip_prefix("### ") {
            current_state = Some(state_name.trim().to_string());
            sections.insert(current_state.clone().unwrap(), Vec::new());
            continue;
        }

        // Detect end of state sections (## header)
        if line.starts_with("## ") && current_state.is_some() {
            current_state = None;
            continue;
        }

        // Parse document links in current state section
        if let Some(ref state) = current_state {
            if line.starts_with("- [") {
                // Extract path from markdown link: - [0001 - Title](path/to/file.md)
                if let Some(caps) = re.captures(line) {
                    if let Some(path) = caps.get(1) {
                        sections.get_mut(state).unwrap().push(path.as_str().to_string());
                    }
                }
            }
        }
    }

    sections
}

/// Get all git-tracked markdown files in the docs directory
pub fn get_git_tracked_docs(docs_dir: impl AsRef<Path>) -> Result<Vec<PathBuf>> {
    use std::process::Command;

    let docs_dir = docs_dir.as_ref();

    // Run git ls-files to get tracked files
    let output = Command::new("git")
        .args(["ls-files", "--full-name"])
        .arg(docs_dir)
        .current_dir(docs_dir.parent().unwrap_or(docs_dir))
        .output()?;

    if !output.status.success() {
        // Fall back to filesystem if git fails
        return get_docs_from_filesystem(docs_dir);
    }

    let stdout = String::from_utf8_lossy(&output.stdout);
    let base_dir = docs_dir.parent().unwrap_or(docs_dir);

    let files: Vec<PathBuf> = stdout
        .lines()
        .filter(|line| line.ends_with(".md"))
        .map(|line| base_dir.join(line))
        .filter(|path| path.exists())
        .collect();

    Ok(files)
}

/// Get all markdown files in state directories (using walkdir, not git)
pub fn get_docs_from_filesystem(docs_dir: impl AsRef<Path>) -> Result<Vec<PathBuf>> {
    use walkdir::WalkDir;

    let docs_dir = docs_dir.as_ref();
    let mut all_docs = Vec::new();

    // Walk through all state directories, excluding dustbin states
    for state in DocState::all_states() {
        // Skip dustbin states (Removed, Overwritten) - they shouldn't appear in index
        if state.is_in_dustbin() {
            continue;
        }

        let state_dir = docs_dir.join(state.directory());
        if !state_dir.exists() {
            continue;
        }

        for entry in WalkDir::new(&state_dir).max_depth(1).into_iter().filter_map(|e| e.ok()) {
            if entry.file_type().is_file() {
                if let Some(ext) = entry.path().extension() {
                    if ext == "md" {
                        all_docs.push(entry.path().to_path_buf());
                    }
                }
            }
        }
    }

    Ok(all_docs)
}

/// Extract document metadata for all docs
pub fn build_doc_map(doc_paths: &[PathBuf]) -> HashMap<String, DesignDoc> {
    let mut map = HashMap::new();

    for path in doc_paths {
        if let Ok(content) = std::fs::read_to_string(path) {
            if let Ok(doc) = DesignDoc::parse(&content, path.clone()) {
                let number = format!("{:04}", doc.metadata.number);
                map.insert(number, doc);
            }
        }
    }

    map
}

/// Types of changes that can occur
#[derive(Debug, Clone)]
pub enum IndexChange {
    TableAdd { number: String, title: String, state: String, updated: String },
    TableUpdate { number: String, field: String, old: String, new: String },
    TableRemove { number: String },
    SectionAdd { state: String, number: String, title: String, path: String },
    SectionRemove { state: String, path: String },
}

impl IndexChange {
    pub fn description(&self) -> String {
        match self {
            IndexChange::TableAdd { number, title, .. } => {
                format!("Add to table: {} - {}", number, title)
            }
            IndexChange::TableUpdate { number, field, old, new } => {
                format!("Update {}: {} ({} → {})", number, field, old, new)
            }
            IndexChange::TableRemove { number } => {
                format!("Remove from table: {}", number)
            }
            IndexChange::SectionAdd { state, number, title, .. } => {
                format!("Add to {}: {} - {}", state, number, title)
            }
            IndexChange::SectionRemove { state, path } => {
                format!("Remove from {}: {}", state, path)
            }
        }
    }
}

/// Compare index with filesystem and determine table changes
pub fn compute_table_changes(
    parsed: &ParsedIndex,
    doc_map: &HashMap<String, DesignDoc>,
) -> Vec<IndexChange> {
    let mut changes = Vec::new();

    // Check for missing or outdated table entries
    for (number, doc) in doc_map {
        if let Some(existing) = parsed.table_entries.get(number) {
            // Check if state differs
            if existing.state != doc.metadata.state.as_str() {
                changes.push(IndexChange::TableUpdate {
                    number: number.clone(),
                    field: "state".to_string(),
                    old: existing.state.clone(),
                    new: doc.metadata.state.as_str().to_string(),
                });
            }

            // Check if updated date differs
            let doc_updated = doc.metadata.updated.to_string();
            if existing.updated != doc_updated {
                changes.push(IndexChange::TableUpdate {
                    number: number.clone(),
                    field: "updated".to_string(),
                    old: existing.updated.clone(),
                    new: doc_updated,
                });
            }

            // Check if title differs
            if existing.title != doc.metadata.title {
                changes.push(IndexChange::TableUpdate {
                    number: number.clone(),
                    field: "title".to_string(),
                    old: existing.title.clone(),
                    new: doc.metadata.title.clone(),
                });
            }
        } else {
            // Document not in table, add it
            changes.push(IndexChange::TableAdd {
                number: number.clone(),
                title: doc.metadata.title.clone(),
                state: doc.metadata.state.as_str().to_string(),
                updated: doc.metadata.updated.to_string(),
            });
        }
    }

    // Check for entries in table that no longer exist
    for number in parsed.table_entries.keys() {
        if !doc_map.contains_key(number) {
            changes.push(IndexChange::TableRemove { number: number.clone() });
        }
    }

    changes
}

/// Compute changes for state sections
pub fn compute_section_changes(
    parsed: &ParsedIndex,
    doc_map: &HashMap<String, DesignDoc>,
    docs_dir: &Path,
) -> Vec<IndexChange> {
    let mut changes = Vec::new();

    // Build map of expected documents by state
    let mut expected_by_state: HashMap<String, Vec<&DesignDoc>> = HashMap::new();
    for state in DocState::all_states() {
        expected_by_state.insert(state.as_str().to_string(), Vec::new());
    }

    for doc in doc_map.values() {
        let state_name = doc.metadata.state.as_str().to_string();
        expected_by_state.get_mut(&state_name).unwrap().push(doc);
    }

    // Check each state section
    for (state_name, expected_docs) in expected_by_state {
        let current_paths = parsed.state_sections.get(&state_name).cloned().unwrap_or_default();

        // Build set of current paths for quick lookup
        let current_set: HashSet<String> = current_paths.iter().cloned().collect();

        // Check for documents that should be in section but aren't
        for doc in &expected_docs {
            let rel_path = doc.path.strip_prefix(docs_dir).unwrap_or(&doc.path);
            let path_str = rel_path.to_string_lossy().to_string();

            if !current_set.contains(&path_str) {
                changes.push(IndexChange::SectionAdd {
                    state: state_name.clone(),
                    number: format!("{:04}", doc.metadata.number),
                    title: doc.metadata.title.clone(),
                    path: path_str,
                });
            }
        }

        // Build set of expected paths
        let expected_set: HashSet<String> = expected_docs
            .iter()
            .map(|doc| {
                let rel_path = doc.path.strip_prefix(docs_dir).unwrap_or(&doc.path);
                rel_path.to_string_lossy().to_string()
            })
            .collect();

        // Check for documents in section that shouldn't be there
        for path in &current_paths {
            if !expected_set.contains(path) {
                changes.push(IndexChange::SectionRemove {
                    state: state_name.clone(),
                    path: path.clone(),
                });
            }
        }
    }

    changes
}

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

    const SAMPLE_INDEX: &str = r#"# Design Documents Index

## All Documents by Number

| Number | Title | State | Updated |
|--------|-------|-------|---------|
| 0001 | First Doc | Draft | 2024-01-01 |
| 0002 | Second Doc | Final | 2024-01-02 |

### Draft

- [0001 - First Doc](01-draft/0001-first-doc.md)

### Final

- [0002 - Second Doc](06-final/0002-second-doc.md)
"#;

    #[test]
    fn test_parse_table_basic() {
        let entries = parse_table(SAMPLE_INDEX);
        assert_eq!(entries.len(), 2);
        assert!(entries.contains_key("0001"));
        assert!(entries.contains_key("0002"));
    }

    #[test]
    fn test_parse_table_entry_fields() {
        let entries = parse_table(SAMPLE_INDEX);
        let entry = entries.get("0001").unwrap();

        assert_eq!(entry.number, "0001");
        assert_eq!(entry.title, "First Doc");
        assert_eq!(entry.state, "Draft");
        assert_eq!(entry.updated, "2024-01-01");
    }

    #[test]
    fn test_parse_table_empty() {
        let empty = "# No table here";
        let entries = parse_table(empty);
        assert!(entries.is_empty());
    }

    #[test]
    fn test_parse_state_sections_basic() {
        let sections = parse_state_sections(SAMPLE_INDEX);
        assert!(sections.contains_key("Draft"));
        assert!(sections.contains_key("Final"));
    }

    #[test]
    fn test_parse_state_sections_paths() {
        let sections = parse_state_sections(SAMPLE_INDEX);
        let draft_docs = sections.get("Draft").unwrap();

        assert_eq!(draft_docs.len(), 1);
        assert_eq!(draft_docs[0], "01-draft/0001-first-doc.md");
    }

    #[test]
    fn test_parse_index_complete() {
        let parsed = ParsedIndex::parse(SAMPLE_INDEX).unwrap();

        assert_eq!(parsed.table_entries.len(), 2);
        assert_eq!(parsed.state_sections.len(), 2);
    }

    #[test]
    fn test_index_change_descriptions() {
        let add = IndexChange::TableAdd {
            number: "0042".to_string(),
            title: "New Doc".to_string(),
            state: "Draft".to_string(),
            updated: "2024-01-01".to_string(),
        };
        assert!(add.description().contains("Add to table"));
        assert!(add.description().contains("0042"));

        let update = IndexChange::TableUpdate {
            number: "0001".to_string(),
            field: "state".to_string(),
            old: "Draft".to_string(),
            new: "Final".to_string(),
        };
        assert!(update.description().contains("Update"));
        assert!(update.description().contains("Draft → Final"));

        let remove = IndexChange::TableRemove { number: "0099".to_string() };
        assert!(remove.description().contains("Remove"));

        let section_add = IndexChange::SectionAdd {
            state: "Draft".to_string(),
            number: "0042".to_string(),
            title: "New".to_string(),
            path: "01-draft/0042-new.md".to_string(),
        };
        assert!(section_add.description().contains("Add to Draft"));

        let section_remove = IndexChange::SectionRemove {
            state: "Draft".to_string(),
            path: "01-draft/old.md".to_string(),
        };
        assert!(section_remove.description().contains("Remove from Draft"));
    }

    #[test]
    fn test_build_doc_map_empty() {
        let map = build_doc_map(&[]);
        assert!(map.is_empty());
    }

    #[test]
    fn test_compute_table_changes_add() {
        use chrono::NaiveDate;

        let parsed = ParsedIndex { table_entries: HashMap::new(), state_sections: HashMap::new() };

        let mut doc_map = HashMap::new();
        let doc = DesignDoc {
            metadata: crate::doc::DocMetadata {
                number: 42,
                title: "New Doc".to_string(),
                author: "Author".to_string(),
                component: None,
                tags: Vec::new(),
                created: NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
                updated: NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
                state: DocState::Draft,
                supersedes: None,
                superseded_by: None,
                version: "1.0".to_string(),
            },
            content: "content".to_string(),
            path: PathBuf::from("01-draft/0042-new-doc.md"),
        };
        doc_map.insert("0042".to_string(), doc);

        let changes = compute_table_changes(&parsed, &doc_map);

        assert_eq!(changes.len(), 1);
        match &changes[0] {
            IndexChange::TableAdd { number, title, .. } => {
                assert_eq!(number, "0042");
                assert_eq!(title, "New Doc");
            }
            _ => panic!("Expected TableAdd"),
        }
    }

    #[test]
    fn test_compute_table_changes_remove() {
        let mut table_entries = HashMap::new();
        table_entries.insert(
            "0099".to_string(),
            IndexEntry {
                number: "0099".to_string(),
                title: "Old Doc".to_string(),
                state: "Draft".to_string(),
                updated: "2024-01-01".to_string(),
            },
        );

        let parsed = ParsedIndex { table_entries, state_sections: HashMap::new() };
        let doc_map = HashMap::new(); // Empty - doc was deleted

        let changes = compute_table_changes(&parsed, &doc_map);

        assert_eq!(changes.len(), 1);
        match &changes[0] {
            IndexChange::TableRemove { number } => {
                assert_eq!(number, "0099");
            }
            _ => panic!("Expected TableRemove"),
        }
    }

    #[test]
    fn test_compute_table_changes_update_state() {
        use chrono::NaiveDate;

        let mut table_entries = HashMap::new();
        table_entries.insert(
            "0042".to_string(),
            IndexEntry {
                number: "0042".to_string(),
                title: "Doc".to_string(),
                state: "Draft".to_string(),
                updated: "2024-01-01".to_string(),
            },
        );

        let parsed = ParsedIndex { table_entries, state_sections: HashMap::new() };

        let mut doc_map = HashMap::new();
        let doc = DesignDoc {
            metadata: crate::doc::DocMetadata {
                number: 42,
                title: "Doc".to_string(),
                author: "Author".to_string(),
                component: None,
                tags: Vec::new(),
                created: NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
                updated: NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
                state: DocState::Final, // Changed!
                supersedes: None,
                superseded_by: None,
                version: "1.0".to_string(),
            },
            content: "content".to_string(),
            path: PathBuf::from("06-final/0042-doc.md"),
        };
        doc_map.insert("0042".to_string(), doc);

        let changes = compute_table_changes(&parsed, &doc_map);

        // Should detect state change
        assert!(!changes.is_empty());
        let has_state_update = changes
            .iter()
            .any(|c| matches!(c, IndexChange::TableUpdate { field, .. } if field == "state"));
        assert!(has_state_update);
    }

    #[test]
    fn test_compute_table_changes_update_title() {
        use chrono::NaiveDate;

        let mut table_entries = HashMap::new();
        table_entries.insert(
            "0042".to_string(),
            IndexEntry {
                number: "0042".to_string(),
                title: "Old Title".to_string(),
                state: "Draft".to_string(),
                updated: "2024-01-01".to_string(),
            },
        );

        let parsed = ParsedIndex { table_entries, state_sections: HashMap::new() };

        let mut doc_map = HashMap::new();
        let doc = DesignDoc {
            metadata: crate::doc::DocMetadata {
                number: 42,
                title: "New Title".to_string(),
                author: "Author".to_string(),
                component: None,
                tags: Vec::new(),
                created: NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
                updated: NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
                state: DocState::Draft,
                supersedes: None,
                superseded_by: None,
                version: "1.0".to_string(),
            },
            content: "content".to_string(),
            path: PathBuf::from("01-draft/0042-new-title.md"),
        };
        doc_map.insert("0042".to_string(), doc);

        let changes = compute_table_changes(&parsed, &doc_map);

        let has_title_update = changes
            .iter()
            .any(|c| matches!(c, IndexChange::TableUpdate { field, .. } if field == "title"));
        assert!(has_title_update);
    }

    #[test]
    fn test_compute_table_changes_update_date() {
        use chrono::NaiveDate;

        let mut table_entries = HashMap::new();
        table_entries.insert(
            "0042".to_string(),
            IndexEntry {
                number: "0042".to_string(),
                title: "Doc".to_string(),
                state: "Draft".to_string(),
                updated: "2024-01-01".to_string(),
            },
        );

        let parsed = ParsedIndex { table_entries, state_sections: HashMap::new() };

        let mut doc_map = HashMap::new();
        let doc = DesignDoc {
            metadata: crate::doc::DocMetadata {
                number: 42,
                title: "Doc".to_string(),
                author: "Author".to_string(),
                component: None,
                tags: Vec::new(),
                created: NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
                updated: NaiveDate::from_ymd_opt(2024, 2, 15).unwrap(),
                state: DocState::Draft,
                supersedes: None,
                superseded_by: None,
                version: "1.0".to_string(),
            },
            content: "content".to_string(),
            path: PathBuf::from("01-draft/0042-doc.md"),
        };
        doc_map.insert("0042".to_string(), doc);

        let changes = compute_table_changes(&parsed, &doc_map);

        let has_updated_change = changes
            .iter()
            .any(|c| matches!(c, IndexChange::TableUpdate { field, .. } if field == "updated"));
        assert!(has_updated_change);
    }

    #[test]
    fn test_compute_table_changes_multiple_updates() {
        use chrono::NaiveDate;

        let mut table_entries = HashMap::new();
        table_entries.insert(
            "0042".to_string(),
            IndexEntry {
                number: "0042".to_string(),
                title: "Old".to_string(),
                state: "Draft".to_string(),
                updated: "2024-01-01".to_string(),
            },
        );

        let parsed = ParsedIndex { table_entries, state_sections: HashMap::new() };

        let mut doc_map = HashMap::new();
        let doc = DesignDoc {
            metadata: crate::doc::DocMetadata {
                number: 42,
                title: "New".to_string(),
                author: "Author".to_string(),
                component: None,
                tags: Vec::new(),
                created: NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
                updated: NaiveDate::from_ymd_opt(2024, 2, 15).unwrap(),
                state: DocState::Final,
                supersedes: None,
                superseded_by: None,
                version: "1.0".to_string(),
            },
            content: "content".to_string(),
            path: PathBuf::from("06-final/0042-new.md"),
        };
        doc_map.insert("0042".to_string(), doc);

        let changes = compute_table_changes(&parsed, &doc_map);

        // Should have state, title, and updated changes
        assert!(changes.len() >= 3);
        assert!(changes
            .iter()
            .any(|c| matches!(c, IndexChange::TableUpdate { field, .. } if field == "state")));
        assert!(changes
            .iter()
            .any(|c| matches!(c, IndexChange::TableUpdate { field, .. } if field == "title")));
        assert!(changes
            .iter()
            .any(|c| matches!(c, IndexChange::TableUpdate { field, .. } if field == "updated")));
    }

    #[test]
    fn test_compute_table_changes_no_changes() {
        use chrono::NaiveDate;

        let mut table_entries = HashMap::new();
        table_entries.insert(
            "0042".to_string(),
            IndexEntry {
                number: "0042".to_string(),
                title: "Doc".to_string(),
                state: "Draft".to_string(),
                updated: "2024-01-01".to_string(),
            },
        );

        let parsed = ParsedIndex { table_entries, state_sections: HashMap::new() };

        let mut doc_map = HashMap::new();
        let doc = DesignDoc {
            metadata: crate::doc::DocMetadata {
                number: 42,
                title: "Doc".to_string(),
                author: "Author".to_string(),
                component: None,
                tags: Vec::new(),
                created: NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
                updated: NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
                state: DocState::Draft,
                supersedes: None,
                superseded_by: None,
                version: "1.0".to_string(),
            },
            content: "content".to_string(),
            path: PathBuf::from("01-draft/0042-doc.md"),
        };
        doc_map.insert("0042".to_string(), doc);

        let changes = compute_table_changes(&parsed, &doc_map);
        assert!(changes.is_empty());
    }

    #[test]
    fn test_compute_section_changes_add() {
        use chrono::NaiveDate;
        use std::env;

        let temp_dir = env::temp_dir();
        let docs_dir = temp_dir.join("test_docs");
        std::fs::create_dir_all(&docs_dir).ok();

        let parsed = ParsedIndex {
            table_entries: HashMap::new(),
            state_sections: {
                let mut sections = HashMap::new();
                sections.insert("Draft".to_string(), Vec::new());
                sections
            },
        };

        let mut doc_map = HashMap::new();
        let draft_dir = docs_dir.join("01-draft");
        std::fs::create_dir_all(&draft_dir).ok();
        let doc_path = draft_dir.join("0042-new-doc.md");

        let doc = DesignDoc {
            metadata: crate::doc::DocMetadata {
                number: 42,
                title: "New Doc".to_string(),
                author: "Author".to_string(),
                component: None,
                tags: Vec::new(),
                created: NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
                updated: NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
                state: DocState::Draft,
                supersedes: None,
                superseded_by: None,
                version: "1.0".to_string(),
            },
            content: "content".to_string(),
            path: doc_path,
        };
        doc_map.insert("0042".to_string(), doc);

        let changes = compute_section_changes(&parsed, &doc_map, &docs_dir);

        let has_section_add = changes
            .iter()
            .any(|c| matches!(c, IndexChange::SectionAdd { state, .. } if state == "Draft"));
        assert!(has_section_add);

        std::fs::remove_dir_all(&docs_dir).ok();
    }

    #[test]
    fn test_compute_section_changes_remove() {
        use std::env;

        let temp_dir = env::temp_dir();
        let docs_dir = temp_dir.join("test_docs_remove");
        std::fs::create_dir_all(&docs_dir).ok();

        let parsed = ParsedIndex {
            table_entries: HashMap::new(),
            state_sections: {
                let mut sections = HashMap::new();
                sections.insert("Draft".to_string(), vec!["01-draft/0042-old-doc.md".to_string()]);
                sections
            },
        };

        let doc_map = HashMap::new(); // Empty - doc was removed

        let changes = compute_section_changes(&parsed, &doc_map, &docs_dir);

        let has_section_remove = changes.iter().any(|c| {
            matches!(c, IndexChange::SectionRemove { state, path }
                if state == "Draft" && path == "01-draft/0042-old-doc.md")
        });
        assert!(has_section_remove);

        std::fs::remove_dir_all(&docs_dir).ok();
    }

    #[test]
    fn test_compute_section_changes_no_changes() {
        use chrono::NaiveDate;
        use std::env;

        let temp_dir = env::temp_dir();
        let docs_dir = temp_dir.join("test_docs_no_change");
        std::fs::create_dir_all(&docs_dir).ok();

        let draft_dir = docs_dir.join("01-draft");
        std::fs::create_dir_all(&draft_dir).ok();
        let doc_path = draft_dir.join("0042-doc.md");

        let parsed = ParsedIndex {
            table_entries: HashMap::new(),
            state_sections: {
                let mut sections = HashMap::new();
                sections.insert("Draft".to_string(), vec!["01-draft/0042-doc.md".to_string()]);
                sections
            },
        };

        let mut doc_map = HashMap::new();
        let doc = DesignDoc {
            metadata: crate::doc::DocMetadata {
                number: 42,
                title: "Doc".to_string(),
                author: "Author".to_string(),
                component: None,
                tags: Vec::new(),
                created: NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
                updated: NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
                state: DocState::Draft,
                supersedes: None,
                superseded_by: None,
                version: "1.0".to_string(),
            },
            content: "content".to_string(),
            path: doc_path,
        };
        doc_map.insert("0042".to_string(), doc);

        let changes = compute_section_changes(&parsed, &doc_map, &docs_dir);

        // Filter out changes for other states (which will be empty)
        let draft_changes: Vec<_> = changes
            .iter()
            .filter(|c| match c {
                IndexChange::SectionAdd { state, .. } => state == "Draft",
                IndexChange::SectionRemove { state, .. } => state == "Draft",
                _ => false,
            })
            .collect();
        assert!(draft_changes.is_empty());

        std::fs::remove_dir_all(&docs_dir).ok();
    }

    #[test]
    fn test_compute_section_changes_multiple_states() {
        use chrono::NaiveDate;
        use std::env;

        let temp_dir = env::temp_dir();
        let docs_dir = temp_dir.join("test_docs_multi");
        std::fs::create_dir_all(&docs_dir).ok();

        let draft_dir = docs_dir.join("01-draft");
        let final_dir = docs_dir.join("06-final");
        std::fs::create_dir_all(&draft_dir).ok();
        std::fs::create_dir_all(&final_dir).ok();

        let parsed = ParsedIndex { table_entries: HashMap::new(), state_sections: HashMap::new() };

        let mut doc_map = HashMap::new();

        // Add draft doc
        let draft_path = draft_dir.join("0001-draft.md");
        let draft_doc = DesignDoc {
            metadata: crate::doc::DocMetadata {
                number: 1,
                title: "Draft".to_string(),
                author: "Author".to_string(),
                component: None,
                tags: Vec::new(),
                created: NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
                updated: NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
                state: DocState::Draft,
                supersedes: None,
                superseded_by: None,
                version: "1.0".to_string(),
            },
            content: "content".to_string(),
            path: draft_path,
        };
        doc_map.insert("0001".to_string(), draft_doc);

        // Add final doc
        let final_path = final_dir.join("0002-final.md");
        let final_doc = DesignDoc {
            metadata: crate::doc::DocMetadata {
                number: 2,
                title: "Final".to_string(),
                author: "Author".to_string(),
                component: None,
                tags: Vec::new(),
                created: NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
                updated: NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
                state: DocState::Final,
                supersedes: None,
                superseded_by: None,
                version: "1.0".to_string(),
            },
            content: "content".to_string(),
            path: final_path,
        };
        doc_map.insert("0002".to_string(), final_doc);

        let changes = compute_section_changes(&parsed, &doc_map, &docs_dir);

        // Should have additions for both states
        let has_draft_add = changes
            .iter()
            .any(|c| matches!(c, IndexChange::SectionAdd { state, .. } if state == "Draft"));
        let has_final_add = changes
            .iter()
            .any(|c| matches!(c, IndexChange::SectionAdd { state, .. } if state == "Final"));

        assert!(has_draft_add);
        assert!(has_final_add);

        std::fs::remove_dir_all(&docs_dir).ok();
    }

    #[test]
    fn test_build_doc_map_with_valid_docs() {
        use std::env;

        let temp_dir = env::temp_dir();
        let test_dir = temp_dir.join("test_build_doc_map");
        std::fs::create_dir_all(&test_dir).ok();

        let doc1_path = test_dir.join("0001-doc.md");
        let doc1_content = "---\nnumber: 1\ntitle: \"First\"\nauthor: \"Author\"\ncreated: 2024-01-01\nupdated: 2024-01-01\nstate: Draft\nsupersedes: null\nsuperseded-by: null\n---\n\nContent";
        std::fs::write(&doc1_path, doc1_content).ok();

        let doc2_path = test_dir.join("0002-doc.md");
        let doc2_content = "---\nnumber: 2\ntitle: \"Second\"\nauthor: \"Author\"\ncreated: 2024-01-01\nupdated: 2024-01-01\nstate: Final\nsupersedes: null\nsuperseded-by: null\n---\n\nContent";
        std::fs::write(&doc2_path, doc2_content).ok();

        let paths = vec![doc1_path.clone(), doc2_path.clone()];
        let map = build_doc_map(&paths);

        assert_eq!(map.len(), 2);
        assert!(map.contains_key("0001"));
        assert!(map.contains_key("0002"));

        let doc1 = map.get("0001").unwrap();
        assert_eq!(doc1.metadata.title, "First");

        std::fs::remove_dir_all(&test_dir).ok();
    }

    #[test]
    fn test_build_doc_map_with_invalid_docs() {
        use std::env;

        let temp_dir = env::temp_dir();
        let test_dir = temp_dir.join("test_build_doc_map_invalid");
        std::fs::create_dir_all(&test_dir).ok();

        let valid_path = test_dir.join("0001-valid.md");
        let valid_content = "---\nnumber: 1\ntitle: \"Valid\"\nauthor: \"Author\"\ncreated: 2024-01-01\nupdated: 2024-01-01\nstate: Draft\nsupersedes: null\nsuperseded-by: null\n---\n\nContent";
        std::fs::write(&valid_path, valid_content).ok();

        let invalid_path = test_dir.join("0002-invalid.md");
        let invalid_content = "No frontmatter here";
        std::fs::write(&invalid_path, invalid_content).ok();

        let paths = vec![valid_path.clone(), invalid_path.clone()];
        let map = build_doc_map(&paths);

        // Should only include valid doc
        assert_eq!(map.len(), 1);
        assert!(map.contains_key("0001"));
        assert!(!map.contains_key("0002"));

        std::fs::remove_dir_all(&test_dir).ok();
    }

    #[test]
    fn test_build_doc_map_nonexistent_file() {
        let paths = vec![PathBuf::from("/nonexistent/file.md")];
        let map = build_doc_map(&paths);
        assert!(map.is_empty());
    }

    #[test]
    fn test_parse_table_malformed_rows() {
        let content = r#"# Index

| Number | Title | State | Updated |
|--------|-------|-------|---------|
| 0001 | Valid Doc | Draft | 2024-01-01 |
| incomplete row
| | | | |
| 0002 | Another Valid | Final | 2024-01-02 |
"#;
        let entries = parse_table(content);
        assert_eq!(entries.len(), 2);
        assert!(entries.contains_key("0001"));
        assert!(entries.contains_key("0002"));
    }

    #[test]
    fn test_parse_table_no_separator() {
        let content = r#"# Index

| Number | Title | State | Updated |
| 0001 | No Separator | Draft | 2024-01-01 |
"#;
        let entries = parse_table(content);
        // Without separator, rows won't be parsed
        assert!(entries.is_empty());
    }

    #[test]
    fn test_parse_table_header_only() {
        let content = r#"# Index

| Number | Title | State | Updated |
|--------|-------|-------|---------|
"#;
        let entries = parse_table(content);
        assert!(entries.is_empty());
    }

    #[test]
    fn test_parse_table_with_empty_number() {
        let content = r#"# Index

| Number | Title | State | Updated |
|--------|-------|-------|---------|
|  | Empty Number | Draft | 2024-01-01 |
| 0001 | Valid | Draft | 2024-01-01 |
"#;
        let entries = parse_table(content);
        assert_eq!(entries.len(), 1);
        assert!(entries.contains_key("0001"));
    }

    #[test]
    fn test_parse_state_sections_empty() {
        let content = "# Index\n\nNo sections here";
        let sections = parse_state_sections(content);
        assert!(sections.is_empty());
    }

    #[test]
    fn test_parse_state_sections_empty_section() {
        let content = r#"# Index

### Draft

### Final

- [0001 - Doc](06-final/0001-doc.md)
"#;
        let sections = parse_state_sections(content);
        assert_eq!(sections.len(), 2);

        let draft = sections.get("Draft").unwrap();
        assert!(draft.is_empty());

        let final_section = sections.get("Final").unwrap();
        assert_eq!(final_section.len(), 1);
    }

    #[test]
    fn test_parse_state_sections_terminated_by_h2() {
        let content = r#"# Index

### Draft

- [0001 - Doc](01-draft/0001-doc.md)
- [0002 - Another](01-draft/0002-another.md)

## All Documents by Number

| Number | Title | State | Updated |
"#;
        let sections = parse_state_sections(content);
        let draft = sections.get("Draft").unwrap();
        assert_eq!(draft.len(), 2);
    }

    #[test]
    fn test_parse_state_sections_multiple_links() {
        let content = r#"### Draft

- [0001 - First](01-draft/0001-first.md)
- [0002 - Second](01-draft/0002-second.md)
- [0003 - Third](01-draft/0003-third.md)

### Final

- [0004 - Fourth](06-final/0004-fourth.md)
"#;
        let sections = parse_state_sections(content);

        let draft = sections.get("Draft").unwrap();
        assert_eq!(draft.len(), 3);
        assert_eq!(draft[0], "01-draft/0001-first.md");
        assert_eq!(draft[1], "01-draft/0002-second.md");
        assert_eq!(draft[2], "01-draft/0003-third.md");

        let final_section = sections.get("Final").unwrap();
        assert_eq!(final_section.len(), 1);
        assert_eq!(final_section[0], "06-final/0004-fourth.md");
    }

    #[test]
    fn test_parse_state_sections_malformed_links() {
        let content = r#"### Draft

- [0001 - Valid](01-draft/0001-valid.md)
- Not a link
- [0002 - No closing paren](01-draft/0002.md
- [0003](01-draft/0003.md)
- Regular bullet point
"#;
        let sections = parse_state_sections(content);
        let draft = sections.get("Draft").unwrap();
        // Only valid markdown links should be captured
        assert_eq!(draft.len(), 2);
        assert_eq!(draft[0], "01-draft/0001-valid.md");
        assert_eq!(draft[1], "01-draft/0003.md");
    }

    #[test]
    fn test_cleanup_formatting_basic() {
        let input = r#"# Title

## Section One


Content here


## Section Two

More content
"#;
        let output = cleanup_formatting(input);

        // Should have consistent spacing
        assert!(output.contains("\n## Section One\n\nContent"));
        assert!(output.contains("\n## Section Two\n\nMore"));
        // Should not have triple blank lines
        assert!(!output.contains("\n\n\n\n"));
    }

    #[test]
    fn test_cleanup_formatting_h3_sections() {
        let input = r#"### Draft


- [0001](01-draft/0001.md)

### Final


- [0002](06-final/0002.md)
"#;
        let output = cleanup_formatting(input);

        // Should have proper spacing around h3
        // First header has no blank line before it
        assert!(output.starts_with("### Draft\n\n"));
        assert!(output.contains("- [0001]"));
        assert!(output.contains("\n### Final\n\n- [0002]"));
    }

    #[test]
    fn test_cleanup_formatting_consecutive_bullets() {
        let input = r#"### Section

- [0001](file1.md)

- [0002](file2.md)

- [0003](file3.md)
"#;
        let output = cleanup_formatting(input);

        // Blank lines between bullets should be removed
        assert!(!output.contains("]\n\n- ["));
    }

    #[test]
    fn test_cleanup_formatting_first_header() {
        let input = r#"## First Header

Content"#;
        let output = cleanup_formatting(input);

        // First header shouldn't have blank line before it
        assert!(output.starts_with("## First Header\n"));
    }

    #[test]
    fn test_cleanup_formatting_ends_with_newline() {
        let input = "# Title\n\nContent";
        let output = cleanup_formatting(input);

        assert!(output.ends_with('\n'));
    }

    #[test]
    fn test_cleanup_formatting_already_clean() {
        let input = r#"# Title

## Section

Content

### Subsection

- [0001](file.md)
- [0002](file2.md)
"#;
        let output = cleanup_formatting(input);

        // Should remain mostly the same
        assert!(output.contains("# Title"));
        assert!(output.contains("## Section"));
        assert!(output.contains("### Subsection"));
    }

    #[test]
    fn test_cleanup_formatting_multiple_trailing_blanks() {
        let input = r#"## Section



Content"#;
        let output = cleanup_formatting(input);

        // Should collapse to single blank line before header
        assert!(output.contains("## Section\n\nContent"));
        assert!(!output.contains("\n\n\n"));
    }

    #[test]
    fn test_cleanup_formatting_mixed_content() {
        let input = r#"# Main Title


## All Documents

| Number | Title |
|--------|-------|
| 0001 | Doc |


### Draft

- [0001](01-draft/0001.md)
- [0002](01-draft/0002.md)


## Footer

End
"#;
        let output = cleanup_formatting(input);

        // Headers should have proper spacing
        assert!(output.contains("\n## All Documents\n\n"));
        assert!(output.contains("\n### Draft\n\n"));
        assert!(output.contains("\n## Footer\n\n"));

        // Bullets should be consecutive
        assert!(output.contains("0001.md)\n- [0002]"));
    }

    #[test]
    fn test_cleanup_formatting_empty_input() {
        let input = "";
        let output = cleanup_formatting(input);
        assert_eq!(output, "");
    }

    #[test]
    fn test_cleanup_formatting_only_headers() {
        let input = r#"# Title
## Section
### Subsection"#;
        let output = cleanup_formatting(input);

        // Headers should be separated by blank lines
        assert!(output.contains("# Title\n\n## Section"));
        assert!(output.ends_with('\n'));
    }

    #[test]
    fn test_cleanup_formatting_preserves_table() {
        let input = r#"## Table

| A | B |
|---|---|
| 1 | 2 |
| 3 | 4 |

## Next"#;
        let output = cleanup_formatting(input);

        // Table lines should be preserved
        assert!(output.contains("| A | B |"));
        assert!(output.contains("| 1 | 2 |"));
        assert!(output.contains("| 3 | 4 |"));
    }

    #[test]
    fn test_get_docs_from_filesystem() {
        use std::env;

        let temp_dir = env::temp_dir();
        let test_dir = temp_dir.join("test_get_docs_fs");
        std::fs::create_dir_all(&test_dir).ok();

        // Create state directories
        let draft_dir = test_dir.join("01-draft");
        let final_dir = test_dir.join("06-final");
        std::fs::create_dir_all(&draft_dir).ok();
        std::fs::create_dir_all(&final_dir).ok();

        // Create some markdown files
        std::fs::write(draft_dir.join("0001-doc.md"), "content").ok();
        std::fs::write(draft_dir.join("0002-doc.md"), "content").ok();
        std::fs::write(final_dir.join("0003-doc.md"), "content").ok();

        // Create non-md file (should be ignored)
        std::fs::write(draft_dir.join("README.txt"), "content").ok();

        let result = get_docs_from_filesystem(&test_dir).unwrap();

        assert_eq!(result.len(), 3);
        assert!(result.iter().any(|p| p.file_name().unwrap() == "0001-doc.md"));
        assert!(result.iter().any(|p| p.file_name().unwrap() == "0002-doc.md"));
        assert!(result.iter().any(|p| p.file_name().unwrap() == "0003-doc.md"));
        assert!(!result.iter().any(|p| p.file_name().unwrap() == "README.txt"));

        std::fs::remove_dir_all(&test_dir).ok();
    }

    #[test]
    fn test_get_docs_from_filesystem_nonexistent_dirs() {
        use std::env;

        let temp_dir = env::temp_dir();
        let test_dir = temp_dir.join("test_get_docs_fs_empty");
        std::fs::create_dir_all(&test_dir).ok();

        // No state directories exist
        let result = get_docs_from_filesystem(&test_dir).unwrap();
        assert!(result.is_empty());

        std::fs::remove_dir_all(&test_dir).ok();
    }

    #[test]
    fn test_get_docs_from_filesystem_nested_files() {
        use std::env;

        let temp_dir = env::temp_dir();
        let test_dir = temp_dir.join("test_get_docs_fs_nested");
        std::fs::create_dir_all(&test_dir).ok();

        let draft_dir = test_dir.join("01-draft");
        std::fs::create_dir_all(&draft_dir).ok();

        // Create file in root
        std::fs::write(draft_dir.join("0001-root.md"), "content").ok();

        // Create nested directory (should be ignored due to max_depth(1))
        let nested_dir = draft_dir.join("subdir");
        std::fs::create_dir_all(&nested_dir).ok();
        std::fs::write(nested_dir.join("0002-nested.md"), "content").ok();

        let result = get_docs_from_filesystem(&test_dir).unwrap();

        // Should only find root-level .md files
        assert_eq!(result.len(), 1);
        assert!(result.iter().any(|p| p.file_name().unwrap() == "0001-root.md"));
        assert!(!result.iter().any(|p| p.file_name().unwrap() == "0002-nested.md"));

        std::fs::remove_dir_all(&test_dir).ok();
    }

    #[test]
    fn test_get_git_tracked_docs_fallback() {
        use std::env;

        // Use a non-git directory to trigger fallback
        let temp_dir = env::temp_dir();
        let test_dir = temp_dir.join("test_git_fallback");
        std::fs::create_dir_all(&test_dir).ok();

        let draft_dir = test_dir.join("01-draft");
        std::fs::create_dir_all(&draft_dir).ok();
        std::fs::write(draft_dir.join("0001-doc.md"), "content").ok();

        // Should fall back to filesystem when git fails
        let result = get_git_tracked_docs(&test_dir);

        // Either succeeds with fallback or returns empty
        match result {
            Ok(docs) => {
                // If fallback worked, should find the doc
                if !docs.is_empty() {
                    assert!(docs.iter().any(|p| p.file_name().unwrap() == "0001-doc.md"));
                }
            }
            Err(_) => {
                // Error is acceptable if filesystem also fails
            }
        }

        std::fs::remove_dir_all(&test_dir).ok();
    }

    #[test]
    fn test_get_git_tracked_docs_filters_md() {
        use std::env;
        use std::process::Command;

        // Create a temp git repo
        let temp_dir = env::temp_dir();
        let test_dir = temp_dir.join("test_git_md_filter");
        std::fs::create_dir_all(&test_dir).ok();

        // Initialize git repo
        Command::new("git").args(["init"]).current_dir(&test_dir).output().ok();

        let draft_dir = test_dir.join("01-draft");
        std::fs::create_dir_all(&draft_dir).ok();

        // Create files
        std::fs::write(draft_dir.join("0001-doc.md"), "content").ok();
        std::fs::write(draft_dir.join("README.txt"), "content").ok();

        // Add files to git
        Command::new("git").args(["add", "."]).current_dir(&test_dir).output().ok();

        let result = get_git_tracked_docs(&test_dir);

        if let Ok(docs) = result {
            // Should only include .md files
            assert!(docs.iter().all(|p| p.extension().map_or(false, |e| e == "md")));
            assert!(!docs.iter().any(|p| p.file_name().map_or(false, |n| n == "README.txt")));
        }

        std::fs::remove_dir_all(&test_dir).ok();
    }

    #[test]
    fn test_parsed_index_parse_integration() {
        let content = r#"# Design Documents Index

## All Documents by Number

| Number | Title | State | Updated |
|--------|-------|-------|---------|
| 0001 | First Doc | Draft | 2024-01-01 |
| 0002 | Second Doc | Final | 2024-01-02 |

### Draft

- [0001 - First Doc](01-draft/0001-first-doc.md)

### Final

- [0002 - Second Doc](06-final/0002-second-doc.md)

## Other Section
"#;

        let parsed = ParsedIndex::parse(content).unwrap();

        // Verify table was parsed
        assert_eq!(parsed.table_entries.len(), 2);
        assert!(parsed.table_entries.contains_key("0001"));
        assert!(parsed.table_entries.contains_key("0002"));

        // Verify sections were parsed
        assert_eq!(parsed.state_sections.len(), 2);
        assert!(parsed.state_sections.contains_key("Draft"));
        assert!(parsed.state_sections.contains_key("Final"));

        let draft_docs = parsed.state_sections.get("Draft").unwrap();
        assert_eq!(draft_docs.len(), 1);
        assert_eq!(draft_docs[0], "01-draft/0001-first-doc.md");
    }

    #[test]
    fn test_index_entry_clone() {
        let entry = IndexEntry {
            number: "0001".to_string(),
            title: "Test".to_string(),
            state: "Draft".to_string(),
            updated: "2024-01-01".to_string(),
        };

        let cloned = entry.clone();
        assert_eq!(entry.number, cloned.number);
        assert_eq!(entry.title, cloned.title);
        assert_eq!(entry.state, cloned.state);
        assert_eq!(entry.updated, cloned.updated);
    }

    #[test]
    fn test_compute_table_changes_mixed_scenario() {
        use chrono::NaiveDate;

        // Index has: 0001 (outdated), 0002 (current), 0003 (deleted)
        // Filesystem has: 0001 (updated), 0002 (unchanged), 0004 (new)
        let mut table_entries = HashMap::new();
        table_entries.insert(
            "0001".to_string(),
            IndexEntry {
                number: "0001".to_string(),
                title: "Old Title".to_string(),
                state: "Draft".to_string(),
                updated: "2024-01-01".to_string(),
            },
        );
        table_entries.insert(
            "0002".to_string(),
            IndexEntry {
                number: "0002".to_string(),
                title: "Unchanged".to_string(),
                state: "Final".to_string(),
                updated: "2024-01-01".to_string(),
            },
        );
        table_entries.insert(
            "0003".to_string(),
            IndexEntry {
                number: "0003".to_string(),
                title: "Deleted".to_string(),
                state: "Draft".to_string(),
                updated: "2024-01-01".to_string(),
            },
        );

        let parsed = ParsedIndex { table_entries, state_sections: HashMap::new() };

        let mut doc_map = HashMap::new();

        // Updated doc
        doc_map.insert(
            "0001".to_string(),
            DesignDoc {
                metadata: crate::doc::DocMetadata {
                    number: 1,
                    title: "New Title".to_string(),
                    author: "Author".to_string(),
                    component: None,
                    tags: Vec::new(),
                    created: NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
                    updated: NaiveDate::from_ymd_opt(2024, 2, 1).unwrap(),
                    state: DocState::Final,
                    supersedes: None,
                    superseded_by: None,
                    version: "1.0".to_string(),
                },
                content: "content".to_string(),
                path: PathBuf::from("06-final/0001-new-title.md"),
            },
        );

        // Unchanged doc
        doc_map.insert(
            "0002".to_string(),
            DesignDoc {
                metadata: crate::doc::DocMetadata {
                    number: 2,
                    title: "Unchanged".to_string(),
                    author: "Author".to_string(),
                    component: None,
                    tags: Vec::new(),
                    created: NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
                    updated: NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
                    state: DocState::Final,
                    supersedes: None,
                    superseded_by: None,
                    version: "1.0".to_string(),
                },
                content: "content".to_string(),
                path: PathBuf::from("06-final/0002-unchanged.md"),
            },
        );

        // New doc
        doc_map.insert(
            "0004".to_string(),
            DesignDoc {
                metadata: crate::doc::DocMetadata {
                    number: 4,
                    title: "New Doc".to_string(),
                    author: "Author".to_string(),
                    component: None,
                    tags: Vec::new(),
                    created: NaiveDate::from_ymd_opt(2024, 2, 1).unwrap(),
                    updated: NaiveDate::from_ymd_opt(2024, 2, 1).unwrap(),
                    state: DocState::Draft,
                    supersedes: None,
                    superseded_by: None,
                    version: "1.0".to_string(),
                },
                content: "content".to_string(),
                path: PathBuf::from("01-draft/0004-new-doc.md"),
            },
        );

        let changes = compute_table_changes(&parsed, &doc_map);

        // Should have updates for 0001, add for 0004, remove for 0003
        assert!(changes
            .iter()
            .any(|c| matches!(c, IndexChange::TableUpdate { number, .. } if number == "0001")));
        assert!(changes
            .iter()
            .any(|c| matches!(c, IndexChange::TableAdd { number, .. } if number == "0004")));
        assert!(changes
            .iter()
            .any(|c| matches!(c, IndexChange::TableRemove { number } if number == "0003")));
    }

    #[test]
    fn test_compute_section_changes_with_path_strip_failure() {
        use chrono::NaiveDate;
        use std::env;

        let temp_dir = env::temp_dir();
        let docs_dir = temp_dir.join("test_path_strip");
        std::fs::create_dir_all(&docs_dir).ok();

        let parsed = ParsedIndex { table_entries: HashMap::new(), state_sections: HashMap::new() };

        let mut doc_map = HashMap::new();

        // Create a doc with path outside docs_dir
        let other_dir = temp_dir.join("other_location");
        std::fs::create_dir_all(&other_dir).ok();
        let doc_path = other_dir.join("0001-external.md");

        let doc = DesignDoc {
            metadata: crate::doc::DocMetadata {
                number: 1,
                title: "External".to_string(),
                author: "Author".to_string(),
                component: None,
                tags: Vec::new(),
                created: NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
                updated: NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
                state: DocState::Draft,
                supersedes: None,
                superseded_by: None,
                version: "1.0".to_string(),
            },
            content: "content".to_string(),
            path: doc_path.clone(),
        };
        doc_map.insert("0001".to_string(), doc);

        // Should handle gracefully when path can't be stripped
        let changes = compute_section_changes(&parsed, &doc_map, &docs_dir);

        // Should still produce a change, using full path
        assert!(!changes.is_empty());

        std::fs::remove_dir_all(&docs_dir).ok();
        std::fs::remove_dir_all(&other_dir).ok();
    }

    #[test]
    fn test_cleanup_formatting_header_followed_by_header() {
        let input = r#"## Section One
## Section Two"#;
        let output = cleanup_formatting(input);

        // Headers immediately following each other should be separated
        assert!(output.contains("Section One\n\n## Section Two"));
    }

    #[test]
    fn test_cleanup_formatting_bullet_after_header() {
        let input = r#"### Section
- Item"#;
        let output = cleanup_formatting(input);

        // Should have blank line between header and bullet
        assert!(output.contains("### Section\n\n- Item"));
    }

    #[test]
    fn test_cleanup_formatting_preserves_non_markdown_lines() {
        let input = r#"## Header

Regular text line
Another line

## Next"#;
        let output = cleanup_formatting(input);

        assert!(output.contains("Regular text line"));
        assert!(output.contains("Another line"));
    }

    #[test]
    fn test_cleanup_formatting_multiple_consecutive_blanks() {
        let input = "Line 1\n\n\n\n\nLine 2";
        let output = cleanup_formatting(input);

        // Should collapse multiple blank lines
        assert!(!output.contains("\n\n\n"));
        assert!(output.contains("Line 1\n\nLine 2"));
    }

    #[test]
    fn test_parse_table_with_number_header_row() {
        let content = r#"# Index

| Number | Title | State | Updated |
|--------|-------|-------|---------|
| Number | Title | State | Updated |
| 0001 | Valid | Draft | 2024-01-01 |
"#;
        let entries = parse_table(content);
        // Should skip the duplicate header row that says "Number"
        assert_eq!(entries.len(), 1);
        assert!(entries.contains_key("0001"));
    }

    #[test]
    fn test_get_docs_from_filesystem_all_states() {
        use std::env;

        let temp_dir = env::temp_dir();
        let test_dir = temp_dir.join("test_all_states");
        std::fs::create_dir_all(&test_dir).ok();

        // Create directories for all states
        for state in DocState::all_states() {
            let state_dir = test_dir.join(state.directory());
            std::fs::create_dir_all(&state_dir).ok();
            let filename = format!("test-{}.md", state.as_str().to_lowercase().replace(' ', "-"));
            std::fs::write(state_dir.join(filename), "content").ok();
        }

        let result = get_docs_from_filesystem(&test_dir).unwrap();

        // Should find docs in all state directories except dustbin states
        // We have 12 states total: 10 active states + 2 dustbin states (Removed, Overwritten)
        // Dustbin states are excluded from index scanning to prevent archived documents
        // from overwriting active documents
        assert_eq!(result.len(), 10);

        std::fs::remove_dir_all(&test_dir).ok();
    }
}

/// Clean up section formatting for consistent spacing
pub fn cleanup_formatting(content: &str) -> String {
    let lines: Vec<&str> = content.lines().collect();
    let mut result: Vec<String> = Vec::new();
    let mut i = 0;

    while i < lines.len() {
        let line = lines[i];

        // Check if this is a section header
        let is_h2 = line.starts_with("## ");
        let is_h3 = line.starts_with("### ");

        if is_h2 || is_h3 {
            // Remove trailing blank lines before header
            while !result.is_empty() && result.last().unwrap().is_empty() {
                result.pop();
            }

            // Add exactly one blank line before header (unless first line)
            if !result.is_empty() {
                result.push(String::new());
            }

            // Add the header
            result.push(line.to_string());

            // Skip blank lines after header
            let mut j = i + 1;
            while j < lines.len() && lines[j].is_empty() {
                j += 1;
            }

            // Add exactly one blank line after header
            if j < lines.len() && !lines[j].starts_with('#') {
                result.push(String::new());
            }

            i = j;
            continue;
        }

        // Check if this is a bullet item
        let is_bullet = line.starts_with("- [");

        if is_bullet {
            result.push(line.to_string());

            // Look ahead to next non-blank line
            let mut j = i + 1;
            while j < lines.len() && lines[j].is_empty() {
                j += 1;
            }

            // If next non-blank is also a bullet, skip blank lines between them
            if j < lines.len() && lines[j].starts_with("- [") {
                i = j;
                continue;
            }

            i += 1;
            continue;
        }

        // For other lines, just add them (but collapse multiple blank lines)
        if line.is_empty() {
            if result.is_empty() || !result.last().unwrap().is_empty() {
                result.push(String::new());
            }
        } else {
            result.push(line.to_string());
        }

        i += 1;
    }

    // Ensure file ends with newline
    if !result.is_empty() && !result.last().unwrap().is_empty() {
        result.push(String::new());
    }

    result.join("\n")
}