tod 0.15.0

An unofficial Todoist command-line client
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
use crate::cargo::Version;
mod file;
mod projects;
mod timezone;
use crate::errors::Error;
use crate::format::maybe_format_url;
use crate::input::page_size;
use crate::legacy;
use crate::projects::Project;
use crate::tasks::Task;
use crate::time::{SystemTimeProvider, TimeProviderEnum};
use crate::{VERSION, cargo, format, input, oauth, time};
use regex::Regex;
use serde::de::Error as DeError;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::path::PathBuf;
use terminal_size::{Height, Width, terminal_size};
use tokio::sync::mpsc::UnboundedSender;

const MAX_COMMENT_LENGTH: u32 = 500;
pub const DEFAULT_TIMEOUT_SECONDS: u64 = 30;
pub const OAUTH: &str = "Login with OAuth (recommended)";
pub const DEVELOPER: &str = "Login with developer API token";
pub const TOKEN_METHOD: &str = "Choose your Todoist login method";
const TODOIST_INTEGRATIONS_URL: &str = "https://todoist.com/prefs/integrations";
pub use file::config_open;
pub use file::config_reset;
pub use file::generate_path;
pub use file::get_config;
pub use file::get_or_create;
pub use file::resolve_config_path;
pub use legacy::LegacySortValue;

#[derive(Deserialize, Serialize, Debug, Clone)]
#[serde(deny_unknown_fields)]
pub struct Completed {
    #[serde(deserialize_with = "deserialize_nonnegative_u32")]
    count: u32,
    date: String,
}

fn deserialize_nonnegative_u32<'de, D>(deserializer: D) -> Result<u32, D::Error>
where
    D: Deserializer<'de>,
{
    let value = i64::deserialize(deserializer)?;

    if value <= 0 {
        Ok(0)
    } else {
        u32::try_from(value).map_err(D::Error::custom)
    }
}

/// App configuration, serialized as json in `$XDG_CONFIG_HOME/tod.cfg`
#[derive(Clone, Serialize, Deserialize, Debug)]
#[serde(default, deny_unknown_fields)]
pub struct Config {
    /// The Todoist Api token
    pub token: Option<String>,
    /// List of Todoist projects and their project numbers
    #[serde(rename = "projectsv1")]
    projects: Option<Vec<Project>>,
    /// Path to config file
    pub path: PathBuf,
    /// The ID of the next task (NO LONGER IN USE)
    next_id: Option<String>,
    /// The next task, for use with complete
    #[serde(rename = "next_taskv1")]
    next_task: Option<Task>,
    /// Whether to trigger terminal bell on success
    #[serde(default)]
    pub bell_on_success: bool,
    /// Whether to trigger terminal bell on error
    #[serde(default = "bell_on_failure_default")]
    pub bell_on_failure: bool,
    /// A command to to run on task creation
    pub task_create_command: Option<String>,
    /// A command to run on task completion
    pub task_complete_command: Option<String>,
    /// A command to run on task comment creation
    pub task_comment_command: Option<String>,
    /// Regex to exclude tasks
    #[serde(with = "serde_regex")]
    pub task_exclude_regex: Option<Regex>,
    /// The timezone to use for the config
    timezone: Option<String>,
    pub timeout: Option<u64>,
    /// The last time we checked crates.io for the version
    pub last_version_check: Option<String>,
    pub mock_url: Option<String>,
    pub mock_string: Option<String>,
    pub mock_select: Option<usize>,
    /// Whether spinners are enabled
    pub spinners: Option<bool>,
    #[serde(default)]
    pub disable_links: bool,
    pub completed: Option<Completed>,
    /// Maximum length for printing comments
    pub max_comment_length: Option<u32>,
    /// Regex to exclude specific comments
    #[serde(with = "serde_regex")]
    pub comment_exclude_regex: Option<Regex>,

