mahbot 0.4.1

An autonomous agentic engineering system that manages software development through role separation, subagents, and deterministic diagnostics.
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
//! Dashboard theme — defines the Flexoki dark color palette, ticket phase colors, role badge
//! colors, log level colors, and workspace status colors for the native Iced GUI.

use iced::Background;
use iced::Color;
use iced::border;
use iced::widget::{container, scrollable};

use crate::WorkspaceStatus;
use crate::board::TicketPhase;

use iced_fonts::lucide;
use std::sync::atomic::AtomicBool;

// ── Flexoki dark palette ─────────────────────────────────────────

pub const BG_BASE: Color = Color::from_rgb(0.063, 0.059, 0.059); // #100f0f
pub const BG_SURFACE: Color = Color::from_rgb(0.110, 0.106, 0.102); // #1c1b1a
pub const BG_ELEVATED: Color = Color::from_rgb(0.157, 0.153, 0.149); // #282726

pub const BORDER: Color = Color::from_rgba(0.808, 0.804, 0.765, 0.08);
pub const BORDER_STRONG: Color = Color::from_rgba(0.808, 0.804, 0.765, 0.12);

pub const TEXT_PRIMARY: Color = Color::from_rgb(0.808, 0.804, 0.765); // #cecdc3
pub const TEXT_SECONDARY: Color = Color::from_rgb(0.529, 0.522, 0.502); // #878580
pub const TEXT_MUTED: Color = Color::from_rgb(0.341, 0.337, 0.325); // #575653
pub const TEXT_FAINT: Color = Color::from_rgb(0.204, 0.200, 0.192); // #343331

pub const ACCENT: Color = Color::from_rgb(0.227, 0.663, 0.624); // #3aa99f
pub const ACCENT_LIGHT: Color = Color::from_rgb(0.357, 0.749, 0.710); // #5bbfb5
pub const ACCENT_DIM: Color = Color::from_rgba(0.227, 0.663, 0.624, 0.3);

// ── Find match highlight colors (Flexoki amber) ──────────────────
//
// Non-current matches: amber/gold at low alpha (background tint).
// Current match: same amber at higher alpha for prominence.
// Both are visually distinct from ACCENT_DIM (teal) used for selection.
//
// Flexoki amber: #D0A215 = rgb(0.816, 0.635, 0.082)
pub const FIND_MATCH_DIM: Color = Color::from_rgba(0.816, 0.635, 0.082, 0.25);
pub const FIND_MATCH_CURRENT: Color = Color::from_rgba(0.816, 0.635, 0.082, 0.45);

/// Bracket matching highlight: subtle teal background (ACCENT_LIGHT at 35%).
pub const BRACKET_MATCH: Color = Color::from_rgba(0.357, 0.749, 0.710, 0.35);

pub const HOVER: Color = Color::from_rgba(0.808, 0.804, 0.765, 0.05);
pub const HOVER_STRONG: Color = Color::from_rgba(0.808, 0.804, 0.765, 0.08);

// ── Status colors ─────────────────────────────────────────────────

pub const STATUS_SUCCESS: Color = Color::from_rgb(0.0, 0.902, 0.541); // #00e68a
pub const STATUS_WARNING: Color = Color::from_rgb(1.0, 0.667, 0.0); // #ffaa00
pub const STATUS_ERROR: Color = Color::from_rgb(1.0, 0.267, 0.4); // #ff4466

// ── Diff widget tints (derived from STATUS_* palette) ─────────────

pub const DIFF_ADDED_TINT: Color =
    Color::from_rgba(STATUS_SUCCESS.r, STATUS_SUCCESS.g, STATUS_SUCCESS.b, 0.10);
pub const DIFF_REMOVED_TINT: Color =
    Color::from_rgba(STATUS_ERROR.r, STATUS_ERROR.g, STATUS_ERROR.b, 0.10);
pub const DIFF_FILE_HEADER_BG: Color =
    Color::from_rgba(STATUS_WARNING.r, STATUS_WARNING.g, STATUS_WARNING.b, 0.06);

// ── Log level colors ──────────────────────────────────────────────

#[must_use]
pub fn log_level_color(level: &str) -> (Color, Color) {
    match level.to_uppercase().as_str() {
        "ERROR" => (STATUS_ERROR, Color::from_rgba(0.937, 0.267, 0.267, 0.08)),
        "WARN" => (STATUS_WARNING, Color::from_rgba(1.0, 0.667, 0.0, 0.08)),
        "INFO" => (
            Color::from_rgb(0.219, 0.741, 0.973),
            Color::from_rgba(0.219, 0.741, 0.973, 0.08),
        ),
        "DEBUG" => (
            Color::from_rgb(0.655, 0.545, 0.980),
            Color::from_rgba(0.655, 0.545, 0.980, 0.08),
        ),
        "TRACE" => (TEXT_MUTED, Color::from_rgba(0.5, 0.5, 0.5, 0.08)),
        _ => (TEXT_MUTED, HOVER),
    }
}

// ── Role badge colors (from roleConfig.ts) ───────────────────────

/// Translucent pill background for a badge foreground color: the foreground
/// at 0.1 alpha. Single source of the badge-background math — the second
/// member of every [`role_badge_color_for`] / [`role_badge_color`] tuple is
/// `badge_bg(fg)` (including the unknown-role fallback), and consumers
/// (role pill, logs span) use that member directly, so rendering cannot
/// drift from this alpha.
const fn badge_bg(fg: Color) -> Color {
    Color::from_rgba(fg.r, fg.g, fg.b, 0.1)
}

