quetty 0.1.9

Terminal-based Azure Service Bus queue manager with intuitive TUI interface
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
use crate::components::common::{ConfigActivityMsg, ConfigUpdateData, Msg};
use crate::components::state::ComponentState;
use crate::config::{self, AppConfig};
use crate::constants::env_vars::*;
use crate::error::AppResult;
use crate::theme::ThemeManager;
use crate::utils::auth::{
    AUTH_METHOD_CLIENT_SECRET, AUTH_METHOD_CONNECTION_STRING, AUTH_METHOD_DEVICE_CODE,
};
use crate::utils::connection_string::ConnectionStringParser;
use tuirealm::command::{Cmd, CmdResult};
use tuirealm::event::KeyEvent;
use tuirealm::props::{Alignment, Style};
use tuirealm::ratatui::layout::{Constraint, Direction, Layout, Rect};
use tuirealm::ratatui::style::Modifier;
use tuirealm::ratatui::text::{Line, Span, Text};
use tuirealm::ratatui::widgets::{Block, Borders, Clear, List, ListItem, Paragraph};
use tuirealm::{
    AttrValue, Attribute, Component, Event, Frame, MockComponent, NoUserEvent, State, StateValue,
};

const CMD_RESULT_SAVE: &str = "Save";
const CMD_RESULT_CANCEL: &str = "Cancel";

// UI text constants
const PLACEHOLDER_ENCRYPTED_CONNECTION_STRING: &str = "<<encrypted-connection-string-present>>";
const PLACEHOLDER_ENCRYPTED_CLIENT_SECRET: &str = "<<encrypted-client-secret-present>>";
const UI_HINT_CYCLE: &str = " (Enter to cycle)";
const UI_CURSOR_INDICATOR: &str = "_";
const UI_EMPTY_FIELD: &str = "<empty>";
const UI_PASSWORD_MASK: &str = "*";
const UI_ENCRYPTED_DATA_MESSAGE: &str = "****** (encrypted data present - enter new to replace)";

// UI size constants
const PASSWORD_DISPLAY_LENGTH: usize = 20;
const MAX_INPUT_LENGTH: usize = 512;

#[derive(Debug, Clone)]
pub struct ConfigFormData {
    pub auth_method: String,
    pub tenant_id: String,
    pub client_id: String,
    pub client_secret: String,
    pub subscription_id: String,
    pub resource_group: String,
    pub namespace: String,
    pub connection_string: String,
    pub master_password: String,
    pub queue_name: String,
}

impl Default for ConfigFormData {
    fn default() -> Self {
        Self {
            auth_method: AUTH_METHOD_CONNECTION_STRING.to_string(),
            tenant_id: String::new(),
            client_id: String::new(),
            client_secret: String::new(),
            subscription_id: String::new(),
            resource_group: String::new(),
            namespace: String::new(),
            connection_string: String::new(),
            master_password: String::new(),
            queue_name: String::new(),
        }
    }
}

pub struct ConfigScreen {
    form_data: ConfigFormData,
    selected_field: usize,
    validation_errors: Vec<String>,
    editing_mode: bool,
    current_input: String,
}

impl Default for ConfigScreen {
    fn default() -> Self {
        Self::new()
    }
}

impl ConfigScreen {
    /// Helper to convert string fields to Option, returning None for empty/whitespace-only strings
    fn string_to_option(value: &str) -> Option<String> {
        if value.trim().is_empty() {
            None
        } else {
            Some(value.to_string())
        }
    }

    /// Create a new configuration screen with current configuration loaded
    ///
    /// Initializes the configuration form with values from the current app configuration
    /// and environment variables. Handles encrypted values by showing placeholders.
    /// Sets up the UI state for configuration editing and validation.
    ///
    /// # Returns
    /// A new ConfigScreen instance ready for user interaction
    pub fn new() -> Self {
        let config = config::get_config_or_panic();
        let form_data = Self::load_current_config(config);

        Self {
            form_data,
            selected_field: 0,
            validation_errors: Vec::new(),
            editing_mode: false,
            current_input: String::new(),
        }
    }

