stakpak 0.3.58

Stakpak: Your DevOps AI Agent. Generate infrastructure code, debug Kubernetes, configure CI/CD, automate deployments, without giving an LLM the keys to production.
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
//! Onboarding flow for first-time users
//!
//! This module provides a styled, interactive onboarding experience that guides users
//! through setting up their Stakpak configuration, including:
//! - Stakpak API authentication (OAuth flow)
//! - Provider selection (OpenAI, Anthropic, Google, Hybrid Providers, BYOM)
//! - Bring Your Own Model (BYOM) configuration
//! - Hybrid provider configurations (mixing providers)

mod byom;
pub mod config_templates;
pub mod menu;
pub mod navigation;
pub mod save_config;
mod styled_output;

use crate::apikey_auth::prompt_for_api_key;
use crate::config::AppConfig;
use crate::onboarding::byom::configure_byom;
use crate::onboarding::config_templates::{
    HybridModelConfig, config_to_toml_preview, generate_anthropic_profile, generate_gemini_profile,
    generate_hybrid_config, generate_openai_profile,
};
use crate::onboarding::menu::{
    prompt_password, prompt_profile_name, select_option, select_option_no_header,
};
use crate::onboarding::navigation::NavResult;
use crate::onboarding::save_config::{preview_and_save_to_profile, save_to_profile};
use crate::onboarding::styled_output::{StepStatus, render_profile_name};
use stakpak_shared::auth_manager::AuthManager;
use stakpak_shared::models::integrations::anthropic::AnthropicModel;
use stakpak_shared::models::integrations::gemini::GeminiModel;
use stakpak_shared::models::integrations::openai::OpenAIModel;
use stakpak_shared::models::model_pricing::ContextAware;
use stakpak_shared::oauth::{OAuthFlow, ProviderRegistry};
use std::io::{self, Write};

fn get_config_path_string(config: &AppConfig) -> String {
    if config.config_path.is_empty() {
        AppConfig::get_config_path::<&str>(None)
            .display()
            .to_string()
    } else {
        config.config_path.clone()
    }
}

/// Onboarding mode
pub enum OnboardingMode {
    /// Default onboarding for existing/default profile
    Default,
    /// Creating a new profile
    New,
}

/// Main onboarding flow entry point
pub async fn run_onboarding(config: &mut AppConfig, mode: OnboardingMode) {
    let profile_name = match mode {
        OnboardingMode::Default => {
            let profile = config.profile_name.clone();

            print!("\r\n");
            crate::onboarding::styled_output::render_title("Welcome to Stakpak");
            print!("\r\n");
            render_profile_name(&profile);
            print!("\r\n");
            crate::onboarding::styled_output::render_info(
                "Configuring stakpak. You can connect to Stakpak API or use your own model/API keys.",
            );
            print!("\r\n");

            profile
        }
        OnboardingMode::New => {
            print!("\r\n");
            crate::onboarding::styled_output::render_title("Creating new profile");
            print!("\r\n");

            let config_path = get_config_path_string(config);
            let custom_path = if config_path.is_empty() {
                None
            } else {
                Some(config_path.as_str())
            };

            let profile_name_result = prompt_profile_name(custom_path);
            let profile_name = match profile_name_result {
                NavResult::Forward(Some(name)) => name,
                NavResult::Forward(None) | NavResult::Back | NavResult::Cancel => {
                    crate::onboarding::styled_output::render_warning("Profile creation cancelled.");
                    return;
                }
            };

            print!("\x1b[2A");
            print!("\x1b[0J");
            print!("\r\n");
            render_profile_name(&profile_name);
            print!("\r\n");
            crate::onboarding::styled_output::render_info(
                "Configuring stakpak. You can connect to Stakpak API or use your own model/API keys.",
            );
            print!("\r\n");

            profile_name
        }
    };

    print!("\x1b[s");

    // Initial decision: Stakpak API or Own Keys
    loop {
        print!("\x1b[u");
        print!("\x1b[0J");
        print!("\x1b[K");
        let _ = io::stdout().flush();
        print!("\x1b[s");

        let initial_choice = select_option(
            "Choose authentication method",
            &[
                (
                    InitialChoice::StakpakAPI,
                    "Use Stakpak API (recommended)",
                    true,
                ),
                (
                    InitialChoice::OwnKeys,
                    "Use my own Model/API Key (or Claude Pro/Max Subscription)",
                    false,
                ),
            ],
            0,
            2,
            false, // Can't go back from first step
        );

        match initial_choice {
            NavResult::Forward(InitialChoice::StakpakAPI) => {
                print!("\x1b[u");
                print!("\x1b[0J");
                let _ = io::stdout().flush();

                match mode {
                    OnboardingMode::Default => {
                        prompt_for_api_key(config).await;
                    }
                    OnboardingMode::New => {
                        handle_stakpak_api_for_new_profile(config, &profile_name).await;
                    }
                }
                break;
            }
            NavResult::Forward(InitialChoice::OwnKeys) => {
                print!("\x1b[s");
                if handle_own_keys_flow(config, &profile_name).await {
                    break;
                }
                continue;
            }
            NavResult::Back => {
                break;
            }
            NavResult::Cancel => {
                print!("\x1b[u");
                print!("\x1b[0J");
                print!("\r\n");
                crate::onboarding::styled_output::render_warning(
                    "Onboarding cancelled. You can run this again later.",
                );
                print!("\r\n");
                break;
            }
        }
    }

    // Update config with the new profile name so callers can use it
    config.profile_name = profile_name;
}