/// Returns the badge (foreground, background) color for a given [`crate::Role`].
///
/// Reads from [`crate::role::role_info()`] and converts the RGB tuple to
/// [`iced::Color`] — this avoids duplicating color data in an exhaustive match.
/// Adding a new [`crate::Role`] variant requires updating the `role_info()`
/// match (`badge_fg` field); the compiler will not catch a missing field here
/// (it defaults from `BASE_ROLE_INFO`), but the `badge_colors_set` test in
/// `role.rs` guards against silent black fallthrough.
///
/// The background member is always the foreground at 0.1 alpha ([`badge_bg`]);
/// consumers that render the pill background use it directly.
#[must_use]
#[allow(clippy::trivially_copy_pass_by_ref)]
pub const fn role_badge_color_for(role: &crate::Role) -> (Color, Color) {
    let info = crate::role::role_info(role);
    let (r, g, b) = info.badge_fg;
    let fg = Color::from_rgb(r, g, b);
    (fg, badge_bg(fg))
}

/// Returns the badge (foreground, background) color for a role name string.
///
/// Accepts canonical names (e.g. `"analyst"`), derivative names with a
/// numeric suffix (e.g. `"analyst_1"`, `"analyst_2"`), and the joint-comment
/// stage roles ("Analysis"/"Review"/"QA" — the comment role is the stage
/// name, per the joint-verdict pipeline). Unknown strings (including LLM API
/// roles like `"user"`, `"assistant"`, `"system"`, `"tool"`) fall back to a
/// muted grey.
///
/// The background member is always the foreground at 0.1 alpha ([`badge_bg`]) —
/// including the fallback, so consuming the second member directly (role pill,
/// logs span) renders the same alpha-scaled background for unknown roles as
/// for canonical ones.
///
/// Delegates to [`role_badge_color_for`] after resolving the string, which
/// reads colors from [`crate::role::role_info()`] as the single source of truth.
#[must_use]
pub fn role_badge_color(role: &str) -> (Color, Color) {
    // Stage-name comment roles from the joint-verdict pipeline ("Analysis"/
    // "Review"/"QA" — the comment role is the stage name). Resolved via the
    // shared inverse mapping so it can't drift from management::stage_name.
    if let Some(r) = crate::management::stage_role(role) {
        return role_badge_color_for(&r);
    }

    // Try exact match first (handles canonical names like "analyst")
    if let Ok(r) = role.parse::<crate::Role>() {
        return role_badge_color_for(&r);
    }

    // Try stripping a trailing `_<digits>` suffix (handles "analyst_1" etc.)
    if let Some(idx) = role.rfind('_')
        && idx + 1 < role.len()
        && role.as_bytes()[idx + 1..].iter().all(u8::is_ascii_digit)
    {
        let stripped = &role[..idx];
        if let Ok(r) = stripped.parse::<crate::Role>() {
            return role_badge_color_for(&r);
        }
    }

    // The background member must stay `badge_bg(fg)` (not an independent
    // constant): consumers render it directly, so this preserves the
    // pre-consolidation rendering for unknown roles.
    (TEXT_MUTED, badge_bg(TEXT_MUTED))
}

// ── Role icon mapping (shared between sidebar and workspaces) ──────

/// Returns the Lucide icon widget for a given agent role.
///
/// Callers apply `.size()`, `.color()`, and `.into()` to style the icon
/// for their specific context.
#[must_use]
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn role_icon(role: &crate::Role) -> iced::widget::Text<'static, iced::Theme, iced::Renderer> {
    match role {
        crate::Role::Manager => lucide::bot(),
        crate::Role::Engineer => lucide::wrench(),
        crate::Role::Analyst => lucide::scan_search(),
        crate::Role::Coder => lucide::code(),
        crate::Role::Qa => lucide::gavel(),
        crate::Role::Maintainer => lucide::cog(),
        crate::Role::Discovery => lucide::search(),
        crate::Role::Artist => lucide::palette(),
        crate::Role::Reviewer => lucide::file_check(),
        crate::Role::Sanitation => lucide::spray_can(),
        crate::Role::Assistant => lucide::message_square(),
    }
}

/// Returns the Lucide icon widget for the workspace GENERAL context
/// (the discovery-produced summary for non-role LLM calls, shown in the
/// Settings → Workspaces row).
#[must_use]
pub fn general_context_icon() -> iced::widget::Text<'static, iced::Theme, iced::Renderer> {
    lucide::book_open_text()
}

/// Bold weight variant of JetBrains Mono (the dashboard default font).
pub const FONT_BOLD: iced::Font = iced::Font {
    family: iced::font::Family::Name("JetBrains Mono"),
    weight: iced::font::Weight::Bold,
    ..iced::Font::DEFAULT
};

/// Regular weight variant of JetBrains Mono.
pub const FONT_REGULAR: iced::Font = iced::Font {
    family: iced::font::Family::Name("JetBrains Mono"),
    ..iced::Font::DEFAULT
};

/// Body text size (px) of the transcript markdown renderer
/// (`markdown_settings`). The sessions-page collapse measurement
/// (`session_preview`) derives its body and heading sizes from this
/// constant, so a theme font-size change cannot silently drift the measured
/// wrap count away from the actual render.
pub const MARKDOWN_TEXT_SIZE: f32 = 13.0;