    fn load_current_config(config: &AppConfig) -> ConfigFormData {
        // Check if we have existing encrypted connection string
        let connection_string = if config.servicebus().has_connection_string() {
            // Show placeholder indicating encrypted data exists - user only needs to enter password
            PLACEHOLDER_ENCRYPTED_CONNECTION_STRING.to_string()
        } else {
            // No encrypted connection string - user needs to enter new one
            String::new()
        };

        ConfigFormData {
            auth_method: config.azure_ad().auth_method.clone(),
            tenant_id: std::env::var(AZURE_AD_TENANT_ID).unwrap_or_default(),
            client_id: std::env::var(AZURE_AD_CLIENT_ID).unwrap_or_default(),
            client_secret: if std::env::var(AZURE_AD_ENCRYPTED_CLIENT_SECRET).is_ok() {
                PLACEHOLDER_ENCRYPTED_CLIENT_SECRET.to_string()
            } else {
                std::env::var(AZURE_AD_CLIENT_SECRET).unwrap_or_default()
            },
            subscription_id: std::env::var(AZURE_AD_SUBSCRIPTION_ID).unwrap_or_default(),
            resource_group: std::env::var(AZURE_AD_RESOURCE_GROUP).unwrap_or_default(),
            namespace: std::env::var(AZURE_AD_NAMESPACE).unwrap_or_default(),
            connection_string,
            master_password: String::new(), // Never pre-populate password for security
            queue_name: std::env::var(SERVICEBUS_QUEUE_NAME).unwrap_or_default(),
        }
    }

    /// Check if we're in "unlock" mode (encrypted data exists, just need password)
    /// vs "setup" mode (need to configure everything from scratch)
    fn is_unlock_mode(&self) -> bool {
        (self.form_data.auth_method == "connection_string"
            && self
                .form_data
                .connection_string
                .contains(PLACEHOLDER_ENCRYPTED_CONNECTION_STRING))
            || (self.form_data.auth_method == "client_secret"
                && self
                    .form_data
                    .client_secret
                    .contains(PLACEHOLDER_ENCRYPTED_CLIENT_SECRET))
    }

    fn get_auth_methods() -> Vec<(&'static str, &'static str)> {
        vec![
            (
                AUTH_METHOD_CONNECTION_STRING,
                "Service Bus Connection String",
            ),
            (AUTH_METHOD_DEVICE_CODE, "Azure AD Device Code Flow"),
            (AUTH_METHOD_CLIENT_SECRET, "Azure AD Client Secret Flow"),
        ]
    }

    fn get_fields_for_auth_method(&self) -> Vec<(&'static str, &'static str, bool)> {
        match self.form_data.auth_method.as_str() {
            AUTH_METHOD_CONNECTION_STRING => {
                let connection_string_required = !self.is_unlock_mode();
                vec![
                    (
                        "connection_string",
                        "Connection String",
                        connection_string_required,
                    ),
                    ("master_password", "Master Password", true),
                    ("queue_name", "Queue Name", false),
                ]
            }
            AUTH_METHOD_DEVICE_CODE => vec![
                ("tenant_id", "Tenant ID", true),
                ("client_id", "Client ID", true),
                ("subscription_id", "Subscription ID", false),
                ("resource_group", "Resource Group", false),
                ("namespace", "Namespace", false),
            ],
            AUTH_METHOD_CLIENT_SECRET => vec![
                ("tenant_id", "Tenant ID", true),
                ("client_id", "Client ID", true),
                ("client_secret", "Client Secret", true),
                ("master_password", "Master Password", true),
                ("subscription_id", "Subscription ID", false),
                ("resource_group", "Resource Group", false),
                ("namespace", "Namespace", false),
            ],
            _ => vec![],
        }
    }