    pub verbose: Option<bool>,
    /// Don't ask for sections
    pub no_sections: Option<bool>,
    /// Goes straight to natural language input in datetime selection
    pub natural_language_only: Option<bool>,
    /// Ordered list of fields used when sorting by value.
    pub sort_order: Option<Vec<SortRule>>,
    /// Legacy numeric sort configuration. Deserialized for migration only.
    #[serde(skip_serializing)]
    pub sort_value: Option<LegacySortValue>,

    /// For storing arguments from the commandline
    #[serde(skip)]
    pub args: Args,

    /// For storing arguments from the commandline
    #[serde(skip)]
    pub internal: Internal,
    /// Optional `TimeProvider` for testing, defaults to `SystemTimeProvider`
    #[serde(skip)]
    pub time_provider: TimeProviderEnum,
}

fn bell_on_failure_default() -> bool {
    true
}

#[derive(Default, Clone, Eq, PartialEq, Debug)]
pub struct Args {
    pub verbose: bool,
    pub timeout: Option<u64>,
}

#[derive(Default, Clone, Debug)]
pub struct Internal {
    pub tx: Option<UnboundedSender<Error>>,
}

#[derive(Copy, Clone, Serialize, Deserialize, Eq, PartialEq, Debug)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub enum SortKey {
    Priority,
    DueDate,
    Overdue,
    Today,
    Now,
    NoDueDate,
    NotRecurring,
    Deadline,
    Order,
}

impl SortKey {
    pub fn default_order() -> Vec<SortKey> {
        vec![
            SortKey::Priority,
            SortKey::DueDate,
            SortKey::Overdue,
            SortKey::Today,
            SortKey::Now,
            SortKey::NoDueDate,
            SortKey::NotRecurring,
            SortKey::Deadline,
            SortKey::Order,
        ]
    }

    fn config_name(self) -> &'static str {
        match self {
            SortKey::Priority => "priority",
            SortKey::DueDate => "due_date",
            SortKey::Overdue => "overdue",
            SortKey::Today => "today",
            SortKey::Now => "now",
            SortKey::NoDueDate => "no_due_date",
            SortKey::NotRecurring => "not_recurring",
            SortKey::Deadline => "deadline",
            SortKey::Order => "order",
        }
    }

    fn from_config_name(value: &str) -> Option<Self> {
        match value {
            "priority" => Some(SortKey::Priority),
            "due_date" => Some(SortKey::DueDate),
            "overdue" => Some(SortKey::Overdue),
            "today" => Some(SortKey::Today),
            "now" => Some(SortKey::Now),
            "no_due_date" => Some(SortKey::NoDueDate),
            "not_recurring" => Some(SortKey::NotRecurring),
            "deadline" => Some(SortKey::Deadline),
            "order" => Some(SortKey::Order),
            _ => None,
        }
    }

    fn default_direction(self) -> SortDirection {
        match self {
            SortKey::Priority
            | SortKey::Overdue
            | SortKey::Today
            | SortKey::Now
            | SortKey::NoDueDate
            | SortKey::NotRecurring => SortDirection::Desc,
            SortKey::DueDate | SortKey::Deadline | SortKey::Order => SortDirection::Asc,
        }
    }
}

#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum SortDirection {
    Asc,
    Desc,
}

impl SortDirection {
    fn config_name(self) -> &'static str {
        match self {
            SortDirection::Asc => "asc",
            SortDirection::Desc => "desc",
        }
    }
}

#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub struct SortRule {
    pub key: SortKey,
    pub direction: SortDirection,
}

impl SortRule {
    pub fn new(key: SortKey, direction: SortDirection) -> Self {
        Self { key, direction }
    }

    pub(crate) fn with_default_direction(key: SortKey) -> Self {
        Self::new(key, key.default_direction())
    }

    pub fn default_order() -> Vec<Self> {
        SortKey::default_order()
            .into_iter()
            .map(Self::with_default_direction)
            .collect()
    }
}

impl Serialize for SortRule {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(&format!(
            "{}:{}",
            self.key.config_name(),
            self.direction.config_name()
        ))
    }
}

