work-tuimer 0.6.0

Simple, keyboard-driven TUI for time-tracking that allows you to quickly add time blocks and automatically group time if same task was done in different sessions
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
use anyhow::{Context, Result};
use ratatui::style::Color;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;

/// Configuration for issue tracker integrations (JIRA, Linear, GitHub, etc.)
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Config {
    #[serde(default)]
    pub integrations: IntegrationConfig,

    #[serde(default)]
    pub theme: ThemeConfig,

    #[serde(default)]
    pub columns: ColumnVisibilityConfig,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct IntegrationConfig {
    /// Default tracker name when auto-detection is ambiguous
    #[serde(default)]
    pub default_tracker: Option<String>,

    /// Map of tracker name to tracker configuration
    #[serde(default)]
    pub trackers: HashMap<String, TrackerConfig>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct TrackerConfig {
    #[serde(default)]
    pub enabled: bool,
    #[serde(default)]
    pub base_url: String,
    /// Regex patterns to match ticket IDs for this tracker
    #[serde(default)]
    pub ticket_patterns: Vec<String>,
    /// URL template for browsing tickets: {base_url}, {ticket}
    #[serde(default)]
    pub browse_url: String,
    /// URL template for worklog page: {base_url}, {ticket}
    #[serde(default)]
    pub worklog_url: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ColumnVisibilityConfig {
    #[serde(default = "default_show_project")]
    pub project: bool,
    #[serde(default = "default_show_customer")]
    pub customer: bool,
    #[serde(default = "default_show_description")]
    pub description: bool,
}

fn default_show_project() -> bool {
    true
}

fn default_show_customer() -> bool {
    false
}

fn default_show_description() -> bool {
    true
}

impl Default for ColumnVisibilityConfig {
    fn default() -> Self {
        Self {
            project: default_show_project(),
            customer: default_show_customer(),
            description: default_show_description(),
        }
    }
}

impl Config {
    /// Load config from file, or return defaults if file doesn't exist
    pub fn load() -> Result<Self> {
        let config_path = Self::get_config_path();

        if config_path.exists() {
            let contents = fs::read_to_string(&config_path)
                .context(format!("Failed to read config file: {:?}", config_path))?;
            let config: Config =
                toml::from_str(&contents).context("Failed to parse config TOML")?;
            Ok(config)
        } else {
            Ok(Config::default())
        }
    }

    /// Get config file path (~/.config/work-tuimer/config.toml)
    /// Respects XDG_CONFIG_HOME environment variable on Unix systems
    fn get_config_path() -> PathBuf {
        // On Unix systems (Linux/macOS), respect XDG_CONFIG_HOME
        #[cfg(unix)]
        {
            if let Ok(xdg_config) = std::env::var("XDG_CONFIG_HOME") {
                return PathBuf::from(xdg_config)
                    .join("work-tuimer")
                    .join("config.toml");
            }
            // Fall back to ~/.config if XDG_CONFIG_HOME is not set
            if let Some(home) = std::env::var_os("HOME") {
                return PathBuf::from(home)
                    .join(".config")
                    .join("work-tuimer")
                    .join("config.toml");
            }
        }

        // On Windows, use dirs::config_dir() which returns AppData/Roaming
        #[cfg(windows)]
        {
            if let Some(config_dir) = dirs::config_dir() {
                return config_dir.join("work-tuimer").join("config.toml");
            }
        }

        // Final fallback for any platform
        PathBuf::from("./config.toml")
    }

    /// Check if any tracker integration is properly configured
    pub fn has_integrations(&self) -> bool {
        self.integrations
            .trackers
            .values()
            .any(|tracker| tracker.enabled && !tracker.base_url.is_empty())
    }

    /// Get the active theme (either pre-defined or custom)
    pub fn get_theme(&self) -> Theme {
        self.theme.get_active_theme()
    }
}

/// Theme configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ThemeConfig {
    /// Name of active theme: "default", "kanagawa", "catppuccin", "gruvbox", "monokai", "dracula", "everforest", "terminal"
    #[serde(default = "default_theme_name")]
    pub active: String,

    /// Custom theme definitions
    #[serde(default)]
    pub custom: HashMap<String, CustomThemeColors>,
}

fn default_theme_name() -> String {
    "default".to_string()
}

impl Default for ThemeConfig {
    fn default() -> Self {
        Self {
            active: default_theme_name(),
            custom: HashMap::new(),
        }
    }
}

impl ThemeConfig {
    /// Get the active theme based on config
    pub fn get_active_theme(&self) -> Theme {
        // Check custom themes first (allows overriding predefined themes)
        if let Some(custom_colors) = self.custom.get(&self.active) {
            return Theme::from_custom(custom_colors);
        }

        // Then check if it's a pre-defined theme
        match self.active.as_str() {
            "default" => Theme::default_theme(),
            "kanagawa" => Theme::kanagawa(),
            "catppuccin" => Theme::catppuccin(),
            "gruvbox" => Theme::gruvbox(),
            "monokai" => Theme::monokai(),
            "dracula" => Theme::dracula(),
            "everforest" => Theme::everforest(),
            "terminal" => Theme::terminal(),
            _ => {
                // Fallback to default if theme not found
                Theme::default_theme()
            }
        }
    }
}

/// Custom theme color definitions (supports hex colors, RGB tuples, and named colors)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CustomThemeColors {
    // Border colors
    pub active_border: String,
    pub inactive_border: String,
    pub searching_border: String,

    // Background colors
    pub selected_bg: String,
    pub selected_inactive_bg: String,
    pub visual_bg: String,
    pub timer_active_bg: String,
    pub row_alternate_bg: String,
    pub edit_bg: String,
    pub focus_bg: String,

    // Text colors
    pub primary_text: String,
    pub secondary_text: String,
    pub highlight_text: String,

    // Status colors
    pub success: String,
    pub warning: String,
    pub error: String,
    pub info: String,

    // Specific element colors
    pub timer_text: String,
    pub badge: String,
}

/// Theme with semantic color names (resolved to ratatui Colors)
#[derive(Debug, Clone)]
pub struct Theme {
    // Border colors
    pub active_border: Color,
    #[allow(dead_code)]
    pub inactive_border: Color,
    #[allow(dead_code)]
    pub searching_border: Color,

    // Background colors
    pub selected_bg: Color,
    #[allow(dead_code)]
    pub selected_inactive_bg: Color,
    pub visual_bg: Color,
    pub timer_active_bg: Color,
    pub row_alternate_bg: Color,
    pub edit_bg: Color,
    pub focus_bg: Color,

    // Text colors
    pub primary_text: Color,
    pub secondary_text: Color,
    pub highlight_text: Color,

    // Status colors
    pub success: Color,
    pub warning: Color,
    pub error: Color,
    pub info: Color,

    // Specific element colors
    pub timer_text: Color,
    pub badge: Color,
}

impl Theme {
    /// Default theme (current color scheme)
    pub fn default_theme() -> Self {
        Self {
            active_border: Color::Cyan,
            inactive_border: Color::DarkGray,
            searching_border: Color::Yellow,
            selected_bg: Color::Rgb(40, 40, 60),
            selected_inactive_bg: Color::Rgb(30, 30, 45),
            visual_bg: Color::Rgb(70, 130, 180),
            timer_active_bg: Color::Rgb(34, 139, 34),
            row_alternate_bg: Color::Rgb(25, 25, 35),
            edit_bg: Color::Rgb(22, 78, 99),
            focus_bg: Color::Rgb(80, 60, 120), // purple focus indicator
            primary_text: Color::White,
            secondary_text: Color::Gray,
            highlight_text: Color::Cyan,
            success: Color::Green,
            warning: Color::Yellow,
            error: Color::LightRed,
            info: Color::Cyan,
            timer_text: Color::Yellow,
            badge: Color::LightMagenta,
        }
    }

    /// Kanagawa theme (dark navy blue aesthetic inspired by Hokusai's painting)
    pub fn kanagawa() -> Self {
        Self {
            active_border: Color::Rgb(127, 180, 202), // springBlue (brighter)
            inactive_border: Color::Rgb(54, 59, 77),  // sumiInk4
            searching_border: Color::Rgb(255, 160, 102), // surimiOrange
            selected_bg: Color::Rgb(42, 42, 55),      // sumiInk3 (slightly lighter for distinction)
            selected_inactive_bg: Color::Rgb(31, 31, 40), // sumiInk1/2
            visual_bg: Color::Rgb(45, 79, 103),       // waveBlue2 (darker for distinction)
            timer_active_bg: Color::Rgb(152, 187, 106), // springGreen (brighter)
            row_alternate_bg: Color::Rgb(22, 25, 32), // sumiInk1 (darker)
            edit_bg: Color::Rgb(106, 149, 137),       // waveAqua1 (distinct teal, not blue!)
            focus_bg: Color::Rgb(149, 127, 184),      // oniViolet (purple focus indicator)
            primary_text: Color::Rgb(220, 215, 186),  // fujiWhite
            secondary_text: Color::Rgb(114, 118, 129), // fujiGray (comments)
            highlight_text: Color::Rgb(230, 195, 132), // carpYellow (warmer highlight)
            success: Color::Rgb(152, 187, 106),       // springGreen
            warning: Color::Rgb(255, 160, 102),       // surimiOrange
            error: Color::Rgb(255, 93, 98),           // peachRed (brighter)
            info: Color::Rgb(127, 180, 202),          // springBlue
            timer_text: Color::Rgb(230, 195, 132),    // carpYellow
            badge: Color::Rgb(149, 127, 184),         // oniViolet
        }
    }

    /// Catppuccin Mocha theme (popular pastel theme)
    pub fn catppuccin() -> Self {
        Self {
            active_border: Color::Rgb(137, 180, 250),     // blue
            inactive_border: Color::Rgb(69, 71, 90),      // surface1
            searching_border: Color::Rgb(249, 226, 175),  // yellow
            selected_bg: Color::Rgb(49, 50, 68),          // surface0
            selected_inactive_bg: Color::Rgb(30, 30, 46), // base
            visual_bg: Color::Rgb(116, 199, 236),         // sapphire
            timer_active_bg: Color::Rgb(166, 227, 161),   // green
            row_alternate_bg: Color::Rgb(24, 24, 37),     // mantle
            edit_bg: Color::Rgb(137, 180, 250),           // blue (dimmed)
            focus_bg: Color::Rgb(203, 166, 247),          // mauve (purple focus indicator)
            primary_text: Color::Rgb(205, 214, 244),      // text
            secondary_text: Color::Rgb(127, 132, 156),    // overlay0
            highlight_text: Color::Rgb(137, 180, 250),    // blue
            success: Color::Rgb(166, 227, 161),           // green
            warning: Color::Rgb(249, 226, 175),           // yellow
            error: Color::Rgb(243, 139, 168),             // red
            info: Color::Rgb(137, 180, 250),              // blue
            timer_text: Color::Rgb(245, 194, 231),        // pink
            badge: Color::Rgb(203, 166, 247),             // mauve
        }
    }

    /// Gruvbox theme (retro warm colors)
    pub fn gruvbox() -> Self {
        Self {
            active_border: Color::Rgb(131, 165, 152),     // aqua
            inactive_border: Color::Rgb(60, 56, 54),      // bg2
            searching_border: Color::Rgb(250, 189, 47),   // yellow
            selected_bg: Color::Rgb(60, 56, 54),          // bg2
            selected_inactive_bg: Color::Rgb(40, 40, 40), // bg0
            visual_bg: Color::Rgb(69, 133, 136),          // blue
            timer_active_bg: Color::Rgb(152, 151, 26),    // green
            row_alternate_bg: Color::Rgb(29, 32, 33),     // bg0_h
            edit_bg: Color::Rgb(80, 73, 69),              // bg3
            focus_bg: Color::Rgb(211, 134, 155),          // purple (warm purple focus)
            primary_text: Color::Rgb(235, 219, 178),      // fg
            secondary_text: Color::Rgb(146, 131, 116),    // fg4
            highlight_text: Color::Rgb(131, 165, 152),    // aqua
            success: Color::Rgb(184, 187, 38),            // green
            warning: Color::Rgb(250, 189, 47),            // yellow
            error: Color::Rgb(251, 73, 52),               // red
            info: Color::Rgb(131, 165, 152),              // aqua
            timer_text: Color::Rgb(254, 128, 25),         // orange
            badge: Color::Rgb(211, 134, 155),             // purple
        }
    }

    /// Monokai theme (classic vibrant)
    pub fn monokai() -> Self {
        Self {
            active_border: Color::Rgb(102, 217, 239), // cyan (vibrant)
            inactive_border: Color::Rgb(73, 72, 62),  // darker gray
            searching_border: Color::Rgb(230, 219, 116), // yellow
            selected_bg: Color::Rgb(73, 72, 62),      // dark gray
            selected_inactive_bg: Color::Rgb(39, 40, 34), // background
            visual_bg: Color::Rgb(249, 38, 114),      // magenta (distinct!)
            timer_active_bg: Color::Rgb(166, 226, 46), // vibrant green
            row_alternate_bg: Color::Rgb(30, 31, 28), // darker bg
            edit_bg: Color::Rgb(100, 85, 60),         // warm brown (distinct from selected_bg!)
            focus_bg: Color::Rgb(174, 129, 255),      // purple (vibrant purple focus)
            primary_text: Color::Rgb(248, 248, 242),  // foreground
            secondary_text: Color::Rgb(117, 113, 94), // comment gray
            highlight_text: Color::Rgb(253, 151, 31), // bright orange (not cyan!)
            success: Color::Rgb(166, 226, 46),        // vibrant green
            warning: Color::Rgb(230, 219, 116),       // yellow
            error: Color::Rgb(249, 38, 114),          // magenta/pink
            info: Color::Rgb(102, 217, 239),          // cyan
            timer_text: Color::Rgb(253, 151, 31),     // orange
            badge: Color::Rgb(174, 129, 255),         // purple
        }
    }

    /// Dracula theme (purple/pink accents)
    pub fn dracula() -> Self {
        Self {
            active_border: Color::Rgb(139, 233, 253),     // cyan
            inactive_border: Color::Rgb(68, 71, 90),      // current line
            searching_border: Color::Rgb(241, 250, 140),  // yellow
            selected_bg: Color::Rgb(68, 71, 90),          // current line
            selected_inactive_bg: Color::Rgb(40, 42, 54), // background
            visual_bg: Color::Rgb(139, 233, 253),         // cyan (distinct from purple)
            timer_active_bg: Color::Rgb(80, 250, 123),    // green
            row_alternate_bg: Color::Rgb(30, 31, 40),     // darker than bg
            edit_bg: Color::Rgb(98, 114, 164),            // purple (distinct from selected_bg!)
            focus_bg: Color::Rgb(189, 147, 249),          // bright purple (focus indicator)
            primary_text: Color::Rgb(248, 248, 242),      // foreground
            secondary_text: Color::Rgb(98, 114, 164),     // purple/comment
            highlight_text: Color::Rgb(189, 147, 249),    // bright purple
            success: Color::Rgb(80, 250, 123),            // green
            warning: Color::Rgb(241, 250, 140),           // yellow
            error: Color::Rgb(255, 85, 85),               // red
            info: Color::Rgb(139, 233, 253),              // cyan
            timer_text: Color::Rgb(255, 184, 108),        // orange
            badge: Color::Rgb(255, 121, 198),             // pink
        }
    }

    /// Everforest theme (green forest aesthetic)
    pub fn everforest() -> Self {
        Self {
            active_border: Color::Rgb(131, 192, 146),     // green
            inactive_border: Color::Rgb(83, 86, 77),      // bg3
            searching_border: Color::Rgb(219, 188, 127),  // yellow
            selected_bg: Color::Rgb(67, 72, 60),          // bg2
            selected_inactive_bg: Color::Rgb(45, 49, 41), // bg1
            visual_bg: Color::Rgb(123, 175, 153),         // aqua
            timer_active_bg: Color::Rgb(131, 192, 146),   // green
            row_alternate_bg: Color::Rgb(35, 38, 32),     // bg0
            edit_bg: Color::Rgb(83, 86, 77),              // bg3
            focus_bg: Color::Rgb(217, 143, 172),          // purple (soft purple focus)
            primary_text: Color::Rgb(211, 198, 170),      // fg
            secondary_text: Color::Rgb(146, 142, 123),    // gray1
            highlight_text: Color::Rgb(123, 175, 153),    // aqua
            success: Color::Rgb(131, 192, 146),           // green
            warning: Color::Rgb(219, 188, 127),           // yellow
            error: Color::Rgb(230, 126, 128),             // red
            info: Color::Rgb(123, 175, 153),              // aqua
            timer_text: Color::Rgb(230, 152, 117),        // orange
            badge: Color::Rgb(217, 143, 172),             // purple
        }
    }

    /// Terminal theme (uses terminal's native colors)
    pub fn terminal() -> Self {
        Self {
            active_border: Color::Cyan,
            inactive_border: Color::Reset,
            searching_border: Color::Yellow,
            selected_bg: Color::Blue,
            selected_inactive_bg: Color::Reset,
            visual_bg: Color::Blue,
            timer_active_bg: Color::Green,
            row_alternate_bg: Color::Reset,
            edit_bg: Color::Cyan,
            focus_bg: Color::Magenta, // magenta/purple focus
            primary_text: Color::Reset,
            secondary_text: Color::DarkGray,
            highlight_text: Color::Cyan,
            success: Color::Green,
            warning: Color::Yellow,
            error: Color::Red,
            info: Color::Cyan,
            timer_text: Color::Yellow,
            badge: Color::Magenta,
        }
    }

    /// Create theme from custom color definitions
    pub fn from_custom(colors: &CustomThemeColors) -> Self {
        Self {
            active_border: parse_color(&colors.active_border),
            inactive_border: parse_color(&colors.inactive_border),
            searching_border: parse_color(&colors.searching_border),
            selected_bg: parse_color(&colors.selected_bg),
            selected_inactive_bg: parse_color(&colors.selected_inactive_bg),
            visual_bg: parse_color(&colors.visual_bg),
            timer_active_bg: parse_color(&colors.timer_active_bg),
            row_alternate_bg: parse_color(&colors.row_alternate_bg),
            edit_bg: parse_color(&colors.edit_bg),
            focus_bg: parse_color(&colors.focus_bg),
            primary_text: parse_color(&colors.primary_text),
            secondary_text: parse_color(&colors.secondary_text),
            highlight_text: parse_color(&colors.highlight_text),
            success: parse_color(&colors.success),
            warning: parse_color(&colors.warning),
            error: parse_color(&colors.error),
            info: parse_color(&colors.info),
            timer_text: parse_color(&colors.timer_text),
            badge: parse_color(&colors.badge),
        }
    }
}

/// Parse color string (supports hex, RGB tuples, and named colors)
fn parse_color(color_str: &str) -> Color {
    let trimmed = color_str.trim();

    // Handle hex colors (#RRGGBB or #RGB)
    if let Some(hex) = trimmed.strip_prefix('#') {
        if hex.len() == 6 {
            if let (Ok(r), Ok(g), Ok(b)) = (
                u8::from_str_radix(&hex[0..2], 16),
                u8::from_str_radix(&hex[2..4], 16),
                u8::from_str_radix(&hex[4..6], 16),
            ) {
                return Color::Rgb(r, g, b);
            }
        } else if hex.len() == 3 {
            // Short hex format #RGB -> #RRGGBB
            if let (Ok(r), Ok(g), Ok(b)) = (
                u8::from_str_radix(&hex[0..1].repeat(2), 16),
                u8::from_str_radix(&hex[1..2].repeat(2), 16),
                u8::from_str_radix(&hex[2..3].repeat(2), 16),
            ) {
                return Color::Rgb(r, g, b);
            }
        }
    }

    // Handle named colors
    match trimmed.to_lowercase().as_str() {
        "reset" | "terminal" | "default" => Color::Reset,
        "black" => Color::Black,
        "red" => Color::Red,
        "green" => Color::Green,
        "yellow" => Color::Yellow,
        "blue" => Color::Blue,
        "magenta" => Color::Magenta,
        "cyan" => Color::Cyan,
        "gray" | "grey" => Color::Gray,
        "darkgray" | "darkgrey" => Color::DarkGray,
        "lightred" => Color::LightRed,
        "lightgreen" => Color::LightGreen,
        "lightyellow" => Color::LightYellow,
        "lightblue" => Color::LightBlue,
        "lightmagenta" => Color::LightMagenta,
        "lightcyan" => Color::LightCyan,
        "white" => Color::White,
        _ => {
            // Fallback: try to parse as RGB tuple "r,g,b" or "(r, g, b)"
            // Strip parentheses if present
            let rgb_str = trimmed.trim_start_matches('(').trim_end_matches(')').trim();
            let parts: Vec<&str> = rgb_str.split(',').map(|s| s.trim()).collect();
            if parts.len() == 3
                && let (Ok(r), Ok(g), Ok(b)) = (
                    parts[0].parse::<u8>(),
                    parts[1].parse::<u8>(),
                    parts[2].parse::<u8>(),
                )
            {
                return Color::Rgb(r, g, b);
            }
            // Final fallback: return default white
            Color::White
        }
    }
}

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

    #[test]
    fn test_default_config() {
        let config = Config::default();
        assert_eq!(config.integrations.default_tracker, None);
        assert!(config.integrations.trackers.is_empty());
        assert!(config.columns.project);
        assert!(!config.columns.customer);
        assert!(config.columns.description);
    }

    #[test]
    fn test_config_serialization() {
        let config = Config::default();
        let toml_str = toml::to_string_pretty(&config).expect("Failed to serialize");
        assert!(toml_str.contains("integrations"));
        assert!(toml_str.contains("columns"));
    }

    #[test]
    fn test_config_deserialization() {
        let toml_str = r#"
[integrations]
default_tracker = "my-jira"

[integrations.trackers.my-jira]
enabled = true
base_url = "https://test.atlassian.net"
ticket_patterns = ["^PROJ-\\d+$"]
browse_url = "{base_url}/browse/{ticket}"
worklog_url = "{base_url}/browse/{ticket}?focusedWorklogId=-1"
        "#;

        let config: Config = toml::from_str(toml_str).expect("Failed to deserialize");
        assert_eq!(
            config.integrations.default_tracker,
            Some("my-jira".to_string())
        );
        let tracker = config.integrations.trackers.get("my-jira").unwrap();
        assert_eq!(tracker.base_url, "https://test.atlassian.net");
        assert_eq!(tracker.ticket_patterns[0], "^PROJ-\\d+$");
        assert!(config.columns.project);
        assert!(!config.columns.customer);
        assert!(config.columns.description);
    }

    #[test]
    fn test_columns_config_deserialization() {
        let toml_str = r#"
[columns]
project = false
customer = true
description = false
        "#;

        let config: Config = toml::from_str(toml_str).expect("Failed to deserialize");
        assert!(!config.columns.project);
        assert!(config.columns.customer);
        assert!(!config.columns.description);
    }

    #[test]
    fn test_columns_config_partial_defaults() {
        let toml_str = r#"
[columns]
customer = true
        "#;

        let config: Config = toml::from_str(toml_str).expect("Failed to deserialize");
        assert!(config.columns.project);
        assert!(config.columns.customer);
        assert!(config.columns.description);
    }

    #[test]
    fn test_tracker_config_defaults() {
        let tracker = TrackerConfig::default();
        assert!(!tracker.enabled);
        assert!(tracker.base_url.is_empty());
        assert!(tracker.ticket_patterns.is_empty());
    }

    // Theme-related tests

    #[test]
    fn test_default_theme_config() {
        let theme_config = ThemeConfig::default();
        assert_eq!(theme_config.active, "default");
        assert!(theme_config.custom.is_empty());
    }

    #[test]
    fn test_theme_config_get_default_theme() {
        let theme_config = ThemeConfig::default();
        let theme = theme_config.get_active_theme();
        // Verify it returns a valid theme (checking a few fields)
        assert!(matches!(theme.active_border, Color::Cyan));
        assert!(matches!(theme.error, Color::LightRed));
    }

    #[test]
    fn test_theme_config_get_kanagawa_theme() {
        let theme_config = ThemeConfig {
            active: "kanagawa".to_string(),
            custom: HashMap::new(),
        };
        let theme = theme_config.get_active_theme();
        // Verify it returns kanagawa theme (check one specific color)
        assert!(matches!(theme.active_border, Color::Rgb(127, 180, 202)));
    }

    #[test]
    fn test_theme_config_get_all_predefined_themes() {
        let theme_names = vec![
            "default",
            "kanagawa",
            "catppuccin",
            "gruvbox",
            "monokai",
            "dracula",
            "everforest",
            "terminal",
        ];

        for name in theme_names {
            let theme_config = ThemeConfig {
                active: name.to_string(),
                custom: HashMap::new(),
            };
            let _theme = theme_config.get_active_theme();
            // Just verify it doesn't panic and returns a theme
        }
    }

    #[test]
    fn test_parse_color_hex_6_digit() {
        let color = parse_color("#7e9cd8");
        assert!(matches!(color, Color::Rgb(126, 156, 216)));

        let color = parse_color("#FF0000");
        assert!(matches!(color, Color::Rgb(255, 0, 0)));
    }

    #[test]
    fn test_parse_color_hex_3_digit() {
        let color = parse_color("#F00");
        assert!(matches!(color, Color::Rgb(255, 0, 0)));

        let color = parse_color("#0F0");
        assert!(matches!(color, Color::Rgb(0, 255, 0)));

        let color = parse_color("#00F");
        assert!(matches!(color, Color::Rgb(0, 0, 255)));
    }

    #[test]
    fn test_parse_color_rgb_tuple() {
        let color = parse_color("255, 128, 64");
        assert!(matches!(color, Color::Rgb(255, 128, 64)));

        let color = parse_color("0,255,0");
        assert!(matches!(color, Color::Rgb(0, 255, 0)));

        let color = parse_color(" 100 , 200 , 150 ");
        assert!(matches!(color, Color::Rgb(100, 200, 150)));
    }

    #[test]
    fn test_parse_color_named_colors() {
        assert!(matches!(parse_color("red"), Color::Red));
        assert!(matches!(parse_color("Red"), Color::Red));
        assert!(matches!(parse_color("RED"), Color::Red));
        assert!(matches!(parse_color("green"), Color::Green));
        assert!(matches!(parse_color("blue"), Color::Blue));
        assert!(matches!(parse_color("yellow"), Color::Yellow));
        assert!(matches!(parse_color("cyan"), Color::Cyan));
        assert!(matches!(parse_color("magenta"), Color::Magenta));
        assert!(matches!(parse_color("white"), Color::White));
        assert!(matches!(parse_color("black"), Color::Black));
        assert!(matches!(parse_color("gray"), Color::Gray));
        assert!(matches!(parse_color("grey"), Color::Gray));
        assert!(matches!(parse_color("darkgray"), Color::DarkGray));
        assert!(matches!(parse_color("lightred"), Color::LightRed));
        assert!(matches!(parse_color("lightgreen"), Color::LightGreen));
        assert!(matches!(parse_color("lightyellow"), Color::LightYellow));
        assert!(matches!(parse_color("lightblue"), Color::LightBlue));
        assert!(matches!(parse_color("lightmagenta"), Color::LightMagenta));
        assert!(matches!(parse_color("lightcyan"), Color::LightCyan));
        assert!(matches!(parse_color("reset"), Color::Reset));
        assert!(matches!(parse_color("terminal"), Color::Reset));
        assert!(matches!(parse_color("default"), Color::Reset));
    }

    #[test]
    fn test_parse_color_invalid_fallback() {
        // Invalid formats should fallback to white
        let color = parse_color("invalid_color");
        assert!(matches!(color, Color::White));

        let color = parse_color("#ZZZ");
        assert!(matches!(color, Color::White));

        let color = parse_color("300, 300, 300"); // Out of range
        assert!(matches!(color, Color::White));
    }

    #[test]
    fn test_custom_theme_deserialization() {
        let toml_str = r##"
[theme]
active = "my-custom"

[theme.custom.my-custom]
active_border = "#7e9cd8"
inactive_border = "darkgray"
searching_border = "255, 160, 102"
selected_bg = "#252b37"
selected_inactive_bg = "#1e222c"
visual_bg = "70, 130, 180"
timer_active_bg = "green"
row_alternate_bg = "#16191f"
edit_bg = "#223d50"
focus_bg = "magenta"
primary_text = "white"
secondary_text = "gray"
highlight_text = "cyan"
success = "lightgreen"
warning = "yellow"
error = "lightred"
info = "cyan"
timer_text = "#ffc777"
badge = "lightmagenta"
        "##;

        let config: Config = toml::from_str(toml_str).expect("Failed to deserialize");
        assert_eq!(config.theme.active, "my-custom");
        assert!(config.theme.custom.contains_key("my-custom"));

        let custom_colors = config.theme.custom.get("my-custom").unwrap();
        assert_eq!(custom_colors.active_border, "#7e9cd8");
        assert_eq!(custom_colors.timer_active_bg, "green");
        assert_eq!(custom_colors.searching_border, "255, 160, 102");
    }

    #[test]
    fn test_custom_theme_from_config() {
        let mut custom = HashMap::new();
        custom.insert(
            "test-theme".to_string(),
            CustomThemeColors {
                active_border: "#FF0000".to_string(),
                inactive_border: "darkgray".to_string(),
                searching_border: "yellow".to_string(),
                selected_bg: "#252b37".to_string(),
                selected_inactive_bg: "#1e222c".to_string(),
                visual_bg: "blue".to_string(),
                timer_active_bg: "green".to_string(),
                row_alternate_bg: "#16191f".to_string(),
                edit_bg: "cyan".to_string(),
                focus_bg: "magenta".to_string(),
                primary_text: "white".to_string(),
                secondary_text: "gray".to_string(),
                highlight_text: "cyan".to_string(),
                success: "green".to_string(),
                warning: "yellow".to_string(),
                error: "red".to_string(),
                info: "cyan".to_string(),
                timer_text: "yellow".to_string(),
                badge: "magenta".to_string(),
            },
        );

        let theme_config = ThemeConfig {
            active: "test-theme".to_string(),
            custom,
        };

        let theme = theme_config.get_active_theme();
        // Verify custom theme is applied (check the custom red border)
        assert!(matches!(theme.active_border, Color::Rgb(255, 0, 0)));
    }

    #[test]
    fn test_fallback_to_default_when_custom_not_found() {
        let theme_config = ThemeConfig {
            active: "non-existent-theme".to_string(),
            custom: HashMap::new(),
        };

        let theme = theme_config.get_active_theme();
        // Should fallback to default theme
        assert!(matches!(theme.active_border, Color::Cyan));
        assert!(matches!(theme.error, Color::LightRed));
    }

    #[test]
    fn test_theme_from_custom_colors() {
        let custom_colors = CustomThemeColors {
            active_border: "#7e9cd8".to_string(),
            inactive_border: "darkgray".to_string(),
            searching_border: "yellow".to_string(),
            selected_bg: "50, 50, 70".to_string(),
            selected_inactive_bg: "#1e222c".to_string(),
            visual_bg: "blue".to_string(),
            timer_active_bg: "green".to_string(),
            row_alternate_bg: "#000000".to_string(),
            edit_bg: "cyan".to_string(),
            focus_bg: "magenta".to_string(),
            primary_text: "white".to_string(),
            secondary_text: "gray".to_string(),
            highlight_text: "cyan".to_string(),
            success: "green".to_string(),
            warning: "yellow".to_string(),
            error: "red".to_string(),
            info: "cyan".to_string(),
            timer_text: "255, 199, 119".to_string(),
            badge: "magenta".to_string(),
        };

        let theme = Theme::from_custom(&custom_colors);

        // Verify different color formats are parsed correctly
        assert!(matches!(theme.active_border, Color::Rgb(126, 156, 216))); // hex
        assert!(matches!(theme.inactive_border, Color::DarkGray)); // named
        assert!(matches!(theme.selected_bg, Color::Rgb(50, 50, 70))); // rgb tuple
        assert!(matches!(theme.timer_text, Color::Rgb(255, 199, 119))); // rgb tuple
    }

    #[test]
    fn test_config_get_theme() {
        let mut custom = HashMap::new();
        custom.insert(
            "custom1".to_string(),
            CustomThemeColors {
                active_border: "red".to_string(),
                inactive_border: "darkgray".to_string(),
                searching_border: "yellow".to_string(),
                selected_bg: "blue".to_string(),
                selected_inactive_bg: "black".to_string(),
                visual_bg: "cyan".to_string(),
                timer_active_bg: "green".to_string(),
                row_alternate_bg: "black".to_string(),
                edit_bg: "blue".to_string(),
                focus_bg: "magenta".to_string(),
                primary_text: "white".to_string(),
                secondary_text: "gray".to_string(),
                highlight_text: "cyan".to_string(),
                success: "green".to_string(),
                warning: "yellow".to_string(),
                error: "red".to_string(),
                info: "cyan".to_string(),
                timer_text: "yellow".to_string(),
                badge: "magenta".to_string(),
            },
        );

        let config = Config {
            integrations: IntegrationConfig::default(),
            theme: ThemeConfig {
                active: "custom1".to_string(),
                custom,
            },
            columns: ColumnVisibilityConfig::default(),
        };

        let theme = config.get_theme();
        assert!(matches!(theme.active_border, Color::Red));
    }

    // Additional comprehensive tests

    #[test]
    fn test_all_predefined_theme_methods() {
        // Test that all theme methods return valid themes with all fields populated
        let default = Theme::default_theme();
        let kanagawa = Theme::kanagawa();
        let catppuccin = Theme::catppuccin();
        let gruvbox = Theme::gruvbox();
        let monokai = Theme::monokai();
        let dracula = Theme::dracula();
        let everforest = Theme::everforest();
        let terminal = Theme::terminal();

        // Verify each theme has different active_border colors (unique themes)
        assert!(matches!(default.active_border, Color::Cyan));
        assert!(matches!(kanagawa.active_border, Color::Rgb(127, 180, 202)));
        assert!(matches!(
            catppuccin.active_border,
            Color::Rgb(137, 180, 250)
        ));
        assert!(matches!(gruvbox.active_border, Color::Rgb(131, 165, 152)));
        assert!(matches!(monokai.active_border, Color::Rgb(102, 217, 239)));
        assert!(matches!(dracula.active_border, Color::Rgb(139, 233, 253)));
        assert!(matches!(
            everforest.active_border,
            Color::Rgb(131, 192, 146)
        ));
        assert!(matches!(terminal.active_border, Color::Cyan));
    }

    #[test]
    fn test_parse_color_hex_edge_cases() {
        // Test lowercase hex
        assert!(matches!(parse_color("#ffffff"), Color::Rgb(255, 255, 255)));
        assert!(matches!(parse_color("#000000"), Color::Rgb(0, 0, 0)));

        // Test uppercase hex
        assert!(matches!(parse_color("#FFFFFF"), Color::Rgb(255, 255, 255)));
        assert!(matches!(parse_color("#ABC"), Color::Rgb(170, 187, 204)));

        // Test mixed case
        assert!(matches!(parse_color("#FfFfFf"), Color::Rgb(255, 255, 255)));

        // Test invalid hex (wrong length)
        assert!(matches!(parse_color("#FF"), Color::White)); // fallback
        assert!(matches!(parse_color("#FFFFFFF"), Color::White)); // fallback

        // Test invalid hex characters
        assert!(matches!(parse_color("#GGGGGG"), Color::White)); // fallback
        assert!(matches!(parse_color("#XYZ"), Color::White)); // fallback
    }

    #[test]
    fn test_parse_color_rgb_edge_cases() {
        // Test boundary values
        assert!(matches!(parse_color("0, 0, 0"), Color::Rgb(0, 0, 0)));
        assert!(matches!(
            parse_color("255, 255, 255"),
            Color::Rgb(255, 255, 255)
        ));

        // Test with parentheses (should be handled by trim)
        assert!(matches!(
            parse_color("(100, 150, 200)"),
            Color::Rgb(100, 150, 200)
        ));

        // Test various spacing
        assert!(matches!(parse_color("10,20,30"), Color::Rgb(10, 20, 30)));
        assert!(matches!(
            parse_color("  50  ,  100  ,  150  "),
            Color::Rgb(50, 100, 150)
        ));

        // Test invalid RGB values
        assert!(matches!(parse_color("256, 100, 100"), Color::White)); // out of range
        assert!(matches!(parse_color("100, 300, 100"), Color::White)); // out of range
        assert!(matches!(parse_color("100, 100, 256"), Color::White)); // out of range
        assert!(matches!(parse_color("-1, 100, 100"), Color::White)); // negative
        assert!(matches!(parse_color("abc, 100, 100"), Color::White)); // non-numeric

        // Test wrong number of components
        assert!(matches!(parse_color("100, 100"), Color::White)); // too few
        assert!(matches!(parse_color("100, 100, 100, 100"), Color::White)); // too many
    }

    #[test]
    fn test_parse_color_named_variations() {
        // Test case variations
        assert!(matches!(parse_color("RED"), Color::Red));
        assert!(matches!(parse_color("Red"), Color::Red));
        assert!(matches!(parse_color("rEd"), Color::Red));

        // Test grey vs gray
        assert!(matches!(parse_color("gray"), Color::Gray));
        assert!(matches!(parse_color("grey"), Color::Gray));
        assert!(matches!(parse_color("GRAY"), Color::Gray));
        assert!(matches!(parse_color("GREY"), Color::Gray));
        assert!(matches!(parse_color("darkgray"), Color::DarkGray));
        assert!(matches!(parse_color("darkgrey"), Color::DarkGray));

        // Test all light colors
        assert!(matches!(parse_color("LightRed"), Color::LightRed));
        assert!(matches!(parse_color("LightGreen"), Color::LightGreen));
        assert!(matches!(parse_color("LightYellow"), Color::LightYellow));
        assert!(matches!(parse_color("LightBlue"), Color::LightBlue));
        assert!(matches!(parse_color("LightMagenta"), Color::LightMagenta));
        assert!(matches!(parse_color("LightCyan"), Color::LightCyan));

        // Test terminal color aliases
        assert!(matches!(parse_color("reset"), Color::Reset));
        assert!(matches!(parse_color("terminal"), Color::Reset));
        assert!(matches!(parse_color("default"), Color::Reset));
        assert!(matches!(parse_color("RESET"), Color::Reset));
        assert!(matches!(parse_color("Terminal"), Color::Reset));
    }

    #[test]
    fn test_parse_color_whitespace_handling() {
        // Test leading/trailing whitespace
        assert!(matches!(parse_color("  red  "), Color::Red));
        assert!(matches!(parse_color("\tblue\t"), Color::Blue));
        assert!(matches!(parse_color(" #FF0000 "), Color::Rgb(255, 0, 0)));
        assert!(matches!(
            parse_color("  100, 200, 150  "),
            Color::Rgb(100, 200, 150)
        ));
    }

    #[test]
    fn test_parse_color_empty_and_whitespace() {
        // Empty strings should fallback to white
        assert!(matches!(parse_color(""), Color::White));
        assert!(matches!(parse_color("   "), Color::White));
        assert!(matches!(parse_color("\t\t"), Color::White));
    }

    #[test]
    fn test_theme_color_consistency() {
        // Verify that all pre-defined themes have consistent structure
        // (all 18 colors are present and valid)
        let themes = vec![
            Theme::default_theme(),
            Theme::kanagawa(),
            Theme::catppuccin(),
            Theme::gruvbox(),
            Theme::monokai(),
            Theme::dracula(),
            Theme::everforest(),
            Theme::terminal(),
        ];

        for theme in themes {
            // Just access all fields to ensure they exist
            let _ = theme.active_border;
            let _ = theme.inactive_border;
            let _ = theme.searching_border;
            let _ = theme.selected_bg;
            let _ = theme.selected_inactive_bg;
            let _ = theme.visual_bg;
            let _ = theme.timer_active_bg;
            let _ = theme.row_alternate_bg;
            let _ = theme.edit_bg;
            let _ = theme.primary_text;
            let _ = theme.secondary_text;
            let _ = theme.highlight_text;
            let _ = theme.success;
            let _ = theme.warning;
            let _ = theme.error;
            let _ = theme.info;
            let _ = theme.timer_text;
            let _ = theme.badge;
        }
    }

    #[test]
    fn test_custom_theme_colors_all_formats() {
        let custom_colors = CustomThemeColors {
            active_border: "#FF0000".to_string(),        // hex
            inactive_border: "darkgray".to_string(),     // named
            searching_border: "255, 255, 0".to_string(), // rgb
            selected_bg: "#00F".to_string(),             // short hex
            selected_inactive_bg: "Black".to_string(),   // named (capitalized)
            visual_bg: "0, 128, 255".to_string(),        // rgb
            timer_active_bg: "lightgreen".to_string(),   // named
            row_alternate_bg: "#111".to_string(),        // short hex
            edit_bg: "(50, 100, 150)".to_string(),       // rgb with parens
            focus_bg: "magenta".to_string(),             // named
            primary_text: "white".to_string(),           // named
            secondary_text: "128, 128, 128".to_string(), // rgb
            highlight_text: "#0FF".to_string(),          // short hex cyan
            success: "green".to_string(),                // named
            warning: "#FFAA00".to_string(),              // hex
            error: "255, 0, 0".to_string(),              // rgb
            info: "cyan".to_string(),                    // named
            timer_text: "#FFA500".to_string(),           // hex
            badge: "magenta".to_string(),                // named
        };

        let theme = Theme::from_custom(&custom_colors);

        // Verify mixed color formats are parsed correctly
        assert!(matches!(theme.active_border, Color::Rgb(255, 0, 0))); // hex
        assert!(matches!(theme.inactive_border, Color::DarkGray)); // named
        assert!(matches!(theme.searching_border, Color::Rgb(255, 255, 0))); // rgb
        assert!(matches!(theme.selected_bg, Color::Rgb(0, 0, 255))); // short hex
        assert!(matches!(theme.selected_inactive_bg, Color::Black)); // named
        assert!(matches!(theme.visual_bg, Color::Rgb(0, 128, 255))); // rgb
        assert!(matches!(theme.timer_active_bg, Color::LightGreen)); // named
        assert!(matches!(theme.row_alternate_bg, Color::Rgb(17, 17, 17))); // short hex
    }

    #[test]
    fn test_multiple_custom_themes_in_config() {
        let toml_str = r##"
[theme]
active = "theme2"

[theme.custom.theme1]
active_border = "red"
inactive_border = "darkgray"
searching_border = "yellow"
selected_bg = "blue"
selected_inactive_bg = "black"
visual_bg = "cyan"
timer_active_bg = "green"
row_alternate_bg = "black"
edit_bg = "blue"
focus_bg = "magenta"
primary_text = "white"
secondary_text = "gray"
highlight_text = "cyan"
success = "green"
warning = "yellow"
error = "red"
info = "cyan"
timer_text = "yellow"
badge = "magenta"

[theme.custom.theme2]
active_border = "#FF00FF"
inactive_border = "darkgray"
searching_border = "yellow"
selected_bg = "blue"
selected_inactive_bg = "black"
visual_bg = "cyan"
timer_active_bg = "green"
row_alternate_bg = "black"
edit_bg = "blue"
focus_bg = "magenta"
primary_text = "white"
secondary_text = "gray"
highlight_text = "cyan"
success = "green"
warning = "yellow"
error = "red"
info = "cyan"
timer_text = "yellow"
badge = "magenta"
        "##;

        let config: Config = toml::from_str(toml_str).expect("Failed to deserialize");
        assert_eq!(config.theme.active, "theme2");
        assert_eq!(config.theme.custom.len(), 2);
        assert!(config.theme.custom.contains_key("theme1"));
        assert!(config.theme.custom.contains_key("theme2"));

        // Verify the active theme is theme2 with magenta border
        let theme = config.get_theme();
        assert!(matches!(theme.active_border, Color::Rgb(255, 0, 255)));
    }

    #[test]
    fn test_theme_config_case_sensitivity() {
        // Theme names should be case-sensitive (lowercase by convention)
        let theme_config = ThemeConfig {
            active: "KANAGAWA".to_string(), // uppercase (not found)
            custom: HashMap::new(),
        };

        let theme = theme_config.get_active_theme();
        // Should fallback to default theme (not kanagawa)
        assert!(matches!(theme.active_border, Color::Cyan)); // default theme
    }

    #[test]
    fn test_custom_theme_overrides_predefined() {
        // Custom theme with same name as predefined should override
        let mut custom = HashMap::new();
        custom.insert(
            "default".to_string(),
            CustomThemeColors {
                active_border: "#FF0000".to_string(), // red instead of cyan
                inactive_border: "darkgray".to_string(),
                searching_border: "yellow".to_string(),
                selected_bg: "blue".to_string(),
                selected_inactive_bg: "black".to_string(),
                visual_bg: "cyan".to_string(),
                timer_active_bg: "green".to_string(),
                row_alternate_bg: "black".to_string(),
                edit_bg: "blue".to_string(),
                focus_bg: "magenta".to_string(),
                primary_text: "white".to_string(),
                secondary_text: "gray".to_string(),
                highlight_text: "cyan".to_string(),
                success: "green".to_string(),
                warning: "yellow".to_string(),
                error: "red".to_string(),
                info: "cyan".to_string(),
                timer_text: "yellow".to_string(),
                badge: "magenta".to_string(),
            },
        );

        let theme_config = ThemeConfig {
            active: "default".to_string(),
            custom,
        };

        let theme = theme_config.get_active_theme();
        // Should use custom theme (red), not predefined default (cyan)
        assert!(matches!(theme.active_border, Color::Rgb(255, 0, 0)));
    }

    #[test]
    fn test_parse_color_rgb_with_parentheses_and_spaces() {
        // RGB tuples can have parentheses (users might include them) - we strip them
        assert!(matches!(
            parse_color("(255, 128, 64)"),
            Color::Rgb(255, 128, 64)
        ));
        assert!(matches!(
            parse_color("( 100 , 200 , 150 )"),
            Color::Rgb(100, 200, 150)
        ));

        // Parentheses are now stripped, so this should parse successfully
        let result = parse_color("(10,20,30)");
        assert!(matches!(result, Color::Rgb(10, 20, 30)));
    }

    #[test]
    fn test_theme_serialization() {
        // Test that ThemeConfig can be serialized/deserialized
        let theme_config = ThemeConfig {
            active: "gruvbox".to_string(),
            custom: HashMap::new(),
        };

        let serialized = toml::to_string(&theme_config).expect("Failed to serialize");
        assert!(serialized.contains("active"));
        assert!(serialized.contains("gruvbox"));

        let deserialized: ThemeConfig = toml::from_str(&serialized).expect("Failed to deserialize");
        assert_eq!(deserialized.active, "gruvbox");
        assert!(deserialized.custom.is_empty());
    }
}