tedi 0.16.1

Personal productivity CLI for task tracking, time management, and GitHub issue integration
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
//! Core issue data structures and parsing/serialization.
//!
//! This module contains the pure Issue type with parsing and serialization.

use std::{collections::HashMap, path::PathBuf};

use arrayvec::ArrayString;
use copy_arrayvec::CopyArrayVec;
use jiff::Timestamp;
use serde::{Deserialize, Serialize};
use url::Url;

use super::events::{indent_into, preserve_paragraph_spacing, wrap_inline_in_paragraphs};

/// Maximum title length enforced by Github.
pub const MAX_TITLE_LENGTH: usize = 256;

/// Maximum index depth (lineage + the issue itself).
pub const MAX_INDEX_DEPTH: usize = MAX_LINEAGE_DEPTH + 1;
pub const MAX_LINEAGE_DEPTH: usize = 8;
pub type IssueChildren<T> = HashMap<IssueSelector, T>;
use super::{
	IssueMarker, Marker,
	blocker::BlockerSequence,
	error::{ParseContext, ParseError},
};

/// Trait for lazily loading an issue from a source.
///
/// `S` is the source type directly (e.g., `LocalIssueSource<FsReader>`, `RemoteSource`).
/// Methods load data on demand; `&mut self` allows caching intermediate results.
#[allow(async_fn_in_trait)]
pub trait LazyIssue<S: Clone + std::fmt::Debug>: Sized {
	/// Error type for operations on this source.
	type Error: std::error::Error;

	/// Resolve parent_index from the source.
	/// Returns None for root-level issues (no parent).
	async fn parent_index(source: &S) -> Result<Option<IssueIndex>, Self::Error>;
	async fn identity(&mut self, source: S) -> Result<IssueIdentity, Self::Error>;
	async fn contents(&mut self, source: S) -> Result<IssueContents, Self::Error>;
	async fn children(&mut self, source: S) -> Result<IssueChildren<Issue>, Self::Error>;

	/// Load a full issue tree from the source.
	/// Default implementation calls parent_index, then populates identity, contents, and children.
	async fn load(source: S) -> Result<Issue, Self::Error>
	where
		Issue: LazyIssue<S, Error = Self::Error>, {
		let parent_index = <Issue as LazyIssue<S>>::parent_index(&source).await?.expect("load requires parent_index to be Some");
		let mut issue = Issue::empty_local(parent_index);
		<Issue as LazyIssue<S>>::identity(&mut issue, source.clone()).await?;
		<Issue as LazyIssue<S>>::contents(&mut issue, source.clone()).await?;
		Box::pin(<Issue as LazyIssue<S>>::children(&mut issue, source)).await?;
		Ok(issue)
	}
}
/// Repository identification: owner and repo name.
/// Uses fixed-size `ArrayString`s to be `Copy`.
/// GitHub limits: owner max 39 chars, repo max 100 chars.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct RepoInfo {
	/// Repository owner (fixed max length; following Github spec)
	owner: ArrayString<39>,
	/// Repository name (fixed max length; following Github spec)
	repo: ArrayString<100>,
}

impl RepoInfo {
	/// Create a new RepoInfo.
	/// Panics if owner exceeds 39 chars or repo exceeds 100 chars.
	pub fn new(owner: &str, repo: &str) -> Self {
		Self {
			owner: ArrayString::from(owner).expect("owner name too long (max 39 chars)"),
			repo: ArrayString::from(repo).expect("repo name too long (max 100 chars)"),
		}
	}

	/// Get the owner.
	pub fn owner(&self) -> &str {
		self.owner.as_str()
	}

	/// Get the repo.
	pub fn repo(&self) -> &str {
		self.repo.as_str()
	}
}

impl From<(&str, &str)> for RepoInfo {
	fn from((owner, repo): (&str, &str)) -> Self {
		Self::new(owner, repo)
	}
}

/// A Github issue identifier. Wraps a URL and derives all properties on demand.
/// Format: `https://github.com/{owner}/{repo}/issues/{number}`
#[derive(Clone, Debug, derive_more::Deref, derive_more::DerefMut, Eq, Hash, PartialEq)]
pub struct IssueLink(Url);

impl IssueLink /*{{{1*/ {
	/// Create from a URL. Returns None if not a valid Github issue URL.
	pub fn new(url: Url) -> Option<Self> {
		// Validate it's a Github issue URL
		if url.host_str() != Some("github.com") {
			return None;
		}
		let segments: Vec<_> = url.path_segments()?.collect();
		// Must be: owner/repo/issues/number
		if segments.len() < 4 || segments[2] != "issues" {
			return None;
		}
		// Number must be valid
		segments[3].parse::<u64>().ok()?;
		Some(Self(url))
	}

	/// Parse from a URL string.
	pub fn parse(url: &str) -> Option<Self> {
		let url = Url::parse(url).ok()?;
		Self::new(url)
	}

	/// Get the underlying URL.
	pub fn url(&self) -> &Url {
		&self.0
	}

	/// Get the repository info (owner and repo).
	pub fn repo_info(&self) -> RepoInfo {
		let mut segments = self.0.path_segments().unwrap();
		let owner = segments.next().unwrap();
		let repo = segments.next().unwrap();
		RepoInfo::new(owner, repo)
	}

	/// Get the owner (first path segment).
	pub fn owner(&self) -> &str {
		self.0.path_segments().unwrap().next().unwrap()
	}

	/// Get the repo (second path segment).
	pub fn repo(&self) -> &str {
		self.0.path_segments().unwrap().nth(1).unwrap()
	}

	/// Get the issue number (fourth path segment).
	pub fn number(&self) -> u64 {
		self.0.path_segments().unwrap().nth(3).unwrap().parse().unwrap()
	}

	/// Build URL string.
	pub fn as_str(&self) -> &str {
		self.0.as_str()
	}
}
//,}}}1

/// Identity of a comment - either linked to Github or pending creation.
/// Note: The first comment (issue body) is always `Body`, not `Linked` or `Pending`.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub enum CommentIdentity {
	/// This is the issue body (first comment), not a separate Github comment
	Body,
	/// Comment exists on Github with this ID, created by given user
	Created { user: String, id: u64 },
	/// Comment is pending creation on Github (will be created in post-sync)
	#[default]
	Pending,
}

//,}}}1

/// Close state of an issue.
/// Maps to Github's binary open/closed, but locally supports additional variants.
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
pub enum CloseState {
	/// Issue is open: `- [ ]`
	#[default]
	Open,
	/// Issue is closed normally: `- [x]`
	Closed,
	/// Issue was closed as not planned: `- [-]`
	/// Treated same as Closed for storage (embedded with .bak)
	NotPlanned,
	/// Issue is a duplicate of another issue: `- [123]`
	/// The number references another issue in the same repo.
	/// These should be removed from local storage entirely.
	Duplicate(u64),
}
impl CloseState /*{{{1*/ {
	/// Returns true if the issue is closed (any close variant)
	pub fn is_closed(&self) -> bool {
		!matches!(self, CloseState::Open)
	}

	/// Returns true if this close state means the issue should be removed from local storage
	pub fn should_remove(&self) -> bool {
		matches!(self, CloseState::Duplicate(_))
	}

	/// Convert to Github API state string
	pub fn to_github_state(&self) -> &'static str {
		match self {
			CloseState::Open => "open",
			_ => "closed",
		}
	}

	/// Convert to Github API state_reason string (for closed issues)
	pub fn to_github_state_reason(&self) -> Option<&'static str> {
		match self {
			CloseState::Open => None,
			CloseState::Closed => Some("completed"),
			CloseState::NotPlanned => Some("not_planned"),
			CloseState::Duplicate(_) => Some("duplicate"),
		}
	}

	/// Create from Github API state and state_reason.
	///
	/// # Panics
	/// Panics if state_reason is "duplicate" - duplicates must be filtered before calling this.
	pub fn from_github(state: &str, state_reason: Option<&str>) -> Self {
		assert!(state_reason != Some("duplicate"), "Duplicate issues must be filtered before calling from_github");

		match (state, state_reason) {
			("open", _) => CloseState::Open,
			("closed", Some("not_planned")) => CloseState::NotPlanned,
			("closed", Some("completed") | None) => CloseState::Closed,
			("closed", Some(unknown)) => {
				tracing::warn!("Unknown state_reason '{unknown}', treating as Closed");
				CloseState::Closed
			}
			(unknown, _) => {
				tracing::warn!("Unknown state '{unknown}', treating as Open");
				CloseState::Open
			}
		}
	}

	/// Returns true if this represents a duplicate (should be filtered from fetch results)
	pub fn is_duplicate_reason(state_reason: Option<&str>) -> bool {
		state_reason == Some("duplicate")
	}

	/// Parse from checkbox content (the character(s) inside `[ ]`)
	pub fn from_checkbox(content: &str) -> Result<Self, String> {
		let content = content.trim();
		match content {
			"" | " " => Ok(CloseState::Open),
			"x" | "X" => Ok(CloseState::Closed),
			"-" => Ok(CloseState::NotPlanned),
			s => s.parse::<u64>().map(CloseState::Duplicate).map_err(|_| s.to_string()),
		}
	}

	/// Convert to checkbox character(s) for serialization
	pub fn to_checkbox_contents(&self) -> String {
		match self {
			CloseState::Open => " ".to_string(),
			CloseState::Closed => "x".to_string(),
			CloseState::NotPlanned => "-".to_string(),
			CloseState::Duplicate(n) => n.to_string(),
		}
	}
}

/// Timestamps tracking when individual fields of an issue were last changed.
/// Each node keeps change info about itself only (not sub-issues).
/// Used for sync conflict resolution.
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
pub struct IssueTimestamps {
	/// Last change to the issue title. Optional because we can't always get this from GitHub.
	pub title: Option<Timestamp>,
	/// Last change to the issue description/body (includes blockers). Optional because we can't always get this from GitHub.
	pub description: Option<Timestamp>,
	/// Last change to labels. Optional because we can't always get this from GitHub.
	pub labels: Option<Timestamp>,
	/// Last state change (open/closed). Optional because we can't always get this from GitHub.
	pub state: Option<Timestamp>,
	/// Per-comment timestamps (indexed by position, excluding body at index 0).
	/// Empty vec means no timestamp info available.
	pub comments: Vec<Timestamp>,
}
impl IssueTimestamps {
	pub fn now() -> Self {
		let now = Timestamp::now();
		Self {
			title: Some(now),
			description: Some(now),
			labels: Some(now),
			state: Some(now),
			comments: Vec::new(),
		}
	}

	/// Update timestamps based on what changed between old and new issue contents.
	/// Sets the current time for any field that changed.
	pub fn update_from_diff(&mut self, old: &super::IssueContents, new: &super::IssueContents) {
		let now = Timestamp::now();

		if old.title != new.title {
			self.title = Some(now);
		}

		// Compare body (first comment) and blockers for description changes
		let old_body = old.comments.description();
		let new_body = new.comments.description();
		if old_body != new_body || old.blockers != new.blockers {
			self.description = Some(now);
		}

		if old.labels != new.labels {
			self.labels = Some(now);
		}

		if old.state != new.state {
			self.state = Some(now);
		}

		// Per-comment timestamps (skip body at index 0)
		let old_comments: Vec<_> = old.comments.iter().skip(1).collect();
		let new_comments: Vec<_> = new.comments.iter().skip(1).collect();
		let new_comment_count = new_comments.len();

		// Resize timestamps vec to match new comment count
		self.comments.resize(new_comment_count, now);

		// Update timestamps for changed comments
		for (i, (old_c, new_c)) in old_comments.iter().zip(&new_comments).enumerate() {
			if old_c.body.to_string() != new_c.body.to_string() {
				self.comments[i] = now;
			}
		}
		// New comments (beyond old length) already have `now` from resize
	}
}