impl<'de> Deserialize<'de> for SortRule {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let value = String::deserialize(deserializer)?;
        let (key, direction) = match value.split_once(':') {
            Some((key, "asc")) => (key, Some(SortDirection::Asc)),
            Some((key, "desc")) => (key, Some(SortDirection::Desc)),
            Some((_, direction)) => {
                return Err(D::Error::custom(format!(
                    "invalid sort direction '{direction}'; expected 'asc' or 'desc'"
                )));
            }
            None => (value.as_str(), None),
        };
        let key = SortKey::from_config_name(key)
            .ok_or_else(|| D::Error::custom(format!("invalid sort key '{key}'")))?;

        Ok(match direction {
            Some(direction) => SortRule::new(key, direction),
            None => SortRule::with_default_direction(key),
        })
    }
}

impl Config {
    fn with_default_sort_order(self) -> Config {
        if self.sort_order.is_some() {
            self
        } else if let Some(sort_order) =
            legacy::detect_and_migrate_sort_value(self.sort_value.as_ref())
        {
            Config {
                sort_order: Some(sort_order),
                ..self
            }
        } else {
            Config {
                sort_order: Some(SortRule::default_order()),
                ..self
            }
        }
    }

    /// Pretty printed message for how to get API token
    pub fn token_message(self: &Config) -> String {
        let url = maybe_format_url(TODOIST_INTEGRATIONS_URL, self);
        format!("Please enter your Todoist API token from {url} ")
    }

    /// Set token on Config struct only
    #[allow(dead_code)]
    pub fn with_token(self: &Config, token: &str) -> Config {
        Config {
            token: Some(token.into()),
            ..self.clone()
        }
    }

    // Returns the maximum comment length if configured, otherwise estimates based on terminal window size (if supported)
    pub fn max_comment_length(&self) -> u32 {
        match self.max_comment_length {
            Some(length) => length,
            None => {
                if let Some((Width(width), Height(height))) = terminal_size() {
                    let menu_height = u16::try_from(page_size()).unwrap_or(height);
                    let comment_rows = height.saturating_sub(menu_height);
                    let estimated = u32::from(comment_rows) * u32::from(width);
                    estimated.min(MAX_COMMENT_LENGTH)
                } else {
                    MAX_COMMENT_LENGTH
                }
            }
        }
    }

    /// Fetches a sender for the error channel
    /// Use this to end errors from an async process
    pub fn tx(self) -> UnboundedSender<Error> {
        self.internal.tx.expect("No tx in Config")
    }

    pub async fn check_for_latest_version(self: Config) -> Result<Config, Error> {
        let last_version = self.last_version_check.clone();
        let today = time::date_string_today(&self)?;

        if last_version != Some(today.clone()) {
            let mut new_config = Config {
                last_version_check: Some(today),
                ..self.clone()
            };

            new_config.save().await?;
            let cloned_config = new_config.clone();
            tokio::spawn(async move {
                match cargo::compare_versions(cloned_config.mock_url).await {
                    Ok(Version::Dated(version)) => {
                        let message = format!(
                            "Your version of Tod is out of date
                        Latest Tod version is {}, you have {} installed.
                        Run {} to update if you installed with Cargo
                        or run {} if you installed with Homebrew",
                            version,
                            VERSION,
                            format::cyan_string("cargo install tod --force"),
                            format::cyan_string("brew update && brew upgrade tod")
                        );
                        let _ = self.tx().send(Error {
                            message,
                            source: "Crates.io".into(),
                        });
                    }
                    Ok(Version::Latest) => (),
                    Err(err) => {
                        let _ = self.tx().send(err);
                    }
                }
            });
            Ok(new_config)
        } else {
            Ok(self)
        }
    }

    pub fn clear_next_task(self) -> Config {
        let next_task: Option<Task> = None;

        Config { next_task, ..self }
    }

