claude-code-switcher 0.15.1

A CLI tool for managing Claude Code setting snapshots and templates
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
//! Apply-TUI application state and event handling.
//!
//! The TUI gathers the user's intent (which key, effort, scope, co-author,
//! variant, auto-compact) for a given target and returns an [`ApplySelection`]; it
//! does not touch settings.json itself.

use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use std::collections::HashMap;
use std::sync::mpsc::{self, Receiver, TryRecvError};
use std::time::{Duration, Instant};

use crate::CredentialManager;
use crate::credentials::{
    ApiKeyChoice, ApiKeySource, CredentialStore, collect_api_key_sources, mask_api_key,
};
use crate::prefs::{KeyRef, Prefs};
use crate::snapshots::SnapshotScope;
use crate::templates::{
    AutoCompactWindow, TemplateType, get_template_instance_with_input, is_generic_target,
    supports_auto_compact_option, variant_options,
};
use crate::usage::{UsageReport, UsageRequest, UsageStatus, query_usage, supports_usage};

use super::input::TextInput;

/// The user's confirmed choices from the TUI.
#[derive(Debug, Clone)]
pub struct ApplySelection {
    pub key: ApiKeyChoice,
    pub effort: Option<String>,
    pub scope: SnapshotScope,
    /// `true` = co-author disabled.
    pub co_author_off: bool,
    pub variant: Option<String>,
    pub auto_compact_window: Option<AutoCompactWindow>,
}

/// What an event produced.
pub enum Outcome {
    /// Keep running.
    Continue,
    /// User confirmed — apply these choices.
    Apply(ApplySelection),
    /// User cancelled (Esc / q).
    Quit,
}

/// Current interaction mode.
pub enum Mode {
    Normal,
    InputNewKey(TextInput),
    InputRename { idx: usize, input: TextInput },
    ConfirmDelete { idx: usize },
    Help,
    Message(String),
}

const EFFORTS: &[&str] = &["max", "xhigh", "high", "medium", "low"];
const SCOPES: &[SnapshotScope] = &[
    SnapshotScope::Common,
    SnapshotScope::Env,
    SnapshotScope::All,
];
const USAGE_CACHE_TTL: Duration = Duration::from_secs(60);
const SPINNER_FRAMES: [char; 4] = ['-', '\\', '|', '/'];

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct UsageCacheKey {
    template_type: TemplateType,
    api_key: String,
    base_url: Option<String>,
}

struct UsageCacheEntry {
    report: UsageReport,
    fetched_at: Instant,
}

struct UsageInFlight {
    key: UsageCacheKey,
    receiver: Receiver<Result<UsageReport, String>>,
}

fn supported_auto_compact_windows_for(
    template_type: &TemplateType,
    alias: &str,
) -> Vec<AutoCompactWindow> {
    let template = get_template_instance_with_input(template_type, alias);
    if supports_auto_compact_option(template.as_ref()) {
        template.supported_auto_compact_windows().to_vec()
    } else {
        Vec::new()
    }
}

/// Which row the cursor is on.
#[derive(Clone, Copy)]
enum Row {
    Key(usize),
    NewKey,
    Effort,
    Scope,
    CoAuthor,
    Variant,
    AutoCompact,
    UsageRefresh,
    Apply,
}

pub struct App {
    pub template_type: TemplateType,
    pub target: String,
    pub display_name: String,
    pub current_label: String,

    sources: Vec<ApiKeySource>,
    selected_key: Option<usize>,
    cursor: usize,

    effort_idx: usize,
    scope_idx: usize,
    co_author: bool, // true = enabled
    variant_aliases: Vec<(&'static str, &'static str)>,
    variant_idx: usize,
    has_variant_row: bool,
    auto_compact_windows: Vec<AutoCompactWindow>,
    auto_compact_idx: usize,

    usage_cache: HashMap<UsageCacheKey, UsageCacheEntry>,
    usage_in_flight: Option<UsageInFlight>,
    usage_status: UsageStatus,
    usage_status_key: Option<UsageCacheKey>,
    spinner_idx: usize,

    pub mode: Mode,
}