    fn get_field_value(&self, field: &str) -> &str {
        match field {
            "auth_method" => &self.form_data.auth_method,
            "tenant_id" => &self.form_data.tenant_id,
            "client_id" => &self.form_data.client_id,
            "client_secret" => &self.form_data.client_secret,
            "subscription_id" => &self.form_data.subscription_id,
            "resource_group" => &self.form_data.resource_group,
            "namespace" => &self.form_data.namespace,
            "connection_string" => &self.form_data.connection_string,
            "master_password" => &self.form_data.master_password,
            "queue_name" => &self.form_data.queue_name,
            _ => "",
        }
    }

    fn set_field_value(&mut self, field: &str, value: String) {
        match field {
            "auth_method" => self.form_data.auth_method = value,
            "tenant_id" => self.form_data.tenant_id = value,
            "client_id" => self.form_data.client_id = value,
            "client_secret" => self.form_data.client_secret = value,
            "subscription_id" => self.form_data.subscription_id = value,
            "resource_group" => self.form_data.resource_group = value,
            "namespace" => self.form_data.namespace = value,
            "connection_string" => self.form_data.connection_string = value,
            "master_password" => self.form_data.master_password = value,
            "queue_name" => self.form_data.queue_name = value,
            _ => {}
        }
    }

    fn validate_config(&self) -> Vec<String> {
        self.validate_config_internal(false)
    }

    fn validate_config_for_save_only(&self) -> Vec<String> {
        self.validate_config_internal(true)
    }

    fn validate_config_internal(&self, require_password_for_encrypted: bool) -> Vec<String> {
        let mut errors = Vec::new();

        match self.form_data.auth_method.as_str() {
            AUTH_METHOD_CONNECTION_STRING => {
                let config = crate::config::get_config_or_panic();
                let has_encrypted_connection = config.servicebus().has_connection_string();
                let connection_string = self.form_data.connection_string.trim();

                log::debug!(
                    "Config validation: has_encrypted_connection={}, connection_string='{}', master_password_len={}, require_password_for_encrypted={}",
                    has_encrypted_connection,
                    connection_string,
                    self.form_data.master_password.len(),
                    require_password_for_encrypted
                );

                if has_encrypted_connection
                    && (connection_string.is_empty()
                        || connection_string.contains("<<encrypted-connection-string-present>>"))
                {
                    // We have an existing encrypted connection string and user didn't provide a new one
                    // For save-only mode, require password. For save-and-proceed mode, allow password popup
                    if require_password_for_encrypted
                        && self.form_data.master_password.trim().is_empty()
                    {
                        errors.push(
                            "Master password is required to access existing connection string"
                                .to_string(),
                        );
                    }
                } else if connection_string.is_empty() {
                    // No connection string provided and no existing one
                    errors.push(
                        "Connection string is required for connection_string auth method"
                            .to_string(),
                    );
                } else {
                    // New connection string provided - validate it
                    if let Err(validation_error) =
                        ConnectionStringParser::validate_connection_string(connection_string)
                    {
                        errors.push(format!("Invalid connection string: {validation_error}"));
                    }

                    if self.form_data.master_password.trim().is_empty() {
                        errors.push(
                            "Master password is required for connection string encryption"
                                .to_string(),
                        );
                    }
                }
            }
            AUTH_METHOD_DEVICE_CODE => {
                if self.form_data.tenant_id.trim().is_empty() {
                    errors.push("Tenant ID is required for device code flow".to_string());
                }
                if self.form_data.client_id.trim().is_empty() {
                    errors.push("Client ID is required for device code flow".to_string());
                }
            }
            AUTH_METHOD_CLIENT_SECRET => {
                if self.form_data.tenant_id.trim().is_empty() {
                    errors.push("Tenant ID is required for client secret flow".to_string());
                }
                if self.form_data.client_id.trim().is_empty() {
                    errors.push("Client ID is required for client secret flow".to_string());
                }
                if self.form_data.client_secret.trim().is_empty() {
                    errors.push("Client Secret is required for client secret flow".to_string());
                } else if !self
                    .form_data
                    .client_secret
                    .contains(PLACEHOLDER_ENCRYPTED_CLIENT_SECRET)
                {
                    // New client secret provided - require master password for encryption
                    if self.form_data.master_password.trim().is_empty() {
                        errors.push(
                            "Master password is required to encrypt the client secret".to_string(),
                        );
                    }
                }
            }
            _ => {
                errors.push("Invalid authentication method selected".to_string());
            }
        }

        errors
    }