    /// Increase the completed count for today
    pub fn increment_completed(&self) -> Result<Config, Error> {
        let date = time::naive_date_today(self)?.to_string();
        let completed = match &self.completed {
            None => Some(Completed { date, count: 1 }),
            Some(completed) => {
                if completed.date == date {
                    Some(Completed {
                        count: completed.count + 1,
                        ..completed.clone()
                    })
                } else {
                    Some(Completed { date, count: 1 })
                }
            }
        };

        Ok(Config {
            completed,
            ..self.clone()
        })
    }

    #[allow(clippy::unused_async)]
    pub async fn new(tx: Option<UnboundedSender<Error>>, path: PathBuf) -> Result<Config, Error> {
        Ok(Config {
            path,
            token: None,
            next_id: None,
            next_task: None,
            last_version_check: None,
            timeout: None,
            bell_on_success: false,
            bell_on_failure: true,
            sort_order: Some(SortRule::default_order()),
            sort_value: None,
            timezone: None,
            completed: None,
            disable_links: false,
            spinners: Some(true),
            mock_url: None,
            no_sections: None,
            natural_language_only: None,
            mock_string: None,
            mock_select: None,
            max_comment_length: None,
            comment_exclude_regex: None,
            task_exclude_regex: None,
            verbose: None,
            internal: Internal { tx },
            args: Args {
                verbose: false,
                timeout: None,
            },
            time_provider: TimeProviderEnum::System(SystemTimeProvider),
            task_comment_command: None,
            task_create_command: None,
            task_complete_command: None,
            projects: Some(Vec::new()),
        })
    }

    pub fn set_next_task(&self, task: Task) -> Config {
        let next_task: Option<Task> = Some(task);

        Config {
            next_task,
            ..self.clone()
        }
    }

    pub fn tasks_completed(&self) -> Result<u32, Error> {
        let date = time::naive_date_today(self)?.to_string();
        match &self.completed {
            None => Ok(0),
            Some(completed) => {
                if completed.date == date {
                    Ok(completed.count)
                } else {
                    Ok(0)
                }
            }
        }
    }

    pub fn next_task(&self) -> Option<Task> {
        self.next_task.clone()
    }

    pub async fn set_token(&mut self, access_token: String) -> Result<String, Error> {
        self.token = Some(access_token);
        self.save().await
    }

    pub async fn set_developer_token(mut self, key: &str) -> Result<Config, Error> {
        let trimmed_key = key.trim();
        if trimmed_key.is_empty() {
            return Err(Error::new(
                "auth token",
                "Todoist API token cannot be empty or whitespace",
            ));
        }

        self.set_token(trimmed_key.to_string()).await?;
        self.maybe_set_timezone().await
    }

    async fn maybe_set_token(self) -> Result<Config, Error> {
        if self.token.clone().unwrap_or_default().trim().is_empty() {
            let mock_select = Some(1);
            let options = vec![OAUTH, DEVELOPER];
            let mut config = match input::select(TOKEN_METHOD, options, mock_select)? {
                OAUTH => {
                    let mut config = self.clone();
                    oauth::login(&mut config, None).await?;
                    config
                }
                DEVELOPER => {
                    let desc = self.token_message();

                    // We can't use mock_string from config here because it can't be set in test.
                    let fake_token = Some("faketoken".into());
                    let token = input::string(&desc, fake_token)?;
                    self.set_developer_token(&token).await?
                }
                _ => unreachable!(),
            };
            config.save().await?;
            Ok(config)
        } else {
            Ok(self)
        }
    }