impl App {
    /// Build the app state, pre-filling from prefs (remembered last selection,
    /// falling back to global defaults).
    pub fn new(
        template_type: TemplateType,
        target: String,
        display_name: String,
        current_label: String,
        prefs: &Prefs,
    ) -> anyhow::Result<Self> {
        let sources = collect_api_key_sources(&template_type)?;

        let variant_aliases = variant_options(&template_type);
        let has_variant_row = !variant_aliases.is_empty() && is_generic_target(&target);
        let tpref = prefs.template_pref(&template_type);

        // variant
        let variant_idx = if !has_variant_row {
            0
        } else {
            let remembered = tpref.and_then(|p| p.variant.as_deref());
            remembered
                .and_then(|v| variant_aliases.iter().position(|(a, _)| *a == v))
                .unwrap_or(0)
        };
        let initial_alias = if has_variant_row {
            variant_aliases[variant_idx].0
        } else {
            target.as_str()
        };
        let auto_compact_windows =
            supported_auto_compact_windows_for(&template_type, initial_alias);

        // auto-compact: last → provider default
        let auto_compact_idx = if auto_compact_windows.is_empty() {
            0
        } else {
            tpref
                .and_then(|p| {
                    p.last_auto_compact_window
                        .as_deref()
                        .or(p.last_context_window.as_deref())
                })
                .and_then(|value| value.parse::<AutoCompactWindow>().ok())
                .and_then(|value| auto_compact_windows.iter().position(|x| *x == value))
                .unwrap_or(0)
        };

        // selected key: remembered & still present, else first
        let selected_key = (|| {
            let kr = tpref.and_then(|p| p.last_key.as_ref())?;
            sources.iter().position(|s| match (s, kr) {
                (ApiKeySource::EnvVar { env_var_name, .. }, KeyRef::EnvVar(n)) => env_var_name == n,
                (ApiKeySource::Saved { credential }, KeyRef::Credential(id)) => {
                    credential.id() == id
                }
                _ => false,
            })
        })()
        .or(if sources.is_empty() { None } else { Some(0) });

        // effort: last → global default → "max"
        let effort_idx = tpref
            .and_then(|p| p.last_effort.as_deref())
            .or(prefs.default_effort.as_deref())
            .and_then(|e| EFFORTS.iter().position(|x| *x == e))
            .unwrap_or(0);

        // scope: last → global default → common (0)
        let scope_idx = tpref
            .and_then(|p| p.last_scope.as_ref())
            .or(Some(&prefs.default_scope))
            .and_then(|s| SCOPES.iter().position(|x| x == s))
            .unwrap_or(0);

        // co-author: last → global default (false = off)
        let co_author = tpref
            .and_then(|p| p.last_co_author)
            .unwrap_or(prefs.default_co_author);

        // initial cursor: on the selected key if any, else NewKey, else Apply
        let cursor = selected_key.unwrap_or({
            // sources empty → NewKey is row index 0 (== sources.len())
            0
        });

        let mut app = Self {
            template_type,
            target,
            display_name,
            current_label,
            sources,
            selected_key,
            cursor,
            effort_idx,
            scope_idx,
            co_author,
            variant_aliases,
            variant_idx,
            has_variant_row,
            auto_compact_windows,
            auto_compact_idx,
            usage_cache: HashMap::new(),
            usage_in_flight: None,
            usage_status: UsageStatus::Unsupported,
            usage_status_key: None,
            spinner_idx: 0,
            mode: Mode::Normal,
        };
        app.refresh_usage(false);
        Ok(app)
    }

    // ── row model ────────────────────────────────────────────────────────────

    fn n_keys(&self) -> usize {
        self.sources.len()
    }
    fn n_options(&self) -> usize {
        3 + if self.has_variant_row { 1 } else { 0 }
            + if self.has_auto_compact_row() { 1 } else { 0 }
            + if self.has_usage_row() { 1 } else { 0 }
    }
    fn total_rows(&self) -> usize {
        self.n_keys() + 1 + self.n_options() + 1
    }
    fn apply_index(&self) -> usize {
        self.total_rows() - 1
    }

    fn row_at(&self, cursor: usize) -> Row {
        let nk = self.n_keys();
        if cursor < nk {
            return Row::Key(cursor);
        }
        if cursor == nk {
            return Row::NewKey;
        }
        if cursor == self.apply_index() {
            return Row::Apply;
        }

        // option rows start after NewKey
        let mut o = cursor - (nk + 1);
        if o == 0 {
            return Row::Effort;
        }
        o -= 1;
        if o == 0 {
            return Row::Scope;
        }
        o -= 1;
        if o == 0 {
            return Row::CoAuthor;
        }
        o -= 1;
        if self.has_variant_row {
            if o == 0 {
                return Row::Variant;
            }
            o -= 1;
        }
        if self.has_auto_compact_row() {
            if o == 0 {
                return Row::AutoCompact;
            }
            o -= 1;
        }
        if self.has_usage_row() && o == 0 {
            return Row::UsageRefresh;
        }
        Row::Apply
    }

    // ── accessors for rendering ──────────────────────────────────────────────