/// Metadata for an issue linked to Github.
/// Parent chain is stored in `IssueIdentity.parent_index`.
#[derive(Clone, Debug, PartialEq)]
pub struct LinkedIssueMeta {
	/// User who created the issue. None = unknown (treated as ourselves).
	pub user: Option<String>,
	/// Link to the issue on Github
	//TODO: I'm pretty sure we can remove this field and just construct it from context when needed
	link: IssueLink,
	/// Timestamps of last changes to individual fields.
	/// Used for sync conflict resolution.
	pub timestamps: IssueTimestamps,
}
impl LinkedIssueMeta {
	pub fn new(user: Option<String>, link: IssueLink, timestamps: IssueTimestamps) -> Self {
		if user.is_none() {
			tracing::warn!("LinkedIssueMeta created without user");
		}
		Self { user, link, timestamps }
	}

	/// Get the link.
	pub fn link(&self) -> &IssueLink {
		&self.link
	}

	/// Get the repository owner from the link.
	pub fn owner(&self) -> &str {
		self.link.owner()
	}

	/// Get the repository name from the link.
	pub fn repo(&self) -> &str {
		self.link.repo()
	}

	/// Get the issue number from the link.
	pub fn number(&self) -> u64 {
		self.link.number()
	}
}

/// Identity of an issue - has optional parent_index (for location) with remote connection info.
#[derive(Clone, Debug)]
pub struct IssueIdentity {
	/// Parent's IssueIndex. For root it's the default variant with null ancestry (but owner+repo specified)
	/// NB: it's index of the PARENT, not ourselves. Done this way to allow for eg title changes.
	pub parent_index: IssueIndex,
	/// Whether this issue belongs to a virtual project (local-only, never synced to Github).
	pub is_virtual: bool,
	/// Linked Github metadata, if this issue has been synced.
	/// `None` = pending creation (or virtual). `Some(_)` = linked to Github.
	pub remote: Option<Box<LinkedIssueMeta>>,
}
impl IssueIdentity {
	/// Create a new linked Github issue identity.
	/// If `parent_index` is None, derives repo_only from the link.
	pub fn new_linked(parent_index: Option<IssueIndex>, user: Option<String>, link: IssueLink, timestamps: IssueTimestamps) -> Self {
		let parent_index = parent_index.unwrap_or_else(|| {
			let repo_info = link.repo_info();
			IssueIndex::repo_only(repo_info)
		});
		Self {
			parent_index,
			is_virtual: false,
			remote: Some(Box::new(LinkedIssueMeta::new(user, link, timestamps))),
		}
	}

	/// Create a new pending Github issue identity (will be created on first sync).
	pub fn pending(parent_index: IssueIndex) -> Self {
		Self {
			parent_index,
			is_virtual: false,
			remote: None,
		}
	}

	/// Create a new virtual (local-only) issue identity.
	pub fn virtual_issue(parent_index: IssueIndex) -> Self {
		Self {
			parent_index,
			is_virtual: true,
			remote: None,
		}
	}

	/// Check if this issue is linked to Github.
	pub fn is_linked(&self) -> bool {
		self.remote.is_some()
	}

	/// Check if this issue is local only (pending creation on Github).
	/// Note: Virtual issues are also local-only but should be checked via is_virtual().
	pub fn is_local(&self) -> bool {
		self.remote.is_none() && !self.is_virtual
	}

	/// Get the linked metadata if linked.
	pub fn as_linked(&self) -> Option<&LinkedIssueMeta> {
		self.remote.as_deref()
	}

	/// Get mutable access to the linked metadata.
	pub fn mut_linked_issue_meta(&mut self) -> Option<&mut LinkedIssueMeta> {
		self.remote.as_deref_mut()
	}

	/// Get the issue link if linked.
	pub fn link(&self) -> Option<&IssueLink> {
		self.as_linked().map(|m| &m.link)
	}

	/// Get the issue number if linked.
	pub fn number(&self) -> Option<u64> {
		self.link().map(|l| l.number())
	}

	/// Get the URL string if linked.
	pub fn url_str(&self) -> Option<&str> {
		self.link().map(|l| l.as_str())
	}

	/// Get the user who created this issue if linked and known.
	pub fn user(&self) -> Option<&str> {
		self.as_linked().and_then(|m| m.user.as_deref())
	}

	/// Check if this issue is owned by the current user.
	/// True if: virtual, or linked with unknown user (None), or linked with matching user.
	pub fn is_owned(&self) -> bool {
		self.is_virtual
			|| match self.as_linked() {
				None => false,
				Some(meta) => meta.user.as_ref().is_none_or(|u| crate::current_user::is(u)),
			}
	}

	/// Get the timestamps if linked.
	pub fn timestamps(&self) -> Option<&IssueTimestamps> {
		self.as_linked().map(|m| &m.timestamps)
	}

	/// Get the repository info.
	pub fn repo_info(&self) -> RepoInfo {
		self.parent_index.repo_info()
	}

	/// Get owner.
	pub fn owner(&self) -> &str {
		self.parent_index.owner()
	}

	/// Get repo.
	pub fn repo(&self) -> &str {
		self.parent_index.repo()
	}

	/// Get lineage (parent issue numbers).
	/// For root issues: empty slice.
	/// For child issues: all GitId numbers in parent_index.
	///
	/// # Errors
	/// Returns `TitleInGitPathError` if any parent selector is a Title (pending issue).
	pub fn git_lineage(&self) -> Result<Vec<u64>, super::error::TitleInGitPathError> {
		self.parent_index.git_num_path()
	}

	/// Create a child's parent_index by appending this issue's number.
	/// Returns None if this issue is not linked (has no number to append).
	pub fn child_parent_index(&self) -> Option<IssueIndex> {
		self.number().map(|n| self.parent_index.child(IssueSelector::GitId(n)))
	}
}

/// Selector for identifying an issue within a repo.
/// GitId is preferred when available, as title can change.
/// Uses `ArrayString` for `Copy` semantics.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[allow(clippy::large_enum_variant)] // Intentional: Title variant is large for Copy semantics
pub enum IssueSelector {
	/// Github issue number (stable identifier)
	GitId(u64),
	/// Issue title (for pending issues not yet synced to Github)
	Title(ArrayString<MAX_TITLE_LENGTH>),
	/// Regex pattern for fuzzy matching (touch mode only, lower priority than Title)
	Regex(ArrayString<MAX_TITLE_LENGTH>),
}

impl IssueSelector {
	/// Create a Title selector from a string.
	/// Panics if title exceeds MAX_TITLE_LENGTH (256 chars).
	pub fn title(title: &str) -> Self {
		Self::Title(ArrayString::from(title).unwrap_or_else(|_| panic!("title too long (max {MAX_TITLE_LENGTH} chars): {}", title.len())))
	}

	/// Try to create a Title selector from a string.
	/// Returns None if title exceeds MAX_TITLE_LENGTH.
	pub fn try_title(title: &str) -> Option<Self> {
		ArrayString::from(title).ok().map(Self::Title)
	}

	/// Create a Regex selector from a pattern string.
	pub fn regex(pattern: &str) -> Self {
		Self::Regex(ArrayString::from(pattern).unwrap_or_else(|_| panic!("pattern too long (max {MAX_TITLE_LENGTH} chars): {}", pattern.len())))
	}
}

/// Minimal descriptor for locating an issue.
/// Contains repo info and a path of selectors from root to the target issue (inclusive).
/// Uses fixed-size storage to be `Copy`.
#[derive(Clone, Copy, Debug, derive_more::Deref, derive_more::DerefMut, Eq, PartialEq)]
pub struct IssueIndex {
	repo_info: RepoInfo,
	/// Path from root to target issue (inclusive).
	#[deref]
	#[deref_mut]
	index: CopyArrayVec<IssueSelector, MAX_INDEX_DEPTH>,
}

impl IssueIndex {
	/// Create descriptor for a root-level issue.
	pub fn root(repo_info: RepoInfo, selector: IssueSelector) -> Self {
		let mut index = CopyArrayVec::new();
		index.push(selector);
		Self { repo_info, index }
	}

	/// Create descriptor with full index path.
	/// Panics if index exceeds MAX_INDEX_DEPTH.
	pub fn with_index(repo_info: RepoInfo, index: Vec<IssueSelector>) -> Self {
		Self {
			repo_info,
			index: index.into_iter().collect(),
		}
	}

	/// Create descriptor for repo only (no specific issue).
	pub fn repo_only(repo_info: RepoInfo) -> Self {
		Self {
			repo_info,
			index: CopyArrayVec::new(),
		}
	}

	/// Add a child selector, returning new descriptor.
	/// Panics if result would exceed MAX_INDEX_DEPTH.
	pub fn child(&self, selector: IssueSelector) -> Self {
		let mut index = self.index;
		index.push(selector);
		Self { repo_info: self.repo_info, index }
	}

	/// Get the index path.
	pub fn index(&self) -> &[IssueSelector] {
		&self.index
	}

	/// Get the repository info.
	pub fn repo_info(&self) -> RepoInfo {
		self.repo_info
	}

	/// Get the owner.
	pub fn owner(&self) -> &str {
		self.repo_info.owner()
	}

	/// Get the repo.
	pub fn repo(&self) -> &str {
		self.repo_info.repo()
	}

	/// Extract numeric issue numbers from the index (GitId selectors only).
	///
	/// # Errors
	/// Returns `TitleInGitPathError` if any selector is a Title (pending issue).
	pub fn git_num_path(&self) -> Result<Vec<u64>, super::error::TitleInGitPathError> {
		use miette::{NamedSource, SourceSpan};

		let mut result = Vec::with_capacity(self.index().len());
		let mut offset = format!("{}/{}", self.repo_info.owner(), self.repo_info.repo()).len();

		for selector in self.index() {
			match selector {
				IssueSelector::GitId(n) => {
					let s = format!("/{n}");
					offset += s.len();
					result.push(*n);
				}
				IssueSelector::Title(title) | IssueSelector::Regex(title) => {
					let span: SourceSpan = (offset + 1, title.len()).into(); // +1 to skip the '/'
					return Err(super::error::TitleInGitPathError {
						index_display: NamedSource::new("IssueIndex", self.to_string()),
						span,
					});
				}
			}
		}
		Ok(result)
	}

	/// Get the issue's own number if the last selector is a GitId.
	pub fn issue_number(&self) -> Option<u64> {
		match self.index().last() {
			Some(IssueSelector::GitId(n)) => Some(*n),
			_ => None,
		}
	}

	/// Get the parent's IssueIndex (all selectors except the last one).
	/// For repo-only or single-selector indices, returns repo_only.
	pub fn parent(&self) -> Option<Self> {
		if self.index.is_empty() {
			None
		} else {
			let mut index = self.index;
			index.pop();
			Some(Self { repo_info: self.repo_info, index })
		}
	}
}

impl std::fmt::Display for IssueIndex {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		write!(f, "{}/{}", self.repo_info.owner(), self.repo_info.repo())?;
		for selector in self.index() {
			match selector {
				IssueSelector::GitId(n) => write!(f, "/{n}")?,
				IssueSelector::Title(t) | IssueSelector::Regex(t) => write!(f, "/{t}")?,
			}
		}
		Ok(())
	}
}

/// Error returned when parsing an `IssueIndex` from a string fails.
#[derive(Debug, thiserror::Error)]
#[error("{0}")]
pub struct IssueIndexParseError(String);

impl std::str::FromStr for IssueIndex {
	type Err = IssueIndexParseError;

	fn from_str(s: &str) -> Result<Self, Self::Err> {
		let parts: Vec<&str> = s.split('/').collect();
		if parts.len() < 2 {
			return Err(IssueIndexParseError(format!("IssueIndex requires at least owner/repo, got: {s}")));
		}
		let repo_info = RepoInfo::new(parts[0], parts[1]);
		let selectors: Vec<IssueSelector> = parts[2..]
			.iter()
			.map(|p| match p.parse::<u64>() {
				Ok(n) => IssueSelector::GitId(n),
				Err(_) => IssueSelector::title(p),
			})
			.collect();
		Ok(Self::with_index(repo_info, selectors))
	}
}

