summaryrefslogtreecommitdiff
path: root/Omni/Jr/Web/Components.hs
blob: 2f885ce9f1f5310da16159737bb1288c72297b00 (plain)
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
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE NoImplicitPrelude #-}

-- : dep lucid
-- : dep servant-lucid
module Omni.Jr.Web.Components
  ( -- * Time formatting
    formatRelativeTime,
    relativeText,
    formatExactTimestamp,
    renderRelativeTimestamp,

    -- * Small components
    metaSep,

    -- * Page layout
    pageHead,
    pageBody,
    pageBodyWithCrumbs,
    navbar,

    -- * JavaScript
    navbarDropdownJs,
    statusDropdownJs,
    priorityDropdownJs,
    complexityDropdownJs,
    liveToggleJs,

    -- * Breadcrumbs
    Breadcrumb (..),
    Breadcrumbs,
    renderBreadcrumbs,
    getAncestors,
    taskBreadcrumbs,

    -- * Badges
    statusBadge,
    complexityBadge,
    statusBadgeWithForm,
    clickableBadge,
    statusDropdownOptions,
    statusOption,
    priorityBadgeWithForm,
    clickablePriorityBadge,
    priorityDropdownOptions,
    priorityOption,
    complexityBadgeWithForm,
    clickableComplexityBadge,
    complexityDropdownOptions,
    complexityOption,

    -- * Sorting
    sortDropdown,
    sortOption,

    -- * Progress bars
    multiColorProgressBar,
    epicProgressBar,

    -- * Task rendering
    renderTaskCard,
    renderBlockedTaskCard,
    renderListGroupItem,
    renderEpicReviewCard,
    renderEpicCardWithStats,
    getDescendants,

    -- * Metrics
    renderAggregatedMetrics,

    -- * Retry context
    renderRetryContextBanner,

    -- * Markdown
    MarkdownBlock (..),
    InlinePart (..),
    renderMarkdown,
    parseBlocks,
    renderBlocks,
    renderBlock,
    renderListItem,
    renderInline,
    parseInline,
    parseBold,
    renderInlineParts,
    renderInlinePart,

    -- * Comments
    commentForm,

    -- * Live toggles
    renderLiveToggle,
    renderAutoscrollToggle,

    -- * Cost/Token metrics
    aggregateCostMetrics,
    formatCostHeader,
    formatTokensHeader,

    -- * Timeline
    renderUnifiedTimeline,
    renderTimelineEvent,
    eventTypeIconAndLabel,
    renderActorLabel,
    renderCommentTimelineEvent,
    renderStatusChangeEvent,
    parseStatusChange,
    renderActivityEvent,
    renderErrorTimelineEvent,
    renderAssistantTimelineEvent,
    renderToolCallTimelineEvent,
    renderToolResultTimelineEvent,
    renderCheckpointEvent,
    renderGuardrailEvent,
    renderGenericEvent,
    parseToolCallContent,
    formatToolCallSummary,
    renderCollapsibleOutput,
    renderDecodedToolResult,
    renderFormattedJson,
    timelineScrollScript,

    -- * Tool rendering helpers
    renderBashToolCall,
    renderReadToolCall,
    renderEditToolCall,
    renderSearchToolCall,
    renderSearchAndReadToolCall,
    renderWriteToolCall,
    renderGenericToolCall,
    extractJsonField,
    extractJsonFieldInt,
    shortenPath,
    DecodedToolResult (..),
    decodeToolResult,
  )
where

import Alpha
import qualified Data.Aeson as Aeson
import qualified Data.Aeson.Key as AesonKey
import qualified Data.Aeson.KeyMap as KeyMap
import qualified Data.ByteString.Lazy as LBS
import qualified Data.List as List
import qualified Data.Text as Text
import Data.Time (NominalDiffTime, UTCTime, defaultTimeLocale, diffUTCTime, formatTime)
import qualified Lucid
import qualified Lucid.Base as Lucid
import Numeric (showFFloat)
import Omni.Jr.Web.Types (SortOrder (..), sortOrderLabel, sortOrderToParam)
import qualified Omni.Task.Core as TaskCore

-- * Time formatting

formatRelativeTime :: UTCTime -> UTCTime -> Text
formatRelativeTime now timestamp =
  let delta = diffUTCTime now timestamp
   in relativeText delta

relativeText :: NominalDiffTime -> Text
relativeText delta
  | delta < 60 = "just now"
  | delta < 3600 = tshow (round (delta / 60) :: Int) <> " minutes ago"
  | delta < 7200 = "1 hour ago"
  | delta < 86400 = tshow (round (delta / 3600) :: Int) <> " hours ago"
  | delta < 172800 = "yesterday"
  | delta < 604800 = tshow (round (delta / 86400) :: Int) <> " days ago"
  | delta < 1209600 = "1 week ago"
  | delta < 2592000 = tshow (round (delta / 604800) :: Int) <> " weeks ago"
  | delta < 5184000 = "1 month ago"
  | delta < 31536000 = tshow (round (delta / 2592000) :: Int) <> " months ago"
  | otherwise = tshow (round (delta / 31536000) :: Int) <> " years ago"

formatExactTimestamp :: UTCTime -> Text
formatExactTimestamp = Text.pack <. formatTime defaultTimeLocale "%Y-%m-%d %H:%M:%S UTC"

renderRelativeTimestamp :: (Monad m) => UTCTime -> UTCTime -> Lucid.HtmlT m ()
renderRelativeTimestamp now timestamp =
  Lucid.span_
    [ Lucid.class_ "relative-time",
      Lucid.title_ (formatExactTimestamp timestamp)
    ]
    (Lucid.toHtml (formatRelativeTime now timestamp))

-- * Small components

metaSep :: (Monad m) => Lucid.HtmlT m ()
metaSep = Lucid.span_ [Lucid.class_ "meta-sep"] "·"

-- * Page layout

pageHead :: (Monad m) => Text -> Lucid.HtmlT m ()
pageHead title =
  Lucid.head_ <| do
    Lucid.title_ (Lucid.toHtml title)
    Lucid.meta_ [Lucid.charset_ "utf-8"]
    Lucid.meta_
      [ Lucid.name_ "viewport",
        Lucid.content_ "width=device-width, initial-scale=1"
      ]
    Lucid.link_ [Lucid.rel_ "stylesheet", Lucid.href_ "/style.css"]
    Lucid.script_
      [ Lucid.src_ "https://unpkg.com/htmx.org@2.0.4",
        Lucid.integrity_ "sha384-HGfztofotfshcF7+8n44JQL2oJmowVChPTg48S+jvZoztPfvwD79OC/LTtG6dMp+",
        Lucid.crossorigin_ "anonymous"
      ]
      ("" :: Text)
    Lucid.script_ [] statusDropdownJs
    Lucid.script_ [] priorityDropdownJs
    Lucid.script_ [] complexityDropdownJs
    Lucid.script_ [] navbarDropdownJs
    Lucid.script_ [] liveToggleJs

-- * JavaScript

navbarDropdownJs :: Text
navbarDropdownJs =
  Text.unlines
    [ "document.addEventListener('DOMContentLoaded', function() {",
      "  document.querySelectorAll('.navbar-dropdown-btn').forEach(function(btn) {",
      "    btn.addEventListener('click', function(e) {",
      "      e.preventDefault();",
      "      var dropdown = btn.closest('.navbar-dropdown');",
      "      var isOpen = dropdown.classList.contains('open');",
      "      document.querySelectorAll('.navbar-dropdown.open').forEach(function(d) {",
      "        d.classList.remove('open');",
      "      });",
      "      if (!isOpen) {",
      "        dropdown.classList.add('open');",
      "      }",
      "    });",
      "  });",
      "  document.addEventListener('click', function(e) {",
      "    if (!e.target.closest('.navbar-dropdown')) {",
      "      document.querySelectorAll('.navbar-dropdown.open').forEach(function(d) {",
      "        d.classList.remove('open');",
      "      });",
      "    }",
      "  });",
      "});"
    ]

statusDropdownJs :: Text
statusDropdownJs =
  Text.unlines
    [ "function toggleStatusDropdown(el) {",
      "  var container = el.parentElement;",
      "  var isOpen = container.classList.toggle('open');",
      "  el.setAttribute('aria-expanded', isOpen);",
      "  if (isOpen) {",
      "    var firstItem = container.querySelector('[role=\"menuitem\"]');",
      "    if (firstItem) firstItem.focus();",
      "  }",
      "}",
      "",
      "function closeStatusDropdown(container) {",
      "  container.classList.remove('open');",
      "  var badge = container.querySelector('[role=\"button\"]');",
      "  if (badge) {",
      "    badge.setAttribute('aria-expanded', 'false');",
      "    badge.focus();",
      "  }",
      "}",
      "",
      "function handleStatusKeydown(event, el) {",
      "  if (event.key === 'Enter' || event.key === ' ') {",
      "    event.preventDefault();",
      "    toggleStatusDropdown(el);",
      "  } else if (event.key === 'Escape') {",
      "    closeStatusDropdown(el.parentElement);",
      "  } else if (event.key === 'ArrowDown') {",
      "    event.preventDefault();",
      "    var container = el.parentElement;",
      "    if (!container.classList.contains('open')) {",
      "      toggleStatusDropdown(el);",
      "    } else {",
      "      var firstItem = container.querySelector('[role=\"menuitem\"]');",
      "      if (firstItem) firstItem.focus();",
      "    }",
      "  }",
      "}",
      "",
      "function handleMenuItemKeydown(event) {",
      "  var container = event.target.closest('.status-badge-dropdown');",
      "  var items = container.querySelectorAll('[role=\"menuitem\"]');",
      "  var currentIndex = Array.from(items).indexOf(event.target);",
      "  ",
      "  if (event.key === 'ArrowDown') {",
      "    event.preventDefault();",
      "    var next = (currentIndex + 1) % items.length;",
      "    items[next].focus();",
      "  } else if (event.key === 'ArrowUp') {",
      "    event.preventDefault();",
      "    var prev = (currentIndex - 1 + items.length) % items.length;",
      "    items[prev].focus();",
      "  } else if (event.key === 'Escape') {",
      "    event.preventDefault();",
      "    closeStatusDropdown(container);",
      "  } else if (event.key === 'Tab') {",
      "    closeStatusDropdown(container);",
      "  }",
      "}",
      "",
      "document.addEventListener('click', function(e) {",
      "  var dropdowns = document.querySelectorAll('.status-badge-dropdown.open');",
      "  dropdowns.forEach(function(d) {",
      "    if (!d.contains(e.target)) {",
      "      closeStatusDropdown(d);",
      "    }",
      "  });",
      "});"
    ]