    pub fn sources(&self) -> &[ApiKeySource] {
        &self.sources
    }
    pub fn selected_key(&self) -> Option<usize> {
        self.selected_key
    }
    pub fn cursor(&self) -> usize {
        self.cursor
    }
    pub fn effort(&self) -> &'static str {
        EFFORTS[self.effort_idx]
    }
    pub fn scope(&self) -> SnapshotScope {
        SCOPES[self.scope_idx].clone()
    }
    pub fn co_author_enabled(&self) -> bool {
        self.co_author
    }
    pub fn has_variant_row(&self) -> bool {
        self.has_variant_row
    }
    pub fn variant_label(&self) -> Option<&'static str> {
        if !self.has_variant_row {
            return None;
        }
        Some(self.variant_aliases[self.variant_idx].1)
    }
    pub fn has_auto_compact_row(&self) -> bool {
        !self.auto_compact_windows.is_empty()
    }
    pub fn auto_compact_label(&self) -> Option<&'static str> {
        if !self.has_auto_compact_row() {
            return None;
        }
        Some(self.auto_compact_windows[self.auto_compact_idx].label())
    }
    pub fn auto_compact_window(&self) -> Option<AutoCompactWindow> {
        if !self.has_auto_compact_row() {
            return None;
        }
        Some(self.auto_compact_windows[self.auto_compact_idx])
    }
    pub fn has_usage_row(&self) -> bool {
        supports_usage(&self.template_type)
    }
    pub fn usage_status(&self) -> &UsageStatus {
        &self.usage_status
    }

    /// Build a template instance reflecting the current variant choice (for
    /// previewing model / base URL).
    pub fn preview_template_instance(&self) -> Box<dyn crate::templates::Template> {
        let alias = if self.has_variant_row {
            self.variant_aliases[self.variant_idx].0
        } else {
            self.target.as_str()
        };
        get_template_instance_with_input(&self.template_type, alias)
    }

    /// Compute the (model, base URL) the current selection would produce, for
    /// the Preview pane.
    pub fn preview_model_and_base(&self) -> (String, String) {
        let inst = self.preview_template_instance();
        let key = self
            .selected_key
            .and_then(|i| self.sources.get(i))
            .map(|s| s.api_key().to_string())
            .unwrap_or_else(|| "sk-preview".to_string());
        let settings = inst
            .create_settings_with_auto_compact(
                &key,
                &SnapshotScope::Common,
                self.auto_compact_window(),
            )
            .unwrap_or_else(|_| inst.create_settings(&key, &SnapshotScope::Common));
        let model = settings.model.unwrap_or_else(|| "(default)".to_string());
        let base = settings
            .env
            .as_ref()
            .and_then(|e| e.get("ANTHROPIC_BASE_URL"))
            .cloned()
            .unwrap_or_else(|| "(none)".to_string());
        (model, base)
    }

    pub fn masked_selected_key(&self) -> String {
        match self.selected_key.and_then(|i| self.sources.get(i)) {
            Some(s) => mask_api_key(s.api_key()),
            None => "(none)".to_string(),
        }
    }

    fn selected_usage_key(&self) -> Option<UsageCacheKey> {
        if !supports_usage(&self.template_type) {
            return None;
        }
        let api_key = self
            .selected_key
            .and_then(|i| self.sources.get(i))
            .map(|s| s.api_key().to_string())?;
        let (_, base_url) = self.preview_model_and_base();
        Some(UsageCacheKey {
            template_type: self.template_type.clone(),
            api_key,
            base_url: if base_url == "(none)" {
                None
            } else {
                Some(base_url)
            },
        })
    }

    pub fn tick(&mut self) {
        self.spinner_idx = (self.spinner_idx + 1) % SPINNER_FRAMES.len();
        self.drain_usage_result();
        if self
            .selected_usage_key()
            .as_ref()
            .is_some_and(|key| self.usage_status_key.as_ref() != Some(key))
        {
            self.refresh_usage(false);
            return;
        }
        if let Some(key) = self.selected_usage_key() {
            let expired = self
                .usage_cache
                .get(&key)
                .is_some_and(|entry| entry.fetched_at.elapsed() >= USAGE_CACHE_TTL);
            let already_loading = self.usage_in_flight.as_ref().is_some_and(|p| p.key == key);
            if expired && !already_loading {
                self.refresh_usage(false);
            } else if matches!(self.usage_status, UsageStatus::Loading { .. }) {
                self.update_loading_spinner();
            }
        }
    }

    fn drain_usage_result(&mut self) {
        let Some(in_flight) = self.usage_in_flight.as_ref() else {
            return;
        };
        match in_flight.receiver.try_recv() {
            Ok(Ok(report)) => {
                let key = in_flight.key.clone();
                self.usage_cache.insert(
                    key.clone(),
                    UsageCacheEntry {
                        report: report.clone(),
                        fetched_at: Instant::now(),
                    },
                );
                self.usage_in_flight = None;
                if self.selected_usage_key().as_ref() == Some(&key) {
                    self.usage_status_key = Some(key);
                    self.usage_status = UsageStatus::Ready(report);
                }
            }
            Ok(Err(message)) => {
                let key = in_flight.key.clone();
                let stale = self.usage_cache.get(&key).map(|entry| entry.report.clone());
                self.usage_in_flight = None;
                if self.selected_usage_key().as_ref() == Some(&key) {
                    self.usage_status_key = Some(key);
                    self.usage_status = UsageStatus::Error { message, stale };
                }
            }
            Err(TryRecvError::Empty) => self.update_loading_spinner(),
            Err(TryRecvError::Disconnected) => {
                let key = in_flight.key.clone();
                let stale = self.usage_cache.get(&key).map(|entry| entry.report.clone());
                self.usage_in_flight = None;
                if self.selected_usage_key().as_ref() == Some(&key) {
                    self.usage_status_key = Some(key);
                    self.usage_status = UsageStatus::Error {
                        message: "usage query worker stopped".to_string(),
                        stale,
                    };
                }
            }
        }
    }

    fn refresh_usage(&mut self, force: bool) {
        self.drain_usage_result();
        if !supports_usage(&self.template_type) {
            self.usage_status_key = None;
            self.usage_status = UsageStatus::Unsupported;
            return;
        }
        let Some(key) = self.selected_usage_key() else {
            self.usage_status_key = None;
            self.usage_status = UsageStatus::NoKey;
            self.usage_in_flight = None;
            return;
        };

        if !force
            && let Some(entry) = self.usage_cache.get(&key)
            && entry.fetched_at.elapsed() < USAGE_CACHE_TTL
        {
            self.usage_status_key = Some(key);
            self.usage_status = UsageStatus::Ready(entry.report.clone());
            return;
        }

        if self.usage_in_flight.as_ref().is_some_and(|p| p.key == key) {
            self.update_loading_spinner();
            return;
        }

        let stale = self.usage_cache.get(&key).map(|entry| entry.report.clone());
        self.start_usage_query(key, stale);
    }

    fn start_usage_query(&mut self, key: UsageCacheKey, stale: Option<UsageReport>) {
        let request = UsageRequest {
            template_type: key.template_type.clone(),
            api_key: key.api_key.clone(),
            anthropic_base_url: key.base_url.clone(),
        };
        let (sender, receiver) = mpsc::channel();
        std::thread::spawn(move || {
            let result = query_usage(&request).map_err(|e| e.to_string());
            let _ = sender.send(result);
        });
        self.usage_status_key = Some(key.clone());
        self.usage_status = UsageStatus::Loading {
            stale,
            spinner: SPINNER_FRAMES[self.spinner_idx],
        };
        self.usage_in_flight = Some(UsageInFlight { key, receiver });
    }

    fn update_loading_spinner(&mut self) {
        if let UsageStatus::Loading { stale, spinner } = &mut self.usage_status {
            *spinner = SPINNER_FRAMES[self.spinner_idx];
            if stale.is_none()
                && let Some(key) = &self.usage_status_key
            {
                *stale = self.usage_cache.get(key).map(|entry| entry.report.clone());
            }
        }
    }

    // ── event handling ───────────────────────────────────────────────────────

    pub fn handle_event(&mut self, key: KeyEvent) -> Outcome {
        // Modes that capture keys: take the mode out by value so input handlers
        // can borrow `self` freely (the TextInput would otherwise alias self).
        let taken = std::mem::replace(&mut self.mode, Mode::Normal);
        match taken {
            Mode::InputNewKey(input) => return self.handle_input_newkey(key, input),
            Mode::InputRename { idx, input } => {
                return self.handle_input_rename(key, idx, input);
            }
            Mode::ConfirmDelete { idx } => return self.handle_confirm_delete(key, idx),
            Mode::Help | Mode::Message(_) => return Outcome::Continue, // self.mode already Normal
            Mode::Normal => {}
        }

        // Ctrl+C always quits.
        if key.modifiers.contains(KeyModifiers::CONTROL) && key.code == KeyCode::Char('c') {
            return Outcome::Quit;
        }

        match key.code {
            KeyCode::Up => self.move_cursor(-1),
            KeyCode::Down => self.move_cursor(1),
            KeyCode::Left | KeyCode::Right => {
                let dir = if key.code == KeyCode::Left { -1 } else { 1 };
                self.cycle_option(dir);
            }
            KeyCode::Enter => match self.row_at(self.cursor) {
                Row::Key(idx) => {
                    self.selected_key = Some(idx);
                    self.refresh_usage(false);
                    return self.build_apply();
                }
                Row::NewKey => self.mode = Mode::InputNewKey(TextInput::empty()),
                Row::Effort => self.cycle_effort(1),
                Row::Scope => self.cycle_scope(1),
                Row::CoAuthor => self.co_author = !self.co_author,
                Row::Variant => self.cycle_variant(1),
                Row::AutoCompact => self.cycle_auto_compact(1),
                Row::UsageRefresh => self.refresh_usage(true),
                Row::Apply => return self.build_apply(),
            },
            KeyCode::Char('a') => return self.build_apply(),
            KeyCode::Char('n') => self.mode = Mode::InputNewKey(TextInput::empty()),
            KeyCode::Char('d') => self.try_delete(),
            KeyCode::Char('r') => self.try_rename(),
            KeyCode::Char('u') => self.refresh_usage(true),
            KeyCode::Char('?') => self.mode = Mode::Help,
            KeyCode::Esc | KeyCode::Char('q') => return Outcome::Quit,
            _ => {}
        }

        Outcome::Continue
    }

    fn move_cursor(&mut self, delta: i32) {
        let total = self.total_rows() as i32;
        if total == 0 {
            return;
        }
        let mut c = self.cursor as i32 + delta;
        if c < 0 {
            c = 0;
        }
        if c >= total {
            c = total - 1;
        }
        self.cursor = c as usize;
        // live-select the key under the cursor
        if let Row::Key(idx) = self.row_at(self.cursor) {
            self.selected_key = Some(idx);
            self.refresh_usage(false);
        }
    }

    fn cycle_option(&mut self, dir: i32) {
        match self.row_at(self.cursor) {
            Row::Effort => self.cycle_effort(dir),
            Row::Scope => self.cycle_scope(dir),
            Row::CoAuthor => self.co_author = !self.co_author,
            Row::Variant => self.cycle_variant(dir),
            Row::AutoCompact => self.cycle_auto_compact(dir),
            Row::UsageRefresh => self.refresh_usage(true),
            _ => {}
        }
    }

    fn cycle_effort(&mut self, dir: i32) {
        let n = EFFORTS.len() as i32;
        self.effort_idx = ((self.effort_idx as i32 + dir).rem_euclid(n)) as usize;
    }
    fn cycle_scope(&mut self, dir: i32) {
        let n = SCOPES.len() as i32;
        self.scope_idx = ((self.scope_idx as i32 + dir).rem_euclid(n)) as usize;
    }
    fn cycle_variant(&mut self, dir: i32) {
        let n = self.variant_aliases.len() as i32;
        if n == 0 {
            return;
        }
        self.variant_idx = ((self.variant_idx as i32 + dir).rem_euclid(n)) as usize;
        self.refresh_auto_compact_windows();
        self.refresh_usage(false);
    }
    fn refresh_auto_compact_windows(&mut self) {
        let current = self.auto_compact_window();
        let alias = if self.has_variant_row {
            self.variant_aliases[self.variant_idx].0
        } else {
            self.target.as_str()
        };
        self.auto_compact_windows = supported_auto_compact_windows_for(&self.template_type, alias);
        self.auto_compact_idx = current
            .and_then(|value| self.auto_compact_windows.iter().position(|x| *x == value))
            .unwrap_or(0);
        let total = self.total_rows();
        if total > 0 && self.cursor >= total {
            self.cursor = total - 1;
        }
    }
    fn cycle_auto_compact(&mut self, dir: i32) {
        let n = self.auto_compact_windows.len() as i32;
        if n == 0 {
            return;
        }
        self.auto_compact_idx = ((self.auto_compact_idx as i32 + dir).rem_euclid(n)) as usize;
    }

    fn build_apply(&mut self) -> Outcome {
        let Some(idx) = self.selected_key else {
            self.mode = Mode::Message("No key selected — add one first (n or ➕).".into());
            return Outcome::Continue;
        };
        let Some(src) = self.sources.get(idx).cloned() else {
            self.mode = Mode::Message("Selected key no longer available.".into());
            return Outcome::Continue;
        };
        // touch last-used for saved credentials
        if let ApiKeySource::Saved { credential } = &src
            && let Ok(store) = CredentialStore::new()
        {
            let _ = store.touch_last_used(credential.id());
        }
        Outcome::Apply(ApplySelection {
            key: ApiKeyChoice {
                key: src.api_key().to_string(),
                source: Some(src.to_key_ref()),
            },
            effort: Some(self.effort().to_string()),
            scope: self.scope(),
            co_author_off: !self.co_author,
            variant: if self.has_variant_row {
                Some(self.variant_aliases[self.variant_idx].0.to_string())
            } else {
                None
            },
            auto_compact_window: self.auto_compact_window(),
        })
    }

    fn try_delete(&mut self) {
        let idx = match self.row_at(self.cursor) {
            Row::Key(i) => i,
            _ => return,
        };
        match self.sources.get(idx) {
            Some(ApiKeySource::Saved { .. }) => self.mode = Mode::ConfirmDelete { idx },
            Some(ApiKeySource::EnvVar { .. }) => {
                self.mode = Mode::Message("Can't delete an env-var key from here.".into());
            }
            None => {}
        }
    }

    fn try_rename(&mut self) {
        let idx = match self.row_at(self.cursor) {
            Row::Key(i) => i,
            _ => return,
        };
        match self.sources.get(idx) {
            Some(ApiKeySource::Saved { credential }) => {
                self.mode = Mode::InputRename {
                    idx,
                    input: TextInput::new(credential.name()),
                };
            }
            Some(ApiKeySource::EnvVar { .. }) => {
                self.mode = Mode::Message("Can't rename an env-var key.".into());
            }
            None => {}
        }
    }

    fn reload_sources(&mut self) {
        if let Ok(src) = collect_api_key_sources(&self.template_type) {
            self.sources = src;
        }
        // keep cursor / selection valid
        let total = self.total_rows();
        if self.cursor >= total && total > 0 {
            self.cursor = total - 1;
        }
        if let Some(s) = self.selected_key
            && s >= self.sources.len()
        {
            self.selected_key = if self.sources.is_empty() {
                None
            } else {
                Some(0)
            };
        }
    }

    fn handle_input_newkey(&mut self, key: KeyEvent, mut input: TextInput) -> Outcome {
        match key.code {
            KeyCode::Esc => self.mode = Mode::Normal,
            KeyCode::Enter => {
                let value = input.value().trim().to_string();
                if value.is_empty() {
                    self.mode = Mode::Message("API key cannot be empty.".into());
                    return Outcome::Continue;
                }
                let tt = self.template_type.clone();
                match CredentialStore::new() {
                    Ok(store) => match store.create_credential_smart(&value, tt, None) {
                        Ok(cred) => {
                            self.mode = Mode::Normal;
                            self.reload_sources();
                            // select the freshly added key
                            if let Some(i) = self
                                .sources
                                .iter()
                                .position(|s| s.api_key() == cred.api_key())
                            {
                                self.selected_key = Some(i);
                                self.cursor = i;
                            }
                        }
                        Err(e) => self.mode = Mode::Message(format!("Failed to save: {e}")),
                    },
                    Err(e) => self.mode = Mode::Message(format!("Credential store error: {e}")),
                }
            }
            KeyCode::Backspace => input.backspace(),
            KeyCode::Delete => input.delete(),
            KeyCode::Left => input.move_left(),
            KeyCode::Right => input.move_right(),
            KeyCode::Home => input.move_start(),
            KeyCode::End => input.move_end(),
            KeyCode::Char(c) => input.insert(c),
            _ => {}
        }
        // keep editing unless a terminal mode was set above
        if matches!(self.mode, Mode::Normal) {
            // entered a branch that didn't reassign (plain editing) — restore input
            self.mode = Mode::InputNewKey(input);
        }
        Outcome::Continue
    }

    fn handle_input_rename(&mut self, key: KeyEvent, idx: usize, mut input: TextInput) -> Outcome {
        match key.code {
            KeyCode::Esc => self.mode = Mode::Normal,
            KeyCode::Enter => {
                let new_name = input.value().trim().to_string();
                if let Some(ApiKeySource::Saved { credential }) = self.sources.get(idx).cloned() {
                    if new_name.is_empty() {
                        self.mode = Mode::Message("Name cannot be empty.".into());
                        return Outcome::Continue;
                    }
                    if new_name != credential.name() {
                        match CredentialStore::new() {
                            Ok(store) => {
                                if let Err(e) = store.update_name(credential.id(), new_name) {
                                    self.mode = Mode::Message(format!("Rename failed: {e}"));
                                    return Outcome::Continue;
                                }
                            }
                            Err(e) => {
                                self.mode = Mode::Message(format!("Credential store error: {e}"));
                                return Outcome::Continue;
                            }
                        }
                    }
                }
                self.mode = Mode::Normal;
                self.reload_sources();
            }
            KeyCode::Backspace => input.backspace(),
            KeyCode::Delete => input.delete(),
            KeyCode::Left => input.move_left(),
            KeyCode::Right => input.move_right(),
            KeyCode::Home => input.move_start(),
            KeyCode::End => input.move_end(),
            KeyCode::Char(c) => input.insert(c),
            _ => {}
        }
        if matches!(self.mode, Mode::Normal) {
            self.mode = Mode::InputRename { idx, input };
        }
        Outcome::Continue
    }

    fn handle_confirm_delete(&mut self, key: KeyEvent, idx: usize) -> Outcome {
        match key.code {
            KeyCode::Enter | KeyCode::Char('y') | KeyCode::Char('Y') => {
                if let Some(ApiKeySource::Saved { credential }) = self.sources.get(idx).cloned() {
                    match CredentialStore::new() {
                        Ok(store) => {
                            if let Err(e) = store.delete_credential(credential.id()) {
                                self.mode = Mode::Message(format!("Delete failed: {e}"));
                                return Outcome::Continue;
                            }
                        }
                        Err(e) => {
                            self.mode = Mode::Message(format!("Credential store error: {e}"));
                            return Outcome::Continue;
                        }
                    }
                }
                self.mode = Mode::Normal;
                self.reload_sources();
            }
            _ => self.mode = Mode::Normal, // Esc / n / anything else cancels
        }
        Outcome::Continue
    }

    /// Borrow the current mode for rendering (input fields, popups).
    pub fn mode_ref(&self) -> &Mode {
        &self.mode
    }
}