impl From<&Issue> for IssueIndex {
	fn from(issue: &Issue) -> Self {
		// Build this issue's IssueIndex by extending parent_index with this issue's selector
		let parent_index = issue.identity.parent_index;

		if let Some(n) = issue.git_id() {
			// Issue has a number - add it as the last selector
			parent_index.child(IssueSelector::GitId(n))
		} else {
			// Pending issue - add title as last selector to distinguish from parent-only paths
			parent_index.child(IssueSelector::title(&issue.contents.title))
		}
	}
}

/// A comment in the issue conversation (first one is always the issue body)
#[derive(Clone, Debug, Default, derive_more::Deref, PartialEq)]
pub struct Comment {
	/// Comment identity - body, linked to Github, or pending creation
	pub identity: CommentIdentity,
	/// The markdown body stored as parsed events for lossless roundtripping
	#[deref]
	pub body: super::Events,
}
impl Comment {
	/// Get the comment ID if linked.
	pub fn id(&self) -> Option<u64> {
		match &self.identity {
			CommentIdentity::Created { id, .. } => Some(*id),
			CommentIdentity::Body | CommentIdentity::Pending => None,
		}
	}

	/// Get the user who created this comment if linked.
	pub fn user(&self) -> Option<&str> {
		match &self.identity {
			CommentIdentity::Created { user, .. } => Some(user),
			CommentIdentity::Body | CommentIdentity::Pending => None,
		}
	}

	/// Check if this is a Github comment (not the issue body).
	pub fn is_comment(&self) -> bool {
		!matches!(self.identity, CommentIdentity::Body)
	}

	/// Check if this comment is pending creation.
	pub fn is_pending(&self) -> bool {
		matches!(self.identity, CommentIdentity::Pending)
	}
}
#[derive(Clone, Debug, derive_more::Deref, derive_more::DerefMut, PartialEq)]
pub struct Comments(pub Vec<Comment>);
impl Comments {
	pub fn description(&self) -> String {
		self.first().map(|c| c.body.to_string()).expect("`new` should've asserted that description comment was provided")
	}

	pub fn new(v: Vec<Comment>) -> Self {
		assert!(
			v.first().is_some_and(|c| matches!(c.identity, CommentIdentity::Body)),
			"Trying to instantiate issue `Comments` without description"
		);
		{
			// creation of pending comments intertwined with submitted ones is invalid, - they're linked //Q: should I use a linked list here?
			let first_pending_id = v.iter().position(|c| c.is_pending());
			if let Some(id) = first_pending_id {
				assert!(v.iter().rev().take(v.len() - id).all(|c| c.is_pending()))
			}
		}
		Self(v)
	}
}
impl Default for Comments {
	fn default() -> Self {
		Self(vec![Comment {
			identity: CommentIdentity::Body,
			..Default::default()
		}])
	}
}
impl From<Vec<Comment>> for Comments {
	fn from(value: Vec<Comment>) -> Self {
		match value.is_empty() {
			true => Self::default(),
			false => Self::new(value),
		}
	}
}