async fn handle_own_keys_flow(config: &mut AppConfig, profile_name: &str) -> bool {
    let mut disclaimer_shown = false;

    loop {
        print!("\x1b[u");
        print!("\x1b[0J");
        print!("\x1b[K");
        let _ = io::stdout().flush();
        print!("\x1b[s");

        if !disclaimer_shown {
            crate::onboarding::styled_output::render_telemetry_disclaimer();
            disclaimer_shown = true;
        }

        let provider_choice = select_option(
            "Choose provider or bring your own model",
            &[
                (ProviderChoice::OpenAI, "OpenAI", false),
                (ProviderChoice::Anthropic, "Anthropic", false),
                (ProviderChoice::Google, "Google", false),
                (
                    ProviderChoice::Hybrid,
                    "Hybrid providers (e.g., Google and Anthropic)",
                    false,
                ),
                (ProviderChoice::Byom, "Bring your own model", false),
            ],
            1,
            3,
            true, // Can go back to previous step
        );

        let completed = match provider_choice {
            NavResult::Forward(ProviderChoice::Anthropic) => {
                handle_anthropic_provider_selection(config, profile_name).await
            }
            NavResult::Forward(ProviderChoice::OpenAI) => {
                handle_openai_setup(config, profile_name).await
            }
            NavResult::Forward(ProviderChoice::Google) => {
                handle_gemini_setup(config, profile_name).await
            }
            NavResult::Forward(ProviderChoice::Hybrid) => {
                handle_hybrid_setup(config, profile_name).await
            }
            NavResult::Forward(ProviderChoice::Byom) => {
                handle_byom_setup(config, profile_name).await
            }
            NavResult::Back => {
                print!("\x1b[u");
                print!("\x1b[0J");
                print!("\x1b[K");
                let _ = io::stdout().flush();
                print!("\x1b[s");
                return false;
            }
            NavResult::Cancel => {
                return false;
            }
        };

        if completed {
            return true;
        }
    }
}

/// Handle Anthropic provider selection (subscription vs API key)
/// Returns true if completed, false if cancelled/back
async fn handle_anthropic_provider_selection(config: &mut AppConfig, profile_name: &str) -> bool {
    // Clear previous step content
    print!("\x1b[u");
    print!("\x1b[0J");
    print!("\x1b[K");
    let _ = io::stdout().flush();
    print!("\x1b[s");

    // Show sub-menu for Anthropic auth method
    let auth_choice = select_option(
        "Choose Anthropic authentication method",
        &[
            (
                AnthropicAuthChoice::ClaudeSubscription,
                "Claude Pro/Max Subscription",
                false,
            ),
            (AnthropicAuthChoice::ApiKey, "API Key", false),
        ],
        2,
        4,
        true, // Can go back
    );

    match auth_choice {
        NavResult::Forward(AnthropicAuthChoice::ClaudeSubscription) => {
            handle_claude_subscription_setup(config, profile_name).await
        }
        NavResult::Forward(AnthropicAuthChoice::ApiKey) => {
            handle_anthropic_api_key_setup(config, profile_name).await
        }
        NavResult::Back => {
            // Go back to provider selection
            print!("\x1b[u");
            print!("\x1b[0J");
            print!("\x1b[K");
            let _ = io::stdout().flush();
            false
        }
        NavResult::Cancel => false,
    }
}