    pub async fn edit_interactive(self) -> Result<String, Error> {
        let Config {
            bell_on_failure,
            bell_on_success,
            comment_exclude_regex,
            disable_links,
            max_comment_length,
            natural_language_only,
            no_sections,
            spinners,
            task_exclude_regex,
            timeout,
            token,
            verbose,

            // this is not implemented as it will be deprecated
            sort_value: _,
            sort_order: _,

            // We don't want user to set the ones below
            args: _,
            completed: _,
            internal: _,
            last_version_check: _,
            mock_select,
            mock_string: _,
            mock_url: _,
            next_id: _,
            next_task: _,
            path: _,
            projects: _,
            task_comment_command: _,
            task_complete_command: _,
            task_create_command: _,
            time_provider: _,
            timezone: _,
        } = self.clone();

        // --- bell_on_failure
        let desc = "
            bell_on_failure
            Ring terminal bell if a command fails
        ";
        let bell_on_failure = input::bool(desc, bell_on_failure, mock_select)?;

        // --- bell_on_success
        let desc = "
            bell_on_success
            Ring terminal bell if a command succeeds
        ";
        let bell_on_success = input::bool(desc, bell_on_success, mock_select)?;

        // --- spinners
        let desc = "
            spinners
            Display a spinner in terminal while waiting for an API call to complete
        ";
        let default_value = spinners.unwrap_or(true);
        let spinners = Some(input::bool(desc, default_value, mock_select)?);

        // --- verbose
        let desc = "
            verbose
            Output additional information to assist with debugging issues
        ";
        let default_value = verbose.unwrap_or(false);
        let verbose = Some(input::bool(desc, default_value, mock_select)?);

        // --- natural_language_only
        let desc = "
            natural_language_only
            Output additional information to assist with debugging issues
        ";
        let default_value = natural_language_only.unwrap_or(false);
        let natural_language_only = Some(input::bool(desc, default_value, mock_select)?);

        // --- disable_links
        let desc = "
            disable_links
            Output additional information to assist with debugging issues
        ";
        let disable_links = input::bool(desc, disable_links, mock_select)?;

        // --- no_sections
        let desc = "
            no_sections
            Do not prompt a user to select a section when working with projects
        ";
        let default_value = no_sections.unwrap_or(false);
        let no_sections = Some(input::bool(desc, default_value, mock_select)?);

        // --- token
        let desc = format!(
            "
            token
            {}
        ",
            self.token_message()
        );
        let token = input::string_with_default(&desc, &token.unwrap_or_default())?;

        let token = if token.is_empty() { None } else { Some(token) };

        // --- comment_exclude_regex
        let desc = "
            comment_exclude_regex
            Rust regex, comments that match will be excluded
            ";
        let default = match comment_exclude_regex {
            Some(regex) => regex.to_string(),
            None => String::new(),
        };
        let comment_exclude_regex = input::string_with_default(desc, &default)?;

        let comment_exclude_regex = if comment_exclude_regex.is_empty() {
            None
        } else {
            Some(Regex::new(&comment_exclude_regex)?)
        };

        // --- task_exclude_regex
        let desc = "
            task_exclude_regex
            Rust regex, tasks that match will be excluded
            ";
        let default = match task_exclude_regex {
            Some(regex) => regex.to_string(),
            None => String::new(),
        };
        let task_exclude_regex = input::string_with_default(desc, &default)?;

        let task_exclude_regex = if task_exclude_regex.is_empty() {
            None
        } else {
            Some(Regex::new(&task_exclude_regex)?)
        };

        // --- max_comment_length
        let desc = "
            max_comment_length
            Comments exceeding this length will be truncated            
            ";
        let default = match max_comment_length {
            Some(comment_length) => comment_length,
            None => MAX_COMMENT_LENGTH,
        };
        let max_comment_length: Option<u32> =
            Some(input::number_with_default(desc, default.try_into()?)?.try_into()?);

        // --- timeout
        let desc = "
            timeout
            Comments exceeding this length will be truncated            
            ";
        let default = match timeout {
            Some(comment_length) => comment_length,
            None => DEFAULT_TIMEOUT_SECONDS,
        };
        let timeout: Option<u64> =
            Some(input::number_with_default(desc, default.try_into()?)?.try_into()?);

        // ---

        let mut config = Config {
            bell_on_failure,
            max_comment_length,
            bell_on_success,
            timeout,
            comment_exclude_regex,
            task_exclude_regex,
            spinners,
            disable_links,
            no_sections,
            verbose,
            token,
            natural_language_only,
            ..self.clone()
        };

        config.save().await
    }
}