/// The full editable content of an issue.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct IssueContents {
	pub title: String,
	pub labels: Vec<String>,
	pub state: CloseState,
	pub comments: Comments,
	pub blockers: BlockerSequence,
}
/// Complete representation of an issue file
#[derive(Clone, Debug, PartialEq)]
pub struct Issue {
	/// Identity - linked to Github or local only
	pub identity: IssueIdentity,
	pub contents: IssueContents,
	/// Sub-issues keyed by selector
	pub children: HashMap<IssueSelector, Issue>,
}
impl Issue /*{{{1*/ {
	/// Iterate over children by mutable reference.
	pub fn iter_mut(&mut self) -> std::collections::hash_map::IterMut<'_, IssueSelector, Issue> {
		self.children.iter_mut()
	}

	/// Check if this issue is local only (not yet on Github).
	pub fn is_local(&self) -> bool {
		self.identity.is_local()
	}

	/// Create an empty local issue with the given parent_index.
	/// Used as initial state when lazily loading an issue.
	pub fn empty_local(parent_index: IssueIndex) -> Self {
		Self {
			identity: IssueIdentity::pending(parent_index),
			contents: IssueContents::default(),
			children: HashMap::new(),
		}
	}

	/// Create a new pending issue with the given title and parent_index.
	///
	/// If `virtual_project` is true, creates a virtual issue (local-only, no Github sync).
	/// Otherwise creates a pending Github issue that will be created on first sync.
	pub fn pending_with_parent(title: impl Into<String>, parent_index: IssueIndex, virtual_project: bool) -> Self {
		let identity = if virtual_project {
			IssueIdentity::virtual_issue(parent_index)
		} else {
			IssueIdentity::pending(parent_index)
		};
		let contents = IssueContents {
			title: title.into(),
			..Default::default()
		};
		Self {
			identity,
			contents,
			children: HashMap::new(),
		}
	}

	/// Create a new pending issue from an IssueIndex descriptor.
	/// The last element of descriptor.index() must be a Title selector.
	///
	/// If `virtual_project` is true, creates a virtual issue (local-only, no Github sync).
	/// Otherwise creates a pending Github issue that will be created on first sync.
	pub fn pending_from_descriptor(descriptor: &IssueIndex, virtual_project: bool) -> Self {
		let index = descriptor.index();
		let (title, parent_selectors): (String, Vec<IssueSelector>) = match index.last() {
			Some(IssueSelector::Title(t)) => {
				let selectors = index[..index.len() - 1].to_vec();
				(t.to_string(), selectors)
			}
			Some(IssueSelector::GitId(_)) => panic!("pending_from_descriptor requires last selector to be Title"),
			Some(IssueSelector::Regex(_)) => panic!("pending_from_descriptor requires last selector to be Title, not Regex"),
			None => panic!("pending_from_descriptor requires non-empty index"),
		};
		let parent_index = IssueIndex::with_index(descriptor.repo_info(), parent_selectors);
		Self::pending_with_parent(title, parent_index, virtual_project)
	}

	/// Check if this issue is linked to Github.
	pub fn is_linked(&self) -> bool {
		self.identity.is_linked()
	}

	/// Get the issue number if linked to Github.
	pub fn git_id(&self) -> Option<u64> {
		self.identity.number()
	}

	/// Get the URL string if linked to Github.
	pub fn url_str(&self) -> Option<&str> {
		self.identity.url_str()
	}

	/// Get the user who created this issue if linked to Github.
	pub fn user(&self) -> Option<&str> {
		self.identity.user()
	}

	/// Update timestamps based on what changed compared to old issue.
	/// This should be called after local modifications to track when fields changed.
	/// Recursively updates children's timestamps too.
	fn update_timestamps_from_diff(&mut self, old: &Issue) {
		if let Some(linked) = self.identity.mut_linked_issue_meta() {
			linked.timestamps.update_from_diff(&old.contents, &self.contents);
		}

		// Recursively update children's timestamps
		// Match children by selector
		for (selector, new_child) in &mut self.children {
			if let Some(old_child) = old.children.get(selector) {
				new_child.update_timestamps_from_diff(old_child);
			}
			// New children (not in old) don't need timestamp updates - they'll use defaults
		}
	}

	/// If this issue is closed, propagate its exact close state to all children recursively.
	fn propagate_closed(&mut self) {
		if !self.contents.state.is_closed() {
			return;
		}
		let state = self.contents.state.clone();
		for child in self.children.values_mut() {
			child.contents.state = state.clone();
			child.propagate_closed();
		}
	}

	/// Post-update hook: call after any modification to an issue.
	/// Updates timestamps from diff and propagates closed state to children.
	pub fn post_update(&mut self, old: &Issue) {
		self.update_timestamps_from_diff(old);
		self.propagate_closed();
	}

	/// Get parent_index.
	pub fn parent_index(&self) -> IssueIndex {
		self.identity.parent_index
	}

	pub fn selector(&self) -> IssueSelector {
		match self.git_id() {
			Some(n) => IssueSelector::GitId(n),
			None => IssueSelector::title(&self.contents.title),
		}
	}

	/// Get full index pointing to this issue.
	/// Combines parent_index with this issue's selector.
	pub fn full_index(&self) -> IssueIndex {
		self.identity.parent_index.child(self.selector())
	}

	/// Get lineage (parent issue numbers).
	///
	/// # Errors
	/// Returns `TitleInGitPathError` if any parent selector is a Title (pending issue).
	pub fn lineage(&self) -> Result<Vec<u64>, super::error::TitleInGitPathError> {
		self.identity.git_lineage()
	}

	/// Get repository info.
	pub fn repo_info(&self) -> RepoInfo {
		self.identity.repo_info()
	}

	/// Get the full issue body including blockers section as events.
	/// This is what should be synced to Github as the issue body.
	pub fn body(&self) -> super::Events {
		//NB: DO NOT CHANGE Output Type
		let mut events: Vec<super::OwnedEvent> = self.contents.comments.first().map(|c| c.body.to_vec()).unwrap_or_default();
		if !self.contents.blockers.is_empty() {
			events.push(super::OwnedEvent::Start(super::OwnedTag::Heading {
				level: pulldown_cmark::HeadingLevel::H1,
				id: None,
				classes: Vec::new(),
				attrs: Vec::new(),
			}));
			events.push(super::OwnedEvent::Text("Blockers".to_string()));
			events.push(super::OwnedEvent::End(super::OwnedTagEnd::Heading(pulldown_cmark::HeadingLevel::H1)));
			events.extend(self.contents.blockers.to_events());
		}
		events.into()
	}

	/// Combine a HollowIssue (identity/metadata) with a VirtualIssue (parsed content) into a full Issue.
	///
	/// The hollow provides identity (remote info, timestamps) while virtual provides contents.
	/// `is_virtual` comes from project metadata - the hollow itself doesn't know.
	/// For children, recursively combines hollow.children with virtual.children by selector.
	///
	/// # Errors
	/// Returns `IssueError::ErroneousComposition` if a git-linked child (GitId selector) exists
	/// in virtual but not in hollow — either an internal bug or user manually embedded a linked marker.
	pub fn from_combined(hollow: HollowIssue, virtual_issue: VirtualIssue, parent_idx: IssueIndex, is_virtual: bool) -> Result<Self, super::error::IssueError> {
		let identity = IssueIdentity {
			parent_index: parent_idx,
			is_virtual,
			remote: hollow.remote.clone(),
		};

		// Recursively combine children
		let mut children = HashMap::new();
		for (selector, virtual_child) in virtual_issue.children {
			let child_hollow = match hollow.children.get(&selector) {
				Some(ch) => ch.clone(),
				None => {
					if let IssueSelector::GitId(n) = selector {
						return Err(super::error::IssueError::ErroneousComposition {
							issue_number: n,
							detail: "either internal bug (HollowIssue was constructed incorrectly) or user manually embedded a `<!-- @user url -->` marker, which is not permitted"
								.to_string(),
						});
					}
					HollowIssue::default()
				}
			};

			// Build child's parent_index
			let child_parent_idx = if let Some(meta) = &identity.remote {
				parent_idx.child(IssueSelector::GitId(meta.number()))
			} else {
				parent_idx
			};

			let child = Self::from_combined(child_hollow, virtual_child, child_parent_idx, is_virtual)?;
			children.insert(selector, child);
		}

		Ok(Issue {
			identity,
			contents: virtual_issue.contents,
			children,
		})
	}

	/// Parse `@user url#issuecomment-id` format into CommentIdentity.
	/// Returns Pending if parsing fails.
	fn parse_comment_identity(s: &str) -> CommentIdentity {
		let s = s.trim();

		// Format: `@username url#issuecomment-123`
		if let Some(rest) = s.strip_prefix('@')
			&& let Some(space_idx) = rest.find(' ')
		{
			let user = rest[..space_idx].to_string();
			let url = rest[space_idx + 1..].trim();
			if let Some(id) = url.split("#issuecomment-").nth(1).and_then(|s| s.parse().ok()) {
				return CommentIdentity::Created { user, id };
			}
		}

		CommentIdentity::Pending
	}

	//==========================================================================
	// Serialization Methods
	//==========================================================================

	/// Serialize for virtual file representation (human-readable, full tree).
	/// Creates a complete markdown file with all children recursively embedded.
	/// Used for temp files in /tmp where user views/edits the full issue tree.
	pub fn serialize_virtual(&self) -> String {
		self.serialize_virtual_at_depth(0, true)
	}

	/// Internal: serialize virtual representation using milestone-style rendering.
	///
	/// Renders title line via cmark as a standalone list item, then renders body/comments/blockers/children
	/// separately and indents them under the title. This matches how `milestone_embed::serialize_item` works,
	/// avoiding cmark's paragraph spacing that would insert blank lines between title and body.
	fn serialize_virtual_at_depth(&self, depth: usize, include_children: bool) -> String {
		use super::{OwnedEvent, OwnedTag, OwnedTagEnd};

		if self.identity.is_virtual {
			assert!(self.user().is_none(), "virtual issue must not have a user tag, got: {:?}", self.user());
		}

		let content_indent = "  ".repeat(depth + 1);
		let mut out = String::new();

		// === Title line: render via cmark as standalone list item ===
		let checkbox_contents = self.contents.state.to_checkbox_contents();
		let issue_marker = IssueMarker::from(&self.identity);
		let labels_part = if self.contents.labels.is_empty() {
			String::new()
		} else {
			format!("({}) ", self.contents.labels.join(", "))
		};

		let title_str: String = super::Events::from(vec![
			OwnedEvent::Start(OwnedTag::List(None)),
			OwnedEvent::Start(OwnedTag::Item),
			OwnedEvent::CheckBox(checkbox_contents),
			OwnedEvent::Text(format!("{labels_part}{} ", self.contents.title)),
			OwnedEvent::InlineHtml(format!("<!-- {} -->", issue_marker.encode())),
			OwnedEvent::End(OwnedTagEnd::Item),
			OwnedEvent::End(OwnedTagEnd::List(false)),
		])
		.into();

		// Indent the title line to the correct depth
		let depth_indent = "  ".repeat(depth);
		for line in title_str.lines() {
			out.push_str(&depth_indent);
			out.push_str(line);
			out.push('\n');
		}

		let is_owned = self.identity.is_owned();

		// Helper: render content below title, indented under the item
		let mut content = String::new();

		// === Body (first comment) ===
		if let Some(body_comment) = self.contents.comments.first()
			&& !body_comment.body.is_empty()
		{
			let body_str: String = super::Events::from(body_comment.body.to_vec()).into();
			if !is_owned {
				// Extra indent for unowned issues
				indent_into(&mut content, &body_str, "  ");
			} else {
				content.push_str(&body_str);
			}
		}

		/// Ensure `content` ends with `\n\n` (blank line separator) when it has trailing content.
		/// cmark rendering sometimes omits trailing newlines, so we normalize before inserting separators.
		fn ensure_blank_line(content: &mut String) {
			if content.is_empty() {
				return;
			}
			if !content.ends_with('\n') {
				content.push('\n');
			}
			if content.lines().last().is_some_and(|l| !l.trim().is_empty()) {
				content.push('\n');
			}
		}

		// === Additional comments ===
		for comment in self.contents.comments.iter().skip(1) {
			if self.identity.is_virtual {
				assert!(
					!comment.is_comment() || comment.user().is_none(),
					"virtual issue must not have linked comments, got: {:?}",
					comment.identity
				);
			}
			let comment_is_owned = comment.user().is_none() || comment.user().is_some_and(crate::current_user::is);

			// Blank line before comment separator
			ensure_blank_line(&mut content);

			// Comment separator marker
			let marker_html = match &comment.identity {
				CommentIdentity::Body | CommentIdentity::Pending => Marker::NewComment.encode(),
				CommentIdentity::Created { user, id } => {
					let url = self.url_str().expect("remote must be initialized");
					format!("<!-- @{user} {url}#issuecomment-{id} -->")
				}
			};
			content.push_str(&marker_html);
			content.push('\n');

			// Comment body
			if !comment.body.is_empty() {
				let body_str: String = super::Events::from(comment.body.to_vec()).into();
				if !comment_is_owned {
					indent_into(&mut content, &body_str, "  ");
				} else {
					content.push_str(&body_str);
				}
			}
		}

		// === Blockers section ===
		if !self.contents.blockers.is_empty() {
			ensure_blank_line(&mut content);
			let header = crate::Header::new(1, "Blockers");
			content.push_str(&header.encode());
			content.push('\n');
			let blockers_str: String = String::from(&self.contents.blockers);
			content.push_str(&blockers_str);
		}

		if !content.is_empty() {
			indent_into(&mut out, &content, &content_indent);
		}

		// === Children — sort: open first by selector, then closed by selector ===
		if include_children {
			let (mut open, mut closed): (Vec<_>, Vec<_>) = self.children.iter().partition(|(_, child)| !child.contents.state.is_closed());
			open.sort_by_key(|(sel, _)| *sel);
			closed.sort_by_key(|(sel, _)| *sel);
			let sorted_children = open.into_iter().chain(closed);

			for (_, child) in sorted_children {
				if out.lines().last().is_some_and(|l| !l.trim().is_empty()) {
					out.push_str(&content_indent);
					out.push('\n');
				}

				if child.contents.state.is_closed() {
					let child_str = child.serialize_virtual_at_depth(depth + 1, true);
					let omitted_start = Marker::OmittedStart.encode();
					let omitted_end = Marker::OmittedEnd.encode();
					let child_content_indent = "  ".repeat(depth + 2);

					let mut lines = child_str.lines();
					// First line is the title — inject omitted marker
					if let Some(title_line) = lines.next() {
						out.push_str(&format!("{title_line} {omitted_start}\n"));
					}
					// Remaining lines are body content
					for line in lines {
						out.push_str(line);
						out.push('\n');
					}
					// Vim fold end
					out.push_str(&child_content_indent);
					out.push_str(&omitted_end);
					out.push('\n');
				} else {
					let child_str = child.serialize_virtual_at_depth(depth + 1, true);
					out.push_str(&child_str);
				}
			}
		}

		out
	}

	/// Serialize for filesystem storage (single node, no children).
	/// Children are stored in separate files within the parent's directory.
	pub fn serialize_filesystem(&self) -> String {
		self.serialize_virtual_at_depth(0, false)
	}

	/// Serialize for GitHub API (markdown body only, no local markers).
	/// This is what gets sent to GitHub as the issue body.
	/// Always outputs markdown format regardless of local file extension.
	pub fn render_github(&self) -> super::Events {
		//NB: DO NOT CHANGE Output Type
		// GitHub body is: body text + blockers section (if any)
		// No title line, no URL markers, no comments - just the body content
		self.body()
	}

	/// parse_virtual but ignore
	///
	/// Find the position (line, col) of the last blocker item in the serialized content.
	/// Returns None if there are no blockers.
	/// Line numbers are 1-indexed to match editor conventions.
	/// Column points to the first character of the item text (after `- `).
	pub fn find_last_blocker_position(&self) -> Option<(u32, u32)> {
		if self.contents.blockers.is_empty() {
			return None;
		}

		// Serialize and find the last blocker item line
		let serialized = self.serialize_virtual();
		let lines: Vec<&str> = serialized.lines().collect();

		// Find where blockers section starts
		let blockers_header = crate::Header::new(1, "Blockers").encode();
		let blockers_start_idx = lines.iter().position(|line| line.trim() == blockers_header)?;

		// Track the last line that's a blocker item (starts with `- ` but not `- [` which is sub-issue)
		let mut last_item_line_num: Option<u32> = None;
		let mut last_item_col: Option<u32> = None;

		for (offset, line) in lines[blockers_start_idx + 1..].iter().enumerate() {
			let trimmed = line.trim();

			// Check if we've reached sub-issues (they start with `- [`)
			if trimmed.starts_with("- [") {
				break;
			}

			// A blocker item starts with `- ` (but not `- [`)
			if trimmed.starts_with("- ") {
				// Line number is 1-indexed
				let line_num = (blockers_start_idx + 1 + offset + 1) as u32;
				// Column: find where `- ` starts, then add 2 to skip past it
				let dash_pos = line.find("- ").unwrap_or(0);
				let col = (dash_pos + 3) as u32; // +2 for "- ", +1 for 1-indexing
				last_item_line_num = Some(line_num);
				last_item_col = Some(col);
			}
		}

		last_item_line_num.zip(last_item_col)
	}

	/// Get a reference to a descendant by lineage (chain of issue numbers).
	/// Returns None if the path doesn't exist.
	pub fn get(&self, lineage: &[u64]) -> Option<&Issue> {
		let mut current = self;
		for &num in lineage {
			current = current.children.get(&IssueSelector::GitId(num))?;
		}
		Some(current)
	}

	/// Get a mutable reference to a descendant by lineage.
	/// Returns None if the path doesn't exist.
	pub fn get_mut(&mut self, lineage: &[u64]) -> Option<&mut Issue> {
		let mut current = self;
		for &num in lineage {
			current = current.children.get_mut(&IssueSelector::GitId(num))?;
		}
		Some(current)
	}
}

#[derive(Clone, Debug, Default, PartialEq, derive_new::new)]
/// Hollow Issue container, - used for parsing virtual repr
///
/// Stripped of all info parsable from virtual
pub struct HollowIssue {
	pub remote: Option<Box<LinkedIssueMeta>>,
	pub children: IssueChildren<HollowIssue>,
}
impl From<Issue> for HollowIssue {
	fn from(value: Issue) -> Self {
		let mut children = HashMap::with_capacity(value.children.capacity());
		for (selector, child) in value.children.into_iter() {
			children.insert(selector, child.into());
		}
		HollowIssue {
			remote: value.identity.remote,
			children,
		}
	}
}

#[derive(Clone, Debug, PartialEq, derive_new::new)]
pub struct VirtualIssue {
	pub selector: IssueSelector,
	pub contents: IssueContents,
	pub children: IssueChildren<Self>,
}
impl VirtualIssue {
	/// Parse virtual representation (markdown with full tree) into a VirtualIssue.
	///
	/// Unlike `Issue::parse_virtual`, this doesn't need a `HollowIssue` - it purely parses content.
	/// Use `Issue::from_combined` to merge with identity info from a `HollowIssue`.
	pub fn parse(content: &str, path: PathBuf) -> Result<Self, ParseError> {
		let ctx = ParseContext::new(content.to_owned(), path);
		let events = super::Events::parse(content);
		Self::parse_from_events(&events, &ctx)
	}

	/// Parse a VirtualIssue from a cmark event stream.
	///
	/// Expects: `Start(List) > Start(Item) > ... > End(Item) > End(List)`
	/// The item contains: CheckBox, title text, InlineHtml marker, body events,
	/// comment markers (Html), blocker heading+list, child issue lists.
	fn parse_from_events(events: &[super::OwnedEvent], ctx: &ParseContext) -> Result<Self, ParseError> {
		Self::parse_from_events_inner(events, ctx, false)
	}