/// Handle Claude Pro/Max Subscription setup via OAuth
/// Returns true if completed, false if cancelled/back
async fn handle_claude_subscription_setup(config: &mut AppConfig, profile_name: &str) -> bool {
    // Clear previous step content WITHOUT touching welcome message
    print!("\x1b[u");
    print!("\x1b[0J"); // Clear from cursor to end of screen
    print!("\x1b[K"); // Clear current line
    let _ = io::stdout().flush();

    // Render step 3 - start immediately after welcome message (no extra newline)
    crate::onboarding::styled_output::render_title("Claude Pro/Max Subscription");
    print!("\r\n");

    // Show step indicators on one line
    let steps = vec![
        ("Step 1".to_string(), StepStatus::Completed),
        ("Step 2".to_string(), StepStatus::Completed),
        ("Step 3".to_string(), StepStatus::Active),
    ];
    crate::onboarding::styled_output::render_steps(&steps);
    print!("\r\n");

    // Show info about Claude subscription
    crate::onboarding::styled_output::render_info(
        "Use your existing Claude Pro or Max subscription to access Claude models.",
    );
    print!("\r\n");

    // Show default models
    crate::onboarding::styled_output::render_default_models(
        &AnthropicModel::DEFAULT_SMART_MODEL.model_name(),
        &AnthropicModel::DEFAULT_ECO_MODEL.model_name(),
    );

    // Get OAuth provider and config
    let registry = ProviderRegistry::new();
    let provider = match registry.get("anthropic") {
        Some(p) => p,
        None => {
            crate::onboarding::styled_output::render_error("Anthropic provider not found");
            return false;
        }
    };

    let oauth_config = match provider.oauth_config("claude-max") {
        Some(c) => c,
        None => {
            crate::onboarding::styled_output::render_error("OAuth not supported for this method");
            return false;
        }
    };

    // Start OAuth flow
    let mut flow = OAuthFlow::new(oauth_config);
    let auth_url = flow.generate_auth_url();

    print!("\r\n");
    crate::onboarding::styled_output::render_info("Opening browser for authentication...");
    print!("\r\n");
    println!("If browser doesn't open, visit:");
    // Use OSC 8 escape sequence to make the URL clickable in supported terminals
    println!("\x1b]8;;{}\x1b\\{}\x1b]8;;\x1b\\", auth_url, auth_url);
    print!("\r\n");

    // Try to open browser
    let _ = open::that(&auth_url);

    // Prompt for authorization code
    print!("Paste the authorization code: ");
    io::stdout().flush().ok();

    let mut code = String::new();
    if io::stdin().read_line(&mut code).is_err() {
        crate::onboarding::styled_output::render_error("Failed to read input");
        return false;
    }
    let code = code.trim();

    if code.is_empty() {
        crate::onboarding::styled_output::render_warning("Authentication cancelled.");
        return false;
    }

    print!("\r\n");
    crate::onboarding::styled_output::render_info("Exchanging code for tokens...");

    // Exchange code for tokens
    let tokens = match flow.exchange_code(code).await {
        Ok(t) => t,
        Err(e) => {
            print!("\r\n");
            crate::onboarding::styled_output::render_error(&format!(
                "Token exchange failed: {}",
                e
            ));
            return false;
        }
    };

    // Post-authorize to get final auth
    let auth = match provider.post_authorize("claude-max", &tokens).await {
        Ok(a) => a,
        Err(e) => {
            print!("\r\n");
            crate::onboarding::styled_output::render_error(&format!(
                "Post-authorization failed: {}",
                e
            ));
            return false;
        }
    };

    // Get config directory
    let config_dir = config.get_config_dir();

    // Save credentials to auth.toml
    let mut auth_manager = match AuthManager::new(&config_dir) {
        Ok(m) => m,
        Err(e) => {
            crate::onboarding::styled_output::render_error(&format!(
                "Failed to load auth manager: {}",
                e
            ));
            return false;
        }
    };

    if let Err(e) = auth_manager.set(profile_name, "anthropic", auth) {
        crate::onboarding::styled_output::render_error(&format!(
            "Failed to save credentials: {}",
            e
        ));
        return false;
    }

    // Generate profile config (credentials are in auth.toml)
    let profile = generate_anthropic_profile();

    // Save profile config
    let config_path = get_config_path_string(config);
    let telemetry = match save_to_profile(&config_path, profile_name, profile.clone()) {
        Ok(t) => t,
        Err(e) => {
            crate::onboarding::styled_output::render_error(&format!(
                "Failed to save configuration: {}",
                e
            ));
            std::process::exit(1);
        }
    };

    print!("\r\n");
    crate::onboarding::styled_output::render_success("Successfully logged in to Claude!");
    print!("\r\n");
    crate::onboarding::styled_output::render_success("Configuration saved successfully");
    print!("\r\n");

    // Update AppConfig with saved values so we can use them immediately
    config.provider = profile
        .provider
        .unwrap_or(crate::config::ProviderType::Local);
    config.providers = profile.providers.clone();
    config.smart_model = profile.smart_model.clone();
    config.eco_model = profile.eco_model.clone();
    config.anonymous_id = telemetry.anonymous_id;
    config.collect_telemetry = telemetry.collect_telemetry;

    true
}