#[cfg(test)]
mod snapshot_tests {
    //! Render the TUI to an in-memory buffer (ratatui `TestBackend`) and dump
    //! the text, so rendering can be inspected without a real terminal.
    //! Run: `cargo test snapshot_states -- --nocapture`.

    use super::*;
    use crate::credentials::CredentialData;
    use crate::templates::TemplateType;
    use crate::usage::{UsageReport, UsageStatus};
    use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
    use ratatui::{Terminal, backend::TestBackend, layout::Position};

    fn cred(name: &str, key: &str, last_used: Option<&str>) -> SavedCredentialStub {
        // SavedCredential == CredentialData
        let mut c = CredentialData::new(name.to_string(), key.to_string(), TemplateType::Zai);
        c.last_used_at = last_used.map(|s| s.to_string());
        c
    }
    // alias for clarity (SavedCredential is CredentialData)
    type SavedCredentialStub = CredentialData;

    fn base_app() -> App {
        App {
            template_type: TemplateType::Zai,
            target: "zai".into(),
            display_name: "ZAI China (智谱AI)".into(),
            current_label: "deepseek".into(),
            sources: vec![
                ApiKeySource::Saved {
                    credential: cred(
                        "work",
                        "sk-workkey-abcdef-1234-5678",
                        Some("2026-06-12 10:00:00 UTC"),
                    ),
                },
                ApiKeySource::Saved {
                    credential: cred("personal", "sk-personal-0987-6543-2100", None),
                },
                ApiKeySource::EnvVar {
                    env_var_name: "Z_AI_API_KEY".to_string(),
                    api_key: "sk-envvar-zzzz-yyyy-xxxx".to_string(),
                },
            ],
            selected_key: Some(0),
            cursor: 0,
            effort_idx: 0,
            scope_idx: 0,
            co_author: false,
            variant_aliases: variant_options(&TemplateType::Zai),
            variant_idx: 0,
            has_variant_row: true,
            auto_compact_windows: vec![
                AutoCompactWindow::K896,
                AutoCompactWindow::K768,
                AutoCompactWindow::K512,
                AutoCompactWindow::K256,
            ],
            auto_compact_idx: 0,
            usage_cache: HashMap::new(),
            usage_in_flight: None,
            usage_status: UsageStatus::Loading {
                stale: None,
                spinner: '-',
            },
            usage_status_key: None,
            spinner_idx: 0,
            mode: Mode::Normal,
        }
    }