	fn parse_from_events_inner(events: &[super::OwnedEvent], ctx: &ParseContext, default_pending: bool) -> Result<Self, ParseError> {
		use super::{OwnedEvent, OwnedTag, OwnedTagEnd};

		let mut pos = 0;

		// Skip to first Start(Item) inside the top-level List
		while pos < events.len() && !matches!(&events[pos], OwnedEvent::Start(OwnedTag::Item)) {
			pos += 1;
		}
		if pos >= events.len() {
			return Err(ParseError::empty_file());
		}
		pos += 1; // past Start(Item)

		// --- Parse title inline content: CheckBox, Text(labels+title), InlineHtml(marker) ---
		let close_state = match &events[pos] {
			OwnedEvent::CheckBox(inner) => {
				pos += 1;
				let needle = format!("[{inner}]");
				CloseState::from_checkbox(inner).map_err(|content| ParseError::invalid_checkbox(ctx.named_source(), ctx.find_line_span(&needle, 1), content))?
			}
			_ => return Err(ParseError::invalid_title(ctx.named_source(), ctx.line_span(1), "missing checkbox".into())),
		};

		// Collect text before the issue marker InlineHtml
		let mut title_text = String::new();
		while pos < events.len() {
			match &events[pos] {
				OwnedEvent::InlineHtml(_) => break,
				OwnedEvent::Text(t) => {
					title_text.push_str(t);
					pos += 1;
				}
				OwnedEvent::Code(c) => {
					title_text.push('`');
					title_text.push_str(c);
					title_text.push('`');
					pos += 1;
				}
				_ => break,
			}
		}

		// Parse the InlineHtml marker, or fall back to `!n` shorthand embedded in title text.
		// When `default_pending` is true (post-blocker checkbox items), missing markers
		// default to Pending instead of erroring.
		let identity_info = match &events[pos] {
			OwnedEvent::InlineHtml(html) => {
				let (marker, _) = IssueMarker::parse_from_end(&format!("x {html}")).ok_or_else(|| ParseError::missing_url_marker(ctx.named_source(), ctx.line_span(1)))?;
				pos += 1;
				marker
			}
			_ => {
				// `!n` shorthand: pulldown_cmark embeds it in the Text event (no separate InlineHtml)
				match IssueMarker::parse_from_end(&title_text) {
					Some((marker, rest)) => {
						title_text = rest.to_string();
						marker
					}
					None if default_pending => IssueMarker::Pending,
					None => return Err(ParseError::missing_url_marker(ctx.named_source(), ctx.line_span(1))),
				}
			}
		};

		// Skip inline omitted markers (and intervening whitespace) that follow the issue marker on the title line
		loop {
			match events.get(pos) {
				Some(OwnedEvent::InlineHtml(html)) => {
					let trimmed = html.trim();
					if (trimmed.starts_with("<!--omitted") && trimmed.contains("{{{")) || trimmed.starts_with("<!--,}}}") {
						pos += 1;
					} else {
						break;
					}
				}
				Some(OwnedEvent::Text(t)) if t.trim().is_empty() => {
					pos += 1; // whitespace between marker and omitted marker
				}
				_ => break,
			}
		}

		// Skip SoftBreak after title (title flows directly into body in tight items)
		if matches!(events.get(pos), Some(OwnedEvent::SoftBreak)) {
			pos += 1;
		}

		// Parse labels from title text
		let title_text = title_text.trim_end();
		let (labels, title) = if title_text.starts_with('(') {
			if let Some(paren_end) = title_text.find(") ") {
				let labels_str = &title_text[1..paren_end];
				let labels: Vec<String> = labels_str.split(',').map(|s| s.trim().to_string()).filter(|s| !s.is_empty()).collect();
				(labels, title_text[paren_end + 2..].to_string())
			} else {
				(vec![], title_text.to_string())
			}
		} else {
			(vec![], title_text.to_string())
		};

		let selector = identity_info.selector(&title);

		// --- Walk remaining events inside this Item to collect body, comments, blockers, children ---
		let mut comments: Vec<Comment> = Vec::new();
		let mut children = HashMap::new();
		let mut body_events: Vec<OwnedEvent> = Vec::new();
		let mut current_comment_events: Vec<OwnedEvent> = Vec::new();
		let mut current_comment_meta: Option<CommentIdentity> = None;
		let mut blocker_events: Vec<OwnedEvent> = Vec::new();
		let mut in_body = true;
		let mut in_blockers = false;
		let mut blocker_list_consumed = false;
		let mut select_blockers = false;

		// Helper: flush current body/comment events into the comments list.
		// Events are wrapped in paragraphs so they match the structure produced
		// by standalone parsing (e.g., remote body from GitHub).
		let flush = |in_body: &mut bool,
		             current_comment_meta: &mut Option<CommentIdentity>,
		             body_events: &mut Vec<OwnedEvent>,
		             current_comment_events: &mut Vec<OwnedEvent>,
		             comments: &mut Vec<Comment>| {
			if *in_body {
				*in_body = false;
				let events = preserve_paragraph_spacing(wrap_inline_in_paragraphs(std::mem::take(body_events)));
				comments.push(Comment {
					identity: CommentIdentity::Body,
					body: events.into(),
				});
			} else if let Some(identity) = current_comment_meta.take() {
				let events = preserve_paragraph_spacing(wrap_inline_in_paragraphs(std::mem::take(current_comment_events)));
				comments.push(Comment { identity, body: events.into() });
			}
		};

		while pos < events.len() {
			match &events[pos] {
				// End of this item — we're done
				OwnedEvent::End(OwnedTagEnd::Item) => break,

				// Inline vim fold markers — skip (these appear on the title line)
				OwnedEvent::InlineHtml(html) if html.trim().starts_with("<!--omitted") && html.contains("{{{") => {
					pos += 1;
					continue;
				}
				OwnedEvent::InlineHtml(html) if html.trim().starts_with("<!--,}}}") => {
					pos += 1;
					continue;
				}

				// HtmlBlock wrappers — skip (the inner Html event is handled below)
				OwnedEvent::Start(OwnedTag::HtmlBlock) | OwnedEvent::End(OwnedTagEnd::HtmlBlock) => {
					pos += 1;
					continue;
				}

				// Html block: comment markers, omitted markers, or `!s`/`!c`
				OwnedEvent::Html(html) => {
					let trimmed = html.trim();

					// vim fold markers — skip
					if trimmed.starts_with("<!--omitted") && trimmed.contains("{{{") {
						pos += 1;
						continue;
					}
					if trimmed.starts_with("<!--,}}}") {
						pos += 1;
						continue;
					}

					// Comment separator: `<!-- @user url#issuecomment-id -->` or `<!-- new comment -->`
					if trimmed.starts_with("<!--") && trimmed.ends_with("-->") {
						let inner = trimmed.strip_prefix("<!--").unwrap().strip_suffix("-->").unwrap().trim();

						if inner == "new comment" || inner.eq_ignore_ascii_case("!c") {
							flush(&mut in_body, &mut current_comment_meta, &mut body_events, &mut current_comment_events, &mut comments);
							current_comment_meta = Some(CommentIdentity::Pending);
							pos += 1;
							continue;
						}
						if inner.contains("#issuecomment-") {
							flush(&mut in_body, &mut current_comment_meta, &mut body_events, &mut current_comment_events, &mut comments);
							current_comment_meta = Some(Issue::parse_comment_identity(inner));
							pos += 1;
							continue;
						}
					}

					// Fallthrough: treat as body/comment content
					if in_blockers {
						blocker_events.push(events[pos].clone());
					} else if in_body {
						body_events.push(events[pos].clone());
					} else if current_comment_meta.is_some() {
						current_comment_events.push(events[pos].clone());
					}
					pos += 1;
				}

				// "!s" or "!c" as standalone paragraph (loose items preserve paragraphs)
				OwnedEvent::Start(OwnedTag::Paragraph)
					if matches!(events.get(pos + 1), Some(OwnedEvent::Text(t)) if t.trim().eq_ignore_ascii_case("!s") || t.trim().eq_ignore_ascii_case("!c"))
						&& matches!(events.get(pos + 2), Some(OwnedEvent::End(OwnedTagEnd::Paragraph))) =>
				{
					let text = match &events[pos + 1] {
						OwnedEvent::Text(t) => t.trim().to_ascii_lowercase(),
						_ => unreachable!(),
					};
					if text == "!s" {
						select_blockers = true;
					} else {
						flush(&mut in_body, &mut current_comment_meta, &mut body_events, &mut current_comment_events, &mut comments);
						current_comment_meta = Some(CommentIdentity::Pending);
					}
					pos += 3; // skip Start(Paragraph), Text, End(Paragraph)
				}

				// "!s" or "!c" as bare text (tight items have no paragraph wrappers)
				OwnedEvent::Text(t) if t.trim().eq_ignore_ascii_case("!s") || t.trim().eq_ignore_ascii_case("!c") => {
					// Remove trailing artifacts from accumulated events.
					// When `!c` appears as first text inside a paragraph (e.g. `Start(P), Text("!c"), SoftBreak, ...`),
					// the Start(P) was already pushed. Strip it along with any preceding SoftBreak.
					let strip_trailing = |evs: &mut Vec<OwnedEvent>| {
						// Pop trailing Start(Paragraph) — stray from paragraph containing `!c`
						if matches!(evs.last(), Some(OwnedEvent::Start(OwnedTag::Paragraph))) {
							evs.pop();
						}
						// Pop trailing SoftBreak — normalization artifact
						if matches!(evs.last(), Some(OwnedEvent::SoftBreak)) {
							evs.pop();
						}
					};
					if in_body {
						strip_trailing(&mut body_events);
					} else if current_comment_meta.is_some() {
						strip_trailing(&mut current_comment_events);
					}
					if t.trim().eq_ignore_ascii_case("!s") {
						select_blockers = true;
					} else {
						flush(&mut in_body, &mut current_comment_meta, &mut body_events, &mut current_comment_events, &mut comments);
						current_comment_meta = Some(CommentIdentity::Pending);
					}
					pos += 1;
					// Skip SoftBreak following `!c`/`!s` text (continuation within same paragraph)
					if matches!(events.get(pos), Some(OwnedEvent::SoftBreak)) {
						pos += 1;
					}
				}

				// Heading: check for "# Blockers" (with optional !s suffix)
				OwnedEvent::Start(OwnedTag::Heading {
					level: pulldown_cmark::HeadingLevel::H1,
					..
				}) => {
					// Collect heading text
					let heading_start = pos;
					pos += 1; // past Start(Heading)
					let mut heading_text = String::new();
					while pos < events.len() && !matches!(&events[pos], OwnedEvent::End(OwnedTagEnd::Heading(pulldown_cmark::HeadingLevel::H1))) {
						if let OwnedEvent::Text(t) = &events[pos] {
							heading_text.push_str(t);
						}
						pos += 1;
					}
					pos += 1; // past End(Heading)

					let heading_trimmed = heading_text.trim();
					let (effective, has_select_suffix) = match heading_trimmed.strip_suffix("!s").or_else(|| heading_trimmed.strip_suffix("!S")) {
						Some(before) => (before.trim(), true),
						None => (heading_trimmed, false),
					};

					if effective.eq_ignore_ascii_case("blockers") {
						if has_select_suffix {
							select_blockers = true;
						}
						flush(&mut in_body, &mut current_comment_meta, &mut body_events, &mut current_comment_events, &mut comments);
						in_blockers = true;
					} else {
						// Not a blockers heading, treat as body/comment content
						let heading_events = &events[heading_start..pos];
						if in_body {
							body_events.extend(heading_events.iter().cloned());
						} else if current_comment_meta.is_some() {
							current_comment_events.extend(heading_events.iter().cloned());
						}
					}
				}

				// List: could be blockers list or child issues list
				OwnedEvent::Start(OwnedTag::List(_)) => {
					// Peek inside to determine what kind of list this is.
					// After blockers, any checkbox list terminates the blocker section
					// (split_blockers_from_checkboxes already separated checkbox items out).
					let has_checkbox = Self::list_has_checkbox(&events[pos..]);
					let is_child_list = has_checkbox;

					if is_child_list {
						// After blockers, checkbox items without markers auto-become Pending
						let children_default_pending = blocker_list_consumed;
						in_blockers = false;
						// Child issues list — parse each item as a child VirtualIssue
						// Collect the full list as a sub-slice and parse each Item
						let list_end = Self::find_matching_end_list(events, pos);
						let list_events = &events[pos..list_end];

						// Flush body/comment before children
						flush(&mut in_body, &mut current_comment_meta, &mut body_events, &mut current_comment_events, &mut comments);

						// Walk items within this list
						let mut inner_pos = 1; // skip Start(List)
						while inner_pos < list_events.len() {
							if matches!(&list_events[inner_pos], OwnedEvent::Start(OwnedTag::Item)) {
								// Find the matching End(Item)
								let item_start = inner_pos;
								let item_end = Self::find_matching_end_item(list_events, inner_pos);

								// Wrap this item in a List for recursive parsing
								let mut child_events = vec![OwnedEvent::Start(OwnedTag::List(None))];
								child_events.extend(list_events[item_start..item_end].iter().cloned());
								child_events.push(OwnedEvent::End(OwnedTagEnd::List(false)));

								let child = Self::parse_from_events_inner(&child_events, ctx, children_default_pending)?;
								children.insert(child.selector, child);

								inner_pos = item_end;
							} else {
								inner_pos += 1;
							}
						}

						pos = list_end;
					} else if in_blockers {
						if blocker_list_consumed {
							// A non-checkbox list appeared after the blocker list was already consumed.
							// This means mixed content types after blockers — invalid.
							return Err(ParseError::invalid_composition(
								ctx.named_source(),
								ctx.line_span(1),
								"non-checkbox list after blockers section".into(),
							));
						}
						// Blocker list — collect events for BlockerSequence parsing
						let list_end = Self::find_matching_end_list(events, pos);
						blocker_events.extend(events[pos..list_end].iter().cloned());
						blocker_list_consumed = true;
						pos = list_end;
					} else {
						// Regular list in body/comment content
						let list_end = Self::find_matching_end_list(events, pos);
						let list_slice = &events[pos..list_end];
						if in_body {
							body_events.extend(list_slice.iter().cloned());
						} else if current_comment_meta.is_some() {
							current_comment_events.extend(list_slice.iter().cloned());
						}
						pos = list_end;
					}
				}

				// Any other event: body or comment content
				_ => {
					if in_blockers && blocker_list_consumed {
						// After the blocker list was consumed, non-list content means
						// invalid composition (only checkbox lists are allowed after blockers).
						// SoftBreak / Html("\n") between blocks are harmless — skip them.
						match &events[pos] {
							OwnedEvent::SoftBreak | OwnedEvent::HardBreak => {
								pos += 1;
								continue;
							}
							OwnedEvent::Html(h) if h.trim().is_empty() => {
								pos += 1;
								continue;
							}
							OwnedEvent::Text(t) if t.trim().is_empty() => {
								pos += 1;
								continue;
							}
							_ => {
								return Err(ParseError::invalid_composition(
									ctx.named_source(),
									ctx.line_span(1),
									format!("unexpected content after blockers section: {:?}", &events[pos]),
								));
							}
						}
					} else if in_blockers {
						blocker_events.push(events[pos].clone());
					} else if in_body {
						body_events.push(events[pos].clone());
					} else if current_comment_meta.is_some() {
						current_comment_events.push(events[pos].clone());
					}
					pos += 1;
				}
			}
		}

		// Flush final body/comment
		flush(&mut in_body, &mut current_comment_meta, &mut body_events, &mut current_comment_events, &mut comments);

		// Parse blockers from collected events
		let blockers = if blocker_events.is_empty() {
			BlockerSequence::default()
		} else {
			// Render blocker events to string and parse with BlockerSequence::parse
			let blocker_text: String = super::Events::from(blocker_events).into();
			BlockerSequence::parse(&blocker_text)
		};

		Ok(VirtualIssue {
			selector,
			contents: IssueContents {
				title,
				labels,
				state: close_state,
				comments: comments.into(),
				blockers: {
					let mut seq = blockers;
					if select_blockers {
						seq.set_state = Some(crate::issue::BlockerSetState::Pending);
					}
					seq
				},
			},
			children,
		})
	}

