utf8proj-render 0.9.1

Rendering backends for utf8proj (SVG, text, etc.)
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
//! Interactive HTML Gantt Chart Renderer
//!
//! Generates standalone HTML files with embedded SVG Gantt charts.
//! Features:
//! - Task bars with critical path highlighting
//! - Dependency arrows (FS, SS, FF, SF)
//! - Hover tooltips with task details
//! - Click to highlight dependencies
//! - Collapsible hierarchical tasks
//! - Responsive zoom controls

use chrono::NaiveDate;
use std::collections::HashMap;
use utf8proj_core::{Project, RenderError, Renderer, Schedule, ScheduledTask, Task};

/// HTML Gantt chart renderer configuration
#[derive(Clone, Debug)]
pub struct HtmlGanttRenderer {
    /// Width of the chart area (excluding labels) in pixels
    pub chart_width: u32,
    /// Height per task row in pixels
    pub row_height: u32,
    /// Width of the label column in pixels
    pub label_width: u32,
    /// Header height in pixels
    pub header_height: u32,
    /// Padding around the chart
    pub padding: u32,
    /// Theme (light or dark)
    pub theme: GanttTheme,
    /// Show dependency arrows
    pub show_dependencies: bool,
    /// Enable interactivity (tooltips, click handlers)
    pub interactive: bool,
    /// Focus view configuration (None = show all tasks)
    pub focus: Option<FocusConfig>,
}

/// Configuration for focus view rendering
#[derive(Clone, Debug, Default)]
pub struct FocusConfig {
    /// Patterns to match for focused (expanded) tasks
    /// Supports prefix matching (e.g., "6.3.2" matches "6.3.2.1", "6.3.2.2")
    pub focus_patterns: Vec<String>,
    /// Depth to show for non-focused tasks (0 = hide, 1 = top-level only)
    pub context_depth: usize,
}

/// Visibility state for a task in focus view
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TaskVisibility {
    /// Show task and all descendants expanded
    Expanded,
    /// Show task as collapsed summary bar (children hidden)
    Collapsed,
    /// Do not show task at all
    Hidden,
}

impl FocusConfig {
    /// Create a new focus configuration
    pub fn new(patterns: Vec<String>, context_depth: usize) -> Self {
        Self {
            focus_patterns: patterns,
            context_depth,
        }
    }

    /// Check if a task ID matches any focus pattern
    pub fn matches_focus(&self, task_id: &str, task_name: &str) -> bool {
        if self.focus_patterns.is_empty() {
            return true; // No focus = everything focused
        }
        for pattern in &self.focus_patterns {
            // Check if task_id starts with pattern (prefix match)
            if task_id.starts_with(pattern) || task_id == pattern {
                return true;
            }
            // Check if task_name contains pattern (for WBS codes in names)
            if task_name.contains(pattern) {
                return true;
            }
            // Check pattern as glob (simple * wildcard support)
            if self.glob_match(pattern, task_id) {
                return true;
            }
        }
        false
    }

    /// Simple glob matching with * wildcard
    fn glob_match(&self, pattern: &str, text: &str) -> bool {
        if !pattern.contains('*') {
            return pattern == text;
        }
        let parts: Vec<&str> = pattern.split('*').collect();
        if parts.is_empty() {
            return true;
        }
        let mut pos = 0;
        for (i, part) in parts.iter().enumerate() {
            if part.is_empty() {
                continue;
            }
            if let Some(found) = text[pos..].find(part) {
                if i == 0 && found != 0 {
                    // First part must match at start (unless pattern starts with *)
                    return false;
                }
                pos += found + part.len();
            } else {
                return false;
            }
        }
        // If pattern ends with *, any suffix is OK; otherwise must match to end
        parts
            .last()
            .map_or(true, |p| p.is_empty() || pos == text.len())
    }

    /// Determine visibility of a task based on focus configuration
    pub fn get_visibility(
        &self,
        task_id: &str,
        task_name: &str,
        depth: usize,
        is_ancestor_of_focused: bool,
        is_descendant_of_focused: bool,
    ) -> TaskVisibility {
        // Direct match - always expanded
        if self.matches_focus(task_id, task_name) {
            return TaskVisibility::Expanded;
        }

        // Ancestor of focused task - expanded to show path
        if is_ancestor_of_focused {
            return TaskVisibility::Expanded;
        }

        // Descendant of focused task - expanded
        if is_descendant_of_focused {
            return TaskVisibility::Expanded;
        }

        // Non-focused: check context depth
        if depth < self.context_depth {
            return TaskVisibility::Collapsed;
        }

        TaskVisibility::Hidden
    }
}

/// Color theme for the Gantt chart
#[derive(Clone, Debug)]
pub struct GanttTheme {
    pub critical_color: String,
    pub normal_color: String,
    pub milestone_color: String,
    pub container_color: String,
    pub background_color: String,
    pub grid_color: String,
    pub text_color: String,
    pub header_bg: String,
    pub arrow_color: String,
    pub highlight_color: String,
}

impl Default for GanttTheme {
    fn default() -> Self {
        Self::light()
    }
}

impl GanttTheme {
    pub fn light() -> Self {
        Self {
            critical_color: "#e74c3c".into(),
            normal_color: "#3498db".into(),
            milestone_color: "#9b59b6".into(),
            container_color: "#95a5a6".into(),
            background_color: "#ffffff".into(),
            grid_color: "#ecf0f1".into(),
            text_color: "#2c3e50".into(),
            header_bg: "#f8f9fa".into(),
            arrow_color: "#7f8c8d".into(),
            highlight_color: "#f39c12".into(),
        }
    }

    pub fn dark() -> Self {
        Self {
            critical_color: "#e74c3c".into(),
            normal_color: "#3498db".into(),
            milestone_color: "#9b59b6".into(),
            container_color: "#7f8c8d".into(),
            background_color: "#1a1a2e".into(),
            grid_color: "#2d2d44".into(),
            text_color: "#eaeaea".into(),
            header_bg: "#16213e".into(),
            arrow_color: "#95a5a6".into(),
            highlight_color: "#f39c12".into(),
        }
    }
}

impl Default for HtmlGanttRenderer {
    fn default() -> Self {
        Self {
            chart_width: 900,
            row_height: 32,
            label_width: 450, // Wider to fit WBS codes + task names
            header_height: 60,
            padding: 20,
            theme: GanttTheme::default(),
            show_dependencies: true,
            interactive: true,
            focus: None,
        }
    }
}

impl HtmlGanttRenderer {
    pub fn new() -> Self {
        Self::default()
    }

    /// Use dark theme
    pub fn dark_theme(mut self) -> Self {
        self.theme = GanttTheme::dark();
        self
    }

    /// Configure chart width
    pub fn chart_width(mut self, width: u32) -> Self {
        self.chart_width = width;
        self
    }

    /// Configure row height
    pub fn row_height(mut self, height: u32) -> Self {
        self.row_height = height;
        self
    }

    /// Disable dependency arrows
    pub fn hide_dependencies(mut self) -> Self {
        self.show_dependencies = false;
        self
    }

    /// Disable interactivity
    pub fn static_chart(mut self) -> Self {
        self.interactive = false;
        self
    }

    /// Set focus view configuration
    ///
    /// Focus view expands tasks matching the patterns while collapsing others.
    ///
    /// # Arguments
    /// * `patterns` - Task ID prefixes or glob patterns to expand
    ///
    /// # Example
    /// ```ignore
    /// let renderer = HtmlGanttRenderer::new()
    ///     .focus(vec!["6.3.2".into(), "8.6".into()]);
    /// ```
    pub fn focus(mut self, patterns: Vec<String>) -> Self {
        let context_depth = self.focus.as_ref().map(|f| f.context_depth).unwrap_or(1);
        self.focus = Some(FocusConfig::new(patterns, context_depth));
        self
    }