    fn with_usage(mut app: App, status: UsageStatus) -> App {
        app.usage_status = status;
        app
    }

    fn usage_report(provider: &str, summary: &str) -> UsageReport {
        UsageReport {
            provider: provider.to_string(),
            summary: summary.to_string(),
        }
    }

    fn render(app: &App, w: u16, h: u16) -> String {
        let backend = TestBackend::new(w, h);
        let mut terminal = Terminal::new(backend).unwrap();
        terminal.draw(|f| crate::tui::view::render(f, app)).unwrap();
        let buf = terminal.backend().buffer();
        let area = buf.area();
        let mut out = String::new();
        for y in 0..area.height {
            let mut row = String::new();
            for x in 0..area.width {
                let sym = buf
                    .cell(Position { x, y })
                    .map(|c| c.symbol())
                    .unwrap_or(" ");
                row.push_str(sym);
            }
            out.push_str(row.trim_end());
            out.push('\n');
        }
        out
    }

    fn key(c: KeyCode) -> KeyEvent {
        KeyEvent::new(c, KeyModifiers::NONE)
    }

    fn banner(title: &str, body: &str) {
        println!("\n================= {title} =================\n{body}");
    }

    #[test]
    fn snapshot_states() {
        // 1. initial
        let app = base_app();
        banner(
            "1. INITIAL (cursor on work key, zai generic → variant row)",
            &render(&app, 76, 22),
        );

        // 2. cursor into options
        let mut app = base_app();
        for _ in 0..6 {
            app.handle_event(key(KeyCode::Down));
        }
        banner("2. AFTER Down x6", &render(&app, 76, 22));

        // 3. change effort
        let mut app = base_app();
        app.cursor = 4; // Effort row (3 keys + newkey + effort)
        app.handle_event(key(KeyCode::Left));
        banner("3. EFFORT changed via Left (→ low)", &render(&app, 76, 22));

        // 4. new-key input
        let mut app = base_app();
        app.handle_event(key(KeyCode::Char('n')));
        banner("4. NEW-KEY INPUT open", &render(&app, 76, 22));

        // 5. help
        let mut app = base_app();
        app.handle_event(key(KeyCode::Char('?')));
        banner("5. HELP overlay", &render(&app, 76, 22));

        // 6. confirm delete
        let mut app = base_app();
        app.cursor = 0;
        app.handle_event(key(KeyCode::Char('d')));
        banner("6. CONFIRM DELETE popup", &render(&app, 76, 22));

        // 7. no keys
        let mut app = base_app();
        app.sources.clear();
        app.selected_key = None;
        app.cursor = 0;
        app.refresh_usage(false);
        banner("7. NO KEYS (cursor on âž•)", &render(&app, 76, 22));

        // 8. narrow terminal
        let app = base_app();
        banner("8. NARROW 52x18", &render(&app, 52, 18));

        // 9. usage loading with stale cache
        let app = with_usage(
            base_app(),
            UsageStatus::Loading {
                stale: Some(usage_report(
                    "ZHIPU",
                    "5h 42%@18:00 | wk 150/1000 15%@06-22 00:00 | MCP 2/30 7%",
                )),
                spinner: '|',
            },
        );
        banner("9. USAGE LOADING", &render(&app, 76, 22));

        // 10. usage ready
        let app = with_usage(
            base_app(),
            UsageStatus::Ready(usage_report(
                "ZHIPU",
                "5h 42%@18:00 | wk 150/1000 15%@06-22 00:00 | MCP 2/30 7%",
            )),
        );
        banner("10. USAGE READY", &render(&app, 76, 22));

        // 11. usage error with stale data
        let app = with_usage(
            base_app(),
            UsageStatus::Error {
                message: "request failed".to_string(),
                stale: Some(usage_report(
                    "DeepSeek",
                    "available | CNY 12.34 (grant 2.00, top-up 10.34)",
                )),
            },
        );
        banner("11. USAGE ERROR", &render(&app, 76, 22));
    }