/// Handle OpenAI setup
/// Returns true if completed, false if cancelled/back
async fn handle_openai_setup(config: &mut AppConfig, profile_name: &str) -> bool {
    // Clear previous step content WITHOUT touching welcome message
    print!("\x1b[u");
    print!("\x1b[0J"); // Clear from cursor to end of screen
    print!("\x1b[K"); // Clear current line
    let _ = io::stdout().flush();

    // Render step 3 - start immediately after welcome message (no extra newline)
    crate::onboarding::styled_output::render_title("OpenAI Configuration");
    print!("\r\n");

    // Show step indicators on one line
    let steps = vec![
        ("Step 1".to_string(), StepStatus::Completed),
        ("Step 2".to_string(), StepStatus::Completed),
        ("Step 3".to_string(), StepStatus::Active),
    ];
    crate::onboarding::styled_output::render_steps(&steps);
    print!("\r\n");

    // Show default models (using same names as config templates)
    crate::onboarding::styled_output::render_default_models(
        &OpenAIModel::DEFAULT_SMART_MODEL.model_name(),
        &OpenAIModel::DEFAULT_ECO_MODEL.model_name(),
    );

    match prompt_password("Enter your OpenAI API key", true) {
        NavResult::Forward(Some(api_key)) => {
            // Save API key to auth.toml
            let config_dir = config.get_config_dir();
            let mut auth_manager = match AuthManager::new(&config_dir) {
                Ok(m) => m,
                Err(e) => {
                    crate::onboarding::styled_output::render_error(&format!(
                        "Failed to load auth manager: {}",
                        e
                    ));
                    return false;
                }
            };

            let auth = stakpak_shared::models::auth::ProviderAuth::api_key(&api_key);
            if let Err(e) = auth_manager.set(profile_name, "openai", auth) {
                crate::onboarding::styled_output::render_error(&format!(
                    "Failed to save credentials: {}",
                    e
                ));
                return false;
            }

            // Generate profile config (credentials are in auth.toml)
            let profile = generate_openai_profile();

            crate::onboarding::styled_output::render_config_preview(&config_to_toml_preview(
                &profile,
            ));

            match crate::onboarding::menu::prompt_yes_no("Proceed with this configuration?", true) {
                NavResult::Forward(Some(true)) | NavResult::Forward(None) => {
                    let config_path = get_config_path_string(config);

                    let telemetry =
                        match save_to_profile(&config_path, profile_name, profile.clone()) {
                            Ok(t) => t,
                            Err(e) => {
                                crate::onboarding::styled_output::render_error(&format!(
                                    "Failed to save configuration: {}",
                                    e
                                ));
                                std::process::exit(1);
                            }
                        };

                    print!("\r\n");
                    crate::onboarding::styled_output::render_success(
                        "✓ Configuration saved successfully",
                    );
                    print!("\r\n");

                    config.provider = profile
                        .provider
                        .unwrap_or(crate::config::ProviderType::Local);
                    config.providers = profile.providers.clone();
                    config.smart_model = profile.smart_model.clone();
                    config.eco_model = profile.eco_model.clone();
                    config.anonymous_id = telemetry.anonymous_id;
                    config.collect_telemetry = telemetry.collect_telemetry;

                    true
                }
                NavResult::Forward(Some(false)) | NavResult::Back | NavResult::Cancel => false,
            }
        }
        NavResult::Back => false,
        NavResult::Forward(None) | NavResult::Cancel => false,
    }
}