    /// Set context depth for non-focused tasks
    ///
    /// * `0` = hide all non-focused tasks
    /// * `1` = show only top-level containers (default)
    /// * `2` = show two levels of hierarchy
    ///
    /// # Example
    /// ```ignore
    /// let renderer = HtmlGanttRenderer::new()
    ///     .focus(vec!["6.3.2".into()])
    ///     .context_depth(0); // Hide all context
    /// ```
    pub fn context_depth(mut self, depth: usize) -> Self {
        if let Some(ref mut focus) = self.focus {
            focus.context_depth = depth;
        } else {
            self.focus = Some(FocusConfig::new(vec![], depth));
        }
        self
    }

    /// Calculate pixels per day based on date range
    fn pixels_per_day(&self, start: NaiveDate, end: NaiveDate) -> f64 {
        let days = (end - start).num_days().max(1) as f64;
        self.chart_width as f64 / days
    }

    /// Convert a date to x position
    fn date_to_x(&self, date: NaiveDate, project_start: NaiveDate, px_per_day: f64) -> f64 {
        let days = (date - project_start).num_days() as f64;
        self.padding as f64 + self.label_width as f64 + (days * px_per_day)
    }

    /// Build flat list of tasks with hierarchy info, respecting focus configuration
    fn flatten_tasks_for_display<'a>(
        &self,
        project: &'a Project,
        schedule: &'a Schedule,
    ) -> Vec<TaskDisplay<'a>> {
        let mut all_tasks = Vec::new();
        self.collect_tasks(&project.tasks, schedule, "", 0, &mut all_tasks);

        // If no focus config, return all tasks as expanded
        let Some(ref focus) = self.focus else {
            return all_tasks;
        };

        // First pass: identify which task IDs are directly focused
        let focused_ids: std::collections::HashSet<String> = all_tasks
            .iter()
            .filter(|t| focus.matches_focus(&t.qualified_id, &t.task.name))
            .map(|t| t.qualified_id.clone())
            .collect();

        // Second pass: identify ancestors of focused tasks
        let ancestor_ids: std::collections::HashSet<String> = all_tasks
            .iter()
            .filter(|t| {
                // Check if any focused task starts with this task's ID + "."
                focused_ids.iter().any(|fid| {
                    fid.starts_with(&t.qualified_id)
                        && fid.len() > t.qualified_id.len()
                        && fid.chars().nth(t.qualified_id.len()) == Some('.')
                })
            })
            .map(|t| t.qualified_id.clone())
            .collect();

        // Third pass: compute visibility and filter
        let mut result = Vec::new();
        let mut skip_children_of: Option<String> = None;

        for mut task_display in all_tasks {
            // Skip children of collapsed containers
            if let Some(ref skip_prefix) = skip_children_of {
                if task_display.qualified_id.starts_with(skip_prefix)
                    && task_display.qualified_id.len() > skip_prefix.len()
                {
                    continue;
                }
                skip_children_of = None;
            }

            let _is_focused = focused_ids.contains(&task_display.qualified_id);
            let is_ancestor = ancestor_ids.contains(&task_display.qualified_id);
            let is_descendant = focused_ids.iter().any(|fid| {
                task_display.qualified_id.starts_with(fid)
                    && task_display.qualified_id.len() > fid.len()
            });

            let visibility = focus.get_visibility(
                &task_display.qualified_id,
                &task_display.task.name,
                task_display.depth,
                is_ancestor,
                is_descendant,
            );

            match visibility {
                TaskVisibility::Hidden => continue,
                TaskVisibility::Collapsed => {
                    // Show this task but skip its children
                    if task_display.is_container {
                        skip_children_of = Some(task_display.qualified_id.clone() + ".");
                    }
                    task_display.visibility = TaskVisibility::Collapsed;
                    result.push(task_display);
                }
                TaskVisibility::Expanded => {
                    task_display.visibility = TaskVisibility::Expanded;
                    result.push(task_display);
                }
            }
        }

        result
    }

    fn collect_tasks<'a>(
        &self,
        tasks: &'a [Task],
        schedule: &'a Schedule,
        prefix: &str,
        depth: usize,
        result: &mut Vec<TaskDisplay<'a>>,
    ) {
        for task in tasks {
            let qualified_id = if prefix.is_empty() {
                task.id.clone()
            } else {
                format!("{}.{}", prefix, task.id)
            };

            let scheduled = schedule.tasks.get(&qualified_id);
            let is_container = !task.children.is_empty();

            result.push(TaskDisplay {
                task,
                qualified_id: qualified_id.clone(),
                scheduled,
                depth,
                is_container,
                child_count: task.children.len(),
                visibility: TaskVisibility::Expanded, // Default, may be changed by focus logic
            });

            if !task.children.is_empty() {
                self.collect_tasks(&task.children, schedule, &qualified_id, depth + 1, result);
            }
        }
    }

    /// Generate the complete HTML document
    fn generate_html(
        &self,
        project: &Project,
        schedule: &Schedule,
        tasks: &[TaskDisplay],
    ) -> String {
        let project_start = project.start;
        let project_end = schedule.project_end;
        let px_per_day = self.pixels_per_day(project_start, project_end);

        let total_width = self.padding * 2 + self.label_width + self.chart_width;
        let total_height =
            self.padding * 2 + self.header_height + (tasks.len() as u32 * self.row_height) + 50;

        let svg_content = self.generate_svg(project, schedule, tasks, px_per_day);
        let css = self.generate_css();
        let js = if self.interactive {
            self.generate_js(tasks)
        } else {
            String::new()
        };

        format!(
            r#"<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{title} - Gantt Chart</title>
    <style>
{css}
    </style>
</head>
<body>
    <div class="gantt-container">
        <div class="gantt-header">
            <h1>{title}</h1>
            <div class="gantt-controls">
                <button onclick="zoomIn()" title="Zoom In">+</button>
                <button onclick="zoomOut()" title="Zoom Out">−</button>
                <button onclick="resetZoom()" title="Reset">Reset</button>
            </div>
        </div>
        <div class="gantt-wrapper" id="gantt-wrapper">
            <svg id="gantt-svg" width="{width}" height="{height}" viewBox="0 0 {width} {height}">
{svg_content}
            </svg>
        </div>
        <div class="gantt-legend">
            <span class="legend-item"><span class="legend-box critical"></span>Critical Path</span>
            <span class="legend-item"><span class="legend-box normal"></span>Normal Task</span>
            <span class="legend-item"><span class="legend-diamond"></span>Milestone</span>
            <span class="legend-item"><span class="legend-box container"></span>Container</span>
        </div>
        <div id="tooltip" class="tooltip"></div>
    </div>
    <script>
{js}
    </script>
</body>
</html>"#,
            title = html_escape(&project.name),
            css = css,
            width = total_width,
            height = total_height,
            svg_content = svg_content,
            js = js,
        )
    }

    /// Generate the SVG content (without the outer <svg> tag)
    fn generate_svg(
        &self,
        project: &Project,
        schedule: &Schedule,
        tasks: &[TaskDisplay],
        px_per_day: f64,
    ) -> String {
        let mut svg = String::new();
        let project_start = project.start;
        let project_end = schedule.project_end;

        // Background
        svg.push_str(&format!(
            r#"                <rect width="100%" height="100%" fill="{}"/>"#,
            self.theme.background_color
        ));
        svg.push('\n');

        // Grid
        svg.push_str(&self.render_grid(tasks.len(), project_start, project_end, px_per_day));

        // Header
        svg.push_str(&self.render_header(project_start, project_end, px_per_day));

        // Task bars
        for (row, task_display) in tasks.iter().enumerate() {
            svg.push_str(&self.render_task_row(task_display, row, project_start, px_per_day));
        }

        // Dependency arrows
        if self.show_dependencies {
            svg.push_str(&self.render_dependencies(
                project,
                schedule,
                tasks,
                project_start,
                px_per_day,
            ));
        }

        svg
    }

    /// Render the timeline header
    fn render_header(
        &self,
        project_start: NaiveDate,
        project_end: NaiveDate,
        px_per_day: f64,
    ) -> String {
        let mut svg = String::new();

        // Header background
        svg.push_str(&format!(
            r#"                <rect x="{}" y="{}" width="{}" height="{}" fill="{}"/>"#,
            self.padding,
            self.padding,
            self.label_width + self.chart_width,
            self.header_height,
            self.theme.header_bg
        ));
        svg.push('\n');

        // Calculate date interval
        let total_days = (project_end - project_start).num_days();
        let interval_days = if total_days <= 14 {
            1
        } else if total_days <= 60 {
            7
        } else if total_days <= 180 {
            14
        } else {
            30
        };

        // Date labels
        let mut current = project_start;
        while current <= project_end {
            let x = self.date_to_x(current, project_start, px_per_day);

            // Tick mark
            svg.push_str(&format!(
                r#"                <line x1="{x}" y1="{y1}" x2="{x}" y2="{y2}" stroke="{color}" stroke-width="1"/>"#,
                x = x,
                y1 = self.padding + self.header_height - 10,
                y2 = self.padding + self.header_height,
                color = self.theme.text_color
            ));
            svg.push('\n');

            // Date label
            let label = if interval_days == 1 {
                current.format("%d").to_string()
            } else {
                current.format("%b %d").to_string()
            };

            svg.push_str(&format!(
                r#"                <text x="{x}" y="{y}" font-size="11" fill="{color}" text-anchor="middle">{label}</text>"#,
                x = x,
                y = self.padding + self.header_height - 15,
                color = self.theme.text_color,
                label = label
            ));
            svg.push('\n');

            current += chrono::Duration::days(interval_days);
        }

        // Month/year label
        let month_label = project_start.format("%B %Y").to_string();
        svg.push_str(&format!(
            r#"                <text x="{x}" y="{y}" font-size="14" font-weight="bold" fill="{color}" text-anchor="middle">{label}</text>"#,
            x = self.padding + self.label_width + self.chart_width / 2,
            y = self.padding + 22,
            color = self.theme.text_color,
            label = month_label
        ));
        svg.push('\n');

        svg
    }

    /// Render grid lines
    fn render_grid(
        &self,
        task_count: usize,
        project_start: NaiveDate,
        project_end: NaiveDate,
        px_per_day: f64,
    ) -> String {
        let mut svg = String::new();
        let chart_top = self.padding + self.header_height;
        let chart_bottom = chart_top + (task_count as u32 * self.row_height);

        // Horizontal lines
        for i in 0..=task_count {
            let y = chart_top + (i as u32 * self.row_height);
            svg.push_str(&format!(
                r#"                <line x1="{x1}" y1="{y}" x2="{x2}" y2="{y}" stroke="{color}" stroke-width="1"/>"#,
                x1 = self.padding,
                y = y,
                x2 = self.padding + self.label_width + self.chart_width,
                color = self.theme.grid_color
            ));
            svg.push('\n');
        }

        // Vertical lines
        let total_days = (project_end - project_start).num_days();
        let interval = if total_days <= 30 { 1 } else { 7 };

        let mut current = project_start;
        while current <= project_end {
            let x = self.date_to_x(current, project_start, px_per_day);
            svg.push_str(&format!(
                r#"                <line x1="{x}" y1="{y1}" x2="{x}" y2="{y2}" stroke="{color}" stroke-width="1"/>"#,
                x = x,
                y1 = chart_top,
                y2 = chart_bottom,
                color = self.theme.grid_color
            ));
            svg.push('\n');
            current += chrono::Duration::days(interval);
        }

        svg
    }

    /// Render a single task row
    fn render_task_row(
        &self,
        task_display: &TaskDisplay,
        row: usize,
        project_start: NaiveDate,
        px_per_day: f64,
    ) -> String {
        let mut svg = String::new();

        let y = self.padding + self.header_height + (row as u32 * self.row_height);
        let bar_height = (self.row_height as f64 * 0.6) as u32;
        let bar_y = y + (self.row_height - bar_height) / 2;

        // Check if task is collapsed (focus view)
        let is_collapsed = task_display.visibility == TaskVisibility::Collapsed;

        // Indent for hierarchy
        let indent = task_display.depth as u32 * 16;
        let label_x = self.padding + 8 + indent;

        // Container expand/collapse icon
        // â–¶ for collapsed, â–¼ for expanded
        if task_display.is_container || is_collapsed {
            let icon_x = label_x - 12;
            let icon_y = y + self.row_height / 2;
            let icon = if is_collapsed { "â–¶" } else { "â–¼" };
            let icon_color = if is_collapsed {
                "#9ca3af" // Muted gray for collapsed
            } else {
                &self.theme.text_color
            };
            svg.push_str(&format!(
                r#"                <text x="{x}" y="{y}" font-size="10" fill="{color}" class="collapse-icon" data-task="{id}" style="cursor:pointer">{icon}</text>"#,
                x = icon_x,
                y = icon_y + 4,
                color = icon_color,
                id = task_display.qualified_id,
                icon = icon
            ));
            svg.push('\n');
        }

        // Task label - calculate max chars based on available width
        // ~7px per char at 12px font, subtract indent (16px per level) and some margin
        let available_px = self.label_width.saturating_sub(indent as u32 + 20);
        let max_chars = (available_px / 7) as usize;
        let label = truncate(&task_display.task.name, max_chars.max(10));
        let label_color = if is_collapsed {
            "#9ca3af" // Muted gray for collapsed tasks
        } else {
            &self.theme.text_color
        };
        svg.push_str(&format!(
            r#"                <text x="{x}" y="{y}" font-size="12" fill="{color}">{label}</text>"#,
            x = label_x,
            y = y + self.row_height / 2 + 4,
            color = label_color,
            label = html_escape(&label)
        ));
        svg.push('\n');

        // Task bar (if scheduled)
        if let Some(scheduled) = task_display.scheduled {
            let x_start = self.date_to_x(scheduled.start, project_start, px_per_day);
            let x_end = self.date_to_x(scheduled.finish, project_start, px_per_day);
            let bar_width = (x_end - x_start).max(4.0);

            let is_milestone = scheduled.duration.minutes == 0;

            if is_milestone && !is_collapsed {
                // Diamond for milestone (not shown when collapsed)
                let cx = x_start;
                let cy = (bar_y + bar_height / 2) as f64;
                let size = (bar_height as f64) / 2.0;

                svg.push_str(&format!(
                    r#"                <polygon points="{p1},{p2} {p3},{p4} {p5},{p6} {p7},{p8}" fill="{color}" class="task-bar milestone" data-task="{id}"/>"#,
                    p1 = cx, p2 = cy - size,
                    p3 = cx + size, p4 = cy,
                    p5 = cx, p6 = cy + size,
                    p7 = cx - size, p8 = cy,
                    color = self.theme.milestone_color,
                    id = task_display.qualified_id
                ));
                svg.push('\n');
            } else if is_collapsed {
                // Collapsed container: solid muted bar with distinct style
                let collapsed_color = "#b8c0cc"; // Light gray for collapsed
                let collapsed_bar_height = (bar_height as f64 * 0.7) as u32;
                let collapsed_bar_y = y + (self.row_height - collapsed_bar_height) / 2;

                svg.push_str(&format!(
                    r#"                <rect x="{x}" y="{y}" width="{w}" height="{h}" rx="2" fill="{color}" opacity="0.7" class="task-bar collapsed" data-task="{id}"/>"#,
                    x = x_start,
                    y = collapsed_bar_y,
                    w = bar_width,
                    h = collapsed_bar_height,
                    color = collapsed_color,
                    id = task_display.qualified_id
                ));
                svg.push('\n');
            } else if task_display.is_container {
                // Expanded container bar (bracket style)
                let bracket_height = 6.0;
                svg.push_str(&format!(
                    r#"                <path d="M{x1},{y1} L{x1},{y2} L{x2},{y2} L{x2},{y1}" fill="none" stroke="{color}" stroke-width="3" class="task-bar container" data-task="{id}"/>"#,
                    x1 = x_start,
                    y1 = bar_y as f64 + bracket_height,
                    y2 = bar_y as f64,
                    x2 = x_start + bar_width,
                    color = self.theme.container_color,
                    id = task_display.qualified_id
                ));
                svg.push('\n');
            } else {
                // Regular task bar
                let color = if scheduled.is_critical {
                    &self.theme.critical_color
                } else {
                    &self.theme.normal_color
                };

                svg.push_str(&format!(
                    r#"                <rect x="{x}" y="{y}" width="{w}" height="{h}" rx="3" fill="{color}" class="task-bar" data-task="{id}"/>"#,
                    x = x_start,
                    y = bar_y,
                    w = bar_width,
                    h = bar_height,
                    color = color,
                    id = task_display.qualified_id
                ));
                svg.push('\n');

                // Progress overlay if complete percentage is set
                if let Some(complete) = task_display.task.complete {
                    let progress_width = bar_width * (complete as f64 / 100.0);
                    svg.push_str(&format!(
                        r#"                <rect x="{x}" y="{y}" width="{w}" height="{h}" rx="3" fill="rgba(255,255,255,0.3)"/>"#,
                        x = x_start,
                        y = bar_y,
                        w = progress_width,
                        h = bar_height
                    ));
                    svg.push('\n');
                }
            }
        }

        svg
    }

    /// Render dependency arrows
    fn render_dependencies(
        &self,
        _project: &Project,
        _schedule: &Schedule,
        tasks: &[TaskDisplay],
        project_start: NaiveDate,
        px_per_day: f64,
    ) -> String {
        let mut svg = String::new();
        svg.push_str(r#"                <g class="dependencies">"#);
        svg.push('\n');

        // Build task position map
        let mut task_positions: HashMap<&str, (usize, &ScheduledTask)> = HashMap::new();
        for (row, task_display) in tasks.iter().enumerate() {
            if let Some(scheduled) = task_display.scheduled {
                task_positions.insert(&task_display.qualified_id, (row, scheduled));
            }
        }

        // Draw arrows
        for task_display in tasks {
            if let Some(to_scheduled) = task_display.scheduled {
                for dep in &task_display.task.depends {
                    // Try to find the predecessor
                    let pred_id = self.resolve_dependency(
                        &dep.predecessor,
                        &task_display.qualified_id,
                        &task_positions,
                    );

                    if let Some((from_row, from_scheduled)) =
                        pred_id.and_then(|id| task_positions.get(id.as_str()))
                    {
                        let to_row = task_positions
                            .get(task_display.qualified_id.as_str())
                            .map(|(r, _)| *r)
                            .unwrap_or(0);

                        let arrow = self.render_arrow(
                            *from_row,
                            from_scheduled,
                            to_row,
                            to_scheduled,
                            &dep.dep_type,
                            project_start,
                            px_per_day,
                        );
                        svg.push_str(&arrow);
                    }
                }
            }
        }

        svg.push_str("                </g>\n");
        svg
    }

    /// Resolve dependency path
    fn resolve_dependency(
        &self,
        dep_path: &str,
        from_id: &str,
        positions: &HashMap<&str, (usize, &ScheduledTask)>,
    ) -> Option<String> {
        // Try absolute path first
        if positions.contains_key(dep_path) {
            return Some(dep_path.to_string());
        }

        // Try relative (sibling) resolution
        if let Some(dot_pos) = from_id.rfind('.') {
            let parent = &from_id[..dot_pos];
            let qualified = format!("{}.{}", parent, dep_path);
            if positions.contains_key(qualified.as_str()) {
                return Some(qualified);
            }
        }

        None
    }

    /// Render a dependency arrow
    fn render_arrow(
        &self,
        from_row: usize,
        from_task: &ScheduledTask,
        to_row: usize,
        to_task: &ScheduledTask,
        dep_type: &utf8proj_core::DependencyType,
        project_start: NaiveDate,
        px_per_day: f64,
    ) -> String {
        let bar_height = (self.row_height as f64 * 0.6) as f64;
        let bar_y_offset = (self.row_height as f64 - bar_height) / 2.0;

        let from_y = self.padding as f64
            + self.header_height as f64
            + (from_row as f64 * self.row_height as f64)
            + bar_y_offset
            + bar_height / 2.0;
        let to_y = self.padding as f64
            + self.header_height as f64
            + (to_row as f64 * self.row_height as f64)
            + bar_y_offset
            + bar_height / 2.0;

        let (from_x, to_x) = match dep_type {
            utf8proj_core::DependencyType::FinishToStart => {
                let fx = self.date_to_x(from_task.finish, project_start, px_per_day) + 2.0;
                let tx = self.date_to_x(to_task.start, project_start, px_per_day) - 2.0;
                (fx, tx)
            }
            utf8proj_core::DependencyType::StartToStart => {
                let fx = self.date_to_x(from_task.start, project_start, px_per_day) - 2.0;
                let tx = self.date_to_x(to_task.start, project_start, px_per_day) - 2.0;
                (fx, tx)
            }
            utf8proj_core::DependencyType::FinishToFinish => {
                let fx = self.date_to_x(from_task.finish, project_start, px_per_day) + 2.0;
                let tx = self.date_to_x(to_task.finish, project_start, px_per_day) + 2.0;
                (fx, tx)
            }
            utf8proj_core::DependencyType::StartToFinish => {
                let fx = self.date_to_x(from_task.start, project_start, px_per_day) - 2.0;
                let tx = self.date_to_x(to_task.finish, project_start, px_per_day) + 2.0;
                (fx, tx)
            }
        };

        // Create curved path
        let mid_x = (from_x + to_x) / 2.0;
        let path = if (to_row as i32 - from_row as i32).abs() <= 1 {
            // Simple curve for adjacent rows
            format!(
                "M{},{} C{},{} {},{} {},{}",
                from_x, from_y, mid_x, from_y, mid_x, to_y, to_x, to_y
            )
        } else {
            // More complex path for distant rows
            let offset = 15.0;
            format!(
                "M{},{} L{},{} L{},{} L{},{}",
                from_x,
                from_y,
                from_x + offset,
                from_y,
                from_x + offset,
                to_y,
                to_x,
                to_y
            )
        };

        format!(
            r#"                    <path d="{path}" fill="none" stroke="{color}" stroke-width="1.5" marker-end="url(#arrowhead)" class="dep-arrow"/>
"#,
            path = path,
            color = self.theme.arrow_color
        )
    }

    /// Generate CSS styles
    fn generate_css(&self) -> String {
        format!(
            r#"        :root {{
            --critical-color: {critical};
            --normal-color: {normal};
            --milestone-color: {milestone};
            --container-color: {container};
            --bg-color: {bg};
            --text-color: {text};
            --highlight-color: {highlight};
        }}
        * {{ margin: 0; padding: 0; box-sizing: border-box; }}
        body {{
            font-family: system-ui, -apple-system, sans-serif;
            background: var(--bg-color);
            color: var(--text-color);
            padding: 20px;
        }}
        .gantt-container {{
            max-width: 100%;
            overflow-x: auto;
        }}
        .gantt-header {{
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 16px;
        }}
        .gantt-header h1 {{
            font-size: 1.5rem;
            font-weight: 600;
        }}
        .gantt-controls button {{
            padding: 8px 16px;
            margin-left: 8px;
            border: 1px solid var(--text-color);
            background: transparent;
            color: var(--text-color);
            cursor: pointer;
            border-radius: 4px;
            font-size: 14px;
        }}
        .gantt-controls button:hover {{
            background: rgba(128,128,128,0.2);
        }}
        .gantt-wrapper {{
            overflow-x: auto;
            border: 1px solid rgba(128,128,128,0.3);
            border-radius: 8px;
        }}
        .gantt-legend {{
            display: flex;
            gap: 24px;
            margin-top: 16px;
            font-size: 13px;
        }}
        .legend-item {{
            display: flex;
            align-items: center;
            gap: 6px;
        }}
        .legend-box {{
            width: 16px;
            height: 12px;
            border-radius: 2px;
        }}
        .legend-box.critical {{ background: var(--critical-color); }}
        .legend-box.normal {{ background: var(--normal-color); }}
        .legend-box.container {{ background: var(--container-color); }}
        .legend-diamond {{
            width: 10px;
            height: 10px;
            background: var(--milestone-color);
            transform: rotate(45deg);
        }}
        .task-bar {{
            cursor: pointer;
            transition: opacity 0.2s;
        }}
        .task-bar:hover {{
            opacity: 0.8;
        }}
        .task-bar.highlighted {{
            stroke: var(--highlight-color);
            stroke-width: 3;
        }}
        .dep-arrow {{
            opacity: 0.6;
            transition: opacity 0.2s;
        }}
        .dep-arrow.highlighted {{
            opacity: 1;
            stroke: var(--highlight-color);
            stroke-width: 2;
        }}
        .tooltip {{
            position: fixed;
            background: rgba(0,0,0,0.9);
            color: white;
            padding: 12px;
            border-radius: 6px;
            font-size: 13px;
            pointer-events: none;
            opacity: 0;
            transition: opacity 0.2s;
            z-index: 1000;
            max-width: 300px;
        }}
        .tooltip.visible {{
            opacity: 1;
        }}
        .tooltip .task-name {{
            font-weight: 600;
            margin-bottom: 8px;
        }}
        .tooltip .task-dates {{
            color: #aaa;
        }}"#,
            critical = self.theme.critical_color,
            normal = self.theme.normal_color,
            milestone = self.theme.milestone_color,
            container = self.theme.container_color,
            bg = self.theme.background_color,
            text = self.theme.text_color,
            highlight = self.theme.highlight_color,
        )
    }

    /// Generate JavaScript for interactivity
    fn generate_js(&self, tasks: &[TaskDisplay]) -> String {
        // Build task data for JS
        let mut task_data = String::from("const taskData = {\n");
        for task_display in tasks {
            if let Some(scheduled) = task_display.scheduled {
                task_data.push_str(&format!(
                    r#"            "{}": {{ name: "{}", start: "{}", finish: "{}", duration: "{} days", critical: {}, deps: [{}] }},
"#,
                    task_display.qualified_id,
                    html_escape(&task_display.task.name),
                    scheduled.start,
                    scheduled.finish,
                    scheduled.duration.as_days() as i64,
                    scheduled.is_critical,
                    task_display.task.depends.iter()
                        .map(|d| format!("\"{}\"", d.predecessor))
                        .collect::<Vec<_>>()
                        .join(", ")
                ));
            }
        }
        task_data.push_str("        };\n");

        format!(
            r#"        {task_data}

        // Zoom functionality
        let currentZoom = 1;
        const wrapper = document.getElementById('gantt-wrapper');
        const svg = document.getElementById('gantt-svg');

        function zoomIn() {{
            currentZoom = Math.min(currentZoom * 1.2, 3);
            applyZoom();
        }}

        function zoomOut() {{
            currentZoom = Math.max(currentZoom / 1.2, 0.5);
            applyZoom();
        }}

        function resetZoom() {{
            currentZoom = 1;
            applyZoom();
        }}

        function applyZoom() {{
            svg.style.transform = `scale(${{currentZoom}})`;
            svg.style.transformOrigin = 'top left';
        }}

        // Tooltip functionality
        const tooltip = document.getElementById('tooltip');

        document.querySelectorAll('.task-bar').forEach(bar => {{
            bar.addEventListener('mouseenter', (e) => {{
                const taskId = bar.getAttribute('data-task');
                const data = taskData[taskId];
                if (data) {{
                    tooltip.innerHTML = `
                        <div class="task-name">${{data.name}}</div>
                        <div class="task-dates">${{data.start}} → ${{data.finish}}</div>
                        <div>Duration: ${{data.duration}}</div>
                        ${{data.critical ? '<div style="color:#e74c3c">Critical Path</div>' : ''}}
                    `;
                    tooltip.classList.add('visible');
                }}
            }});

            bar.addEventListener('mousemove', (e) => {{
                tooltip.style.left = (e.clientX + 15) + 'px';
                tooltip.style.top = (e.clientY + 15) + 'px';
            }});

            bar.addEventListener('mouseleave', () => {{
                tooltip.classList.remove('visible');
            }});

            // Click to highlight dependencies
            bar.addEventListener('click', () => {{
                const taskId = bar.getAttribute('data-task');
                highlightDependencies(taskId);
            }});
        }});

        function highlightDependencies(taskId) {{
            // Clear previous highlights
            document.querySelectorAll('.highlighted').forEach(el => {{
                el.classList.remove('highlighted');
            }});

            // Highlight selected task
            const taskBar = document.querySelector(`[data-task="${{taskId}}"]`);
            if (taskBar) taskBar.classList.add('highlighted');

            // Highlight dependencies (simplified - would need full dep graph)
            const data = taskData[taskId];
            if (data && data.deps) {{
                data.deps.forEach(depId => {{
                    const depBar = document.querySelector(`[data-task="${{depId}}"]`);
                    if (depBar) depBar.classList.add('highlighted');
                }});
            }}
        }}

        // Arrow marker definition
        const defs = document.createElementNS('http://www.w3.org/2000/svg', 'defs');
        defs.innerHTML = `
            <marker id="arrowhead" markerWidth="10" markerHeight="7" refX="9" refY="3.5" orient="auto">
                <polygon points="0 0, 10 3.5, 0 7" fill="{arrow_color}" />
            </marker>
        `;
        svg.insertBefore(defs, svg.firstChild);"#,
            task_data = task_data,
            arrow_color = self.theme.arrow_color
        )
    }
}