    fn render_authentication_tab(&self, f: &mut Frame, area: Rect) {
        let chunks = Layout::default()
            .direction(Direction::Vertical)
            .constraints([
                Constraint::Min(0),    // All fields including auth method
                Constraint::Length(3), // Actions
            ])
            .split(area);

        // Create a combined list with auth method + fields
        let auth_methods = Self::get_auth_methods();
        let fields = self.get_fields_for_auth_method();
        let mut all_items = Vec::new();

        // Add auth method selection as first item
        all_items.push(("auth_method", "Authentication Method", true));

        // Add other fields
        all_items.extend(fields);

        let field_items: Vec<ListItem> = all_items
            .iter()
            .enumerate()
            .map(|(i, (field, label, required))| {
                let is_selected = i == self.selected_field;
                let req_indicator = if *required { "*" } else { " " };

                let display_value = if *field == "auth_method" {
                    // Show current auth method selection with indicator
                    let method_desc = auth_methods
                        .iter()
                        .find(|(method, _)| *method == self.form_data.auth_method)
                        .map(|(_, desc)| desc.to_string())
                        .unwrap_or_else(|| self.form_data.auth_method.clone());
                    format!("{method_desc}{UI_HINT_CYCLE}")
                } else {
                    let value = self.get_field_value(field);

                    // Show editing state if this field is being edited
                    if self.editing_mode && is_selected {
                        if *field == "client_secret"
                            || *field == "connection_string"
                            || *field == "master_password"
                        {
                            format!(
                                "{}{UI_CURSOR_INDICATOR}",
                                UI_PASSWORD_MASK.repeat(self.current_input.len())
                            )
                        } else {
                            format!("{}{UI_CURSOR_INDICATOR}", self.current_input)
                        }
                    } else if *field == "client_secret" || *field == "master_password" {
                        if value.is_empty() {
                            UI_EMPTY_FIELD.to_string()
                        } else {
                            UI_PASSWORD_MASK.repeat(value.len().min(PASSWORD_DISPLAY_LENGTH))
                        }
                    } else if *field == "connection_string" {
                        if value.contains(PLACEHOLDER_ENCRYPTED_CONNECTION_STRING) {
                            UI_ENCRYPTED_DATA_MESSAGE.to_string()
                        } else if value.is_empty() {
                            UI_EMPTY_FIELD.to_string()
                        } else {
                            UI_PASSWORD_MASK.repeat(value.len().min(PASSWORD_DISPLAY_LENGTH))
                        }
                    } else if value.is_empty() {
                        UI_EMPTY_FIELD.to_string()
                    } else {
                        value.to_string()
                    }
                };

                let (label_style, value_style) = if is_selected {
                    let value_color = if *field == "auth_method" {
                        // Color-code auth method values when selected
                        match self.form_data.auth_method.as_str() {
                            "connection_string" => ThemeManager::status_success(), // Green for connection string
                            "device_code" => ThemeManager::status_warning(), // Yellow for device code
                            "client_secret" => ThemeManager::message_delivery_count(), // Magenta for client secret
                            _ => ThemeManager::text_primary(), // White for others
                        }
                    } else {
                        ThemeManager::text_primary() // White for other fields
                    };

                    (
                        Style::default()
                            .fg(ThemeManager::primary_accent()) // Bright cyan for selected field name
                            .add_modifier(Modifier::BOLD),
                        Style::default().fg(value_color),
                    )
                } else if *required && self.get_field_value(field).trim().is_empty() {
                    (
                        Style::default().fg(ThemeManager::status_error()), // Red for error field name
                        Style::default().fg(ThemeManager::text_primary()), // Value stays white even on error
                    )
                } else {
                    let value_color = if *field == "auth_method" {
                        // Color-code auth method values when not selected
                        match self.form_data.auth_method.as_str() {
                            "connection_string" => ThemeManager::status_success(), // Green for connection string
                            "device_code" => ThemeManager::status_warning(), // Yellow for device code
                            "client_secret" => ThemeManager::message_delivery_count(), // Magenta for client secret
                            _ => ThemeManager::text_primary(), // White for others
                        }
                    } else {
                        ThemeManager::text_primary() // White for other fields
                    };

                    (
                        Style::default().fg(ThemeManager::message_id()), // Light blue for normal field names
                        Style::default().fg(value_color),
                    )
                };

                ListItem::new(Line::from(vec![
                    Span::styled(format!("{req_indicator}{label}: "), label_style),
                    Span::styled(display_value, value_style),
                ]))
            })
            .collect();

        let fields_list = List::new(field_items).block(
            Block::default()
                .borders(Borders::ALL)
                .border_type(tuirealm::ratatui::widgets::BorderType::Rounded)
                .border_style(
                    tuirealm::ratatui::style::Style::default().fg(ThemeManager::primary_accent()),
                )
                .title("Configuration Manager")
                .title_style(
                    tuirealm::ratatui::style::Style::default().fg(ThemeManager::text_primary()),
                ),
        );

        f.render_widget(fields_list, chunks[0]);

        // Actions - styled exactly like help bar
        let actions_text = if self.editing_mode {
            vec![
                ("Type to edit".to_string(), false),
                (" ".to_string(), false),
                ("[Enter]".to_string(), true),
                (" confirm ".to_string(), false),
                ("[Esc]".to_string(), true),
                (" cancel editing".to_string(), false),
            ]
        } else {
            vec![
                ("[Enter]".to_string(), true),
                (" edit field/cycle auth ".to_string(), false),
                ("[↑↓]".to_string(), true),
                (" navigate ".to_string(), false),
                ("[Esc]".to_string(), true),
                (" cancel ".to_string(), false),
                ("[s]".to_string(), true),
                (" save for next startup ".to_string(), false),
                ("[Ctrl+S]".to_string(), true),
                (" save & login".to_string(), false),
            ]
        };

        let mut spans: Vec<Span> = Vec::new();

        // Add each shortcut pair with separators (exactly like help bar)
        for (i, (text, highlight)) in actions_text.iter().enumerate() {
            // Add separator before each pair (except the first one)
            if i > 0 && i % 2 == 0 {
                spans.push(Span::styled(
                    " │ ",
                    tuirealm::ratatui::style::Style::default().fg(ThemeManager::text_muted()),
                ));
            }

            // Add the shortcut text
            if *highlight {
                spans.push(Span::styled(
                    text.clone(),
                    tuirealm::ratatui::style::Style::default().fg(ThemeManager::shortcut_key()),
                ));
            } else {
                spans.push(Span::styled(
                    text.clone(),
                    tuirealm::ratatui::style::Style::default()
                        .fg(ThemeManager::shortcut_description()),
                ));
            }
        }

        let actions = Paragraph::new(Text::from(Line::from(spans)))
            .block(
                Block::default()
                    .borders(Borders::ALL)
                    .border_type(tuirealm::ratatui::widgets::BorderType::Rounded)
                    .border_style(
                        tuirealm::ratatui::style::Style::default()
                            .fg(ThemeManager::primary_accent()),
                    )
                    .title("Actions")
                    .title_style(
                        tuirealm::ratatui::style::Style::default().fg(ThemeManager::text_primary()),
                    ),
            )
            .alignment(Alignment::Center);

        f.render_widget(actions, chunks[1]);
    }