priorityDropdownJs :: Text
priorityDropdownJs =
  Text.unlines
    [ "function togglePriorityDropdown(el) {",
      "  var container = el.parentElement;",
      "  var isOpen = container.classList.toggle('open');",
      "  el.setAttribute('aria-expanded', isOpen);",
      "  if (isOpen) {",
      "    var firstItem = container.querySelector('[role=\"menuitem\"]');",
      "    if (firstItem) firstItem.focus();",
      "  }",
      "}",
      "",
      "function closePriorityDropdown(container) {",
      "  container.classList.remove('open');",
      "  var badge = container.querySelector('[role=\"button\"]');",
      "  if (badge) {",
      "    badge.setAttribute('aria-expanded', 'false');",
      "    badge.focus();",
      "  }",
      "}",
      "",
      "function handlePriorityKeydown(event, el) {",
      "  if (event.key === 'Enter' || event.key === ' ') {",
      "    event.preventDefault();",
      "    togglePriorityDropdown(el);",
      "  } else if (event.key === 'Escape') {",
      "    closePriorityDropdown(el.parentElement);",
      "  } else if (event.key === 'ArrowDown') {",
      "    event.preventDefault();",
      "    var container = el.parentElement;",
      "    if (!container.classList.contains('open')) {",
      "      togglePriorityDropdown(el);",
      "    } else {",
      "      var firstItem = container.querySelector('[role=\"menuitem\"]');",
      "      if (firstItem) firstItem.focus();",
      "    }",
      "  }",
      "}",
      "",
      "function handlePriorityMenuItemKeydown(event) {",
      "  var container = event.target.closest('.priority-badge-dropdown');",
      "  var items = container.querySelectorAll('[role=\"menuitem\"]');",
      "  var currentIndex = Array.from(items).indexOf(event.target);",
      "  ",
      "  if (event.key === 'ArrowDown') {",
      "    event.preventDefault();",
      "    var next = (currentIndex + 1) % items.length;",
      "    items[next].focus();",
      "  } else if (event.key === 'ArrowUp') {",
      "    event.preventDefault();",
      "    var prev = (currentIndex - 1 + items.length) % items.length;",
      "    items[prev].focus();",
      "  } else if (event.key === 'Escape') {",
      "    event.preventDefault();",
      "    closePriorityDropdown(container);",
      "  } else if (event.key === 'Tab') {",
      "    closePriorityDropdown(container);",
      "  }",
      "}",
      "",
      "document.addEventListener('click', function(e) {",
      "  var dropdowns = document.querySelectorAll('.priority-badge-dropdown.open');",
      "  dropdowns.forEach(function(d) {",
      "    if (!d.contains(e.target)) {",
      "      closePriorityDropdown(d);",
      "    }",
      "  });",
      "});"
    ]

complexityDropdownJs :: Text
complexityDropdownJs =
  Text.unlines
    [ "function toggleComplexityDropdown(el) {",
      "  var container = el.parentElement;",
      "  var isOpen = container.classList.toggle('open');",
      "  el.setAttribute('aria-expanded', isOpen);",
      "  if (isOpen) {",
      "    var firstItem = container.querySelector('[role=\"menuitem\"]');",
      "    if (firstItem) firstItem.focus();",
      "  }",
      "}",
      "",
      "function closeComplexityDropdown(container) {",
      "  container.classList.remove('open');",
      "  var badge = container.querySelector('[role=\"button\"]');",
      "  if (badge) {",
      "    badge.setAttribute('aria-expanded', 'false');",
      "    badge.focus();",
      "  }",
      "}",
      "",
      "function handleComplexityKeydown(event, el) {",
      "  if (event.key === 'Enter' || event.key === ' ') {",
      "    event.preventDefault();",
      "    toggleComplexityDropdown(el);",
      "  } else if (event.key === 'Escape') {",
      "    closeComplexityDropdown(el.parentElement);",
      "  } else if (event.key === 'ArrowDown') {",
      "    event.preventDefault();",
      "    var container = el.parentElement;",
      "    if (!container.classList.contains('open')) {",
      "      toggleComplexityDropdown(el);",
      "    } else {",
      "      var firstItem = container.querySelector('[role=\"menuitem\"]');",
      "      if (firstItem) firstItem.focus();",
      "    }",
      "  }",
      "}",
      "",
      "function handleComplexityMenuItemKeydown(event) {",
      "  var container = event.target.closest('.complexity-badge-dropdown');",
      "  var items = container.querySelectorAll('[role=\"menuitem\"]');",
      "  var currentIndex = Array.from(items).indexOf(event.target);",
      "  ",
      "  if (event.key === 'ArrowDown') {",
      "    event.preventDefault();",
      "    var next = (currentIndex + 1) % items.length;",
      "    items[next].focus();",
      "  } else if (event.key === 'ArrowUp') {",
      "    event.preventDefault();",
      "    var prev = (currentIndex - 1 + items.length) % items.length;",
      "    items[prev].focus();",
      "  } else if (event.key === 'Escape') {",
      "    event.preventDefault();",
      "    closeComplexityDropdown(container);",
      "  } else if (event.key === 'Tab') {",
      "    closeComplexityDropdown(container);",
      "  }",
      "}",
      "",
      "document.addEventListener('click', function(e) {",
      "  var dropdowns = document.querySelectorAll('.complexity-badge-dropdown.open');",
      "  dropdowns.forEach(function(d) {",
      "    if (!d.contains(e.target)) {",
      "      closeComplexityDropdown(d);",
      "    }",
      "  });",
      "});"
    ]

liveToggleJs :: Text
liveToggleJs =
  Text.unlines
    [ "var liveUpdatesEnabled = true;",
      "var autoscrollEnabled = true;",
      "",
      "function toggleLiveUpdates() {",
      "  liveUpdatesEnabled = !liveUpdatesEnabled;",
      "  var btn = document.getElementById('live-toggle');",
      "  if (btn) {",
      "    btn.classList.toggle('timeline-live-paused', !liveUpdatesEnabled);",
      "  }",
      "}",
      "",
      "function toggleAutoscroll() {",
      "  autoscrollEnabled = !autoscrollEnabled;",
      "  var btn = document.getElementById('autoscroll-toggle');",
      "  if (btn) {",
      "    btn.classList.toggle('timeline-autoscroll-disabled', !autoscrollEnabled);",
      "  }",
      "}",
      "",
      "document.body.addEventListener('htmx:beforeRequest', function(evt) {",
      "  var timeline = document.getElementById('unified-timeline');",
      "  if (timeline && timeline.contains(evt.target) && !liveUpdatesEnabled) {",
      "    evt.preventDefault();",
      "  }",
      "});",
      "",
      "document.body.addEventListener('htmx:afterSettle', function(evt) {",
      "  if (autoscrollEnabled) {",
      "    var log = document.querySelector('.timeline-events');",
      "    if (log) {",
      "      log.scrollTop = log.scrollHeight;",
      "    }",
      "  }",
      "});"
    ]

pageBody :: (Monad m) => Lucid.HtmlT m () -> Lucid.HtmlT m ()
pageBody content =
  Lucid.body_ [Lucid.makeAttribute "hx-boost" "true"] <| do
    navbar
    content

-- * Breadcrumbs

data Breadcrumb = Breadcrumb
  { _crumbLabel :: Text,
    _crumbHref :: Maybe Text
  }

type Breadcrumbs = [Breadcrumb]

pageBodyWithCrumbs :: (Monad m) => Breadcrumbs -> Lucid.HtmlT m () -> Lucid.HtmlT m ()
pageBodyWithCrumbs crumbs content =
  Lucid.body_ [Lucid.makeAttribute "hx-boost" "true"] <| do
    navbar
    unless (null crumbs) <| do
      Lucid.div_ [Lucid.class_ "breadcrumb-container"] <| do
        Lucid.div_ [Lucid.class_ "container"] <| renderBreadcrumbs crumbs
    content