/// Task display info for rendering
struct TaskDisplay<'a> {
    task: &'a Task,
    qualified_id: String,
    scheduled: Option<&'a ScheduledTask>,
    depth: usize,
    is_container: bool,
    #[allow(dead_code)]
    child_count: usize,
    /// Visibility state for focus view
    visibility: TaskVisibility,
}

impl Renderer for HtmlGanttRenderer {
    type Output = String;

    fn render(&self, project: &Project, schedule: &Schedule) -> Result<String, RenderError> {
        let tasks = self.flatten_tasks_for_display(project, schedule);

        if tasks.is_empty() {
            return Err(RenderError::InvalidData("No tasks to render".into()));
        }

        Ok(self.generate_html(project, schedule, &tasks))
    }
}

/// HTML-escape a string
fn html_escape(s: &str) -> String {
    s.replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
        .replace('"', "&quot;")
}

/// Truncate a string with ellipsis
fn truncate(s: &str, max: usize) -> String {
    if s.chars().count() <= max {
        s.to_string()
    } else {
        format!(
            "{}…",
            s.chars().take(max.saturating_sub(1)).collect::<String>()
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::NaiveDate;
    use std::collections::HashMap;
    use utf8proj_core::{Duration, Schedule, ScheduledTask, TaskStatus};

    fn create_test_project() -> Project {
        let mut project = Project::new("Test Project");
        project.start = NaiveDate::from_ymd_opt(2025, 1, 6).unwrap();
        project.tasks.push(
            Task::new("design")
                .name("Design Phase")
                .effort(Duration::days(5)),
        );
        project.tasks.push(
            Task::new("implement")
                .name("Implementation")
                .effort(Duration::days(10))
                .depends_on("design"),
        );
        project.tasks.push(
            Task::new("test")
                .name("Testing")
                .effort(Duration::days(3))
                .depends_on("implement"),
        );
        project
    }

    fn create_test_schedule() -> Schedule {
        let mut tasks = HashMap::new();

        let start1 = NaiveDate::from_ymd_opt(2025, 1, 6).unwrap();
        let finish1 = NaiveDate::from_ymd_opt(2025, 1, 10).unwrap();
        tasks.insert(
            "design".to_string(),
            ScheduledTask {
                task_id: "design".to_string(),
                start: start1,
                finish: finish1,
                duration: Duration::days(5),
                assignments: vec![],
                slack: Duration::zero(),
                is_critical: true,
                early_start: start1,
                early_finish: finish1,
                late_start: start1,
                late_finish: finish1,
                forecast_start: start1,
                forecast_finish: finish1,
                remaining_duration: Duration::days(5),
                percent_complete: 0,
                status: TaskStatus::NotStarted,
                cost_range: None,
                has_abstract_assignments: false,
                baseline_start: start1,
                baseline_finish: finish1,
                start_variance_days: 0,
                finish_variance_days: 0,
            },
        );

        let start2 = NaiveDate::from_ymd_opt(2025, 1, 13).unwrap();
        let finish2 = NaiveDate::from_ymd_opt(2025, 1, 24).unwrap();
        tasks.insert(
            "implement".to_string(),
            ScheduledTask {
                task_id: "implement".to_string(),
                start: start2,
                finish: finish2,
                duration: Duration::days(10),
                assignments: vec![],
                slack: Duration::zero(),
                is_critical: true,
                early_start: start2,
                early_finish: finish2,
                late_start: start2,
                late_finish: finish2,
                forecast_start: start2,
                forecast_finish: finish2,
                remaining_duration: Duration::days(10),
                percent_complete: 0,
                status: TaskStatus::NotStarted,
                cost_range: None,
                has_abstract_assignments: false,
                baseline_start: start2,
                baseline_finish: finish2,
                start_variance_days: 0,
                finish_variance_days: 0,
            },
        );

        let start3 = NaiveDate::from_ymd_opt(2025, 1, 27).unwrap();
        let finish3 = NaiveDate::from_ymd_opt(2025, 1, 29).unwrap();
        tasks.insert(
            "test".to_string(),
            ScheduledTask {
                task_id: "test".to_string(),
                start: start3,
                finish: finish3,
                duration: Duration::days(3),
                assignments: vec![],
                slack: Duration::zero(),
                is_critical: true,
                early_start: start3,
                early_finish: finish3,
                late_start: start3,
                late_finish: finish3,
                forecast_start: start3,
                forecast_finish: finish3,
                remaining_duration: Duration::days(3),
                percent_complete: 0,
                status: TaskStatus::NotStarted,
                cost_range: None,
                has_abstract_assignments: false,
                baseline_start: start3,
                baseline_finish: finish3,
                start_variance_days: 0,
                finish_variance_days: 0,
            },
        );

        let project_end = NaiveDate::from_ymd_opt(2025, 1, 29).unwrap();
        Schedule {
            tasks,
            critical_path: vec![
                "design".to_string(),
                "implement".to_string(),
                "test".to_string(),
            ],
            project_duration: Duration::days(18),
            project_end,
            total_cost: None,
            total_cost_range: None,
            project_progress: 0,
            project_baseline_finish: project_end,
            project_forecast_finish: project_end,
            project_variance_days: 0,
            planned_value: 0,
            earned_value: 0,
            spi: 1.0,
        }
    }

    #[test]
    fn html_gantt_renderer_creation() {
        let renderer = HtmlGanttRenderer::new();
        assert_eq!(renderer.chart_width, 900);
        assert_eq!(renderer.row_height, 32);
        assert!(renderer.interactive);
    }

    #[test]
    fn html_gantt_with_dark_theme() {
        let renderer = HtmlGanttRenderer::new().dark_theme();
        assert_eq!(renderer.theme.background_color, "#1a1a2e");
    }

    #[test]
    fn html_gantt_produces_valid_html() {
        let renderer = HtmlGanttRenderer::new();
        let project = create_test_project();
        let schedule = create_test_schedule();

        let result = renderer.render(&project, &schedule);
        assert!(result.is_ok());

        let html = result.unwrap();
        assert!(html.starts_with("<!DOCTYPE html>"));
        assert!(html.contains("</html>"));
        assert!(html.contains("Test Project"));
        assert!(html.contains("Design Phase"));
    }

    #[test]
    fn html_gantt_includes_svg() {
        let renderer = HtmlGanttRenderer::new();
        let project = create_test_project();
        let schedule = create_test_schedule();

        let html = renderer.render(&project, &schedule).unwrap();
        assert!(html.contains("<svg"));
        assert!(html.contains("</svg>"));
    }

    #[test]
    fn html_gantt_includes_interactivity() {
        let renderer = HtmlGanttRenderer::new();
        let project = create_test_project();
        let schedule = create_test_schedule();

        let html = renderer.render(&project, &schedule).unwrap();
        assert!(html.contains("zoomIn()"));
        assert!(html.contains("tooltip"));
        assert!(html.contains("taskData"));
    }

    #[test]
    fn html_gantt_static_mode() {
        let renderer = HtmlGanttRenderer::new().static_chart();
        let project = create_test_project();
        let schedule = create_test_schedule();

        let html = renderer.render(&project, &schedule).unwrap();
        // Should not have interactive JS
        assert!(!html.contains("taskData"));
    }

    #[test]
    fn html_gantt_empty_schedule_fails() {
        let renderer = HtmlGanttRenderer::new();
        let project = Project::new("Empty");
        let project_end = NaiveDate::from_ymd_opt(2025, 1, 1).unwrap();
        let schedule = Schedule {
            tasks: HashMap::new(),
            critical_path: vec![],
            project_duration: Duration::zero(),
            project_end,
            total_cost: None,
            total_cost_range: None,
            project_progress: 0,
            project_baseline_finish: project_end,
            project_forecast_finish: project_end,
            project_variance_days: 0,
            planned_value: 0,
            earned_value: 0,
            spi: 1.0,
        };

        let result = renderer.render(&project, &schedule);
        assert!(result.is_err());
    }

    #[test]
    fn html_escape_works() {
        assert_eq!(html_escape("<script>"), "&lt;script&gt;");
        assert_eq!(html_escape("a & b"), "a &amp; b");
    }

    #[test]
    fn truncate_works() {
        assert_eq!(truncate("Short", 20), "Short");
        assert_eq!(truncate("This is a very long name", 10), "This is a…");
    }

    #[test]
    fn html_gantt_row_height_option() {
        // Test row_height builder method (lines 123-125)
        let renderer = HtmlGanttRenderer::new().row_height(48);
        assert_eq!(renderer.row_height, 48);
    }

    #[test]
    fn html_gantt_with_ss_dependency() {
        // Test Start-to-Start dependency rendering (lines 623-625)
        let mut project = Project::new("SS Deps");
        project.start = NaiveDate::from_ymd_opt(2025, 1, 6).unwrap();
        project
            .tasks
            .push(Task::new("a").name("Task A").effort(Duration::days(5)));
        let mut task_b = Task::new("b").name("Task B").effort(Duration::days(3));
        task_b.depends.push(utf8proj_core::Dependency {
            predecessor: "a".to_string(),
            dep_type: utf8proj_core::DependencyType::StartToStart,
            lag: None,
        });
        project.tasks.push(task_b);

        let start1 = NaiveDate::from_ymd_opt(2025, 1, 6).unwrap();
        let finish1 = NaiveDate::from_ymd_opt(2025, 1, 10).unwrap();
        let finish2 = NaiveDate::from_ymd_opt(2025, 1, 8).unwrap();
        let mut tasks = HashMap::new();
        tasks.insert(
            "a".to_string(),
            ScheduledTask {
                task_id: "a".to_string(),
                start: start1,
                finish: finish1,
                duration: Duration::days(5),
                assignments: vec![],
                slack: Duration::zero(),
                is_critical: true,
                early_start: start1,
                early_finish: finish1,
                late_start: start1,
                late_finish: finish1,
                forecast_start: start1,
                forecast_finish: finish1,
                remaining_duration: Duration::days(5),
                percent_complete: 0,
                status: TaskStatus::NotStarted,
                cost_range: None,
                has_abstract_assignments: false,
                baseline_start: start1,
                baseline_finish: finish1,
                start_variance_days: 0,
                finish_variance_days: 0,
            },
        );
        tasks.insert(
            "b".to_string(),
            ScheduledTask {
                task_id: "b".to_string(),
                start: start1, // SS: starts at same time
                finish: finish2,
                duration: Duration::days(3),
                assignments: vec![],
                slack: Duration::zero(),
                is_critical: false,
                early_start: start1,
                early_finish: finish2,
                late_start: start1,
                late_finish: finish2,
                forecast_start: start1,
                forecast_finish: finish2,
                remaining_duration: Duration::days(3),
                percent_complete: 0,
                status: TaskStatus::NotStarted,
                cost_range: None,
                has_abstract_assignments: false,
                baseline_start: start1,
                baseline_finish: finish2,
                start_variance_days: 0,
                finish_variance_days: 0,
            },
        );

        let schedule = Schedule {
            tasks,
            critical_path: vec!["a".to_string()],
            project_duration: Duration::days(5),
            project_end: finish1,
            total_cost: None,
            total_cost_range: None,
            project_progress: 0,
            project_baseline_finish: finish1,
            project_forecast_finish: finish1,
            project_variance_days: 0,
            planned_value: 0,
            earned_value: 0,
            spi: 1.0,
        };

        let renderer = HtmlGanttRenderer::new();
        let result = renderer.render(&project, &schedule);
        assert!(result.is_ok());
        let html = result.unwrap();
        // Should contain dependency arrow path
        assert!(html.contains("dep-arrow"));
    }

    #[test]
    fn html_gantt_with_ff_dependency() {
        // Test Finish-to-Finish dependency rendering (lines 628-630)
        let mut project = Project::new("FF Deps");
        project.start = NaiveDate::from_ymd_opt(2025, 1, 6).unwrap();
        project
            .tasks
            .push(Task::new("a").name("Task A").effort(Duration::days(5)));
        let mut task_b = Task::new("b").name("Task B").effort(Duration::days(3));
        task_b.depends.push(utf8proj_core::Dependency {
            predecessor: "a".to_string(),
            dep_type: utf8proj_core::DependencyType::FinishToFinish,
            lag: None,
        });
        project.tasks.push(task_b);

        let start1 = NaiveDate::from_ymd_opt(2025, 1, 6).unwrap();
        let finish1 = NaiveDate::from_ymd_opt(2025, 1, 10).unwrap();
        let start2 = NaiveDate::from_ymd_opt(2025, 1, 8).unwrap();
        let mut tasks = HashMap::new();
        tasks.insert(
            "a".to_string(),
            ScheduledTask {
                task_id: "a".to_string(),
                start: start1,
                finish: finish1,
                duration: Duration::days(5),
                assignments: vec![],
                slack: Duration::zero(),
                is_critical: true,
                early_start: start1,
                early_finish: finish1,
                late_start: start1,
                late_finish: finish1,
                forecast_start: start1,
                forecast_finish: finish1,
                remaining_duration: Duration::days(5),
                percent_complete: 0,
                status: TaskStatus::NotStarted,
                cost_range: None,
                has_abstract_assignments: false,
                baseline_start: start1,
                baseline_finish: finish1,
                start_variance_days: 0,
                finish_variance_days: 0,
            },
        );
        tasks.insert(
            "b".to_string(),
            ScheduledTask {
                task_id: "b".to_string(),
                start: start2,
                finish: finish1, // FF: finishes at same time
                duration: Duration::days(3),
                assignments: vec![],
                slack: Duration::zero(),
                is_critical: false,
                early_start: start2,
                early_finish: finish1,
                late_start: start2,
                late_finish: finish1,
                forecast_start: start2,
                forecast_finish: finish1,
                remaining_duration: Duration::days(3),
                percent_complete: 0,
                status: TaskStatus::NotStarted,
                cost_range: None,
                has_abstract_assignments: false,
                baseline_start: start2,
                baseline_finish: finish1,
                start_variance_days: 0,
                finish_variance_days: 0,
            },
        );

        let schedule = Schedule {
            tasks,
            critical_path: vec!["a".to_string()],
            project_duration: Duration::days(5),
            project_end: finish1,
            total_cost: None,
            total_cost_range: None,
            project_progress: 0,
            project_baseline_finish: finish1,
            project_forecast_finish: finish1,
            project_variance_days: 0,
            planned_value: 0,
            earned_value: 0,
            spi: 1.0,
        };

        let renderer = HtmlGanttRenderer::new();
        let result = renderer.render(&project, &schedule);
        assert!(result.is_ok());
    }

    #[test]
    fn html_gantt_with_sf_dependency() {
        // Test Start-to-Finish dependency rendering (lines 633-635)
        let mut project = Project::new("SF Deps");
        project.start = NaiveDate::from_ymd_opt(2025, 1, 6).unwrap();
        project
            .tasks
            .push(Task::new("a").name("Task A").effort(Duration::days(5)));
        let mut task_b = Task::new("b").name("Task B").effort(Duration::days(3));
        task_b.depends.push(utf8proj_core::Dependency {
            predecessor: "a".to_string(),
            dep_type: utf8proj_core::DependencyType::StartToFinish,
            lag: None,
        });
        project.tasks.push(task_b);

        let start1 = NaiveDate::from_ymd_opt(2025, 1, 6).unwrap();
        let finish1 = NaiveDate::from_ymd_opt(2025, 1, 10).unwrap();
        let start2 = NaiveDate::from_ymd_opt(2025, 1, 3).unwrap();
        let mut tasks = HashMap::new();
        tasks.insert(
            "a".to_string(),
            ScheduledTask {
                task_id: "a".to_string(),
                start: start1,
                finish: finish1,
                duration: Duration::days(5),
                assignments: vec![],
                slack: Duration::zero(),
                is_critical: true,
                early_start: start1,
                early_finish: finish1,
                late_start: start1,
                late_finish: finish1,
                forecast_start: start1,
                forecast_finish: finish1,
                remaining_duration: Duration::days(5),
                percent_complete: 0,
                status: TaskStatus::NotStarted,
                cost_range: None,
                has_abstract_assignments: false,
                baseline_start: start1,
                baseline_finish: finish1,
                start_variance_days: 0,
                finish_variance_days: 0,
            },
        );
        tasks.insert(
            "b".to_string(),
            ScheduledTask {
                task_id: "b".to_string(),
                start: start2,
                finish: start1, // SF: b finishes when a starts
                duration: Duration::days(3),
                assignments: vec![],
                slack: Duration::zero(),
                is_critical: false,
                early_start: start2,
                early_finish: start1,
                late_start: start2,
                late_finish: start1,
                forecast_start: start2,
                forecast_finish: start1,
                remaining_duration: Duration::days(3),
                percent_complete: 0,
                status: TaskStatus::NotStarted,
                cost_range: None,
                has_abstract_assignments: false,
                baseline_start: start2,
                baseline_finish: start1,
                start_variance_days: 0,
                finish_variance_days: 0,
            },
        );

        let schedule = Schedule {
            tasks,
            critical_path: vec!["a".to_string()],
            project_duration: Duration::days(5),
            project_end: finish1,
            total_cost: None,
            total_cost_range: None,
            project_progress: 0,
            project_baseline_finish: finish1,
            project_forecast_finish: finish1,
            project_variance_days: 0,
            planned_value: 0,
            earned_value: 0,
            spi: 1.0,
        };

        let renderer = HtmlGanttRenderer::new();
        let result = renderer.render(&project, &schedule);
        assert!(result.is_ok());
    }

    // =========================================================================
    // Focus View Tests (TDD)
    // =========================================================================

    #[test]
    fn focus_config_empty_patterns_matches_all() {
        let config = FocusConfig::new(vec![], 1);
        assert!(config.matches_focus("any_task", "Any Task Name"));
        assert!(config.matches_focus("6.3.2.1", "[6.3.2.1] GNU Validation"));
    }

    #[test]
    fn focus_config_prefix_matching() {
        let config = FocusConfig::new(vec!["6.3.2".to_string()], 1);

        // Should match: task IDs starting with "6.3.2"
        assert!(config.matches_focus("6.3.2", "Container"));
        assert!(config.matches_focus("6.3.2.1", "Child 1"));
        assert!(config.matches_focus("6.3.2.5.1", "Grandchild"));

        // Should NOT match: different prefixes
        assert!(!config.matches_focus("6.3.1", "Different stream"));
        assert!(!config.matches_focus("7.1", "Different section"));
    }

    #[test]
    fn focus_config_name_matching() {
        let config = FocusConfig::new(vec!["[6.3.2]".to_string()], 1);

        // Should match: task names containing "[6.3.2]"
        assert!(config.matches_focus("task_2259", "[6.3.2] OS Script Migration"));

        // Should NOT match
        assert!(!config.matches_focus("task_2258", "[6.3.1] Something Else"));
    }

    #[test]
    fn focus_config_glob_matching() {
        let config = FocusConfig::new(vec!["*.3.2.*".to_string()], 1);

        // Should match: glob pattern
        assert!(config.matches_focus("6.3.2.1", "Task"));
        assert!(config.matches_focus("7.3.2.5", "Task"));

        // Should NOT match
        assert!(!config.matches_focus("6.4.2.1", "Task"));
    }

    #[test]
    fn focus_config_multiple_patterns() {
        let config = FocusConfig::new(vec!["6.3.2".to_string(), "8.6".to_string()], 1);

        assert!(config.matches_focus("6.3.2.1", "Task"));
        assert!(config.matches_focus("8.6.2", "Task"));
        assert!(!config.matches_focus("7.1", "Task"));
    }

    #[test]
    fn focus_visibility_direct_match() {
        let config = FocusConfig::new(vec!["6.3.2".to_string()], 1);

        let vis = config.get_visibility("6.3.2.1", "Task", 2, false, false);
        assert_eq!(vis, TaskVisibility::Expanded);
    }

    #[test]
    fn focus_visibility_ancestor_of_focused() {
        let config = FocusConfig::new(vec!["6.3.2".to_string()], 1);

        // "6" is ancestor of "6.3.2" - should be expanded to show path
        let vis = config.get_visibility("6", "Section 6", 0, true, false);
        assert_eq!(vis, TaskVisibility::Expanded);
    }

    #[test]
    fn focus_visibility_descendant_of_focused() {
        let config = FocusConfig::new(vec!["6.3".to_string()], 1);

        // "6.3.2.1" is descendant of "6.3" - should be expanded
        let vis = config.get_visibility("6.3.2.1", "Task", 3, false, true);
        assert_eq!(vis, TaskVisibility::Expanded);
    }

    #[test]
    fn focus_visibility_context_depth() {
        let config = FocusConfig::new(vec!["6.3.2".to_string()], 1);

        // Depth 0 (top-level): collapsed (within context_depth)
        let vis = config.get_visibility("7", "Other Section", 0, false, false);
        assert_eq!(vis, TaskVisibility::Collapsed);

        // Depth 1: hidden (exceeds context_depth)
        let vis = config.get_visibility("7.1", "Subsection", 1, false, false);
        assert_eq!(vis, TaskVisibility::Hidden);
    }

    #[test]
    fn focus_visibility_context_depth_zero_hides_all() {
        let config = FocusConfig::new(vec!["6.3.2".to_string()], 0);

        // context_depth=0 means hide all non-focused tasks
        let vis = config.get_visibility("7", "Other Section", 0, false, false);
        assert_eq!(vis, TaskVisibility::Hidden);
    }

    #[test]
    fn focus_visibility_context_depth_two() {
        let config = FocusConfig::new(vec!["6.3.2".to_string()], 2);

        // Depth 0: collapsed
        let vis = config.get_visibility("7", "Other", 0, false, false);
        assert_eq!(vis, TaskVisibility::Collapsed);

        // Depth 1: collapsed (within context_depth=2)
        let vis = config.get_visibility("7.1", "Subsection", 1, false, false);
        assert_eq!(vis, TaskVisibility::Collapsed);

        // Depth 2: hidden
        let vis = config.get_visibility("7.1.1", "Task", 2, false, false);
        assert_eq!(vis, TaskVisibility::Hidden);
    }

    #[test]
    fn focus_config_with_renderer() {
        let mut renderer = HtmlGanttRenderer::new();
        renderer.focus = Some(FocusConfig::new(vec!["6.3.2".to_string()], 1));

        assert!(renderer.focus.is_some());
        assert_eq!(renderer.focus.as_ref().unwrap().focus_patterns.len(), 1);
        assert_eq!(renderer.focus.as_ref().unwrap().context_depth, 1);
    }
}