	/// Check if a list (starting at `Start(List(...))`) contains any CheckBox items.
	fn list_has_checkbox(events: &[super::OwnedEvent]) -> bool {
		use super::{OwnedEvent, OwnedTag, OwnedTagEnd};
		let mut depth = 0;
		for ev in events {
			match ev {
				OwnedEvent::Start(OwnedTag::List(_)) => depth += 1,
				OwnedEvent::End(OwnedTagEnd::List(_)) => {
					depth -= 1;
					if depth == 0 {
						break;
					}
				}
				OwnedEvent::CheckBox(_) if depth == 1 => return true,
				_ => {}
			}
		}
		false
	}

	/// Find the position after the matching `End(List)` for a `Start(List)` at `start`.
	fn find_matching_end_list(events: &[super::OwnedEvent], start: usize) -> usize {
		use super::{OwnedEvent, OwnedTag, OwnedTagEnd};
		let mut depth = 0;
		for (i, event) in events.iter().enumerate().skip(start) {
			match event {
				OwnedEvent::Start(OwnedTag::List(_)) => depth += 1,
				OwnedEvent::End(OwnedTagEnd::List(_)) => {
					depth -= 1;
					if depth == 0 {
						return i + 1;
					}
				}
				_ => {}
			}
		}
		events.len()
	}

	/// Find the position after the matching `End(Item)` for a `Start(Item)` at `start`.
	fn find_matching_end_item(events: &[super::OwnedEvent], start: usize) -> usize {
		use super::{OwnedEvent, OwnedTag, OwnedTagEnd};
		let mut depth = 0;
		for (i, event) in events.iter().enumerate().skip(start) {
			match event {
				OwnedEvent::Start(OwnedTag::Item) => depth += 1,
				OwnedEvent::End(OwnedTagEnd::Item) => {
					depth -= 1;
					if depth == 0 {
						return i + 1;
					}
				}
				_ => {}
			}
		}
		events.len()
	}
}

impl From<Issue> for VirtualIssue {
	fn from(issue: Issue) -> Self {
		let selector = issue.selector();
		let children = issue.children.into_iter().map(|(sel, child)| (sel, child.into())).collect();
		Self {
			selector,
			contents: issue.contents,
			children,
		}
	}
}

//,}}}1

impl PartialEq for IssueIdentity {
	fn eq(&self, other: &IssueIdentity) -> bool {
		self.parent_index == other.parent_index
	}
}

//,}}}1

//==============================================================================
// Index by issue number
//==============================================================================

// PERF: Linear search through children for each index operation.
// We sacrifice some performance for determinism - the tree structure
// is navigated by issue numbers rather than positional indices.

/// Implement Index<u64> and IndexMut<u64> for types with `children: IssueChildren<Self>`.
macro_rules! impl_issue_tree_index {
	($ty:ty) => {
		impl std::ops::Index<u64> for $ty {
			type Output = $ty;

			/// Index into children by issue number.
			/// Panics if no child with that number exists.
			fn index(&self, issue_number: u64) -> &Self::Output {
				self.children
					.get(&IssueSelector::GitId(issue_number))
					.unwrap_or_else(|| panic!("no child with issue number {issue_number}"))
			}
		}

		impl std::ops::IndexMut<u64> for $ty {
			/// Index into children by issue number (mutable).
			/// Panics if no child with that number exists.
			fn index_mut(&mut self, issue_number: u64) -> &mut Self::Output {
				self.children
					.get_mut(&IssueSelector::GitId(issue_number))
					.unwrap_or_else(|| panic!("no child with issue number {issue_number}"))
			}
		}

		impl std::ops::Index<IssueSelector> for $ty {
			type Output = $ty;

			/// Index into children by selector.
			/// Panics if no child with that selector exists.
			fn index(&self, selector: IssueSelector) -> &Self::Output {
				self.children.get(&selector).unwrap_or_else(|| panic!("no child with selector {selector:?}"))
			}
		}

		impl std::ops::IndexMut<IssueSelector> for $ty {
			/// Index into children by selector (mutable).
			/// Panics if no child with that selector exists.
			fn index_mut(&mut self, selector: IssueSelector) -> &mut Self::Output {
				self.children.get_mut(&selector).unwrap_or_else(|| panic!("no child with selector {selector:?}"))
			}
		}
	};
}