renderBreadcrumbs :: (Monad m) => Breadcrumbs -> Lucid.HtmlT m ()
renderBreadcrumbs [] = pure ()
renderBreadcrumbs crumbs =
  Lucid.nav_ [Lucid.class_ "breadcrumbs", Lucid.makeAttribute "aria-label" "Breadcrumb"] <| do
    Lucid.ol_ [Lucid.class_ "breadcrumb-list"] <| do
      traverse_ renderCrumb (zip [0 ..] crumbs)
  where
    renderCrumb :: (Monad m') => (Int, Breadcrumb) -> Lucid.HtmlT m' ()
    renderCrumb (idx, Breadcrumb label mHref) = do
      Lucid.li_ [Lucid.class_ "breadcrumb-item"] <| do
        when (idx > 0) <| Lucid.span_ [Lucid.class_ "breadcrumb-sep"] ">"
        case mHref of
          Just href -> Lucid.a_ [Lucid.href_ href] (Lucid.toHtml label)
          Nothing -> Lucid.span_ [Lucid.class_ "breadcrumb-current"] (Lucid.toHtml label)

getAncestors :: [TaskCore.Task] -> TaskCore.Task -> [TaskCore.Task]
getAncestors allTasks task =
  case TaskCore.taskParent task of
    Nothing -> [task]
    Just pid -> case TaskCore.findTask pid allTasks of
      Nothing -> [task]
      Just parent -> getAncestors allTasks parent ++ [task]

taskBreadcrumbs :: [TaskCore.Task] -> TaskCore.Task -> Breadcrumbs
taskBreadcrumbs allTasks task =
  let ancestors = getAncestors allTasks task
      taskCrumbs = [Breadcrumb (TaskCore.taskId t) (Just ("/tasks/" <> TaskCore.taskId t)) | t <- List.init ancestors]
      currentCrumb = Breadcrumb (TaskCore.taskId task) Nothing
   in [Breadcrumb "Jr" (Just "/"), Breadcrumb "Tasks" (Just "/tasks")]
        ++ taskCrumbs
        ++ [currentCrumb]

-- * Navbar

navbar :: (Monad m) => Lucid.HtmlT m ()
navbar =
  Lucid.nav_ [Lucid.class_ "navbar"] <| do
    Lucid.a_ [Lucid.href_ "/", Lucid.class_ "navbar-brand"] "Junior"
    Lucid.input_
      [ Lucid.type_ "checkbox",
        Lucid.id_ "navbar-toggle",
        Lucid.class_ "navbar-toggle-checkbox"
      ]
    Lucid.label_
      [ Lucid.for_ "navbar-toggle",
        Lucid.class_ "navbar-hamburger"
      ]
      <| do
        Lucid.span_ [Lucid.class_ "hamburger-line"] ""
        Lucid.span_ [Lucid.class_ "hamburger-line"] ""
        Lucid.span_ [Lucid.class_ "hamburger-line"] ""
    Lucid.div_ [Lucid.class_ "navbar-links"] <| do
      Lucid.a_ [Lucid.href_ "/", Lucid.class_ "navbar-link"] "Dashboard"
      Lucid.div_ [Lucid.class_ "navbar-dropdown"] <| do
        Lucid.button_ [Lucid.class_ "navbar-dropdown-btn"] "Tasks ▾"
        Lucid.div_ [Lucid.class_ "navbar-dropdown-content"] <| do
          Lucid.a_ [Lucid.href_ "/ready", Lucid.class_ "navbar-dropdown-item"] "Ready"
          Lucid.a_ [Lucid.href_ "/blocked", Lucid.class_ "navbar-dropdown-item"] "Blocked"
          Lucid.a_ [Lucid.href_ "/intervention", Lucid.class_ "navbar-dropdown-item"] "Human Action"
          Lucid.a_ [Lucid.href_ "/tasks", Lucid.class_ "navbar-dropdown-item"] "All"
      Lucid.div_ [Lucid.class_ "navbar-dropdown"] <| do
        Lucid.button_ [Lucid.class_ "navbar-dropdown-btn"] "Plans ▾"
        Lucid.div_ [Lucid.class_ "navbar-dropdown-content"] <| do
          Lucid.a_ [Lucid.href_ "/epics", Lucid.class_ "navbar-dropdown-item"] "Epics"
          Lucid.a_ [Lucid.href_ "/kb", Lucid.class_ "navbar-dropdown-item"] "KB"
      Lucid.a_ [Lucid.href_ "/stats", Lucid.class_ "navbar-link"] "Stats"

-- * Badges

statusBadge :: (Monad m) => TaskCore.Status -> Lucid.HtmlT m ()
statusBadge status =
  let (cls, label) = case status of
        TaskCore.Draft -> ("badge badge-draft", "Draft")
        TaskCore.Open -> ("badge badge-open", "Open")
        TaskCore.InProgress -> ("badge badge-inprogress", "In Progress")
        TaskCore.Review -> ("badge badge-review", "Review")
        TaskCore.Approved -> ("badge badge-approved", "Approved")
        TaskCore.Done -> ("badge badge-done", "Done")
        TaskCore.NeedsHelp -> ("badge badge-needshelp", "Needs Help")
   in Lucid.span_ [Lucid.class_ cls] label

complexityBadge :: (Monad m) => Int -> Lucid.HtmlT m ()
complexityBadge complexity =
  let cls = "badge badge-complexity badge-complexity-" <> tshow complexity
      label = "ℂ " <> tshow complexity
   in Lucid.span_ [Lucid.class_ cls, Lucid.title_ "Task Complexity (1-5)"] (Lucid.toHtml label)

-- * Sort dropdown

sortDropdown :: (Monad m) => Text -> SortOrder -> Lucid.HtmlT m ()
sortDropdown basePath currentSort =
  Lucid.div_ [Lucid.class_ "sort-dropdown"] <| do
    Lucid.span_ [Lucid.class_ "sort-label"] "Sort:"
    Lucid.div_ [Lucid.class_ "sort-dropdown-wrapper navbar-dropdown"] <| do
      Lucid.button_ [Lucid.class_ "sort-dropdown-btn navbar-dropdown-btn"]
        <| Lucid.toHtml (sortOrderLabel currentSort <> " ▾")
      Lucid.div_ [Lucid.class_ "sort-dropdown-content navbar-dropdown-content"] <| do
        sortOption basePath SortNewest currentSort
        sortOption basePath SortOldest currentSort
        sortOption basePath SortUpdated currentSort
        sortOption basePath SortPriorityHigh currentSort
        sortOption basePath SortPriorityLow currentSort

sortOption :: (Monad m) => Text -> SortOrder -> SortOrder -> Lucid.HtmlT m ()
sortOption basePath option currentSort =
  let cls = "sort-dropdown-item navbar-dropdown-item" <> if option == currentSort then " active" else ""
      href = basePath <> "?sort=" <> sortOrderToParam option
   in Lucid.a_ [Lucid.href_ href, Lucid.class_ cls] (Lucid.toHtml (sortOrderLabel option))

-- * Progress bars

multiColorProgressBar :: (Monad m) => TaskCore.TaskStats -> Lucid.HtmlT m ()
multiColorProgressBar stats =
  let total = TaskCore.totalTasks stats
      doneCount = TaskCore.doneTasks stats
      inProgressCount = TaskCore.inProgressTasks stats
      openCount = TaskCore.openTasks stats + TaskCore.reviewTasks stats + TaskCore.approvedTasks stats
      donePct = if total == 0 then 0 else (doneCount * 100) `div` total
      inProgressPct = if total == 0 then 0 else (inProgressCount * 100) `div` total
      openPct = if total == 0 then 0 else (openCount * 100) `div` total
   in Lucid.div_ [Lucid.class_ "multi-progress-container"] <| do
        Lucid.div_ [Lucid.class_ "multi-progress-bar"] <| do
          when (donePct > 0)
            <| Lucid.div_
              [ Lucid.class_ "multi-progress-segment progress-done",
                Lucid.style_ ("width: " <> tshow donePct <> "%"),
                Lucid.title_ (tshow doneCount <> " done")
              ]
              ""
          when (inProgressPct > 0)
            <| Lucid.div_
              [ Lucid.class_ "multi-progress-segment progress-inprogress",
                Lucid.style_ ("width: " <> tshow inProgressPct <> "%"),
                Lucid.title_ (tshow inProgressCount <> " in progress")
              ]
              ""
          when (openPct > 0)
            <| Lucid.div_
              [ Lucid.class_ "multi-progress-segment progress-open",
                Lucid.style_ ("width: " <> tshow openPct <> "%"),
                Lucid.title_ (tshow openCount <> " open")
              ]
              ""
        Lucid.div_ [Lucid.class_ "progress-legend"] <| do
          Lucid.span_ [Lucid.class_ "legend-item"] <| do
            Lucid.span_ [Lucid.class_ "legend-dot legend-done"] ""
            Lucid.toHtml ("Done " <> tshow doneCount)
          Lucid.span_ [Lucid.class_ "legend-item"] <| do
            Lucid.span_ [Lucid.class_ "legend-dot legend-inprogress"] ""
            Lucid.toHtml ("In Progress " <> tshow inProgressCount)
          Lucid.span_ [Lucid.class_ "legend-item"] <| do
            Lucid.span_ [Lucid.class_ "legend-dot legend-open"] ""
            Lucid.toHtml ("Open " <> tshow openCount)

epicProgressBar :: (Monad m) => Int -> Int -> Int -> Int -> Lucid.HtmlT m ()
epicProgressBar doneCount inProgressCount openCount totalCount =
  let donePct = if totalCount == 0 then 0 else (doneCount * 100) `div` totalCount
      inProgressPct = if totalCount == 0 then 0 else (inProgressCount * 100) `div` totalCount
      openPct = if totalCount == 0 then 0 else (openCount * 100) `div` totalCount
   in Lucid.div_ [Lucid.class_ "multi-progress-container epic-progress"] <| do
        Lucid.div_ [Lucid.class_ "multi-progress-bar"] <| do
          when (donePct > 0)
            <| Lucid.div_
              [ Lucid.class_ "multi-progress-segment progress-done",
                Lucid.style_ ("width: " <> tshow donePct <> "%"),
                Lucid.title_ (tshow doneCount <> " done")
              ]
              ""
          when (inProgressPct > 0)
            <| Lucid.div_
              [ Lucid.class_ "multi-progress-segment progress-inprogress",
                Lucid.style_ ("width: " <> tshow inProgressPct <> "%"),
                Lucid.title_ (tshow inProgressCount <> " in progress")
              ]
              ""
          when (openPct > 0)
            <| Lucid.div_
              [ Lucid.class_ "multi-progress-segment progress-open",
                Lucid.style_ ("width: " <> tshow openPct <> "%"),
                Lucid.title_ (tshow openCount <> " open")
              ]
              ""
        Lucid.div_ [Lucid.class_ "progress-legend"] <| do
          Lucid.span_ [Lucid.class_ "legend-item"] <| do
            Lucid.span_ [Lucid.class_ "legend-dot legend-done"] ""
            Lucid.toHtml (tshow doneCount)
          Lucid.span_ [Lucid.class_ "legend-item"] <| do
            Lucid.span_ [Lucid.class_ "legend-dot legend-inprogress"] ""
            Lucid.toHtml (tshow inProgressCount)
          Lucid.span_ [Lucid.class_ "legend-item"] <| do
            Lucid.span_ [Lucid.class_ "legend-dot legend-open"] ""
            Lucid.toHtml (tshow openCount)

-- * Status badge with form

statusBadgeWithForm :: (Monad m) => TaskCore.Status -> Text -> Lucid.HtmlT m ()
statusBadgeWithForm status tid =
  Lucid.div_
    [ Lucid.id_ "status-badge-container",
      Lucid.class_ "status-badge-dropdown"
    ]
    <| do
      clickableBadge status tid
      statusDropdownOptions status tid

clickableBadge :: (Monad m) => TaskCore.Status -> Text -> Lucid.HtmlT m ()
clickableBadge status _tid =
  let (cls, label) = case status of
        TaskCore.Draft -> ("badge badge-draft status-badge-clickable", "Draft" :: Text)
        TaskCore.Open -> ("badge badge-open status-badge-clickable", "Open")
        TaskCore.InProgress -> ("badge badge-inprogress status-badge-clickable", "In Progress")
        TaskCore.Review -> ("badge badge-review status-badge-clickable", "Review")
        TaskCore.Approved -> ("badge badge-approved status-badge-clickable", "Approved")
        TaskCore.Done -> ("badge badge-done status-badge-clickable", "Done")
        TaskCore.NeedsHelp -> ("badge badge-needshelp status-badge-clickable", "Needs Help")
   in Lucid.span_
        [ Lucid.class_ cls,
          Lucid.tabindex_ "0",
          Lucid.role_ "button",
          Lucid.makeAttribute "aria-haspopup" "true",
          Lucid.makeAttribute "aria-expanded" "false",
          Lucid.makeAttribute "onclick" "toggleStatusDropdown(this)",
          Lucid.makeAttribute "onkeydown" "handleStatusKeydown(event, this)"
        ]
        <| do
          Lucid.toHtml label
          Lucid.span_ [Lucid.class_ "dropdown-arrow", Lucid.makeAttribute "aria-hidden" "true"] " ▾"

statusDropdownOptions :: (Monad m) => TaskCore.Status -> Text -> Lucid.HtmlT m ()
statusDropdownOptions currentStatus tid =
  Lucid.div_
    [ Lucid.class_ "status-dropdown-menu",
      Lucid.role_ "menu",
      Lucid.makeAttribute "aria-label" "Change task status"
    ]
    <| do
      statusOption TaskCore.Draft currentStatus tid
      statusOption TaskCore.Open currentStatus tid
      statusOption TaskCore.InProgress currentStatus tid
      statusOption TaskCore.Review currentStatus tid
      statusOption TaskCore.Approved currentStatus tid
      statusOption TaskCore.Done currentStatus tid
      statusOption TaskCore.NeedsHelp currentStatus tid

statusOption :: (Monad m) => TaskCore.Status -> TaskCore.Status -> Text -> Lucid.HtmlT m ()
statusOption opt currentStatus tid =
  let (cls, label) = case opt of
        TaskCore.Draft -> ("badge badge-draft", "Draft" :: Text)
        TaskCore.Open -> ("badge badge-open", "Open")
        TaskCore.InProgress -> ("badge badge-inprogress", "In Progress")
        TaskCore.Review -> ("badge badge-review", "Review")
        TaskCore.Approved -> ("badge badge-approved", "Approved")
        TaskCore.Done -> ("badge badge-done", "Done")
        TaskCore.NeedsHelp -> ("badge badge-needshelp", "Needs Help")
      isSelected = opt == currentStatus
      optClass = cls <> " status-dropdown-option" <> if isSelected then " selected" else ""
   in Lucid.form_
        [ Lucid.class_ "status-option-form",
          Lucid.role_ "none",
          Lucid.makeAttribute "hx-post" ("/tasks/" <> tid <> "/status"),
          Lucid.makeAttribute "hx-target" "#status-badge-container",
          Lucid.makeAttribute "hx-swap" "outerHTML"
        ]
        <| do
          Lucid.input_ [Lucid.type_ "hidden", Lucid.name_ "status", Lucid.value_ (tshow opt)]
          Lucid.button_
            [ Lucid.type_ "submit",
              Lucid.class_ optClass,
              Lucid.role_ "menuitem",
              Lucid.tabindex_ "-1",
              Lucid.makeAttribute "onkeydown" "handleMenuItemKeydown(event)"
            ]
            (Lucid.toHtml label)

-- * Priority badge with form

priorityBadgeWithForm :: (Monad m) => TaskCore.Priority -> Text -> Lucid.HtmlT m ()
priorityBadgeWithForm priority tid =
  Lucid.div_
    [ Lucid.id_ "priority-badge-container",
      Lucid.class_ "priority-badge-dropdown"
    ]
    <| do
      clickablePriorityBadge priority tid
      priorityDropdownOptions priority tid

clickablePriorityBadge :: (Monad m) => TaskCore.Priority -> Text -> Lucid.HtmlT m ()
clickablePriorityBadge priority _tid =
  let (cls, label) = case priority of
        TaskCore.P0 -> ("badge badge-p0 priority-badge-clickable", "P0 Critical" :: Text)
        TaskCore.P1 -> ("badge badge-p1 priority-badge-clickable", "P1 High")
        TaskCore.P2 -> ("badge badge-p2 priority-badge-clickable", "P2 Normal")
        TaskCore.P3 -> ("badge badge-p3 priority-badge-clickable", "P3 Low")
        TaskCore.P4 -> ("badge badge-p4 priority-badge-clickable", "P4 Defer")
   in Lucid.span_
        [ Lucid.class_ cls,
          Lucid.tabindex_ "0",
          Lucid.role_ "button",
          Lucid.makeAttribute "aria-haspopup" "true",
          Lucid.makeAttribute "aria-expanded" "false",
          Lucid.makeAttribute "onclick" "togglePriorityDropdown(this)",
          Lucid.makeAttribute "onkeydown" "handlePriorityKeydown(event, this)"
        ]
        <| do
          Lucid.toHtml label
          Lucid.span_ [Lucid.class_ "dropdown-arrow", Lucid.makeAttribute "aria-hidden" "true"] " ▾"

priorityDropdownOptions :: (Monad m) => TaskCore.Priority -> Text -> Lucid.HtmlT m ()
priorityDropdownOptions currentPriority tid =
  Lucid.div_
    [ Lucid.class_ "priority-dropdown-menu",
      Lucid.role_ "menu",
      Lucid.makeAttribute "aria-label" "Change task priority"
    ]
    <| do
      priorityOption TaskCore.P0 currentPriority tid
      priorityOption TaskCore.P1 currentPriority tid
      priorityOption TaskCore.P2 currentPriority tid
      priorityOption TaskCore.P3 currentPriority tid
      priorityOption TaskCore.P4 currentPriority tid

priorityOption :: (Monad m) => TaskCore.Priority -> TaskCore.Priority -> Text -> Lucid.HtmlT m ()
priorityOption opt currentPriority tid =
  let (cls, label) = case opt of
        TaskCore.P0 -> ("badge badge-p0", "P0 Critical" :: Text)
        TaskCore.P1 -> ("badge badge-p1", "P1 High")
        TaskCore.P2 -> ("badge badge-p2", "P2 Normal")
        TaskCore.P3 -> ("badge badge-p3", "P3 Low")
        TaskCore.P4 -> ("badge badge-p4", "P4 Defer")
      isSelected = opt == currentPriority
      optClass = cls <> " priority-dropdown-option" <> if isSelected then " selected" else ""
   in Lucid.form_
        [ Lucid.class_ "priority-option-form",
          Lucid.role_ "none",
          Lucid.makeAttribute "hx-post" ("/tasks/" <> tid <> "/priority"),
          Lucid.makeAttribute "hx-target" "#priority-badge-container",
          Lucid.makeAttribute "hx-swap" "outerHTML"
        ]
        <| do
          Lucid.input_ [Lucid.type_ "hidden", Lucid.name_ "priority", Lucid.value_ (tshow opt)]
          Lucid.button_
            [ Lucid.type_ "submit",
              Lucid.class_ optClass,
              Lucid.role_ "menuitem",
              Lucid.tabindex_ "-1",
              Lucid.makeAttribute "onkeydown" "handlePriorityMenuItemKeydown(event)"
            ]
            (Lucid.toHtml label)

-- * Complexity badge with form

complexityBadgeWithForm :: (Monad m) => Maybe Int -> Text -> Lucid.HtmlT m ()
complexityBadgeWithForm complexity tid =
  Lucid.div_
    [ Lucid.id_ "complexity-badge-container",
      Lucid.class_ "complexity-badge-dropdown"
    ]
    <| do
      clickableComplexityBadge complexity tid
      complexityDropdownOptions complexity tid

clickableComplexityBadge :: (Monad m) => Maybe Int -> Text -> Lucid.HtmlT m ()
clickableComplexityBadge complexity _tid =
  let (cls, label) = case complexity of
        Nothing -> ("badge badge-complexity-none complexity-badge-clickable", "Set Complexity" :: Text)
        Just 1 -> ("badge badge-complexity-1 complexity-badge-clickable", "ℂ 1")
        Just 2 -> ("badge badge-complexity-2 complexity-badge-clickable", "ℂ 2")
        Just 3 -> ("badge badge-complexity-3 complexity-badge-clickable", "ℂ 3")
        Just 4 -> ("badge badge-complexity-4 complexity-badge-clickable", "ℂ 4")
        Just 5 -> ("badge badge-complexity-5 complexity-badge-clickable", "ℂ 5")
        Just _ -> ("badge badge-complexity-none complexity-badge-clickable", "Invalid")
   in Lucid.span_
        [ Lucid.class_ cls,
          Lucid.tabindex_ "0",
          Lucid.role_ "button",
          Lucid.makeAttribute "aria-haspopup" "true",
          Lucid.makeAttribute "aria-expanded" "false",
          Lucid.makeAttribute "onclick" "toggleComplexityDropdown(this)",
          Lucid.makeAttribute "onkeydown" "handleComplexityKeydown(event, this)"
        ]
        <| do
          Lucid.toHtml label
          Lucid.span_ [Lucid.class_ "dropdown-arrow", Lucid.makeAttribute "aria-hidden" "true"] " ▾"

complexityDropdownOptions :: (Monad m) => Maybe Int -> Text -> Lucid.HtmlT m ()
complexityDropdownOptions currentComplexity tid =
  Lucid.div_
    [ Lucid.class_ "complexity-dropdown-menu",
      Lucid.role_ "menu",
      Lucid.makeAttribute "aria-label" "Change task complexity"
    ]
    <| do
      complexityOption Nothing currentComplexity tid
      complexityOption (Just 1) currentComplexity tid
      complexityOption (Just 2) currentComplexity tid
      complexityOption (Just 3) currentComplexity tid
      complexityOption (Just 4) currentComplexity tid
      complexityOption (Just 5) currentComplexity tid

complexityOption :: (Monad m) => Maybe Int -> Maybe Int -> Text -> Lucid.HtmlT m ()
complexityOption opt currentComplexity tid =
  let (cls, label, val) = case opt of
        Nothing -> ("badge badge-complexity-none", "None" :: Text, "none" :: Text)
        Just 1 -> ("badge badge-complexity-1", "ℂ 1", "1")
        Just 2 -> ("badge badge-complexity-2", "ℂ 2", "2")
        Just 3 -> ("badge badge-complexity-3", "ℂ 3", "3")
        Just 4 -> ("badge badge-complexity-4", "ℂ 4", "4")
        Just 5 -> ("badge badge-complexity-5", "ℂ 5", "5")
        Just _ -> ("badge badge-complexity-none", "Invalid", "none")
      isSelected = opt == currentComplexity
      optClass = cls <> " complexity-dropdown-option" <> if isSelected then " selected" else ""
   in Lucid.form_
        [ Lucid.class_ "complexity-option-form",
          Lucid.role_ "none",
          Lucid.makeAttribute "hx-post" ("/tasks/" <> tid <> "/complexity"),
          Lucid.makeAttribute "hx-target" "#complexity-badge-container",
          Lucid.makeAttribute "hx-swap" "outerHTML"
        ]
        <| do
          Lucid.input_ [Lucid.type_ "hidden", Lucid.name_ "complexity", Lucid.value_ val]
          Lucid.button_
            [ Lucid.type_ "submit",
              Lucid.class_ optClass,
              Lucid.role_ "menuitem",
              Lucid.tabindex_ "-1",
              Lucid.makeAttribute "onkeydown" "handleComplexityMenuItemKeydown(event)"
            ]
            (Lucid.toHtml label)

-- * Task rendering

renderTaskCard :: (Monad m) => TaskCore.Task -> Lucid.HtmlT m ()
renderTaskCard t =
  Lucid.a_
    [ Lucid.class_ "task-card task-card-link",
      Lucid.href_ ("/tasks/" <> TaskCore.taskId t)
    ]
    <| do
      Lucid.div_ [Lucid.class_ "task-header"] <| do
        Lucid.span_ [Lucid.class_ "task-id"] (Lucid.toHtml (TaskCore.taskId t))
        statusBadge (TaskCore.taskStatus t)
        Lucid.span_ [Lucid.class_ "priority"] (Lucid.toHtml (tshow (TaskCore.taskPriority t)))
      Lucid.p_ [Lucid.class_ "task-title"] (Lucid.toHtml (TaskCore.taskTitle t))

renderBlockedTaskCard :: (Monad m) => (TaskCore.Task, Int) -> Lucid.HtmlT m ()
renderBlockedTaskCard (t, impact) =
  Lucid.a_
    [ Lucid.class_ "task-card task-card-link",
      Lucid.href_ ("/tasks/" <> TaskCore.taskId t)
    ]
    <| do
      Lucid.div_ [Lucid.class_ "task-header"] <| do
        Lucid.span_ [Lucid.class_ "task-id"] (Lucid.toHtml (TaskCore.taskId t))
        statusBadge (TaskCore.taskStatus t)
        Lucid.span_ [Lucid.class_ "priority"] (Lucid.toHtml (tshow (TaskCore.taskPriority t)))
        when (impact > 0)
          <| Lucid.span_ [Lucid.class_ "blocking-impact"] (Lucid.toHtml ("Blocks " <> tshow impact))
      Lucid.p_ [Lucid.class_ "task-title"] (Lucid.toHtml (TaskCore.taskTitle t))

renderListGroupItem :: (Monad m) => TaskCore.Task -> Lucid.HtmlT m ()
renderListGroupItem t =
  Lucid.a_
    [ Lucid.class_ "list-group-item",
      Lucid.href_ ("/tasks/" <> TaskCore.taskId t),
      Lucid.makeAttribute "hx-boost" "true",
      Lucid.makeAttribute "hx-target" "body",
      Lucid.makeAttribute "hx-swap" "innerHTML"
    ]
    <| do
      Lucid.div_ [Lucid.class_ "list-group-item-content"] <| do
        Lucid.span_ [Lucid.class_ "list-group-item-id"] (Lucid.toHtml (TaskCore.taskId t))
        Lucid.span_ [Lucid.class_ "list-group-item-title"] (Lucid.toHtml (TaskCore.taskTitle t))
      Lucid.div_ [Lucid.class_ "list-group-item-meta"] <| do
        statusBadge (TaskCore.taskStatus t)
        Lucid.span_ [Lucid.class_ "priority"] (Lucid.toHtml (tshow (TaskCore.taskPriority t)))

renderEpicReviewCard :: (Monad m) => TaskCore.EpicForReview -> Lucid.HtmlT m ()
renderEpicReviewCard epicReview = do
  let task = TaskCore.epicTask epicReview
      total = TaskCore.epicTotal epicReview
      completed = TaskCore.epicCompleted epicReview
      progressText = tshow completed <> "/" <> tshow total <> " subtasks done"
  Lucid.div_ [Lucid.class_ "task-card"] <| do
    Lucid.div_ [Lucid.class_ "task-card-header"] <| do
      Lucid.div_ [Lucid.class_ "task-title-row"] <| do
        Lucid.a_
          [Lucid.href_ ("/tasks/" <> TaskCore.taskId task), Lucid.class_ "task-link"]
          <| Lucid.toHtml (TaskCore.taskTitle task)
        Lucid.span_ [Lucid.class_ "badge badge-epic"] "Epic"
      Lucid.span_ [Lucid.class_ "task-id"] <| Lucid.toHtml (TaskCore.taskId task)
    Lucid.div_ [Lucid.class_ "task-card-body"] <| do
      Lucid.div_ [Lucid.class_ "progress-info"] <| do
        Lucid.span_ [Lucid.class_ "badge badge-success"] <| Lucid.toHtml progressText
      Lucid.div_ [Lucid.class_ "epic-actions"] <| do
        Lucid.form_
          [ Lucid.method_ "POST",
            Lucid.action_ ("/tasks/" <> TaskCore.taskId task <> "/status"),
            Lucid.class_ "inline-form"
          ]
          <| do
            Lucid.input_ [Lucid.type_ "hidden", Lucid.name_ "status", Lucid.value_ "done"]
            Lucid.button_ [Lucid.type_ "submit", Lucid.class_ "btn btn-success btn-sm"] "Approve & Close"

renderEpicCardWithStats :: (Monad m) => [TaskCore.Task] -> TaskCore.Task -> Lucid.HtmlT m ()
renderEpicCardWithStats allTasks t =
  let children = getDescendants allTasks (TaskCore.taskId t)
      openCount = length [c | c <- children, TaskCore.taskStatus c == TaskCore.Open]
      inProgressCount = length [c | c <- children, TaskCore.taskStatus c == TaskCore.InProgress]
      reviewCount = length [c | c <- children, TaskCore.taskStatus c == TaskCore.Review]
      doneCount = length [c | c <- children, TaskCore.taskStatus c == TaskCore.Done]
      totalCount = length children
      openAndReview = openCount + reviewCount
   in Lucid.a_
        [ Lucid.class_ "task-card task-card-link",
          Lucid.href_ ("/tasks/" <> TaskCore.taskId t)
        ]
        <| do
          Lucid.div_ [Lucid.class_ "task-header"] <| do
            Lucid.span_ [Lucid.class_ "task-id"] (Lucid.toHtml (TaskCore.taskId t))
            statusBadge (TaskCore.taskStatus t)
            Lucid.span_ [Lucid.class_ "priority"] (Lucid.toHtml (tshow (TaskCore.taskPriority t)))
          Lucid.p_ [Lucid.class_ "task-title"] (Lucid.toHtml (TaskCore.taskTitle t))
          when (totalCount > 0) <| epicProgressBar doneCount inProgressCount openAndReview totalCount
          unless (Text.null (TaskCore.taskDescription t))
            <| Lucid.p_ [Lucid.class_ "kb-preview"] (Lucid.toHtml (Text.take 200 (TaskCore.taskDescription t) <> "..."))

getDescendants :: [TaskCore.Task] -> Text -> [TaskCore.Task]
getDescendants allTasks parentId =
  let children = [c | c <- allTasks, maybe False (TaskCore.matchesId parentId) (TaskCore.taskParent c)]
   in children ++ concatMap (getDescendants allTasks <. TaskCore.taskId) children

-- * Aggregated metrics

renderAggregatedMetrics :: (Monad m) => [TaskCore.Task] -> TaskCore.Task -> TaskCore.AggregatedMetrics -> Lucid.HtmlT m ()
renderAggregatedMetrics allTasks task metrics =
  let descendants = getDescendants allTasks (TaskCore.taskId task)
      totalCount = length descendants
      costCents = TaskCore.aggTotalCostCents metrics
      durationSecs = TaskCore.aggTotalDurationSeconds metrics
      completedCount = TaskCore.aggCompletedTasks metrics
      tokensUsed = TaskCore.aggTotalTokens metrics
   in Lucid.div_ [Lucid.class_ "detail-section aggregated-metrics"] <| do
        Lucid.h3_ "Execution Summary"
        Lucid.div_ [Lucid.class_ "metrics-grid"] <| do
          Lucid.div_ [Lucid.class_ "metric-card"] <| do
            Lucid.div_ [Lucid.class_ "metric-value"] (Lucid.toHtml (tshow completedCount <> "/" <> tshow totalCount))
            Lucid.div_ [Lucid.class_ "metric-label"] "Tasks Completed"
          Lucid.div_ [Lucid.class_ "metric-card"] <| do
            Lucid.div_ [Lucid.class_ "metric-value"] (Lucid.toHtml (formatCostMetric costCents))
            Lucid.div_ [Lucid.class_ "metric-label"] "Total Cost"
          Lucid.div_ [Lucid.class_ "metric-card"] <| do
            Lucid.div_ [Lucid.class_ "metric-value"] (Lucid.toHtml (formatDurationMetric durationSecs))
            Lucid.div_ [Lucid.class_ "metric-label"] "Total Time"
          when (tokensUsed > 0) <| do
            Lucid.div_ [Lucid.class_ "metric-card"] <| do
              Lucid.div_ [Lucid.class_ "metric-value"] (Lucid.toHtml (formatTokensMetric tokensUsed))
              Lucid.div_ [Lucid.class_ "metric-label"] "Tokens Used"
  where
    formatCostMetric :: Int -> Text
    formatCostMetric cents =
      let dollars = fromIntegral cents / 100.0 :: Double
       in "$" <> Text.pack (showFFloat (Just 2) dollars "")

    formatDurationMetric :: Int -> Text
    formatDurationMetric secs
      | secs < 60 = tshow secs <> "s"
      | secs < 3600 =
          let mins = secs `div` 60
              remSecs = secs `mod` 60
           in tshow mins <> "m " <> tshow remSecs <> "s"
      | otherwise =
          let hrs = secs `div` 3600
              mins = (secs `mod` 3600) `div` 60
           in tshow hrs <> "h " <> tshow mins <> "m"

    formatTokensMetric :: Int -> Text
    formatTokensMetric t
      | t < 1000 = tshow t
      | t < 1000000 = Text.pack (showFFloat (Just 1) (fromIntegral t / 1000.0 :: Double) "") <> "K"
      | otherwise = Text.pack (showFFloat (Just 2) (fromIntegral t / 1000000.0 :: Double) "") <> "M"

-- * Retry context banner

renderRetryContextBanner :: (Monad m) => Text -> Maybe TaskCore.RetryContext -> Lucid.HtmlT m ()
renderRetryContextBanner _ Nothing = pure ()
renderRetryContextBanner tid (Just ctx) =
  Lucid.div_ [Lucid.class_ bannerClass] <| do
    Lucid.div_ [Lucid.class_ "retry-banner-header"] <| do
      Lucid.span_ [Lucid.class_ "retry-icon"] retryIcon
      Lucid.span_ [Lucid.class_ "retry-attempt"] (Lucid.toHtml attemptText)
      when maxRetriesExceeded
        <| Lucid.span_ [Lucid.class_ "retry-warning-badge"] "Needs Human Intervention"

    Lucid.div_ [Lucid.class_ "retry-banner-details"] <| do
      Lucid.div_ [Lucid.class_ "retry-detail-row"] <| do
        Lucid.span_ [Lucid.class_ "retry-label"] "Failure Reason:"
        Lucid.span_ [Lucid.class_ "retry-value"] (Lucid.toHtml (summarizeReason (TaskCore.retryReason ctx)))

      let commit = TaskCore.retryOriginalCommit ctx
      unless (Text.null commit) <| do
        Lucid.div_ [Lucid.class_ "retry-detail-row"] <| do
          Lucid.span_ [Lucid.class_ "retry-label"] "Original Commit:"
          Lucid.code_ [Lucid.class_ "retry-commit"] (Lucid.toHtml (Text.take 8 commit))

      let conflicts = TaskCore.retryConflictFiles ctx
      unless (null conflicts) <| do
        Lucid.div_ [Lucid.class_ "retry-detail-row"] <| do
          Lucid.span_ [Lucid.class_ "retry-label"] "Conflict Files:"
          Lucid.ul_ [Lucid.class_ "retry-conflict-list"]
            <| traverse_ (Lucid.li_ <. Lucid.toHtml) conflicts

    when maxRetriesExceeded <| do
      Lucid.div_
        [Lucid.class_ "retry-warning-message"]
        "This task has exceeded the maximum number of retries. A human must review the failure and either fix the issue manually or reset the retry count."

      Lucid.p_ [Lucid.class_ "retry-hint"] "Use comments below to provide guidance for retry."

      Lucid.div_ [Lucid.class_ "retry-reset-section"] <| do
        Lucid.h4_ "Reset Retries"
        Lucid.p_ [Lucid.class_ "notes-help"] "Clear retry context and give task a fresh start:"
        Lucid.form_ [Lucid.method_ "POST", Lucid.action_ ("/tasks/" <> tid <> "/reset-retries")] <| do
          Lucid.button_ [Lucid.type_ "submit", Lucid.class_ "reset-btn"] "Reset Retries"
  where
    attempt = TaskCore.retryAttempt ctx
    maxRetriesExceeded = attempt >= 3
    bannerClass = if maxRetriesExceeded then "retry-banner retry-banner-critical" else "retry-banner retry-banner-warning"
    retryIcon = if maxRetriesExceeded then "⚠" else "↻"
    attemptText = "Attempt " <> tshow attempt <> " of 3"

    summarizeReason :: Text -> Text
    summarizeReason reason
      | "rejected:" `Text.isPrefixOf` reason = "Rejected: " <> Text.strip (Text.drop 9 reason)
      | "Test failure:" `Text.isPrefixOf` reason = "Test failure (see details below)"
      | "MERGE CONFLICT" `Text.isPrefixOf` reason = "Merge conflict with concurrent changes"
      | otherwise = Text.take 100 reason <> if Text.length reason > 100 then "..." else ""

-- * Markdown rendering

renderMarkdown :: (Monad m) => Text -> Lucid.HtmlT m ()
renderMarkdown input = renderBlocks (parseBlocks (Text.lines input))

data MarkdownBlock
  = MdHeader Int Text
  | MdParagraph [Text]
  | MdCodeBlock [Text]
  | MdList [Text]
  deriving (Show, Eq)

parseBlocks :: [Text] -> [MarkdownBlock]
parseBlocks [] = []
parseBlocks lns = case lns of
  (l : rest)
    | "```" `Text.isPrefixOf` l ->
        let (codeLines, afterCode) = List.span (not <. Text.isPrefixOf "```") rest
            remaining = List.drop 1 afterCode
         in MdCodeBlock codeLines : parseBlocks remaining
    | "### " `Text.isPrefixOf` l ->
        MdHeader 3 (Text.drop 4 l) : parseBlocks rest
    | "## " `Text.isPrefixOf` l ->
        MdHeader 2 (Text.drop 3 l) : parseBlocks rest
    | "# " `Text.isPrefixOf` l ->
        MdHeader 1 (Text.drop 2 l) : parseBlocks rest
    | isListItem l ->
        let (listLines, afterList) = List.span isListItem lns
         in MdList (map stripListPrefix listLines) : parseBlocks afterList
    | Text.null (Text.strip l) ->
        parseBlocks rest
    | otherwise ->
        let (paraLines, afterPara) = List.span isParagraphLine lns
         in MdParagraph paraLines : parseBlocks afterPara
  where
    isListItem t =
      let stripped = Text.stripStart t
       in "- " `Text.isPrefixOf` stripped || "* " `Text.isPrefixOf` stripped
    stripListPrefix t =
      let stripped = Text.stripStart t
       in Text.drop 2 stripped
    isParagraphLine t =
      not (Text.null (Text.strip t))
        && not ("```" `Text.isPrefixOf` t)
        && not ("#" `Text.isPrefixOf` t)
        && not (isListItem t)

renderBlocks :: (Monad m) => [MarkdownBlock] -> Lucid.HtmlT m ()
renderBlocks = traverse_ renderBlock

renderBlock :: (Monad m) => MarkdownBlock -> Lucid.HtmlT m ()
renderBlock block = case block of
  MdHeader 1 txt -> Lucid.h2_ [Lucid.class_ "md-h1"] (renderInline txt)
  MdHeader 2 txt -> Lucid.h3_ [Lucid.class_ "md-h2"] (renderInline txt)
  MdHeader 3 txt -> Lucid.h4_ [Lucid.class_ "md-h3"] (renderInline txt)
  MdHeader _ txt -> Lucid.h4_ (renderInline txt)
  MdParagraph lns -> Lucid.p_ [Lucid.class_ "md-para"] (renderInline (Text.unlines lns))
  MdCodeBlock lns -> Lucid.pre_ [Lucid.class_ "md-code"] (Lucid.code_ (Lucid.toHtml (Text.unlines lns)))
  MdList items -> Lucid.ul_ [Lucid.class_ "md-list"] (traverse_ renderListItem items)

renderListItem :: (Monad m) => Text -> Lucid.HtmlT m ()
renderListItem txt = Lucid.li_ (renderInline txt)

renderInline :: (Monad m) => Text -> Lucid.HtmlT m ()
renderInline txt = renderInlineParts (parseInline txt)

data InlinePart = PlainText Text | InlineCode Text | BoldText Text
  deriving (Show, Eq)

parseInline :: Text -> [InlinePart]
parseInline t
  | Text.null t = []
  | otherwise = case Text.breakOn "`" t of
      (before, rest)
        | Text.null rest -> parseBold before
        | otherwise ->
            let afterTick = Text.drop 1 rest
             in case Text.breakOn "`" afterTick of
                  (code, rest2)
                    | Text.null rest2 ->
                        parseBold before ++ [PlainText ("`" <> afterTick)]
                    | otherwise ->
                        parseBold before ++ [InlineCode code] ++ parseInline (Text.drop 1 rest2)

parseBold :: Text -> [InlinePart]
parseBold t
  | Text.null t = []
  | otherwise = case Text.breakOn "**" t of
      (before, rest)
        | Text.null rest -> [PlainText before | not (Text.null before)]
        | otherwise ->
            let afterBold = Text.drop 2 rest
             in case Text.breakOn "**" afterBold of
                  (boldText, rest2)
                    | Text.null rest2 ->
                        [PlainText before | not (Text.null before)] ++ [PlainText ("**" <> afterBold)]
                    | otherwise ->
                        [PlainText before | not (Text.null before)]
                          ++ [BoldText boldText]
                          ++ parseBold (Text.drop 2 rest2)

renderInlineParts :: (Monad m) => [InlinePart] -> Lucid.HtmlT m ()
renderInlineParts = traverse_ renderInlinePart

renderInlinePart :: (Monad m) => InlinePart -> Lucid.HtmlT m ()
renderInlinePart part = case part of
  PlainText txt -> Lucid.toHtml txt
  InlineCode txt -> Lucid.code_ [Lucid.class_ "md-inline-code"] (Lucid.toHtml txt)
  BoldText txt -> Lucid.strong_ (Lucid.toHtml txt)

-- * Comment form

commentForm :: (Monad m) => Text -> Lucid.HtmlT m ()
commentForm tid =
  Lucid.form_
    [ Lucid.method_ "POST",
      Lucid.action_ ("/tasks/" <> tid <> "/comment"),
      Lucid.class_ "comment-form"
    ]
    <| do
      Lucid.textarea_
        [ Lucid.name_ "comment",
          Lucid.placeholder_ "Add a comment...",
          Lucid.rows_ "3",
          Lucid.class_ "comment-textarea"
        ]
        ""
      Lucid.button_ [Lucid.type_ "submit", Lucid.class_ "btn btn-primary"] "Post Comment"

-- * Live toggles

renderLiveToggle :: (Monad m) => Lucid.HtmlT m ()
renderLiveToggle =
  Lucid.button_
    [ Lucid.class_ "timeline-live-toggle",
      Lucid.id_ "live-toggle",
      Lucid.makeAttribute "onclick" "toggleLiveUpdates()",
      Lucid.title_ "Click to pause/resume live updates"
    ]
    " LIVE"

renderAutoscrollToggle :: (Monad m) => Lucid.HtmlT m ()
renderAutoscrollToggle =
  Lucid.button_
    [ Lucid.class_ "timeline-autoscroll-toggle",
      Lucid.id_ "autoscroll-toggle",
      Lucid.makeAttribute "onclick" "toggleAutoscroll()",
      Lucid.title_ "Toggle automatic scrolling to newest events"
    ]
    " ⬇ Auto-scroll"

-- * Cost/Token metrics

aggregateCostMetrics :: [TaskCore.StoredEvent] -> (Int, Int)
aggregateCostMetrics events =
  let costEvents = filter (\e -> TaskCore.storedEventType e == "Cost") events
      aggregateOne (totalCents, totalTokens) event =
        case Aeson.decode (LBS.fromStrict (str (TaskCore.storedEventContent event))) of
          Just (Aeson.Object obj) ->
            let cents = case KeyMap.lookup "cents" obj of
                  Just (Aeson.Number n) -> floor n
                  _ -> 0
                tokens = case KeyMap.lookup "tokens" obj of
                  Just (Aeson.Number n) -> floor n
                  _ -> 0
             in (totalCents + cents, totalTokens + tokens)
          _ -> (totalCents, totalTokens)
   in foldl' aggregateOne (0, 0) costEvents

formatCostHeader :: Int -> Text
formatCostHeader cents =
  let dollars = fromIntegral cents / 100.0 :: Double
   in "$" <> Text.pack (showFFloat (Just 2) dollars "")

formatTokensHeader :: Int -> Text
formatTokensHeader t
  | t < 1000 = tshow t
  | t < 1000000 = Text.pack (showFFloat (Just 1) (fromIntegral t / 1000.0 :: Double) "") <> "K"
  | otherwise = Text.pack (showFFloat (Just 2) (fromIntegral t / 1000000.0 :: Double) "") <> "M"

-- * Timeline

renderUnifiedTimeline :: (Monad m) => Text -> [TaskCore.Comment] -> [TaskCore.StoredEvent] -> TaskCore.Status -> UTCTime -> Lucid.HtmlT m ()
renderUnifiedTimeline tid legacyComments events status now = do
  let isInProgress = status == TaskCore.InProgress
      pollAttrs =
        if isInProgress
          then
            [ Lucid.makeAttribute "hx-get" ("/partials/task/" <> tid <> "/events"),
              Lucid.makeAttribute "hx-trigger" "every 3s",
              Lucid.makeAttribute "hx-swap" "innerHTML",
              Lucid.makeAttribute "hx-on::before-request" "var log = this.querySelector('.timeline-events'); if(log) this.dataset.scroll = log.scrollTop",
              Lucid.makeAttribute "hx-on::after-swap" "var log = this.querySelector('.timeline-events'); if(log && this.dataset.scroll) log.scrollTop = this.dataset.scroll"
            ]
          else []
      nonCostEvents = filter (\e -> TaskCore.storedEventType e /= "Cost") events
      eventCount = length nonCostEvents + length legacyComments
      (totalCents, totalTokens) = aggregateCostMetrics events
  Lucid.div_ ([Lucid.class_ "unified-timeline-section", Lucid.id_ "unified-timeline"] <> pollAttrs) <| do
    Lucid.h3_ <| do
      Lucid.toHtml ("Timeline (" <> tshow eventCount <> ")")
      when (totalCents > 0 || totalTokens > 0) <| do
        Lucid.span_ [Lucid.class_ "timeline-cost-summary"] <| do
          metaSep
          when (totalCents > 0) <| Lucid.toHtml (formatCostHeader totalCents)
          when (totalCents > 0 && totalTokens > 0) <| metaSep
          when (totalTokens > 0) <| Lucid.toHtml (formatTokensHeader totalTokens <> " tokens")
      when isInProgress <| do
        renderLiveToggle
        renderAutoscrollToggle

    if null nonCostEvents && null legacyComments
      then Lucid.p_ [Lucid.class_ "empty-msg"] "No activity yet."
      else do
        Lucid.div_ [Lucid.class_ "timeline-events"] <| do
          traverse_ (renderTimelineEvent now) nonCostEvents
        when isInProgress <| timelineScrollScript

    commentForm tid

renderTimelineEvent :: (Monad m) => UTCTime -> TaskCore.StoredEvent -> Lucid.HtmlT m ()
renderTimelineEvent now event =
  let eventType = TaskCore.storedEventType event
      content = TaskCore.storedEventContent event
      timestamp = TaskCore.storedEventTimestamp event
      actor = TaskCore.storedEventActor event
      eventId = TaskCore.storedEventId event
      (icon, label) = eventTypeIconAndLabel eventType
   in Lucid.div_
        [ Lucid.class_ ("timeline-event timeline-event-" <> eventType),
          Lucid.makeAttribute "data-event-id" (tshow eventId)
        ]
        <| do
          case eventType of
            "comment" -> renderCommentTimelineEvent content actor timestamp now
            "status_change" -> renderStatusChangeEvent content actor timestamp now
            "claim" -> renderActivityEvent icon label content actor timestamp now
            "running" -> renderActivityEvent icon label content actor timestamp now
            "reviewing" -> renderActivityEvent icon label content actor timestamp now
            "retrying" -> renderActivityEvent icon label content actor timestamp now
            "complete" -> renderActivityEvent icon label content actor timestamp now
            "error" -> renderErrorTimelineEvent content actor timestamp now
            "Assistant" -> renderAssistantTimelineEvent content actor timestamp now
            "ToolCall" -> renderToolCallTimelineEvent content actor timestamp now
            "ToolResult" -> renderToolResultTimelineEvent content actor timestamp now
            "Cost" -> pure ()
            "Checkpoint" -> renderCheckpointEvent content actor timestamp now
            "Guardrail" -> renderGuardrailEvent content actor timestamp now
            _ -> renderGenericEvent eventType content actor timestamp now

eventTypeIconAndLabel :: Text -> (Text, Text)
eventTypeIconAndLabel "comment" = ("💬", "Comment")
eventTypeIconAndLabel "status_change" = ("🔄", "Status")
eventTypeIconAndLabel "claim" = ("🤖", "Claimed")
eventTypeIconAndLabel "running" = ("▶️", "Running")
eventTypeIconAndLabel "reviewing" = ("👀", "Reviewing")
eventTypeIconAndLabel "retrying" = ("🔁", "Retrying")
eventTypeIconAndLabel "complete" = ("✅", "Complete")
eventTypeIconAndLabel "error" = ("❌", "Error")
eventTypeIconAndLabel "Assistant" = ("💭", "Thought")
eventTypeIconAndLabel "ToolCall" = ("🔧", "Tool")
eventTypeIconAndLabel "ToolResult" = ("📄", "Result")
eventTypeIconAndLabel "Cost" = ("💰", "Cost")
eventTypeIconAndLabel "Checkpoint" = ("📍", "Checkpoint")
eventTypeIconAndLabel "Guardrail" = ("⚠️", "Guardrail")
eventTypeIconAndLabel t = ("📝", t)

renderActorLabel :: (Monad m) => TaskCore.CommentAuthor -> Lucid.HtmlT m ()
renderActorLabel actor =
  let (cls, label) :: (Text, Text) = case actor of
        TaskCore.Human -> ("actor-human", "human")
        TaskCore.Junior -> ("actor-junior", "junior")
        TaskCore.System -> ("actor-system", "system")
   in Lucid.span_ [Lucid.class_ ("actor-label " <> cls)] (Lucid.toHtml ("[" <> label <> "]"))

renderCommentTimelineEvent :: (Monad m) => Text -> TaskCore.CommentAuthor -> UTCTime -> UTCTime -> Lucid.HtmlT m ()
renderCommentTimelineEvent content actor timestamp now =
  Lucid.div_ [Lucid.class_ "timeline-comment"] <| do
    Lucid.div_ [Lucid.class_ "event-header"] <| do
      Lucid.span_ [Lucid.class_ "event-icon"] "💬"
      renderActorLabel actor
      renderRelativeTimestamp now timestamp
    Lucid.div_ [Lucid.class_ "event-content comment-bubble markdown-content"] <| do
      renderMarkdown content

renderStatusChangeEvent :: (Monad m) => Text -> TaskCore.CommentAuthor -> UTCTime -> UTCTime -> Lucid.HtmlT m ()
renderStatusChangeEvent content actor timestamp now =
  Lucid.div_ [Lucid.class_ "timeline-status-change"] <| do
    Lucid.span_ [Lucid.class_ "event-icon"] "🔄"
    renderActorLabel actor
    Lucid.span_ [Lucid.class_ "status-change-text"] (Lucid.toHtml (parseStatusChange content))
    renderRelativeTimestamp now timestamp

parseStatusChange :: Text -> Text
parseStatusChange content =
  case Aeson.decode (LBS.fromStrict (str content)) of
    Just (Aeson.Object obj) ->
      let fromStatus = case KeyMap.lookup "from" obj of
            Just (Aeson.String s) -> s
            _ -> "?"
          toStatus = case KeyMap.lookup "to" obj of
            Just (Aeson.String s) -> s
            _ -> "?"
       in fromStatus <> " → " <> toStatus
    _ -> content

renderActivityEvent :: (Monad m) => Text -> Text -> Text -> TaskCore.CommentAuthor -> UTCTime -> UTCTime -> Lucid.HtmlT m ()
renderActivityEvent icon label content actor timestamp now =
  Lucid.div_ [Lucid.class_ "timeline-activity"] <| do
    Lucid.span_ [Lucid.class_ "event-icon"] (Lucid.toHtml icon)
    Lucid.span_ [Lucid.class_ "event-label"] (Lucid.toHtml label)
    renderActorLabel actor
    unless (Text.null content) <| Lucid.span_ [Lucid.class_ "activity-detail"] (Lucid.toHtml content)
    renderRelativeTimestamp now timestamp

renderErrorTimelineEvent :: (Monad m) => Text -> TaskCore.CommentAuthor -> UTCTime -> UTCTime -> Lucid.HtmlT m ()
renderErrorTimelineEvent content actor timestamp now =
  Lucid.div_ [Lucid.class_ "timeline-error"] <| do
    Lucid.div_ [Lucid.class_ "event-header"] <| do
      Lucid.span_ [Lucid.class_ "event-icon"] "❌"
      Lucid.span_ [Lucid.class_ "event-label"] "Error"
      renderActorLabel actor
      renderRelativeTimestamp now timestamp
    Lucid.div_ [Lucid.class_ "event-content error-message"] (renderFormattedJson content)

renderAssistantTimelineEvent :: (Monad m) => Text -> TaskCore.CommentAuthor -> UTCTime -> UTCTime -> Lucid.HtmlT m ()
renderAssistantTimelineEvent content _actor timestamp now =
  Lucid.div_ [Lucid.class_ "timeline-thought"] <| do
    Lucid.div_ [Lucid.class_ "event-header"] <| do
      Lucid.span_ [Lucid.class_ "event-icon"] "💭"
      Lucid.span_ [Lucid.class_ "event-label"] "Thought"
      renderActorLabel TaskCore.Junior
      renderRelativeTimestamp now timestamp
    Lucid.div_ [Lucid.class_ "event-content thought-bubble markdown-content"] <| do
      let truncated = Text.take 2000 content
          isTruncated = Text.length content > 2000
      renderMarkdown truncated
      when isTruncated <| Lucid.span_ [Lucid.class_ "event-truncated"] "..."

renderToolCallTimelineEvent :: (Monad m) => Text -> TaskCore.CommentAuthor -> UTCTime -> UTCTime -> Lucid.HtmlT m ()
renderToolCallTimelineEvent content _actor _timestamp _now =
  let (toolName, argsJson) = parseToolCallContent content
   in case toolName of
        "run_bash" -> renderBashToolCall argsJson
        "read_file" -> renderReadToolCall argsJson
        "edit_file" -> renderEditToolCall argsJson
        "search_codebase" -> renderSearchToolCall argsJson
        "search_and_read" -> renderSearchAndReadToolCall argsJson
        "write_file" -> renderWriteToolCall argsJson
        _ -> renderGenericToolCall toolName argsJson

renderBashToolCall :: (Monad m) => Text -> Lucid.HtmlT m ()
renderBashToolCall argsJson =
  let cmd = extractJsonField "command" argsJson
   in Lucid.div_ [Lucid.class_ "tool-bash"] <| do
        Lucid.span_ [Lucid.class_ "tool-bash-prompt"] "ϟ"
        Lucid.code_ [Lucid.class_ "tool-bash-cmd"] (Lucid.toHtml cmd)

renderReadToolCall :: (Monad m) => Text -> Lucid.HtmlT m ()
renderReadToolCall argsJson =
  let path = extractJsonField "path" argsJson
      startLine = extractJsonFieldInt "start_line" argsJson
      endLine = extractJsonFieldInt "end_line" argsJson
      lineRange = case (startLine, endLine) of
        (Just s, Just e) -> " @" <> tshow s <> "-" <> tshow e
        (Just s, Nothing) -> " @" <> tshow s <> "+"
        _ -> ""
   in Lucid.div_ [Lucid.class_ "tool-compact"] <| do
        Lucid.span_ [Lucid.class_ "tool-check"] "✓"
        Lucid.span_ [Lucid.class_ "tool-label"] "Read"
        Lucid.code_ [Lucid.class_ "tool-path"] (Lucid.toHtml (shortenPath path <> lineRange))

renderEditToolCall :: (Monad m) => Text -> Lucid.HtmlT m ()
renderEditToolCall argsJson =
  let path = extractJsonField "path" argsJson
   in Lucid.div_ [Lucid.class_ "tool-compact"] <| do
        Lucid.span_ [Lucid.class_ "tool-check"] "✓"
        Lucid.span_ [Lucid.class_ "tool-label"] "Edit"
        Lucid.code_ [Lucid.class_ "tool-path"] (Lucid.toHtml (shortenPath path))

renderSearchToolCall :: (Monad m) => Text -> Lucid.HtmlT m ()
renderSearchToolCall argsJson =
  let searchPat = extractJsonField "pattern" argsJson
      searchPath = extractJsonField "path" argsJson
      pathSuffix = if Text.null searchPath || searchPath == "." then "" else " in " <> shortenPath searchPath
   in Lucid.div_ [Lucid.class_ "tool-compact"] <| do
        Lucid.span_ [Lucid.class_ "tool-check"] "✓"
        Lucid.span_ [Lucid.class_ "tool-label"] "Grep"
        Lucid.code_ [Lucid.class_ "tool-pattern"] (Lucid.toHtml searchPat)
        unless (Text.null pathSuffix)
          <| Lucid.span_ [Lucid.class_ "tool-path-suffix"] (Lucid.toHtml pathSuffix)

renderWriteToolCall :: (Monad m) => Text -> Lucid.HtmlT m ()
renderWriteToolCall argsJson =
  let path = extractJsonField "path" argsJson
   in Lucid.div_ [Lucid.class_ "tool-compact"] <| do
        Lucid.span_ [Lucid.class_ "tool-check"] "✓"
        Lucid.span_ [Lucid.class_ "tool-label"] "Write"
        Lucid.code_ [Lucid.class_ "tool-path"] (Lucid.toHtml (shortenPath path))

renderSearchAndReadToolCall :: (Monad m) => Text -> Lucid.HtmlT m ()
renderSearchAndReadToolCall argsJson =
  let searchPat = extractJsonField "pattern" argsJson
      searchPath = extractJsonField "path" argsJson
      pathSuffix = if Text.null searchPath || searchPath == "." then "" else " in " <> shortenPath searchPath
   in Lucid.div_ [Lucid.class_ "tool-compact"] <| do
        Lucid.span_ [Lucid.class_ "tool-check"] "✓"
        Lucid.span_ [Lucid.class_ "tool-label"] "Find"
        Lucid.code_ [Lucid.class_ "tool-pattern"] (Lucid.toHtml searchPat)
        unless (Text.null pathSuffix)
          <| Lucid.span_ [Lucid.class_ "tool-path-suffix"] (Lucid.toHtml pathSuffix)

renderGenericToolCall :: (Monad m) => Text -> Text -> Lucid.HtmlT m ()
renderGenericToolCall toolName argsJson =
  Lucid.details_ [Lucid.class_ "tool-generic"] <| do
    Lucid.summary_ <| do
      Lucid.span_ [Lucid.class_ "tool-check"] "✓"
      Lucid.span_ [Lucid.class_ "tool-label"] (Lucid.toHtml toolName)
    Lucid.pre_ [Lucid.class_ "tool-args-pre"] (Lucid.toHtml argsJson)

extractJsonField :: Text -> Text -> Text
extractJsonField field jsonText =
  case Aeson.decode (LBS.fromStrict (str jsonText)) of
    Just (Aeson.Object obj) ->
      case KeyMap.lookup (AesonKey.fromText field) obj of
        Just (Aeson.String s) -> s
        _ -> ""
    _ -> ""

extractJsonFieldInt :: Text -> Text -> Maybe Int
extractJsonFieldInt field jsonText =
  case Aeson.decode (LBS.fromStrict (str jsonText)) of
    Just (Aeson.Object obj) ->
      case KeyMap.lookup (AesonKey.fromText field) obj of
        Just (Aeson.Number n) -> Just (floor n)
        _ -> Nothing
    _ -> Nothing

shortenPath :: Text -> Text
shortenPath path =
  let parts = Text.splitOn "/" path
      relevant = dropWhile (\p -> p `elem` ["", "home", "ben", "omni"]) parts
   in Text.intercalate "/" relevant

renderToolResultTimelineEvent :: (Monad m) => Text -> TaskCore.CommentAuthor -> UTCTime -> UTCTime -> Lucid.HtmlT m ()
renderToolResultTimelineEvent content _actor _timestamp _now =
  let decoded = decodeToolResult content
      isSuccess = toolResultIsSuccess decoded
      output = toolResultOutput' decoded
      lineCount = length (Text.lines output)
   in if Text.null output || (isSuccess && lineCount <= 1)
        then pure ()
        else
          Lucid.div_ [Lucid.class_ "tool-result-output"] <| do
            when (lineCount > 10)
              <| Lucid.details_ [Lucid.class_ "result-collapsible"]
              <| do
                Lucid.summary_ (Lucid.toHtml (tshow lineCount <> " lines"))
                Lucid.pre_ [Lucid.class_ "tool-output-pre"] (Lucid.toHtml output)
            when (lineCount <= 10)
              <| Lucid.pre_ [Lucid.class_ "tool-output-pre"] (Lucid.toHtml output)

data DecodedToolResult = DecodedToolResult
  { toolResultIsSuccess :: Bool,
    toolResultOutput' :: Text,
    toolResultError' :: Maybe Text
  }

decodeToolResult :: Text -> DecodedToolResult
decodeToolResult content =
  case Aeson.decode (LBS.fromStrict (str content)) of
    Just (Aeson.Object obj) ->
      DecodedToolResult
        { toolResultIsSuccess = case KeyMap.lookup "success" obj of
            Just (Aeson.Bool b) -> b
            _ -> True,
          toolResultOutput' = case KeyMap.lookup "output" obj of
            Just (Aeson.String s) -> s
            _ -> "",
          toolResultError' = case KeyMap.lookup "error" obj of
            Just (Aeson.String s) -> Just s
            _ -> Nothing
        }
    _ -> DecodedToolResult True content Nothing

renderCheckpointEvent :: (Monad m) => Text -> TaskCore.CommentAuthor -> UTCTime -> UTCTime -> Lucid.HtmlT m ()
renderCheckpointEvent content actor timestamp now =
  Lucid.div_ [Lucid.class_ "timeline-checkpoint"] <| do
    Lucid.div_ [Lucid.class_ "event-header"] <| do
      Lucid.span_ [Lucid.class_ "event-icon"] "📍"
      Lucid.span_ [Lucid.class_ "event-label"] "Checkpoint"
      renderActorLabel actor
      renderRelativeTimestamp now timestamp
    Lucid.div_ [Lucid.class_ "event-content checkpoint-content"] (Lucid.toHtml content)

renderGuardrailEvent :: (Monad m) => Text -> TaskCore.CommentAuthor -> UTCTime -> UTCTime -> Lucid.HtmlT m ()
renderGuardrailEvent content actor timestamp now =
  Lucid.div_ [Lucid.class_ "timeline-guardrail"] <| do
    Lucid.div_ [Lucid.class_ "event-header"] <| do
      Lucid.span_ [Lucid.class_ "event-icon"] "⚠️"
      Lucid.span_ [Lucid.class_ "event-label"] "Guardrail"
      renderActorLabel actor
      renderRelativeTimestamp now timestamp
    Lucid.div_ [Lucid.class_ "event-content guardrail-content"] (renderFormattedJson content)

renderGenericEvent :: (Monad m) => Text -> Text -> TaskCore.CommentAuthor -> UTCTime -> UTCTime -> Lucid.HtmlT m ()
renderGenericEvent eventType content actor timestamp now =
  Lucid.div_ [Lucid.class_ "timeline-generic"] <| do
    Lucid.div_ [Lucid.class_ "event-header"] <| do
      Lucid.span_ [Lucid.class_ "event-icon"] "📝"
      Lucid.span_ [Lucid.class_ "event-label"] (Lucid.toHtml eventType)
      renderActorLabel actor
      renderRelativeTimestamp now timestamp
    unless (Text.null content) <| Lucid.div_ [Lucid.class_ "event-content"] (Lucid.toHtml content)

parseToolCallContent :: Text -> (Text, Text)
parseToolCallContent content =
  case Text.breakOn ":" content of
    (name, rest)
      | Text.null rest -> (content, "")
      | otherwise -> (Text.strip name, Text.strip (Text.drop 1 rest))

formatToolCallSummary :: Text -> Text -> Text
formatToolCallSummary toolName argsJson =
  case Aeson.decode (LBS.fromStrict (str argsJson)) of
    Just (Aeson.Object obj) ->
      let keyArg = case toolName of
            "run_bash" -> KeyMap.lookup "command" obj
            "read_file" -> KeyMap.lookup "path" obj
            "edit_file" -> KeyMap.lookup "path" obj
            "write_file" -> KeyMap.lookup "path" obj
            "search_codebase" -> KeyMap.lookup "pattern" obj
            "glob_files" -> KeyMap.lookup "pattern" obj
            "list_directory" -> KeyMap.lookup "path" obj
            _ -> Nothing
       in case keyArg of
            Just (Aeson.String s) -> "`" <> Text.take 100 s <> "`"
            _ -> Text.take 80 argsJson
    _ -> Text.take 80 argsJson

renderCollapsibleOutput :: (Monad m) => Text -> Lucid.HtmlT m ()
renderCollapsibleOutput content =
  let lineCount = length (Text.lines content)
   in if lineCount > 20
        then
          Lucid.details_ [Lucid.class_ "output-collapsible"] <| do
            Lucid.summary_ (Lucid.toHtml (tshow lineCount <> " lines"))
            Lucid.pre_ [Lucid.class_ "tool-output-pre"] (Lucid.toHtml content)
        else Lucid.pre_ [Lucid.class_ "tool-output-pre"] (Lucid.toHtml content)

renderDecodedToolResult :: (Monad m) => Text -> Lucid.HtmlT m ()
renderDecodedToolResult content =
  case Aeson.decode (LBS.fromStrict (str content)) of
    Just (Aeson.Object obj) ->
      case KeyMap.lookup "output" obj of
        Just (Aeson.String output) -> Lucid.toHtml output
        _ -> Lucid.toHtml content
    _ -> Lucid.toHtml content

-- | Format JSON content for human-readable display.
-- Tries to pretty-print JSON, falls back to raw text if not valid JSON.
renderFormattedJson :: (Monad m) => Text -> Lucid.HtmlT m ()
renderFormattedJson content =
  case Aeson.decode (LBS.fromStrict (str content)) of
    Just (val :: Aeson.Value) ->
      Lucid.pre_ [Lucid.class_ "formatted-json"] <| do
        Lucid.toHtml (decodeUtf8 (LBS.toStrict (Aeson.encode val)))
    Nothing -> Lucid.toHtml content

timelineScrollScript :: (Monad m) => Lucid.HtmlT m ()
timelineScrollScript =
  Lucid.script_
    [ Lucid.type_ "text/javascript"
    ]
    ( Text.unlines
        [ "(function() {",
          "  if (typeof autoscrollEnabled !== 'undefined' && !autoscrollEnabled) return;",
          "  var log = document.querySelector('.timeline-events');",
          "  if (log) {",
          "    var isNearBottom = log.scrollHeight - log.scrollTop - log.clientHeight < 100;",
          "    if (isNearBottom) {",
          "      log.scrollTop = log.scrollHeight;",
          "    }",
          "  }",
          "})();"
        ]
    )