/// Markdown rendering settings consistent with the Flexoki dark theme.
#[must_use]
pub fn markdown_settings() -> iced::widget::markdown::Settings {
    let style = iced::widget::markdown::Style {
        font: FONT_REGULAR,
        inline_code_highlight: iced::widget::markdown::Highlight {
            background: iced::Color::from_rgba(0.0, 0.0, 0.0, 0.15).into(),
            border: iced::border::rounded(4),
        },
        inline_code_padding: iced::padding::left(1).right(1),
        inline_code_color: TEXT_PRIMARY,
        inline_code_font: FONT_REGULAR,
        code_block_font: FONT_REGULAR,
        link_color: ACCENT,
    };
    iced::widget::markdown::Settings::with_text_size(MARKDOWN_TEXT_SIZE, style)
}

// ── Ticket phase colors ───────────────────────────────────────────
// 16 TicketPhase variants, exhaustively matched — no catch-all.

#[must_use]
pub const fn ticket_phase_color(phase: TicketPhase) -> (Color, Color) {
    use TicketPhase::{
        Analysis, Backlog, Cancelled, DiagnosticsDone, Done, Failed, InDevelopment, InDiagnostics,
        InQa, InReview, InSanitation, Planning, QaPassed, ReadyForDevelopment, Reviewed,
        SanitationPassed,
    };
    match phase {
        // Early phases — cool/muted, neutral
        Backlog => (
            Color::from_rgb(0.176, 0.176, 0.176),
            Color::from_rgb(0.808, 0.804, 0.765),
        ),
        Planning => (
            Color::from_rgb(0.263, 0.243, 0.114),
            Color::from_rgb(0.902, 0.863, 0.784),
        ),
        Analysis => (
            Color::from_rgb(0.114, 0.216, 0.310),
            Color::from_rgb(0.784, 0.863, 0.949),
        ),
        // Ready — olive gateway (Manager→Engineer)
        ReadyForDevelopment => (
            Color::from_rgb(0.263, 0.224, 0.114),
            Color::from_rgb(0.902, 0.863, 0.784),
        ),
        // Active phases — warm
        InDevelopment => (
            Color::from_rgb(0.380, 0.216, 0.078),
            Color::from_rgb(0.941, 0.878, 0.784),
        ),
        // Diagnostic phases — amber/teal
        InDiagnostics => (
            Color::from_rgb(0.310, 0.224, 0.102),
            Color::from_rgb(0.902, 0.863, 0.784),
        ),
        DiagnosticsDone => (
            Color::from_rgb(0.161, 0.235, 0.224),
            Color::from_rgb(0.784, 0.902, 0.871),
        ),
        // Sanitation phases — neutral gray
        InSanitation => (
            Color::from_rgb(0.310, 0.310, 0.310),
            Color::from_rgb(0.788, 0.788, 0.788),
        ),
        SanitationPassed => (
            Color::from_rgb(0.247, 0.247, 0.247),
            Color::from_rgb(0.863, 0.863, 0.863),
        ),
        // Review & QA
        InReview => (
            Color::from_rgb(0.184, 0.216, 0.380),
            Color::from_rgb(0.816, 0.816, 0.933),
        ),
        Reviewed => (
            Color::from_rgb(0.224, 0.208, 0.322),
            Color::from_rgb(0.878, 0.816, 0.933),
        ),
        InQa => (
            Color::from_rgb(0.216, 0.184, 0.380),
            Color::from_rgb(0.816, 0.816, 0.933),
        ),
        QaPassed => (
            Color::from_rgb(0.176, 0.310, 0.208),
            Color::from_rgb(0.784, 0.902, 0.816),
        ),
        // Unblocking phases — distinct
        Done => (
            Color::from_rgb(0.114, 0.176, 0.114),
            Color::from_rgb(0.753, 0.816, 0.753),
        ),
        Cancelled => (
            Color::from_rgb(0.145, 0.145, 0.145),
            Color::from_rgb(0.690, 0.690, 0.690),
        ),
        Failed => (
            Color::from_rgb(0.310, 0.114, 0.114),
            Color::from_rgb(0.878, 0.753, 0.753),
        ),
    }
}

// ── Ticket priority chip colors (Flexoki muted palette) ──────────
//
// Priority uses the same muted-tone approach as phase badges:
// darker backgrounds with lighter, readable text on a dark theme.
//
// P0 (urgent):     red     — failed-like
// P1 (high):       orange  — in-development-like
// P2 (medium):     yellow  — in-diagnostics-like
// P3 (low):        green   — qa-passed-like
// P4+ (lowest):    green   — done-like

#[must_use]
pub fn ticket_priority_color(priority: i64) -> (Color, Color) {
    match priority {
        0 => (
            Color::from_rgb(0.310, 0.114, 0.114),
            Color::from_rgb(0.878, 0.753, 0.753),
        ),
        1 => (
            Color::from_rgb(0.380, 0.216, 0.078),
            Color::from_rgb(0.941, 0.878, 0.784),
        ),
        2 => (
            Color::from_rgb(0.310, 0.224, 0.102),
            Color::from_rgb(0.902, 0.863, 0.784),
        ),
        3 => (
            Color::from_rgb(0.176, 0.310, 0.208),
            Color::from_rgb(0.784, 0.902, 0.816),
        ),
        _ => (
            Color::from_rgb(0.114, 0.176, 0.114),
            Color::from_rgb(0.753, 0.816, 0.753),
        ),
    }
}