    fn to_config_update_data(&self) -> ConfigUpdateData {
        ConfigUpdateData {
            auth_method: self.form_data.auth_method.clone(),
            tenant_id: Self::string_to_option(&self.form_data.tenant_id),
            client_id: Self::string_to_option(&self.form_data.client_id),
            client_secret: Self::string_to_option(&self.form_data.client_secret),
            subscription_id: Self::string_to_option(&self.form_data.subscription_id),
            resource_group: Self::string_to_option(&self.form_data.resource_group),
            namespace: Self::string_to_option(&self.form_data.namespace),
            connection_string: Self::string_to_option(&self.form_data.connection_string),
            master_password: Self::string_to_option(&self.form_data.master_password),
            queue_name: Self::string_to_option(&self.form_data.queue_name),
        }
    }

    /// Handle keyboard events in a focused manner
    fn handle_keyboard_event(&mut self, key_event: KeyEvent) -> Option<Msg> {
        use tuirealm::event::Key;
        use tuirealm::event::KeyModifiers;

        match key_event {
            KeyEvent { code: Key::Esc, .. } => self.handle_escape_key(),
            KeyEvent {
                code: Key::Char('s'),
                modifiers: KeyModifiers::NONE,
                ..
            } => self.handle_save_key(),
            KeyEvent {
                code: Key::Char('s'),
                modifiers: KeyModifiers::CONTROL,
                ..
            } => self.handle_save_and_proceed_key(),
            KeyEvent { code: Key::Tab, .. } => None, // Tab no longer needed
            KeyEvent { code: Key::Up, .. } => {
                self.handle_navigation_key(tuirealm::command::Direction::Up)
            }
            KeyEvent {
                code: Key::Down, ..
            } => self.handle_navigation_key(tuirealm::command::Direction::Down),
            KeyEvent {
                code: Key::Left, ..
            } => self.handle_navigation_key(tuirealm::command::Direction::Left),
            KeyEvent {
                code: Key::Right, ..
            } => self.handle_navigation_key(tuirealm::command::Direction::Right),
            KeyEvent {
                code: Key::Enter, ..
            } => self.handle_enter_key(),
            KeyEvent {
                code: Key::Backspace,
                ..
            } => self.handle_backspace_key(),
            KeyEvent {
                code: Key::Char('C'),
                modifiers: KeyModifiers::CONTROL,
                ..
            } => self.handle_reload_config_key(),
            KeyEvent {
                code: Key::Char(c), ..
            } => self.handle_character_input(c),
            _ => self.handle_other_keyboard_events(),
        }
    }