impl Default for Config {
    fn default() -> Self {
        Config {
            token: None,
            path: PathBuf::new(),
            next_id: None,
            next_task: None,
            last_version_check: None,
            timeout: None,
            bell_on_success: false,
            bell_on_failure: true,
            task_create_command: None,
            task_complete_command: None,
            task_comment_command: None,
            task_exclude_regex: None,
            comment_exclude_regex: None,
            sort_order: None,
            sort_value: None,
            timezone: None,
            completed: None,
            disable_links: false,
            spinners: Some(true),
            mock_url: None,
            no_sections: None,
            natural_language_only: None,
            mock_string: None,
            mock_select: None,
            max_comment_length: None,
            verbose: None,
            internal: Internal { tx: None },
            args: Args {
                verbose: false,
                timeout: None,
            },
            time_provider: TimeProviderEnum::System(SystemTimeProvider),
            projects: Some(Vec::new()),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test;
    use crate::test_time::FixedTimeProvider;
    use pretty_assertions::assert_eq;
    use std::path::PathBuf;
    use tempfile::{TempDir, tempdir};

    impl Config {
        pub fn default_test() -> Self {
            Config {
                token: Some("default-token".to_string()),
                path: PathBuf::from("/tmp/test.cfg"),
                time_provider: TimeProviderEnum::Fixed(FixedTimeProvider),
                args: Args {
                    verbose: false,
                    timeout: None,
                },
                internal: Internal { tx: None },
                sort_order: Some(SortRule::default_order()),
                sort_value: None,
                projects: Some(vec![]),
                next_id: None,
                next_task: None,
                bell_on_success: false,
                bell_on_failure: true,
                task_create_command: None,
                task_complete_command: None,
                task_comment_command: None,
                task_exclude_regex: None,
                comment_exclude_regex: None,
                timezone: Some("UTC".to_string()),
                timeout: None,
                last_version_check: None,
                mock_url: None,
                mock_string: None,
                mock_select: None,
                spinners: None,
                disable_links: false,
                completed: None,
                max_comment_length: None,
                verbose: None,
                no_sections: None,
                natural_language_only: None,
            }
        }
        // Mock the url used for fetching projects and tasks
        pub fn with_mock_url(self, url: String) -> Config {
            Config {
                mock_url: Some(url),
                ..self
            }
        }
        // Mock the string returned by the mock url
        pub fn with_mock_string(self, string: &str) -> Config {
            Config {
                mock_string: Some(string.to_string()),
                ..self
            }
        }

        pub fn mock_select(self, index: usize) -> Config {
            Config {
                mock_select: Some(index),
                ..self
            }
        }

        pub fn with_path(self: &Config, path: PathBuf) -> Config {
            Config {
                path,
                ..self.clone()
            }
        }

        pub fn with_projects(self: &Config, projects: Vec<Project>) -> Config {
            Config {
                projects: Some(projects),
                ..self.clone()
            }
        }
        /// Set the `TimeProvider` for testing
        pub fn with_time_provider(self: &Config, provider_type: TimeProviderEnum) -> Config {
            let mut config = self.clone();
            config.time_provider = provider_type;
            config
        }
    }

    fn temp_config_path(file_name: &str) -> (TempDir, PathBuf) {
        let dir = tempdir().expect("Could not create temp config directory");
        let path = dir.path().join(file_name);
        (dir, path)
    }

    #[tokio::test]
    async fn set_and_clear_next_task_should_work() {
        let config = test::fixtures::config().await;
        assert_eq!(config.next_task, None);
        let task = test::fixtures::today_task().await;
        let config = config.set_next_task(task.clone());
        assert_eq!(config.next_task, Some(task));
        let config = config.clear_next_task();
        assert_eq!(config.next_task, None);
    }

    #[tokio::test]
    async fn add_project_should_work() {
        let mut config = test::fixtures::config().await;
        let projects_count = config
            .projects()
            .await
            .expect("Failed to fetch projects asynchronously")
            .len();
        assert_eq!(projects_count, 1);
        config.add_project(test::fixtures::project());
        let projects_count = config
            .projects()
            .await
            .expect("Failed to fetch projects asynchronously")
            .len();
        assert_eq!(projects_count, 2);
    }

    #[tokio::test]
    async fn remove_project_should_work() {
        let mut config = test::fixtures::config().await;
        let projects = config
            .projects()
            .await
            .expect("Failed to fetch projects asynchronously");
        let project = projects
            .first()
            .expect("Expected at least one project in projects list");
        let projects_count = config
            .projects()
            .await
            .expect("Failed to fetch projects asynchronously")
            .len();
        assert_eq!(projects_count, 1);
        config.remove_project(project);
        let projects_count = config
            .projects()
            .await
            .expect("Failed to fetch projects asynchronously")
            .len();
        assert_eq!(projects_count, 0);
    }

    #[tokio::test]
    async fn debug_impl_for_config_should_work() {
        let config = test::fixtures::config().await;
        let debug_output = format!("{config:?}");
        // Assert that the debug output contains the struct name and some fields
        assert!(debug_output.contains("Config"));
        assert!(debug_output.contains("token"));
        assert!(debug_output.contains(&config.token.expect("No token found in config")));
    }

    #[test]
    fn debug_impls_for_config_components_should_work() {
        use tokio::sync::mpsc::unbounded_channel;

        let args = Args {
            verbose: true,
            timeout: Some(42),
        };
        let args_debug = format!("{args:?}");
        assert!(args_debug.contains("Args"));
        assert!(args_debug.contains("verbose"));
        assert!(args_debug.contains("timeout"));

        let (tx, _rx) = unbounded_channel::<Error>();
        let internal = Internal { tx: Some(tx) };
        let internal_debug = format!("{internal:?}");
        assert!(internal_debug.contains("Internal"));

        let sort_key = SortKey::Priority;
        let sort_key_debug = format!("{sort_key:?}");
        assert!(sort_key_debug.contains("Priority"));
    }

    #[test]
    fn trait_impls_for_config_components_should_work() {
        let args = Args {
            verbose: true,
            timeout: Some(10),
        };
        let args_clone = args.clone();
        assert_eq!(args, args_clone);

        let internal = Internal { tx: None };
        let internal_clone = internal.clone();
        assert_eq!(internal.tx.is_none(), internal_clone.tx.is_none());

        let sort_key = SortKey::Priority;
        let sort_key_copy = sort_key;
        assert_eq!(sort_key, sort_key_copy);

        assert_eq!(
            args,
            Args {
                verbose: true,
                timeout: Some(10)
            }
        );
        assert_ne!(
            args,
            Args {
                verbose: false,
                timeout: Some(5)
            }
        );

        let default_args = Args::default();
        assert_eq!(default_args.verbose, false);
        assert_eq!(default_args.timeout, None);

        let default_internal = Internal::default();
        assert!(default_internal.tx.is_none());

        assert_eq!(SortKey::default_order().first(), Some(&SortKey::Priority));
    }

    #[tokio::test]
    async fn test_config_with_methods() {
        let path = generate_path().await.expect("Could not generate path");
        let base_config = Config::new(None, path)
            .await
            .expect("Failed to create base config");

        let tz_config = base_config.with_timezone("America/New_York");
        assert_eq!(tz_config.timezone, Some("America/New_York".to_string()));

        let mock_url = "http://localhost:1234";
        let mock_config = base_config.clone().with_mock_url(mock_url.to_string());
        assert_eq!(mock_config.mock_url, Some(mock_url.to_string()));

        let mock_str_config = base_config.clone().with_mock_string("mock response");
        assert_eq!(
            mock_str_config.mock_string,
            Some("mock response".to_string())
        );

        let select_config = base_config.clone().mock_select(2);
        assert_eq!(select_config.mock_select, Some(2));

        let path_config = base_config.with_path(PathBuf::from("some/test/path.cfg"));
        assert_eq!(path_config.path, PathBuf::from("some/test/path.cfg"));

        let projects = vec![Project {
            id: "test123".to_string(),
            can_assign_tasks: true,
            child_order: 0,
            color: "blue".to_string(),
            created_at: None,
            is_archived: false,
            is_deleted: false,
            is_favorite: false,
            is_frozen: false,
            name: "Test Project".to_string(),
            updated_at: None,
            view_style: "list".to_string(),
            default_order: 0,
            description: "desc".to_string(),
            parent_id: None,
            inbox_project: None,
            is_collapsed: false,
            is_shared: false,
        }];
        let project_config = Config {
            projects: Some(projects.clone()),
            ..base_config.clone()
        };
        assert!(project_config.projects.is_some());
    }

    #[test]
    fn test_config_debug_with_time_provider() {
        let config = Config::default_test()
            .with_time_provider(TimeProviderEnum::Fixed(FixedTimeProvider))
            .with_path(PathBuf::from("/tmp/test.cfg"));

        let debug_output = format!("{config:?}");
        assert!(debug_output.contains("Config"));
        assert!(debug_output.contains("/tmp/test.cfg"));
    }
    // Test function for max_comment_length
    #[test]
    fn max_comment_length_should_return_configured_value() {
        let config = Config {
            max_comment_length: Some(1234),
            ..Config::default_test()
        };

        assert_eq!(config.max_comment_length(), 1234);
    }

    #[test]
    fn max_comment_length_should_fallback_when_not_set() {
        let config = Config {
            max_comment_length: None,
            ..Config::default_test()
        };

        let result = config.max_comment_length();

        // In CI or test environments terminal_size might return None
        // so just ensure it's a positive, nonzero value
        assert!(result > 0);
        assert!(result <= MAX_COMMENT_LENGTH);
    }

    #[tokio::test]
    async fn check_for_latest_version_should_skip_if_checked_today() {
        let today =
            time::date_string_today(&Config::default_test()).expect("Could not get today's date");
        let config = Config {
            last_version_check: Some(today.clone()),
            ..Config::default_test()
        };

        let result = config
            .clone()
            .check_for_latest_version()
            .await
            .expect("Expected check_for_latest_version to succeed");

        assert_eq!(result.last_version_check, Some(today));
        assert_eq!(result.path, config.path);
    }

    #[tokio::test]
    async fn check_for_latest_version_should_update_when_not_checked_today() {
        let (_temp_dir, path) = temp_config_path("version_check.cfg");
        let mut config = Config::default_test().with_path(path.clone());
        config = config.create().await.expect("Should create config file");
        config.last_version_check = Some("2000-01-01".to_string());

        let today = time::date_string_today(&config).expect("Could not get today's date");
        let result = config
            .check_for_latest_version()
            .await
            .expect("Expected check_for_latest_version to succeed");

        assert_eq!(result.last_version_check, Some(today.clone()));

        let saved = Config::load(&path)
            .await
            .expect("Expected config to be saved to disk");
        assert_eq!(saved.last_version_check, Some(today));
    }

    #[test]
    fn test_unknown_field_rejected() {
        let json = r#"
    {
        "token": "abc123",
        "Bobby": {
            "bobby_enabled": true
        }
    }
    "#;

        let result: Result<Config, _> = serde_json::from_str(json);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("unknown field `Bobby`"));
    }

    #[tokio::test]
    async fn test_edit_interactive() {
        // Create a temporary config for testing
        let (_temp_dir, path) = temp_config_path("test_edit_interactive.cfg");

        // Initialize with some default values
        let mut config = Config::default_test()
            .with_path(path.clone())
            .mock_select(0);
        config = config.create().await.expect("Should create config file");

        // Mock the input to return specific values for testing
        // We'll test by mocking the input functions and verifying the result

        // This tests that edit_interactive can be called without panicking
        let result = config.edit_interactive().await;
        assert!(
            result.is_ok(),
            "edit_interactive should complete successfully"
        );

        // Verify the config file was saved
        assert!(
            tokio::fs::try_exists(&path)
                .await
                .expect("Could not check if file exists"),
            "Config file should exist after edit_interactive"
        );
    }
}