/// One-shot guard to log timestamp parse failure only once.
static TIMESTAMP_PARSE_WARNED: AtomicBool = AtomicBool::new(false);

/// Format an ISO 8601 timestamp string into a human-readable absolute form.
/// Output style: "Jun 5, 21:54" — no microseconds, no raw timezone suffixes.
/// If parsing fails, returns the first 16 characters as a fallback.
#[must_use]
pub fn format_timestamp(ts: &str) -> String {
    if let Ok(dt) = crate::turso::parse_utc_timestamp(ts) {
        dt.format("%b %-d, %H:%M").to_string()
    } else {
        if !TIMESTAMP_PARSE_WARNED.swap(true, std::sync::atomic::Ordering::Relaxed) {
            tracing::warn!(timestamp = %ts, "Failed to parse timestamp, falling back to truncated string");
        }
        crate::util::truncate_bytes(ts, 16).to_string()
    }
}

/// Extract the HH:MM:SS portion of an ISO 8601 timestamp, or the full
/// string when shorter than 20 chars. Char-boundary safe (mirrors
/// `format_timestamp`'s hardening); byte-identical to a raw `ts[11..19]`
/// slice for ASCII timestamps.
pub fn format_hhmmss(ts: &str) -> &str {
    if ts.len() > 19 {
        &ts[ts.floor_char_boundary(11)..ts.floor_char_boundary(19)]
    } else {
        ts
    }
}

/// Compact session-length format: raw below 1,000 ("500"), one decimal in the
/// k-range ("12.3k" for 12300), one decimal at/above one million ("1.2M") —
/// sessions routinely exceed 200K tokens with context windows up to 1M.
/// Values that would round up past a unit boundary are rendered in the next
/// unit (999_999 → "1.0M", never "1000.0k"). Shared by the Running Agents
/// card and the Sessions page session cards.
#[expect(clippy::cast_precision_loss)] // token counts are far below f64's 2^53 exact range
pub(crate) fn format_compact_tokens(tokens: u64) -> String {
    if tokens < 1_000 {
        tokens.to_string()
    } else if tokens < 999_950 {
        // k-range stops below values that would round to "1000.0k"
        // (999_950 / 1000 = 999.95).
        format!("{:.1}k", tokens as f64 / 1_000.0)
    } else if tokens < 999_950_000 {
        format!("{:.1}M", tokens as f64 / 1_000_000.0)
    } else {
        format!("{:.1}G", tokens as f64 / 1_000_000_000.0)
    }
}

// ── Workspace status colors ───────────────────────────────────────

#[must_use]
pub fn workspace_status_color(status: WorkspaceStatus) -> (Color, Color) {
    match status {
        WorkspaceStatus::Ready => (
            Color::from_rgb(0.133, 0.773, 0.369),
            Color::from_rgba(0.133, 0.773, 0.369, 0.1),
        ),
        WorkspaceStatus::Analyzing => (
            Color::from_rgb(0.851, 0.557, 0.0),
            Color::from_rgba(0.851, 0.557, 0.0, 0.1),
        ),
        WorkspaceStatus::Failed => (
            Color::from_rgb(0.957, 0.247, 0.369),
            Color::from_rgba(0.957, 0.247, 0.369, 0.1),
        ),
        WorkspaceStatus::Pending => (
            Color::from_rgb(0.631, 0.631, 0.631),
            Color::from_rgba(0.631, 0.631, 0.631, 0.1),
        ),
    }
}

// ── Animation timing constants ────────────────────────────────────

/// Log entry fade‑in duration (ms).
pub const ANIM_LOG_FADE_MS: u64 = 100;
/// Selected row background transition (ms).
pub const ANIM_SELECTED_MS: u64 = 150;

// ── Shared scrollbar helpers ─────────────────────────────────────

/// Returns a [`scrollable::Scrollbar`] with thin 6px dimensions for
/// both the rail and scroller widths. Used across all scrollable
/// widgets in the dashboard for a consistent appearance.
#[must_use]
pub fn thin_scrollbar() -> scrollable::Scrollbar {
    scrollable::Scrollbar::new().width(6).scroller_width(6)
}

/// Returns a vertical [`scrollable::Direction`] with the thin scrollbar.
///
/// Convenience wrapper around [`thin_scrollbar`] — prefer this over
/// spelling out `scrollable::Direction::Vertical(theme::thin_scrollbar())`
/// at every call site.
#[must_use]
pub fn vertical_scrollbar() -> scrollable::Direction {
    scrollable::Direction::Vertical(thin_scrollbar())
}

/// Returns a horizontal [`scrollable::Direction`] with the thin scrollbar.
///
/// Convenience wrapper around [`thin_scrollbar`] — prefer this over
/// spelling out `scrollable::Direction::Horizontal(theme::thin_scrollbar())`
/// at every call site.
#[must_use]
pub fn horizontal_scrollbar() -> scrollable::Direction {
    scrollable::Direction::Horizontal(thin_scrollbar())
}