    /// Handle Escape key press
    fn handle_escape_key(&mut self) -> Option<Msg> {
        if self.editing_mode {
            // Cancel editing
            self.editing_mode = false;
            self.current_input.clear();
            // Update global editing state to re-enable global key handling
            Some(Msg::SetEditingMode(false))
        } else {
            Some(Msg::ConfigActivity(ConfigActivityMsg::Cancel))
        }
    }

    /// Handle save key ('s') press
    fn handle_save_key(&mut self) -> Option<Msg> {
        if !self.editing_mode {
            log::debug!("S pressed - validating config for save without login");
            self.validation_errors = self.validate_config_for_save_only();
            if self.validation_errors.is_empty() {
                log::debug!("Validation passed - saving config for next startup");
                Some(Msg::ConfigActivity(ConfigActivityMsg::Save(
                    self.to_config_update_data(),
                )))
            } else {
                log::debug!("Validation failed - not saving");
                None
            }
        } else {
            // In editing mode, treat 's' as character input
            self.handle_character_input('s')
        }
    }

    /// Handle save and proceed key (Ctrl+S) press
    fn handle_save_and_proceed_key(&mut self) -> Option<Msg> {
        log::debug!("Ctrl+S pressed - validating config for save and proceed");
        self.validation_errors = self.validate_config();
        log::debug!("Validation errors: {:?}", self.validation_errors);
        if self.validation_errors.is_empty() {
            log::debug!("Validation passed - saving config and triggering login");
            Some(Msg::ConfigActivity(ConfigActivityMsg::ConfirmAndProceed(
                self.to_config_update_data(),
            )))
        } else {
            log::debug!("Validation failed - not proceeding");
            None
        }
    }

    /// Handle navigation key press
    fn handle_navigation_key(&mut self, direction: tuirealm::command::Direction) -> Option<Msg> {
        if self.editing_mode {
            None // Ignore navigation while editing
        } else {
            let result = self.perform(Cmd::Move(direction));
            if matches!(result, CmdResult::Changed(_)) {
                Some(Msg::ForceRedraw)
            } else {
                None
            }
        }
    }