/// Handle Gemini setup
/// Returns true if completed, false if cancelled/back
async fn handle_gemini_setup(config: &mut AppConfig, profile_name: &str) -> bool {
    // Clear previous step content WITHOUT touching welcome message
    print!("\x1b[u");
    print!("\x1b[0J"); // Clear from cursor to end of screen
    print!("\x1b[K"); // Clear current line
    let _ = io::stdout().flush();

    // Render step 3 - start immediately after welcome message (no extra newline)
    crate::onboarding::styled_output::render_title("Gemini Configuration");
    print!("\r\n");

    // Show step indicators on one line
    let steps = vec![
        ("Step 1".to_string(), StepStatus::Completed),
        ("Step 2".to_string(), StepStatus::Completed),
        ("Step 3".to_string(), StepStatus::Active),
    ];
    crate::onboarding::styled_output::render_steps(&steps);
    print!("\r\n");

    // Show default models (using same names as config templates)
    crate::onboarding::styled_output::render_default_models(
        &GeminiModel::DEFAULT_SMART_MODEL.model_name(),
        &GeminiModel::DEFAULT_ECO_MODEL.model_name(),
    );

    match prompt_password("Enter your Gemini API key", true) {
        NavResult::Forward(Some(api_key)) => {
            // Save API key to auth.toml
            let config_dir = config.get_config_dir();
            let mut auth_manager = match AuthManager::new(&config_dir) {
                Ok(m) => m,
                Err(e) => {
                    crate::onboarding::styled_output::render_error(&format!(
                        "Failed to load auth manager: {}",
                        e
                    ));
                    return false;
                }
            };

            let auth = stakpak_shared::models::auth::ProviderAuth::api_key(&api_key);
            if let Err(e) = auth_manager.set(profile_name, "gemini", auth) {
                crate::onboarding::styled_output::render_error(&format!(
                    "Failed to save credentials: {}",
                    e
                ));
                return false;
            }

            // Generate profile config (credentials are in auth.toml)
            let profile = generate_gemini_profile();

            // Show confirmation
            crate::onboarding::styled_output::render_config_preview(&config_to_toml_preview(
                &profile,
            ));

            match crate::onboarding::menu::prompt_yes_no("Proceed with this configuration?", true) {
                NavResult::Forward(Some(true)) | NavResult::Forward(None) => {
                    let config_path = get_config_path_string(config);

                    let telemetry =
                        match save_to_profile(&config_path, profile_name, profile.clone()) {
                            Ok(t) => t,
                            Err(e) => {
                                crate::onboarding::styled_output::render_error(&format!(
                                    "Failed to save configuration: {}",
                                    e
                                ));
                                std::process::exit(1);
                            }
                        };

                    print!("\r\n");
                    crate::onboarding::styled_output::render_success(
                        "✓ Configuration saved successfully",
                    );
                    print!("\r\n");

                    // Update AppConfig with saved values so we can use them immediately
                    config.provider = profile
                        .provider
                        .unwrap_or(crate::config::ProviderType::Local);
                    config.providers = profile.providers.clone();
                    config.smart_model = profile.smart_model.clone();
                    config.eco_model = profile.eco_model.clone();
                    config.anonymous_id = telemetry.anonymous_id;
                    config.collect_telemetry = telemetry.collect_telemetry;

                    true
                }
                NavResult::Forward(Some(false)) | NavResult::Back | NavResult::Cancel => false,
            }
        }
        NavResult::Back => false,
        NavResult::Forward(None) | NavResult::Cancel => false,
    }
}