/// Standard scrollbar style for the dark Flexoki theme.
///
/// Uses [`TEXT_PRIMARY`] as the scroller base color, varying opacity:
/// * Active: 0.4
/// * Hovered: 0.6
/// * Dragged: 0.8
///
/// The rail background is transparent, both rail and scroller borders
/// are rounded (2px radius). Other fields (`container`, `gap`,
/// `auto_scroll`) are inherited from [`scrollable::default`].
#[must_use]
pub fn scrollbar_style(theme: &iced::Theme, status: scrollable::Status) -> scrollable::Style {
    let base = scrollable::default(theme, status);

    let opacity = match status {
        scrollable::Status::Active { .. } => 0.4,
        scrollable::Status::Hovered { .. } => 0.6,
        scrollable::Status::Dragged { .. } => 0.8,
    };

    let rail = scrollable::Rail {
        background: None,
        border: border::rounded(2),
        scroller: scrollable::Scroller {
            background: Background::Color(TEXT_PRIMARY.scale_alpha(opacity)),
            border: border::rounded(2),
        },
    };

    scrollable::Style {
        vertical_rail: rail,
        horizontal_rail: rail,
        ..base
    }
}

// ── Button theme helpers ──────────────────────────────────────────

/// Transparent button with no background. Useful for icon-only buttons
/// embedded in bars, toolbars, and tab close buttons.
#[must_use]
pub fn button_transparent(
    _: &iced::Theme,
    _status: iced::widget::button::Status,
) -> iced::widget::button::Style {
    iced::widget::button::Style {
        background: None,
        ..Default::default()
    }
}

/// Transparent-background button style with a hover/press highlight. Shared
/// factory for the chat composer controls (role/mic, send button) and dropdown
/// menu items — the two differ only in highlight colors and corner radius.
#[must_use = "button style factory returns a style closure"]
fn transparent_button_style(
    hover: Color,
    pressed: Color,
    radius: f32,
    disabled: bool,
) -> impl Fn(&iced::Theme, iced::widget::button::Status) -> iced::widget::button::Style {
    move |_: &iced::Theme, status| {
        let bg = if disabled {
            Color::TRANSPARENT
        } else {
            match status {
                iced::widget::button::Status::Hovered => hover,
                iced::widget::button::Status::Pressed => pressed,
                _ => Color::TRANSPARENT,
            }
        };
        iced::widget::button::Style {
            background: Some(Background::Color(bg)),
            border: iced::Border {
                radius: radius.into(),
                width: 0.0,
                color: Color::TRANSPARENT,
            },
            ..Default::default()
        }
    }
}

/// Icon-only button with a subtle hover/press background (chat composer
/// role/mic controls, send button). Pass `disabled: true` to suppress the
/// highlight (used for the greyed send button on empty input).
#[must_use = "button style factory returns a style closure"]
pub fn icon_button_style(
    disabled: bool,
) -> impl Fn(&iced::Theme, iced::widget::button::Status) -> iced::widget::button::Style {
    transparent_button_style(HOVER_STRONG, ACCENT_DIM, 6.0, disabled)
}

/// Tab-button style for editor/shell tab bars: active tabs use [`BG_ELEVATED`],
/// hovered [`HOVER`], otherwise [`BG_SURFACE`], with a zero-radius border.
pub fn tab_button_style(
    is_active: bool,
) -> impl Fn(&iced::Theme, iced::widget::button::Status) -> iced::widget::button::Style {
    move |_: &iced::Theme, status| {
        let bg = if is_active {
            BG_ELEVATED
        } else if status == iced::widget::button::Status::Hovered {
            HOVER
        } else {
            BG_SURFACE
        };
        iced::widget::button::Style {
            background: Some(Background::Color(bg)),
            border: iced::Border {
                radius: 0.0.into(),
                width: 0.0,
                color: Color::TRANSPARENT,
            },
            ..Default::default()
        }
    }
}

/// Primary action button (Save, Submit, Confirm). Uses Flexoki accent green.
#[must_use]
pub fn button_primary(
    _: &iced::Theme,
    status: iced::widget::button::Status,
) -> iced::widget::button::Style {
    let base = match status {
        iced::widget::button::Status::Active => ACCENT,
        iced::widget::button::Status::Hovered => ACCENT_LIGHT,
        iced::widget::button::Status::Pressed => Color::from_rgb(0.165, 0.482, 0.455),
        iced::widget::button::Status::Disabled => TEXT_FAINT,
    };
    let text = match status {
        iced::widget::button::Status::Disabled => TEXT_MUTED,
        _ => BG_BASE,
    };
    iced::widget::button::Style {
        background: Some(iced::Background::Color(base)),
        text_color: text,
        border: iced::Border {
            radius: 4.0.into(),
            width: 0.0,
            color: Color::TRANSPARENT,
        },
        ..iced::widget::button::Style::default()
    }
}

/// Danger button (Delete, Purge, Clear). Uses Flexoki error red.
#[must_use]
pub fn button_danger(
    _: &iced::Theme,
    status: iced::widget::button::Status,
) -> iced::widget::button::Style {
    let base = match status {
        iced::widget::button::Status::Active => Color::from_rgba(1.0, 0.267, 0.4, 0.15),
        iced::widget::button::Status::Hovered => Color::from_rgba(1.0, 0.267, 0.4, 0.25),
        iced::widget::button::Status::Pressed => Color::from_rgba(1.0, 0.267, 0.4, 0.35),
        iced::widget::button::Status::Disabled => Color::TRANSPARENT,
    };
    let text = match status {
        iced::widget::button::Status::Disabled => TEXT_MUTED,
        _ => STATUS_ERROR,
    };
    iced::widget::button::Style {
        background: Some(iced::Background::Color(base)),
        text_color: text,
        border: iced::Border {
            radius: 4.0.into(),
            width: 1.0,
            color: Color::from_rgba(1.0, 0.267, 0.4, 0.2),
        },
        ..iced::widget::button::Style::default()
    }
}