impl_issue_tree_index!(Issue);
impl_issue_tree_index!(HollowIssue);
impl_issue_tree_index!(VirtualIssue);

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

	fn unsafe_mock_parse_virtual(content: &str) -> Issue {
		let virtual_issue = VirtualIssue::parse(content, PathBuf::from("test.md")).unwrap();
		let hollow = hollow_from_virtual(&virtual_issue);
		let parent_idx = IssueIndex::repo_only(("owner", "repo").into());
		Issue::from_combined(hollow, virtual_issue, parent_idx, false).unwrap()
	}

	/// needed to not fall for checks for existence of GitId-linked issue on the base structure (useful in the real world; but our tests here really only care about parsing itself.
	fn hollow_from_virtual(v: &VirtualIssue) -> HollowIssue {
		let remote = match &v.selector {
			IssueSelector::GitId(n) => {
				let link = IssueLink::parse(&format!("https://github.com/owner/repo/issues/{n}")).unwrap();
				Some(Box::new(LinkedIssueMeta::new(None, link, IssueTimestamps::default())))
			}
			IssueSelector::Title(_) | IssueSelector::Regex(_) => None,
		};
		let children = v.children.iter().map(|(sel, child)| (*sel, hollow_from_virtual(child))).collect();
		HollowIssue::new(remote, children)
	}

	#[test]
	fn test_close_state_from_checkbox() {
		let cases = [" ", "", "x", "X", "-", "123", "42", "invalid"];
		let results: Vec<_> = cases.iter().map(|c| format!("{c:?} => {:?}", CloseState::from_checkbox(c))).collect();
		insta::assert_snapshot!(results.join("\n"), @r#"
		" " => Ok(Open)
		"" => Ok(Open)
		"x" => Ok(Closed)
		"X" => Ok(Closed)
		"-" => Ok(NotPlanned)
		"123" => Ok(Duplicate(123))
		"42" => Ok(Duplicate(42))
		"invalid" => Err("invalid")
		"#);
	}

	#[test]
	fn test_close_state_to_checkbox() {
		let cases = [CloseState::Open, CloseState::Closed, CloseState::NotPlanned, CloseState::Duplicate(123)];
		let results: Vec<_> = cases.iter().map(|s| format!("{s:?} => {:?}", s.to_checkbox_contents())).collect();
		insta::assert_snapshot!(results.join("\n"), @r#"
		Open => " "
		Closed => "x"
		NotPlanned => "-"
		Duplicate(123) => "123"
		"#);
	}

	#[test]
	fn test_close_state_is_closed() {
		let cases = [CloseState::Open, CloseState::Closed, CloseState::NotPlanned, CloseState::Duplicate(123)];
		let results: Vec<_> = cases.iter().map(|s| format!("{s:?} => {}", s.is_closed())).collect();
		insta::assert_snapshot!(results.join("\n"), @"
		Open => false
		Closed => true
		NotPlanned => true
		Duplicate(123) => true
		");
	}

	#[test]
	fn test_close_state_should_remove() {
		let cases = [CloseState::Open, CloseState::Closed, CloseState::NotPlanned, CloseState::Duplicate(123)];
		let results: Vec<_> = cases.iter().map(|s| format!("{s:?} => {}", s.should_remove())).collect();
		insta::assert_snapshot!(results.join("\n"), @"
		Open => false
		Closed => false
		NotPlanned => false
		Duplicate(123) => true
		");
	}

	#[test]
	fn test_close_state_to_github_state() {
		let cases = [CloseState::Open, CloseState::Closed, CloseState::NotPlanned, CloseState::Duplicate(123)];
		let results: Vec<_> = cases.iter().map(|s| format!("{s:?} => {}", s.to_github_state())).collect();
		insta::assert_snapshot!(results.join("\n"), @"
		Open => open
		Closed => closed
		NotPlanned => closed
		Duplicate(123) => closed
		");
	}

	#[test]
	fn test_parse_invalid_checkbox_returns_error() {
		let root_err = VirtualIssue::parse("- [abc] Invalid issue <!-- https://github.com/owner/repo/issues/123 -->\n\n  Body\n", PathBuf::from("test.md")).unwrap_err();
		let sub_err = VirtualIssue::parse(
			"- [ ] Parent <!-- https://github.com/owner/repo/issues/1 -->\n\n  Body\n\n  - [xyz] Bad sub <!--sub https://github.com/owner/repo/issues/2 -->\n",
			PathBuf::from("test.md"),
		)
		.unwrap_err();
		insta::assert_snapshot!(format!("root: {root_err}\nsub: {sub_err}"), @r#"
		root: tedi::parse::invalid_checkbox

		  × invalid checkbox content: 'abc'
		   ╭─[test.md:1:1]
		 1 │ - [abc] Invalid issue <!-- https://github.com/owner/repo/issues/123 -->
		   · ───────────────────────────────────┬───────────────────────────────────
		   ·                                    ╰── unrecognized checkbox value
		 2 │ 
		   ╰────
		  help: valid checkbox values are: ' ' (open), 'x' (closed), '-' (not
		        planned), or a number like '123' (duplicate of issue #123)



		sub: tedi::parse::invalid_checkbox

		  × invalid checkbox content: 'xyz'
		   ╭─[test.md:5:1]
		 4 │ 
		 5 │   - [xyz] Bad sub <!--sub https://github.com/owner/repo/issues/2 -->
		   · ──────────────────────────────────┬─────────────────────────────────
		   ·                                   ╰── unrecognized checkbox value
		   ╰────
		  help: valid checkbox values are: ' ' (open), 'x' (closed), '-' (not
		        planned), or a number like '123' (duplicate of issue #123)
		"#);
	}

	#[test]
	fn test_parse_and_serialize_not_planned() {
		let content = "- [-] Not planned issue <!-- https://github.com/owner/repo/issues/123 -->\n\n  Body text\n";
		let vi = VirtualIssue::parse(content, PathBuf::from("test.md")).unwrap();
		insta::assert_snapshot!(format!("state: {:?}\ntitle: {}", vi.contents.state, vi.contents.title), @"
		state: NotPlanned
		title: Not planned issue
		");
	}

	#[test]
	fn test_parse_and_serialize_duplicate() {
		let content = "- [456] Duplicate issue <!-- https://github.com/owner/repo/issues/123 -->\n\n  Body text\n";
		let vi = VirtualIssue::parse(content, PathBuf::from("test.md")).unwrap();
		insta::assert_snapshot!(format!("state: {:?}\ntitle: {}", vi.contents.state, vi.contents.title), @"
		state: Duplicate(456)
		title: Duplicate issue
		");
	}

	#[test]
	fn test_parse_sub_issue_close_types() {
		let content = "- [ ] Parent issue <!-- https://github.com/owner/repo/issues/1 -->\n\n  Body\n\n  - [x] Closed sub <!-- https://github.com/owner/repo/issues/2 --> <!--omitted {{{always-->\n\n    closed body\n    <!--,}}}-->\n\n  - [-] Not planned sub <!-- https://github.com/owner/repo/issues/3 --> <!--omitted {{{always-->\n\n    not planned body\n    <!--,}}}-->\n\n  - [42] Duplicate sub <!-- https://github.com/owner/repo/issues/4 --> <!--omitted {{{always-->\n\n    duplicate body\n    <!--,}}}-->\n";
		let issue = unsafe_mock_parse_virtual(content);
		// snapshot has trailing spaces on lines between closed children
		insta::assert_snapshot!(issue.serialize_virtual(), @r"
		- [ ] Parent issue <!-- https://github.com/owner/repo/issues/1 -->
		  Body
		  
		  - [x] Closed sub <!-- https://github.com/owner/repo/issues/2 --> <!--omitted {{{always-->
		    closed body
		    <!--,}}}-->
		  
		  - \[-] Not planned sub <!-- https://github.com/owner/repo/issues/3 --> <!--omitted {{{always-->
		    not planned body
		    <!--,}}}-->
		  
		  - \[42] Duplicate sub <!-- https://github.com/owner/repo/issues/4 --> <!--omitted {{{always-->
		    duplicate body
		    <!--,}}}-->
		");
	}

	#[test]
	fn test_find_last_blocker_position_empty() {
		let content = "- [ ] Issue <!-- https://github.com/owner/repo/issues/1 -->\n\n  Body\n";
		let issue = unsafe_mock_parse_virtual(content);
		assert!(issue.find_last_blocker_position().is_none());
	}

	#[test]
	fn test_find_last_blocker_position_single_item() {
		let content = "- [ ] Issue <!-- https://github.com/owner/repo/issues/1 -->\n\n  Body\n\n  # Blockers\n  - task 1\n";
		let issue = unsafe_mock_parse_virtual(content);
		insta::assert_snapshot!(format!("{:?}", issue.find_last_blocker_position()), @"Some((5, 5))");
	}

	#[test]
	fn test_find_last_blocker_position_multiple_items() {
		let content = "- [ ] Issue <!-- https://github.com/owner/repo/issues/1 -->\n\n  Body\n\n  # Blockers\n  - task 1\n  - task 2\n  - task 3\n";
		let issue = unsafe_mock_parse_virtual(content);
		insta::assert_snapshot!(format!("{:?}", issue.find_last_blocker_position()), @"Some((7, 5))");
	}

	#[test]
	fn test_find_last_blocker_position_with_nesting() {
		let content = "- [ ] Issue <!-- https://github.com/owner/repo/issues/1 -->\n\n  Body\n\n  # Blockers\n  - Phase 1\n    - task a\n  - Phase 2\n    - task b\n";
		let issue = unsafe_mock_parse_virtual(content);
		insta::assert_snapshot!(format!("{:?}", issue.find_last_blocker_position()), @"Some((8, 7))");
	}

	#[test]
	fn test_find_last_blocker_position_before_sub_issues() {
		let content = "- [ ] Issue <!-- https://github.com/owner/repo/issues/1 -->\n\n  Body\n\n  # Blockers\n  - blocker task\n\n  - [ ] Sub issue <!--sub https://github.com/owner/repo/issues/2 -->\n";
		let issue = unsafe_mock_parse_virtual(content);
		insta::assert_snapshot!(format!("{:?}", issue.find_last_blocker_position()), @"Some((5, 5))");
	}

	#[test]
	fn test_serialize_filesystem_no_children() {
		let content = "- [ ] Parent <!-- https://github.com/owner/repo/issues/1 -->\n\n  Parent body\n\n  - [ ] Child 1 <!--sub https://github.com/owner/repo/issues/2 -->\n\n    Child 1 body\n\n  - [ ] Child 2 <!--sub https://github.com/owner/repo/issues/3 -->\n\n    Child 2 body\n";
		let issue = unsafe_mock_parse_virtual(content);
		assert_eq!(issue.children.len(), 2);
		// filesystem serialization should NOT include children
		insta::assert_snapshot!(issue.serialize_filesystem(), @"
		- [ ] Parent <!-- https://github.com/owner/repo/issues/1 -->
		  Parent body
		");
	}

	#[test]
	fn test_serialize_filesystem_roundtrip_blocker_escaping() {
		let cases = vec![
			"- [ ] Title <!-- https://github.com/owner/repo/issues/1 -->\n  # Blockers\n  - `insert` semantics on `RoutingHub`\n",
			"- [ ] Title <!-- https://github.com/owner/repo/issues/1 -->\n  # Blockers\n  - `insert`semantics on`RoutingHub`\n",
			"- [ ] Title <!-- https://github.com/owner/repo/issues/1 -->\n  # Blockers\n  - move clap interface into \\_strategy\n",
			"- [ ] Title <!-- https://github.com/owner/repo/issues/1 -->\n  # Blockers\n  - some certainty\\*val range\n",
			"- [ ] Title <!-- https://github.com/owner/repo/issues/1 -->\n  # Blockers\n  - text with ` lone backtick\n",
		];

		for initial_content in cases {
			let issue = unsafe_mock_parse_virtual(initial_content);
			let s1 = issue.serialize_filesystem();
			for cycle in 1..=5 {
				let re = unsafe_mock_parse_virtual(&s1);
				let sn = re.serialize_filesystem();
				assert_eq!(s1, sn, "serialize_filesystem not idempotent at cycle {cycle} for input: {initial_content:?}");
			}
		}
	}

	#[test]
	fn test_serialize_virtual_includes_children() {
		let content =
			"- [ ] Parent <!-- https://github.com/owner/repo/issues/1 -->\n\n  Parent body\n\n  - [ ] Child 1 <!--sub https://github.com/owner/repo/issues/2 -->\n\n    Child 1 body\n";
		let issue = unsafe_mock_parse_virtual(content);
		insta::assert_snapshot!(issue.serialize_virtual(), @"
		- [ ] Parent <!-- https://github.com/owner/repo/issues/1 -->
		  Parent body
		  
		  - [ ] Child 1 <!-- https://github.com/owner/repo/issues/2 -->
		    Child 1 body
		");
	}

	#[test]
	fn test_parse_virtual_includes_inline_children() {
		let content = "- [ ] Parent <!-- https://github.com/owner/repo/issues/1 -->\n\n  Parent body\n\n  - [ ] Child <!--sub https://github.com/owner/repo/issues/2 -->\n\n    Child body\n";
		let vi = VirtualIssue::parse(content, PathBuf::from("test.md")).unwrap();
		insta::assert_snapshot!(
			format!("children: {}\nparent: {}\nchild: {}", vi.children.len(), vi.contents.title, vi[2].contents.title),
			@"
		children: 1
		parent: Parent
		child: Child
		"
		);
	}

	#[test]
	fn test_virtual_roundtrip() {
		let content = "- [ ] Parent <!-- https://github.com/owner/repo/issues/1 -->\n\n  Parent body\n\n  - [ ] Child <!--sub https://github.com/owner/repo/issues/2 -->\n\n    Child body\n";
		let issue = unsafe_mock_parse_virtual(content);
		let serialized = issue.serialize_virtual();
		let reparsed = unsafe_mock_parse_virtual(&serialized);
		insta::assert_snapshot!(
			format!("title: {}\nchildren: {}", reparsed.contents.title, reparsed.children.len()),
			@"
		title: Parent
		children: 1
		"
		);
	}

	#[test]
	fn test_select_blockers_standalone_line() {
		let content = "- [ ] Issue <!-- https://github.com/owner/repo/issues/1 -->\n\n  Body\n\n  !s\n  # Blockers\n  - task 1\n  - task 2\n";
		let vi = VirtualIssue::parse(content, PathBuf::from("test.md")).unwrap();
		assert!(matches!(vi.contents.blockers.set_state, Some(crate::issue::BlockerSetState::Pending)));
		// !s must not appear in re-serialized output, blockers preserved
		insta::assert_snapshot!(unsafe_mock_parse_virtual(content).serialize_virtual(), @"
		- [ ] Issue <!-- https://github.com/owner/repo/issues/1 -->
		  Body

		  # Blockers
		  - task 1
		  - task 2
		");
	}

	#[test]
	fn test_select_blockers_suffix_on_header() {
		let content = "- [ ] Issue <!-- https://github.com/owner/repo/issues/1 -->\n\n  Body\n\n  # Blockers !s\n  - one\n";
		let vi = VirtualIssue::parse(content, PathBuf::from("test.md")).unwrap();
		assert!(matches!(vi.contents.blockers.set_state, Some(crate::issue::BlockerSetState::Pending)));
		// blocker text parsed correctly despite !s suffix on header
		insta::assert_snapshot!(vi.contents.blockers.items[0].text, @"one");
	}

	#[test]
	fn test_select_blockers_not_set_by_default() {
		let content = "- [ ] Issue <!-- https://github.com/owner/repo/issues/1 -->\n\n  Body\n\n  # Blockers\n  - task\n";
		let virtual_issue = VirtualIssue::parse(content, PathBuf::from("test.md")).unwrap();

		assert!(virtual_issue.contents.blockers.set_state.is_none());
	}

	#[test]
	fn test_select_blockers_case_insensitive() {
		let content = "- [ ] Issue <!-- https://github.com/owner/repo/issues/1 -->\n\n  !S\n  # Blockers\n  - task\n";
		let virtual_issue = VirtualIssue::parse(content, PathBuf::from("test.md")).unwrap();

		assert!(matches!(virtual_issue.contents.blockers.set_state, Some(crate::issue::BlockerSetState::Pending)));
	}

	#[test]
	fn test_serialize_github_body_only() {
		let content = "- [ ] Issue <!-- https://github.com/owner/repo/issues/1 -->\n\n  This is the body text.\n\n  # Blockers\n  - task 1\n  - task 2\n";
		let issue = unsafe_mock_parse_virtual(content);
		let github_body: String = issue.render_github().into();
		insta::assert_snapshot!(github_body, @"
		This is the body text.
		# Blockers
		- task 1
		- task 2
		");
	}

	#[test]
	fn test_parse_virtual_child_open_to_closed() {
		let initial = "- [ ] Parent <!-- https://github.com/owner/repo/issues/1 -->\n\n  Body\n\n  - [ ] Child <!--sub https://github.com/owner/repo/issues/2 -->\n\n    Child body\n";
		let updated = "- [ ] Parent <!-- https://github.com/owner/repo/issues/1 -->\n\n  Body\n\n  - [x] Child <!--sub https://github.com/owner/repo/issues/2 -->\n\n    Child body\n";
		let initial_vi = VirtualIssue::parse(initial, PathBuf::from("test.md")).unwrap();
		assert_eq!(initial_vi[2].contents.state, CloseState::Open);
		let updated_vi = VirtualIssue::parse(updated, PathBuf::from("test.md")).unwrap();
		// child should transition to Closed
		insta::assert_snapshot!(format!("{:?}", updated_vi[2].contents.state), @"Closed");
	}

	#[test]
	fn test_parse_virtual_child_open_to_not_planned() {
		let initial = "- [ ] Parent <!-- https://github.com/owner/repo/issues/1 -->\n\n  Body\n\n  - [ ] Child <!--sub https://github.com/owner/repo/issues/2 -->\n\n    Child body\n";
		let updated = "- [ ] Parent <!-- https://github.com/owner/repo/issues/1 -->\n\n  Body\n\n  - [-] Child <!--sub https://github.com/owner/repo/issues/2 -->\n\n    Child body\n";
		let initial_vi = VirtualIssue::parse(initial, PathBuf::from("test.md")).unwrap();
		assert_eq!(initial_vi[2].contents.state, CloseState::Open);
		let updated_vi = VirtualIssue::parse(updated, PathBuf::from("test.md")).unwrap();
		// child should transition to NotPlanned
		insta::assert_snapshot!(format!("{:?}", updated_vi[2].contents.state), @"NotPlanned");
	}

	#[test]
	fn test_parse_virtual_child_open_to_duplicate() {
		let initial = "- [ ] Parent <!-- https://github.com/owner/repo/issues/1 -->\n\n  Body\n\n  - [ ] Child <!--sub https://github.com/owner/repo/issues/2 -->\n\n    Child body\n";
		let updated = "- [ ] Parent <!-- https://github.com/owner/repo/issues/1 -->\n\n  Body\n\n  - [99] Child <!--sub https://github.com/owner/repo/issues/2 -->\n\n    Child body\n";
		let initial_vi = VirtualIssue::parse(initial, PathBuf::from("test.md")).unwrap();
		assert_eq!(initial_vi[2].contents.state, CloseState::Open);
		let updated_vi = VirtualIssue::parse(updated, PathBuf::from("test.md")).unwrap();
		// child should transition to Duplicate(99)
		insta::assert_snapshot!(format!("{:?}", updated_vi[2].contents.state), @"Duplicate(99)");
	}

	#[test]
	fn test_parse_virtual_child_closed_to_open() {
		let initial = "- [ ] Parent <!-- https://github.com/owner/repo/issues/1 -->\n\n  Body\n\n  - [x] Child <!--sub https://github.com/owner/repo/issues/2 -->\n\n    Child body\n";
		let updated = "- [ ] Parent <!-- https://github.com/owner/repo/issues/1 -->\n\n  Body\n\n  - [ ] Child <!--sub https://github.com/owner/repo/issues/2 -->\n\n    Child body\n";
		let initial_vi = VirtualIssue::parse(initial, PathBuf::from("test.md")).unwrap();
		assert_eq!(initial_vi[2].contents.state, CloseState::Closed);
		let updated_vi = VirtualIssue::parse(updated, PathBuf::from("test.md")).unwrap();
		// child should transition to Open
		insta::assert_snapshot!(format!("{:?}", updated_vi[2].contents.state), @"Open");
	}

	#[test]
	fn test_serialize_roundtrip_idempotent() {
		crate::current_user::set("mock_user".to_string());
		let input = "- [ ] Test Issue <!-- @mock_user https://github.com/o/r/issues/1 -->\n\n  original body\n";
		let issue = unsafe_mock_parse_virtual(input);
		let s1 = issue.serialize_virtual();
		let issue2 = unsafe_mock_parse_virtual(&s1);
		let s2 = issue2.serialize_virtual();
		assert_eq!(s1, s2, "serialize_virtual must be idempotent");
	}

	#[test]
	fn test_serialize_roundtrip_custom_checkboxes_idempotent() {
		crate::current_user::set("mock_user".to_string());
		let input = "- [ ] Parent <!-- @mock_user https://github.com/o/r/issues/1 -->\n\n  Body\n\n  - [-] Not planned <!--sub @mock_user https://github.com/o/r/issues/2 -->\n\n    np body\n\n  - [42] Duplicate <!--sub @mock_user https://github.com/o/r/issues/3 -->\n\n    dup body\n";
		let issue = unsafe_mock_parse_virtual(input);
		let s1 = issue.serialize_virtual();
		for cycle in 1..=5 {
			let re = unsafe_mock_parse_virtual(&s1);
			let sn = re.serialize_virtual();
			assert_eq!(s1, sn, "serialize_virtual must be idempotent at cycle {cycle}");
		}
	}

	#[test]
	fn test_parse_nested_subissues() {
		let input = "- [ ] Grandparent <!-- @mock_user https://github.com/o/r/issues/1 -->\n\n  grandparent body\n\n  - [ ] Parent <!--sub @mock_user https://github.com/o/r/issues/2 -->\n\n    original parent body\n\n    - [ ] Child <!--sub @mock_user https://github.com/o/r/issues/3 -->\n\n      child body\n";
		let vi = VirtualIssue::parse(input, PathBuf::from("test.md")).unwrap();
		eprintln!("parsed: title={}, children={}", vi.contents.title, vi.children.len());
	}

	#[test]
	fn test_parse_blockers_and_child_at_same_indent() {
		let input = "- [ ] Parent <!-- https://github.com/owner/repo/issues/2 -->\n  \n  body\n  \n  # Blockers\n  - local blocker\n  \n  - [ ] Child <!--sub https://github.com/owner/repo/issues/3 -->\n    \n    child body\n";
		let vi = VirtualIssue::parse(input, PathBuf::from("test.md")).unwrap();
		assert_eq!(vi.contents.blockers.items.len(), 1, "should have 1 blocker");
		assert_eq!(vi.children.len(), 1, "should have 1 child");
	}

	#[test]
	fn test_blocker_section_does_not_absorb_checkbox_items() {
		// Checkbox list items after blockers must NOT be treated as blockers.
		// They are separate child issues (with implicit Pending marker).
		let input = "- [ ] Parent <!-- https://github.com/owner/repo/issues/1 -->\n\n  body\n\n  # Blockers\n  - vector series\n    - all the others\n    - ex 10\n\n  - [ ] series\n    up to and including ex 8\n";
		let vi = VirtualIssue::parse(input, PathBuf::from("test.md")).unwrap();
		assert_eq!(vi.contents.blockers.items.len(), 1, "should have 1 blocker (vector series)");
		assert_eq!(vi.contents.blockers.items[0].text, "vector series");
		assert_eq!(vi.contents.blockers.items[0].children.len(), 2, "vector series has 2 children");
		assert_eq!(vi.children.len(), 1, "should have 1 child issue (series)");
		let child = vi.children.values().next().unwrap();
		assert_eq!(child.contents.title, "series");
	}

	#[test]
	fn test_blocker_section_terminates_on_empty_line_before_checkbox() {
		// Even with an empty line between blockers and checkbox items,
		// the checkbox items must NOT be absorbed into blockers.
		let input = "- [ ] Parent <!-- https://github.com/owner/repo/issues/1 -->\n\n  # Blockers\n  - task A\n  - task B\n\n  - [ ] new child\n    child body\n";
		let vi = VirtualIssue::parse(input, PathBuf::from("test.md")).unwrap();
		assert_eq!(vi.contents.blockers.items.len(), 2, "should have 2 blockers");
		assert_eq!(vi.children.len(), 1, "checkbox item after empty line becomes child");
		let child = vi.children.values().next().unwrap();
		assert_eq!(child.contents.title, "new child");
	}

	#[test]
	fn test_body_blank_lines_preserved_roundtrip() {
		crate::current_user::set("mock_user".to_string());
		let input = "- [ ] Issue <!-- @mock_user https://github.com/o/r/issues/1 -->\n\n  some text\n\n  ```rust\n  fn main() {\n    println!(\"hello\");\n  }\n  ```\n\n  more text\n";
		let issue = unsafe_mock_parse_virtual(input);
		let serialized = issue.serialize_virtual();
		insta::assert_snapshot!(serialized, @r#"
		- [ ] Issue <!-- https://github.com/owner/repo/issues/1 -->
		  some text

		  ````rust
		  fn main() {
		    println!("hello");
		  }
		  ````

		  more text
		"#);
	}

	#[test]
	fn test_body_multiple_paragraphs_preserved_roundtrip() {
		crate::current_user::set("mock_user".to_string());
		let input = "- [ ] Issue <!-- @mock_user https://github.com/o/r/issues/1 -->\n\n  first paragraph\n\n  second paragraph\n\n  third paragraph\n";
		let issue = unsafe_mock_parse_virtual(input);
		let serialized = issue.serialize_virtual();
		insta::assert_snapshot!(serialized, @"
		- [ ] Issue <!-- https://github.com/owner/repo/issues/1 -->
		  first paragraph

		  second paragraph

		  third paragraph
		");
	}

	#[test]
	fn test_body_blank_lines_idempotent() {
		crate::current_user::set("mock_user".to_string());
		let input = "- [ ] Issue <!-- @mock_user https://github.com/o/r/issues/1 -->\n\n  some text\n\n  ```rust\n  fn main() {\n    println!(\"hello\");\n  }\n  ```\n\n  more text\n";
		let issue = unsafe_mock_parse_virtual(input);
		let s1 = issue.serialize_virtual();
		let issue2 = unsafe_mock_parse_virtual(&s1);
		let s2 = issue2.serialize_virtual();
		assert_eq!(s1, s2, "body blank lines must be preserved idempotently");
	}
}