/// Handle Anthropic API key setup
/// Returns true if completed, false if cancelled/back
async fn handle_anthropic_api_key_setup(config: &mut AppConfig, profile_name: &str) -> bool {
    // Clear previous step content WITHOUT touching welcome message
    print!("\x1b[u");
    print!("\x1b[0J"); // Clear from cursor to end of screen
    print!("\x1b[K"); // Clear current line
    let _ = io::stdout().flush();

    // Render step 4 - API key configuration
    crate::onboarding::styled_output::render_title("Anthropic API Key Configuration");
    print!("\r\n");

    // Show step indicators on one line
    let steps = vec![
        ("Step 1".to_string(), StepStatus::Completed),
        ("Step 2".to_string(), StepStatus::Completed),
        ("Step 3".to_string(), StepStatus::Completed),
        ("Step 4".to_string(), StepStatus::Active),
    ];
    crate::onboarding::styled_output::render_steps(&steps);
    print!("\r\n");

    // Show default models (using same names as config templates)
    crate::onboarding::styled_output::render_default_models(
        &AnthropicModel::DEFAULT_SMART_MODEL.model_name(),
        &AnthropicModel::DEFAULT_ECO_MODEL.model_name(),
    );

    match prompt_password("Enter your Anthropic API key", true) {
        NavResult::Forward(Some(api_key)) => {
            // Save API key to auth.toml
            let config_dir = config.get_config_dir();
            let mut auth_manager = match AuthManager::new(&config_dir) {
                Ok(m) => m,
                Err(e) => {
                    crate::onboarding::styled_output::render_error(&format!(
                        "Failed to load auth manager: {}",
                        e
                    ));
                    return false;
                }
            };

            let auth = stakpak_shared::models::auth::ProviderAuth::api_key(&api_key);
            if let Err(e) = auth_manager.set(profile_name, "anthropic", auth) {
                crate::onboarding::styled_output::render_error(&format!(
                    "Failed to save credentials: {}",
                    e
                ));
                return false;
            }

            // Generate profile config (credentials are in auth.toml)
            let profile = generate_anthropic_profile();

            // Show confirmation
            crate::onboarding::styled_output::render_config_preview(&config_to_toml_preview(
                &profile,
            ));

            match crate::onboarding::menu::prompt_yes_no("Proceed with this configuration?", true) {
                NavResult::Forward(Some(true)) | NavResult::Forward(None) => {
                    let config_path = get_config_path_string(config);

                    let telemetry =
                        match save_to_profile(&config_path, profile_name, profile.clone()) {
                            Ok(t) => t,
                            Err(e) => {
                                crate::onboarding::styled_output::render_error(&format!(
                                    "Failed to save configuration: {}",
                                    e
                                ));
                                std::process::exit(1);
                            }
                        };

                    print!("\r\n");
                    crate::onboarding::styled_output::render_success(
                        "✓ Configuration saved successfully",
                    );
                    print!("\r\n");

                    // Update AppConfig with saved values so we can use them immediately
                    config.provider = profile
                        .provider
                        .unwrap_or(crate::config::ProviderType::Local);
                    config.providers = profile.providers.clone();
                    config.smart_model = profile.smart_model.clone();
                    config.eco_model = profile.eco_model.clone();
                    config.anonymous_id = telemetry.anonymous_id;
                    config.collect_telemetry = telemetry.collect_telemetry;

                    true
                }
                NavResult::Forward(Some(false)) | NavResult::Back | NavResult::Cancel => false,
            }
        }
        NavResult::Back => false,
        NavResult::Forward(None) | NavResult::Cancel => false,
    }
}