/// Secondary/neutral button (Cancel, Close). Uses Flexoki surface tones.
#[must_use]
pub fn button_secondary(
    _: &iced::Theme,
    status: iced::widget::button::Status,
) -> iced::widget::button::Style {
    let bg = match status {
        iced::widget::button::Status::Active => HOVER,
        iced::widget::button::Status::Hovered => HOVER_STRONG,
        iced::widget::button::Status::Pressed => Color::from_rgba(0.808, 0.804, 0.765, 0.12),
        iced::widget::button::Status::Disabled => Color::TRANSPARENT,
    };
    let text = match status {
        iced::widget::button::Status::Disabled => TEXT_MUTED,
        _ => TEXT_PRIMARY,
    };
    iced::widget::button::Style {
        background: Some(iced::Background::Color(bg)),
        text_color: text,
        border: iced::Border {
            radius: 4.0.into(),
            width: 1.0,
            color: BORDER,
        },
        ..iced::widget::button::Style::default()
    }
}

/// Shared text-button factory: hover/pressed background plus an enabled
/// text color that differs between the normal and danger variants.
fn button_text_style(
    status: iced::widget::button::Status,
    enabled_color: Color,
) -> iced::widget::button::Style {
    let bg = match status {
        iced::widget::button::Status::Hovered => HOVER,
        iced::widget::button::Status::Pressed => HOVER_STRONG,
        _ => Color::TRANSPARENT,
    };
    let text = match status {
        iced::widget::button::Status::Disabled => TEXT_MUTED,
        _ => enabled_color,
    };
    iced::widget::button::Style {
        background: Some(iced::Background::Color(bg)),
        text_color: text,
        border: iced::Border {
            radius: 4.0.into(),
            width: 0.0,
            color: Color::TRANSPARENT,
        },
        ..iced::widget::button::Style::default()
    }
}

/// Text-only danger button (Cancel in modals, delete triggers). Like [`button_text`]
/// but with red text. No colored background, subtle hover highlight, red text only.
#[must_use]
pub fn button_text_danger(
    _theme: &iced::Theme,
    status: iced::widget::button::Status,
) -> iced::widget::button::Style {
    button_text_style(status, STATUS_ERROR)
}

/// Text-only button (sidebar nav items, inline actions). Minimal Flexoki styling.
#[must_use]
pub fn button_text(
    _theme: &iced::Theme,
    status: iced::widget::button::Status,
) -> iced::widget::button::Style {
    button_text_style(status, TEXT_PRIMARY)
}

// ── Container styles ─────────────────────────────────────────────

/// Shared container style factory: background fill plus border parameters.
/// Public wrappers below delegate here so the variants stay in one place.
/// Callers with a one-off combination may use this directly.
pub(crate) fn container_style(
    bg: Color,
    radius: f32,
    width: f32,
    border_color: Color,
) -> container::Style {
    container::Style {
        background: Some(Background::Color(bg)),
        border: iced::Border {
            radius: radius.into(),
            width,
            color: border_color,
        },
        ..container::Style::default()
    }
}

/// Style for tooltip containers: elevated background with subtle rounded
/// corners and a hairline border, matching the `dialog_container_style`
/// convention used for modal dialogs.
///
/// Applying this via `.style(theme::tooltip_style)` gives every tooltip a
/// dark/neutral fill that stays readable regardless of what content is
/// underneath it.
#[must_use]
pub fn tooltip_style(_theme: &iced::Theme) -> container::Style {
    container_style(BG_ELEVATED, 6.0, 1.0, BORDER_STRONG)
}

/// Style for chat message bubbles and the typing indicator.
///
/// Shared padding/radius/border across all bubbles.  Background is
/// parameterized (user vs agent messages); `text_color` is optional —
/// message bubbles set it to `TEXT_PRIMARY`, while the typing indicator
/// leaves it inherited (the inner `text()` widget sets its own color).
pub fn bubble_style(
    bg: Color,
    text_color: Option<Color>,
) -> impl Fn(&iced::Theme) -> container::Style {
    move |_theme: &iced::Theme| container::Style {
        background: Some(iced::Background::Color(bg)),
        text_color,
        border: iced::Border {
            radius: 8.0.into(),
            width: 0.0,
            color: iced::Color::TRANSPARENT,
        },
        ..container::Style::default()
    }
}

/// Style for bar containers (find/replace bar, go-to-line bar).
/// Flat elevated background with zero-radius border.
#[must_use]
pub fn container_bar(_theme: &iced::Theme) -> container::Style {
    container_style(BG_ELEVATED, 0.0, 0.0, Color::TRANSPARENT)
}

/// Style for surface cards: surface background with a 1px border and
/// 4px rounded corners. Used for ticket detail sections, comment cards,
/// log entries, and session transcript messages.
#[must_use]
pub fn surface_card_style(_theme: &iced::Theme) -> container::Style {
    container_style(BG_SURFACE, 4.0, 1.0, BORDER)
}