    /// Handle Enter key press
    fn handle_enter_key(&mut self) -> Option<Msg> {
        if self.editing_mode {
            self.handle_enter_confirm_editing()
        } else {
            self.handle_enter_start_editing()
        }
    }

    /// Handle Enter key when in editing mode (confirm editing)
    fn handle_enter_confirm_editing(&mut self) -> Option<Msg> {
        let mut all_items = vec![("auth_method", "Authentication Method", true)];
        let fields = self.get_fields_for_auth_method();
        all_items.extend(fields);

        if let Some((field_name, _, _)) = all_items.get(self.selected_field) {
            self.set_field_value(field_name, self.current_input.clone());
        }

        self.editing_mode = false;
        self.current_input.clear();
        // Update global editing state to re-enable global key handling
        Some(Msg::SetEditingMode(false))
    }

    /// Handle Enter key when not in editing mode (start editing or toggle auth method)
    fn handle_enter_start_editing(&mut self) -> Option<Msg> {
        let mut all_items = vec![("auth_method", "Authentication Method", true)];
        let fields = self.get_fields_for_auth_method();
        all_items.extend(fields);

        if let Some((field_name, _, _)) = all_items.get(self.selected_field) {
            log::debug!("Enter pressed on field: {field_name}");

            if *field_name == "auth_method" {
                self.handle_auth_method_toggle()
            } else {
                self.start_field_editing(field_name)
            }
        } else {
            None
        }
    }

    /// Handle auth method toggle
    fn handle_auth_method_toggle(&mut self) -> Option<Msg> {
        let auth_methods = Self::get_auth_methods();
        let current_idx = auth_methods
            .iter()
            .position(|(method, _)| *method == self.form_data.auth_method)
            .unwrap_or(0);
        let next_idx = (current_idx + 1) % auth_methods.len();
        let new_auth_method = auth_methods[next_idx].0.to_string();

        log::debug!(
            "Auth method toggled from '{}' to '{}' - display only (not applied yet)",
            self.form_data.auth_method,
            new_auth_method
        );

        self.form_data.auth_method = new_auth_method;
        Some(Msg::ForceRedraw)
    }

    /// Start editing a field
    fn start_field_editing(&mut self, field_name: &str) -> Option<Msg> {
        self.editing_mode = true;
        // For sensitive fields, always start with empty input for security
        self.current_input = if field_name == "master_password" || field_name == "client_secret" {
            String::new()
        } else {
            self.get_field_value(field_name).to_string()
        };
        // Update global editing state to disable global key handling
        Some(Msg::SetEditingMode(true))
    }

    /// Handle Backspace key press
    fn handle_backspace_key(&mut self) -> Option<Msg> {
        if self.editing_mode {
            self.current_input.pop();
            Some(Msg::ForceRedraw)
        } else {
            None
        }
    }

    /// Handle config reload key (Ctrl+C) press
    fn handle_reload_config_key(&mut self) -> Option<Msg> {
        if !self.editing_mode {
            // Reload config data from current state
            log::debug!("ConfigScreen: Reloading configuration data");
            let config = crate::config::get_config_or_panic();
            self.form_data = Self::load_current_config(config);
            Some(Msg::ForceRedraw)
        } else {
            // In editing mode, treat 'C' as character input
            self.handle_character_input('C')
        }
    }

    /// Handle character input
    fn handle_character_input(&mut self, c: char) -> Option<Msg> {
        if self.editing_mode {
            log::debug!(
                "ConfigScreen: Adding character '{c}' to input (current length: {})",
                self.current_input.len()
            );
            // Add character to current input (limit length for practical reasons)
            if self.current_input.len() < MAX_INPUT_LENGTH {
                self.current_input.push(c);
                log::debug!("ConfigScreen: Current input now: '{}'", self.current_input);
                Some(Msg::ForceRedraw)
            } else {
                Some(Msg::ForceRedraw) // Still consume the event and redraw
            }
        } else {
            // When not editing, don't consume the event so global keys work
            None
        }
    }