/// Handle Hybrid Providers setup
/// Returns true if completed, false if cancelled/back
async fn handle_hybrid_setup(config: &mut AppConfig, profile_name: &str) -> bool {
    let config_path = get_config_path_string(config);

    // Clear previous step content WITHOUT touching welcome message
    print!("\x1b[u");
    print!("\x1b[0J"); // Clear from cursor to end of screen
    print!("\x1b[K"); // Clear current line
    let _ = io::stdout().flush();

    // Render step 3 - start immediately after welcome message (no extra newline)
    crate::onboarding::styled_output::render_title("Hybrid Providers Configuration");
    print!("\r\n");

    // Show step indicators on one line
    let steps = vec![
        ("Step 1".to_string(), StepStatus::Completed),
        ("Step 2".to_string(), StepStatus::Completed),
        ("Step 3".to_string(), StepStatus::Active),
    ];
    crate::onboarding::styled_output::render_steps(&steps);
    print!("\r\n");

    crate::onboarding::styled_output::render_info(
        "You'll configure smart_model and eco_model separately, each from different providers if desired.",
    );
    print!("\r\n");

    // Configure smart model
    crate::onboarding::styled_output::render_subtitle("Configure Smart Model");
    let smart = match configure_hybrid_model(None) {
        Some(model) => model,
        None => return false,
    };

    print!("\r\n");

    // Configure eco model - pass smart provider to reuse API key if same
    crate::onboarding::styled_output::render_subtitle("Configure Eco Model");
    let eco = match configure_hybrid_model(Some(&smart)) {
        Some(model) => model,
        None => return false,
    };

    let profile = generate_hybrid_config(smart, eco);

    // Show confirmation
    crate::onboarding::styled_output::render_config_preview(&config_to_toml_preview(&profile));

    match crate::onboarding::menu::prompt_yes_no("Proceed with this configuration?", true) {
        NavResult::Forward(Some(true)) | NavResult::Forward(None) => {
            let telemetry = match save_to_profile(&config_path, profile_name, profile.clone()) {
                Ok(t) => t,
                Err(e) => {
                    crate::onboarding::styled_output::render_error(&format!(
                        "Failed to save configuration: {}",
                        e
                    ));
                    std::process::exit(1);
                }
            };

            print!("\r\n");
            crate::onboarding::styled_output::render_success("✓ Configuration saved successfully");
            print!("\r\n");

            // Update AppConfig with saved values so we can use them immediately
            config.provider = profile
                .provider
                .unwrap_or(crate::config::ProviderType::Local);
            config.providers = profile.providers.clone();
            config.smart_model = profile.smart_model.clone();
            config.eco_model = profile.eco_model.clone();
            config.recovery_model = profile.recovery_model.clone();
            if let Some(key) = &profile.api_key {
                config.api_key = Some(key.clone());
            }
            config.anonymous_id = telemetry.anonymous_id;
            config.collect_telemetry = telemetry.collect_telemetry;

            true
        }
        NavResult::Forward(Some(false)) | NavResult::Back | NavResult::Cancel => false,
    }
}

/// Configure a single model for hybrid setup
/// If `previous_config` is provided and uses the same provider, reuse the API key
fn configure_hybrid_model(
    previous_config: Option<&HybridModelConfig>,
) -> Option<HybridModelConfig> {
    use crate::onboarding::config_templates::HybridProvider;

    // Select provider
    let providers = [
        (HybridProvider::OpenAI, "OpenAI", false),
        (HybridProvider::Gemini, "Gemini", false),
        (HybridProvider::Anthropic, "Anthropic", false),
    ];

    let provider = match select_option_no_header(&providers, true) {
        NavResult::Forward(p) => p,
        NavResult::Back | NavResult::Cancel => return None,
    };

    // Select model based on provider
    let model = select_model_for_provider(&provider)?;

    // Check if we can reuse API key from previous config
    let api_key = if let Some(prev) = previous_config {
        if prev.provider == provider {
            // Same provider, reuse API key
            prev.api_key.clone()
        } else {
            // Different provider, ask for API key
            match crate::onboarding::menu::prompt_password(
                &format!("Enter {} API key", provider.as_str()),
                true,
            ) {
                NavResult::Forward(Some(key)) => key,
                NavResult::Forward(None) | NavResult::Back | NavResult::Cancel => return None,
            }
        }
    } else {
        // No previous config, ask for API key
        match crate::onboarding::menu::prompt_password(
            &format!("Enter {} API key", provider.as_str()),
            true,
        ) {
            NavResult::Forward(Some(key)) => key,
            NavResult::Forward(None) | NavResult::Back | NavResult::Cancel => return None,
        }
    };

    Some(HybridModelConfig {
        provider,
        model,
        api_key,
    })
}