/// Style for elevated cards: elevated background with a 1px border and
/// 4px rounded corners. Used for ticket board cards and session round cards.
#[must_use]
pub fn elevated_card_style(_theme: &iced::Theme) -> container::Style {
    container_style(BG_ELEVATED, 4.0, 1.0, BORDER)
}

/// Style for modal dialog containers: elevated background, 8px rounded
/// corners, and a strong border. Shared by all modal overlays across the
/// dashboard (board detail, settings dialogs, editor overlays, diff/branch
/// modals, etc.).
#[must_use]
pub fn dialog_container_style(_theme: &iced::Theme) -> container::Style {
    container_style(BG_ELEVATED, 8.0, 1.0, BORDER_STRONG)
}

/// Style for the base page background: just the BG_BASE fill with no border.
/// Used as the outermost container on most pages (home, sessions, logs).
#[must_use]
pub fn base_container_style(_theme: &iced::Theme) -> container::Style {
    container_style(BG_BASE, 0.0, 0.0, Color::TRANSPARENT)
}

/// Style for surface-only containers: surface background with no border.
/// Used for sidebar panels, tab bars, and filter bars.
#[must_use]
pub fn surface_container_style(_theme: &iced::Theme) -> container::Style {
    container_style(BG_SURFACE, 0.0, 0.0, Color::TRANSPARENT)
}