    #[test]
    fn auto_compact_row_changes_preview_and_selection() {
        let mut app = base_app();
        app.cursor = 8; // 3 keys + new-key + effort + scope + co-author + variant

        app.handle_event(key(KeyCode::Right));

        assert_eq!(app.auto_compact_window(), Some(AutoCompactWindow::K768));
        assert_eq!(
            app.preview_model_and_base().0,
            "glm-5.2[1m]",
            "auto compact must not remove the [1m] model suffix"
        );

        match app.handle_event(key(KeyCode::Char('a'))) {
            Outcome::Apply(selection) => {
                assert_eq!(selection.auto_compact_window, Some(AutoCompactWindow::K768));
            }
            _ => panic!("expected apply outcome"),
        }
    }

    #[test]
    fn auto_compact_row_requires_supported_1m_template() {
        let mut app = base_app();
        app.template_type = TemplateType::DeepSeek;
        app.target = "deepseek".into();
        app.variant_aliases.clear();
        app.has_variant_row = false;
        app.refresh_auto_compact_windows();

        assert_eq!(app.preview_model_and_base().0, "deepseek-v4-pro[1m]");
        assert!(!app.has_auto_compact_row());
        assert_eq!(app.auto_compact_window(), None);
    }

    #[test]
    fn usage_snapshot_states_cover_loading_ready_and_stale_error() {
        let loading = render(
            &with_usage(
                base_app(),
                UsageStatus::Loading {
                    stale: Some(usage_report(
                        "ZHIPU",
                        "5h 42%@18:00 | wk 150/1000 15%@06-22 00:00 | MCP 2/30 7%",
                    )),
                    spinner: '/',
                },
            ),
            76,
            22,
        );
        assert!(loading.contains("usage: / loading"));
        assert!(loading.contains("cached: ZHIPU 5h 42%"));
        assert!(!loading.contains("24h"));

        let ready = render(
            &with_usage(
                base_app(),
                UsageStatus::Ready(usage_report("DeepSeek", "available | CNY 12.34")),
            ),
            76,
            22,
        );
        assert!(ready.contains("usage: DeepSeek available | CNY 12.34"));

        let error = render(
            &with_usage(
                base_app(),
                UsageStatus::Error {
                    message: "request failed".to_string(),
                    stale: Some(usage_report("DeepSeek", "available | CNY 12.34")),
                },
            ),
            76,
            22,
        );
        assert!(error.contains("usage: error: request failed"));
        assert!(error.contains("cached: DeepSeek available | CNY 12.34"));
    }

    #[test]
    fn usage_loading_state_keeps_stale_cache_visible() {
        let mut app = base_app();
        let key = app.selected_usage_key().unwrap();
        app.usage_cache.insert(
            key.clone(),
            UsageCacheEntry {
                report: UsageReport {
                    provider: "ZHIPU".to_string(),
                    summary: "5h 42%@18:00".to_string(),
                },
                fetched_at: Instant::now() - USAGE_CACHE_TTL - Duration::from_secs(1),
            },
        );
        app.usage_status_key = Some(key.clone());
        app.usage_status = UsageStatus::Loading {
            stale: None,
            spinner: '-',
        };
        let (_sender, receiver) = mpsc::channel();
        app.usage_in_flight = Some(UsageInFlight {
            key: key.clone(),
            receiver,
        });

        app.tick();

        match app.usage_status() {
            UsageStatus::Loading { stale, .. } => {
                assert_eq!(
                    stale.as_ref().map(|report| report.summary.as_str()),
                    Some("5h 42%@18:00")
                );
            }
            other => panic!("expected loading usage status, got {other:?}"),
        }
    }
}