/// Select model for a provider
fn select_model_for_provider(
    provider: &crate::onboarding::config_templates::HybridProvider,
) -> Option<String> {
    use std::collections::HashSet;

    let (candidates, smart_id) = match provider {
        crate::onboarding::config_templates::HybridProvider::OpenAI => {
            let smart = OpenAIModel::DEFAULT_SMART_MODEL;
            let eco = OpenAIModel::DEFAULT_ECO_MODEL;
            (
                vec![
                    (smart.to_string(), smart.model_name()),
                    (eco.to_string(), eco.model_name()),
                ],
                smart.to_string(),
            )
        }
        crate::onboarding::config_templates::HybridProvider::Gemini => {
            let smart = GeminiModel::DEFAULT_SMART_MODEL;
            let eco = GeminiModel::DEFAULT_ECO_MODEL;
            (
                vec![
                    (smart.to_string(), smart.model_name()),
                    (eco.to_string(), eco.model_name()),
                ],
                smart.to_string(),
            )
        }
        crate::onboarding::config_templates::HybridProvider::Anthropic => {
            let smart = AnthropicModel::DEFAULT_SMART_MODEL;
            let eco = AnthropicModel::DEFAULT_ECO_MODEL;
            (
                vec![
                    (smart.to_string(), smart.model_name()),
                    (eco.to_string(), eco.model_name()),
                ],
                smart.to_string(),
            )
        }
    };

    let mut options = Vec::new();
    let mut seen = HashSet::new();

    for (id, name) in candidates {
        if !seen.insert(id.clone()) {
            continue;
        }
        let is_recommended = id == smart_id;
        options.push((id, name, is_recommended));
    }

    let options_refs: Vec<(String, &str, bool)> = options
        .iter()
        .map(|(id, name, rec)| (id.clone(), name.as_str(), *rec))
        .collect();

    match select_option_no_header(&options_refs, true) {
        NavResult::Forward(model) => Some(model),
        NavResult::Back | NavResult::Cancel => None,
    }
}

/// Handle BYOM / Custom Provider setup
/// Returns true if completed, false if cancelled/back
async fn handle_byom_setup(config: &mut AppConfig, profile_name: &str) -> bool {
    let config_path = get_config_path_string(config);

    if let Some(profile) = configure_byom(2, 4) {
        let telemetry =
            match preview_and_save_to_profile(&config_path, profile_name, profile.clone()) {
                Ok(t) => t,
                Err(e) => {
                    crate::onboarding::styled_output::render_error(&format!(
                        "Failed to save configuration: {}",
                        e
                    ));
                    std::process::exit(1);
                }
            };

        // Update AppConfig with saved values so we can use them immediately
        config.provider = profile
            .provider
            .unwrap_or(crate::config::ProviderType::Local);
        config.providers = profile.providers.clone();
        config.smart_model = profile.smart_model.clone();
        config.eco_model = profile.eco_model.clone();
        config.recovery_model = profile.recovery_model.clone();
        if let Some(key) = &profile.api_key {
            config.api_key = Some(key.clone());
        }
        config.anonymous_id = telemetry.anonymous_id;
        config.collect_telemetry = telemetry.collect_telemetry;

        true
    } else {
        crate::onboarding::styled_output::render_warning(
            "Custom provider configuration cancelled.",
        );
        false
    }
}

/// Handle Stakpak API setup for a new profile
/// Saves API key to the new profile, copying endpoint from default but using new API key
async fn handle_stakpak_api_for_new_profile(config: &AppConfig, profile_name: &str) {
    use crate::apikey_auth::prompt_for_api_key;

    // Create a temporary config with the new profile name for OAuth flow
    let mut temp_config = config.clone();
    temp_config.profile_name = profile_name.to_string();

    // Get the API key via OAuth flow (this will update temp_config)
    prompt_for_api_key(&mut temp_config).await;

    // Now save the new profile with the API key and endpoint
    let config_path = get_config_path_string(config);

    // Create profile config with Remote provider, new API key, and same endpoint
    use crate::config::ProfileConfig;
    use crate::config::ProviderType;
    let new_profile = ProfileConfig {
        provider: Some(ProviderType::Remote),
        api_key: temp_config.api_key.clone(),
        api_endpoint: Some(config.api_endpoint.clone()), // Copy endpoint from default
        ..ProfileConfig::default()
    };

    // Save to the new profile
    if let Err(e) =
        crate::onboarding::save_config::save_to_profile(&config_path, profile_name, new_profile)
    {
        crate::onboarding::styled_output::render_error(&format!(
            "Failed to save configuration: {}",
            e
        ));
        std::process::exit(1);
    }

    print!("\r\n");
    crate::onboarding::styled_output::render_success("✓ Configuration saved successfully");
    print!("\r\n");
}

/// Initial choice enum
#[derive(Clone, Copy, PartialEq)]
enum InitialChoice {
    StakpakAPI,
    OwnKeys,
}

/// Provider choice enum
#[derive(Clone, Copy, PartialEq)]
enum ProviderChoice {
    OpenAI,
    Anthropic,
    Google,
    Hybrid,
    Byom,
}

/// Anthropic auth method choice
#[derive(Clone, Copy, PartialEq)]
enum AnthropicAuthChoice {
    ClaudeSubscription,
    ApiKey,
}