    /// Handle other keyboard events
    fn handle_other_keyboard_events(&mut self) -> Option<Msg> {
        // When in editing mode, consume other keyboard events to prevent global key handling
        if self.editing_mode {
            Some(Msg::ForceRedraw)
        } else {
            None
        }
    }
}

impl ComponentState for ConfigScreen {
    fn mount(&mut self) -> AppResult<()> {
        log::debug!("Mounting ConfigScreen component");
        Ok(())
    }
}

impl MockComponent for ConfigScreen {
    fn view(&mut self, frame: &mut Frame, area: Rect) {
        // Clear the area
        frame.render_widget(Clear, area);

        // Create popup area (centered, 80% width, 80% height)
        let popup_width = area.width * 80 / 100;
        let popup_height = area.height * 80 / 100;
        let popup_x = (area.width - popup_width) / 2;
        let popup_y = (area.height - popup_height) / 2;
        let popup_area = Rect::new(popup_x, popup_y, popup_width, popup_height);

        // Create main layout
        let chunks = Layout::default()
            .direction(Direction::Vertical)
            .constraints([
                Constraint::Length(3), // Title
                Constraint::Min(0),    // Content
            ])
            .split(popup_area);

        // Title
        let title = Paragraph::new("Quetty Configuration")
            .block(
                Block::default()
                    .borders(Borders::ALL)
                    .border_type(tuirealm::ratatui::widgets::BorderType::Rounded)
                    .border_style(
                        tuirealm::ratatui::style::Style::default()
                            .fg(ThemeManager::primary_accent()),
                    ), // Same cyan as main config
            )
            .alignment(Alignment::Center)
            .style(
                tuirealm::ratatui::style::Style::default()
                    .fg(ThemeManager::message_delivery_count()) // Magenta - closest to #EA6F92
                    .add_modifier(Modifier::BOLD),
            );
        frame.render_widget(title, chunks[0]);

        // Just render authentication directly - no tabs needed
        self.render_authentication_tab(frame, chunks[1]);
    }

    fn query(&self, attr: Attribute) -> Option<AttrValue> {
        match attr {
            Attribute::Content => Some(AttrValue::String("ConfigScreen".to_string())),
            _ => None,
        }
    }

    fn attr(&mut self, _attr: Attribute, _value: AttrValue) {}

    fn state(&self) -> State {
        State::None
    }

    fn perform(&mut self, cmd: Cmd) -> CmdResult {
        match cmd {
            Cmd::Submit => {
                self.validation_errors = self.validate_config();
                if self.validation_errors.is_empty() {
                    CmdResult::Submit(State::One(StateValue::String(CMD_RESULT_SAVE.to_string())))
                } else {
                    CmdResult::None
                }
            }
            Cmd::Cancel => CmdResult::Submit(State::One(StateValue::String(
                CMD_RESULT_CANCEL.to_string(),
            ))),
            Cmd::Move(tuirealm::command::Direction::Up) => {
                if self.selected_field > 0 {
                    self.selected_field -= 1;
                }
                CmdResult::Changed(self.state())
            }
            Cmd::Move(tuirealm::command::Direction::Down) => {
                // Count total items (auth method + fields)
                let fields = self.get_fields_for_auth_method();
                let total_items = 1 + fields.len(); // 1 for auth method + fields
                if self.selected_field < total_items.saturating_sub(1) {
                    self.selected_field += 1;
                }
                CmdResult::Changed(self.state())
            }
            Cmd::Move(tuirealm::command::Direction::Left) => {
                // No horizontal navigation needed anymore
                CmdResult::None
            }
            Cmd::Move(tuirealm::command::Direction::Right) => {
                // No horizontal navigation needed anymore
                CmdResult::None
            }
            _ => CmdResult::None,
        }
    }
}

impl Component<Msg, NoUserEvent> for ConfigScreen {
    fn on(&mut self, ev: Event<NoUserEvent>) -> Option<Msg> {
        log::debug!("ConfigScreen received event: {ev:?}");
        match ev {
            Event::Keyboard(key_event) => self.handle_keyboard_event(key_event),
            _ => None,
        }
    }
}