/// Style for badge pills: a rounded container with the given background and
/// no border (canonical 4px radius). Used for log level tags, tool badges,
/// workspace status pills, and notes panels.
pub fn pill_style(bg: Color) -> impl Fn(&iced::Theme) -> container::Style {
    move |_theme: &iced::Theme| container_style(bg, 4.0, 0.0, Color::TRANSPARENT)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::Role;
    use strum::IntoEnumIterator;

    #[test]
    fn canonical_names_match() {
        for role in Role::iter() {
            let name = role.as_str();
            assert_eq!(
                role_badge_color_for(&role),
                role_badge_color(name),
                "role_badge_color_for and role_badge_color must agree for {name}"
            );
        }
    }

    #[test]
    fn derivative_analyst_names_get_correct_color() {
        let analyst_color = role_badge_color_for(&crate::Role::Analyst);
        assert_eq!(role_badge_color("analyst_1"), analyst_color);
        assert_eq!(role_badge_color("analyst_2"), analyst_color);
        assert_eq!(role_badge_color("analyst_3"), analyst_color);
    }

    #[test]
    fn derivative_other_role_names_get_correct_color() {
        for role in Role::iter() {
            let name = role.as_str();
            let expected = role_badge_color_for(&role);
            assert_eq!(
                role_badge_color(&format!("{name}_1")),
                expected,
                "derivative {name}_1 should match role_badge_color_for"
            );
            assert_eq!(
                role_badge_color(&format!("{name}_42")),
                expected,
                "derivative {name}_42 should match role_badge_color_for"
            );
        }
    }

    #[test]
    fn non_numeric_suffix_is_unknown() {
        assert_eq!(
            role_badge_color("analyst_final"),
            (TEXT_MUTED, badge_bg(TEXT_MUTED))
        );
        assert_eq!(
            role_badge_color("coder_abc"),
            (TEXT_MUTED, badge_bg(TEXT_MUTED))
        );
    }

    #[test]
    fn compact_token_format_edges() {
        // Raw below 1,000; one decimal in the k-range; one decimal at/above
        // 1M (sessions routinely exceed 200K with context windows up to 1M).
        // Values rounding up past a unit boundary render in the next unit.
        assert_eq!(format_compact_tokens(0), "0");
        assert_eq!(format_compact_tokens(500), "500");
        assert_eq!(format_compact_tokens(999), "999");
        assert_eq!(format_compact_tokens(1_000), "1.0k");
        assert_eq!(format_compact_tokens(12_300), "12.3k");
        assert_eq!(format_compact_tokens(200_000), "200.0k");
        assert_eq!(format_compact_tokens(999_949), "999.9k");
        assert_eq!(format_compact_tokens(999_999), "1.0M");
        assert_eq!(format_compact_tokens(1_000_000), "1.0M");
        assert_eq!(format_compact_tokens(1_234_567), "1.2M");
        assert_eq!(format_compact_tokens(999_949_999), "999.9M");
        assert_eq!(format_compact_tokens(999_950_000), "1.0G");
        assert_eq!(format_compact_tokens(1_000_000_000), "1.0G");
    }

    #[test]
    fn joint_comment_stage_roles_get_their_role_colors() {
        // The joint-verdict pipeline writes comments with the STAGE NAME as the
        // comment role ("Analysis"/"Review"/"QA") — they must render with the
        // corresponding role color, not the muted-grey fallback.
        assert_eq!(
            role_badge_color("Analysis"),
            role_badge_color_for(&crate::Role::Analyst)
        );
        assert_eq!(
            role_badge_color("Review"),
            role_badge_color_for(&crate::Role::Reviewer)
        );
        assert_eq!(
            role_badge_color("QA"),
            role_badge_color_for(&crate::Role::Qa)
        );
    }

    #[test]
    fn llm_api_roles_are_unknown() {
        assert_eq!(role_badge_color("user"), (TEXT_MUTED, badge_bg(TEXT_MUTED)));
        assert_eq!(
            role_badge_color("system"),
            (TEXT_MUTED, badge_bg(TEXT_MUTED))
        );
        assert_eq!(role_badge_color("tool"), (TEXT_MUTED, badge_bg(TEXT_MUTED)));
    }

    #[test]
    fn empty_and_garbage_are_unknown() {
        assert_eq!(role_badge_color(""), (TEXT_MUTED, badge_bg(TEXT_MUTED)));
        assert_eq!(
            role_badge_color("garbage"),
            (TEXT_MUTED, badge_bg(TEXT_MUTED))
        );
        assert_eq!(
            role_badge_color("unknown_role"),
            (TEXT_MUTED, badge_bg(TEXT_MUTED))
        );
        assert_eq!(role_badge_color("_1"), (TEXT_MUTED, badge_bg(TEXT_MUTED)));
    }

    #[test]
    fn case_insensitive_parse() {
        let analyst_color = role_badge_color_for(&crate::Role::Analyst);
        assert_eq!(role_badge_color("ANALYST"), analyst_color);
        assert_eq!(role_badge_color("Analyst"), analyst_color);
        assert_eq!(role_badge_color("ANALYST_1"), analyst_color);
    }

    /// The badge tuple's background member must always be the foreground at
    /// 0.1 alpha ([`badge_bg`]). The role pill (`pill_style(colors.1)`) and the
    /// logs span consume the second member directly, so any divergence — e.g.
    /// an independent constant like the old `(TEXT_MUTED, HOVER)` fallback —
    /// would silently change the rendered fallback background.
    #[test]
    fn badge_background_is_alpha_scaled_foreground() {
        for role in Role::iter() {
            let (fg, bg) = role_badge_color_for(&role);
            assert_eq!(bg, badge_bg(fg), "canonical role {role:?}");
        }
        // Unknown/fallback strings, LLM API roles, and stage-name comment roles
        // all flow through the same tuple invariant.
        for name in [
            "analyst_final",
            "coder_abc",
            "user",
            "assistant",
            "system",
            "tool",
            "Analysis",
            "Review",
            "QA",
            "analyst_3",
            "",
            "garbage",
            "unknown_role",
            "_1",
        ] {
            let (fg, bg) = role_badge_color(name);
            assert_eq!(bg, badge_bg(fg), "fallback/stage/derivative role {name:?}");
        }
    }

    /// Locks the pre-consolidation values of the container factories and
    /// the button_text/button_text_danger pair so parameterization cannot
    /// silently change any rendered style.
    #[test]
    fn style_factory_values_unchanged() {
        let theme = iced::Theme::Dark;
        let bg = |c: iced::Color| Some(iced::Background::Color(c));
        let border = |r: f32, w: f32, c: iced::Color| iced::Border {
            radius: r.into(),
            width: w,
            color: c,
        };

        assert_eq!(
            tooltip_style(&theme),
            iced::widget::container::Style {
                background: bg(BG_ELEVATED),
                border: border(6.0, 1.0, BORDER_STRONG),
                ..iced::widget::container::Style::default()
            }
        );
        assert_eq!(
            container_bar(&theme),
            iced::widget::container::Style {
                background: bg(BG_ELEVATED),
                border: border(0.0, 0.0, iced::Color::TRANSPARENT),
                ..iced::widget::container::Style::default()
            }
        );
        assert_eq!(
            surface_card_style(&theme),
            iced::widget::container::Style {
                background: bg(BG_SURFACE),
                border: border(4.0, 1.0, BORDER),
                ..iced::widget::container::Style::default()
            }
        );
        assert_eq!(
            elevated_card_style(&theme),
            iced::widget::container::Style {
                background: bg(BG_ELEVATED),
                border: border(4.0, 1.0, BORDER),
                ..iced::widget::container::Style::default()
            }
        );
        assert_eq!(
            pill_style(BG_ELEVATED)(&theme),
            iced::widget::container::Style {
                background: bg(BG_ELEVATED),
                border: border(4.0, 0.0, iced::Color::TRANSPARENT),
                ..iced::widget::container::Style::default()
            }
        );
        assert_eq!(
            dialog_container_style(&theme),
            iced::widget::container::Style {
                background: bg(BG_ELEVATED),
                border: border(8.0, 1.0, BORDER_STRONG),
                ..iced::widget::container::Style::default()
            }
        );
        assert_eq!(
            base_container_style(&theme),
            iced::widget::container::Style {
                background: bg(BG_BASE),
                ..iced::widget::container::Style::default()
            }
        );
        assert_eq!(
            surface_container_style(&theme),
            iced::widget::container::Style {
                background: bg(BG_SURFACE),
                ..iced::widget::container::Style::default()
            }
        );

        for status in [
            iced::widget::button::Status::Active,
            iced::widget::button::Status::Hovered,
            iced::widget::button::Status::Pressed,
            iced::widget::button::Status::Disabled,
        ] {
            let expected = |enabled: iced::Color| iced::widget::button::Style {
                background: bg(match status {
                    iced::widget::button::Status::Hovered => HOVER,
                    iced::widget::button::Status::Pressed => HOVER_STRONG,
                    _ => iced::Color::TRANSPARENT,
                }),
                text_color: if status == iced::widget::button::Status::Disabled {
                    TEXT_MUTED
                } else {
                    enabled
                },
                border: border(4.0, 0.0, iced::Color::TRANSPARENT),
                ..iced::widget::button::Style::default()
            };
            assert_eq!(button_text(&theme, status), expected(TEXT_PRIMARY));
            assert_eq!(button_text_danger(&theme, status), expected(STATUS_ERROR));
        }
    }
}