davit 0.3.0

Kubernetes management utilities
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
mod blueprint;
mod config;
mod dashboard;
mod git;
mod info;
mod registry;

use anyhow::{Context, Result};
use blueprint::Blueprint;
use chrono::Utc;
use clap::{Parser, Subcommand};
use config::{Config, Environment, ServiceSource, YamlSource};
use crossterm::{
    cursor::MoveToColumn,
    event::{self, Event, KeyCode, KeyEventKind},
    terminal::{Clear, ClearType, disable_raw_mode, enable_raw_mode},
};
use dashboard::{Dashboard, DashboardExit};
use git::Git;
use inquire::{Confirm, Select, Text};
use registry::{ImageMetadata, Registry};
use std::collections::HashSet;
use std::fs;
use std::io::{self, Write};
use std::process::Command;
use std::sync::Arc;
use std::thread;
use std::time::{Duration, Instant};

const MAX_PARALLEL_PULLS: usize = 5;
const TAG_RETRY_INTERVAL: Duration = Duration::from_secs(60);
const TAG_WAIT_POLL_INTERVAL: Duration = Duration::from_millis(250);
const TAG_WAIT_CANCELLED_MESSAGE: &str = "__TAG_WAIT_CANCELLED__";

#[derive(Parser)]
#[command(name = "davit")]
#[command(about = "A safe Kubernetes deployment wrapper & TUI", long_about = None)]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Deploy a service to an environment
    Deploy {
        /// Target environment (e.g., staging, production)
        #[arg(short, long)]
        env: Option<String>,

        /// Service name to deploy
        #[arg(short, long)]
        service: Option<String>,

        /// Image tag to deploy
        #[arg(short, long)]
        tag: Option<String>,

        /// Wait until the provided image tag appears in the registry instead of prompting for similar tags
        #[arg(long, conflicts_with = "tag", value_name = "TAG")]
        wait_for_tag: Option<String>,

        /// Dry run: show commands without executing them
        #[arg(long)]
        dry_run: bool,

        /// Apply the selected version immediately and continue automatically through rollout and Git steps
        #[arg(long)]
        auto_apply: bool,

        /// After `kubectl apply`, continue automatically through rollout completion and Git push unless errors occur
        #[arg(long)]
        auto_continue: bool,
    },
    /// Show deployment information for a service
    Info {
        /// Target environment (e.g., staging, production)
        #[arg(short, long)]
        env: Option<String>,

        /// Kubernetes namespace filter
        #[arg(short, long)]
        namespace: Option<String>,

        /// Service name to inspect
        #[arg(short, long)]
        service: Option<String>,
    },
    /// Configuration management
    Config {
        #[command(subcommand)]
        command: ConfigCommands,
    },
}

#[derive(Subcommand)]
enum ConfigCommands {
    /// Show current configuration
    Show,
    /// Get path to configuration file
    Path,
}

#[tokio::main]
async fn main() -> Result<()> {
    let cli = Cli::parse();
    let config = Config::load().context("Failed to load configuration")?;

    match cli.command {
        Commands::Deploy {
            env,
            service,
            tag,
            wait_for_tag,
            dry_run,
            auto_apply,
            auto_continue,
        } => {
            let auto_continue = auto_continue || auto_apply;
            let selected_env = resolve_environment(&config, env)?;

            pull_yaml_sources(&selected_env, dry_run, "deployment")?;

            let selected_service = resolve_service(&selected_env, service)?;

            let selected_tag =
                match resolve_tag(&selected_env, &selected_service, tag, wait_for_tag) {
                    Ok(tag) => tag,
                    Err(err) if err.to_string() == TAG_WAIT_CANCELLED_MESSAGE => {
                        println!("Tag wait cancelled. Deployment aborted.");
                        return Ok(());
                    }
                    Err(err) => return Err(err),
                };

            // 6.3 Production Protection
            if selected_env.protected.unwrap_or(false) {
                println!(
                    "⚠️  WARNING: Deployment to {} is PROTECTED!",
                    selected_env.name
                );
                let confirmation = Text::new(&format!(
                    "Type the environment name '{}' to confirm:",
                    selected_env.name
                ))
                .prompt()
                .context("Production confirmation was cancelled")?;

                if confirmation != selected_env.name {
                    return Err(anyhow::anyhow!("Confirmation failed. Deployment aborted."));
                }
            }

            // Phase 4 - YAML modification & Visual Diff
            let yaml_path = selected_service.yaml_path.clone();

            let original_content = fs::read_to_string(&yaml_path)
                .with_context(|| format!("Failed to read YAML file at {}", yaml_path.display()))?;

            let base_image = selected_service
                .image_path
                .split([':', '@'])
                .next()
                .unwrap_or(&selected_service.image_path);

            let updated_content =
                Blueprint::update_image_tag(&original_content, base_image, &selected_tag)
                    .context("Failed to update image tag in YAML")?;

            let mut show_unified = true;
            let filename = yaml_path
                .file_name()
                .and_then(|n| n.to_str())
                .unwrap_or("deployment.yaml");

            loop {
                Blueprint::show_diff(&original_content, &updated_content, filename, show_unified);

                if auto_apply {
                    if dry_run {
                        println!(
                            "Dry-run: would write updated YAML to {}",
                            yaml_path.display()
                        );
                    } else {
                        fs::write(&yaml_path, &updated_content).with_context(|| {
                            format!("Failed to write updated YAML to {}", yaml_path.display())
                        })?;
                    }
                    println!("Auto-apply enabled. Local YAML updated. Executing kubectl apply...");
                    break;
                }

                let choices = if show_unified {
                    vec!["Apply", "Show full diff", "Dismiss"]
                } else {
                    vec!["Apply", "Show unified diff", "Dismiss"]
                };

                let selection = Select::new("Action:", choices).prompt()?;

                match selection {
                    "Apply" => {
                        if dry_run {
                            println!(
                                "Dry-run: would write updated YAML to {}",
                                yaml_path.display()
                            );
                        } else {
                            fs::write(&yaml_path, &updated_content).with_context(|| {
                                format!("Failed to write updated YAML to {}", yaml_path.display())
                            })?;
                        }
                        println!("Local YAML updated. Executing kubectl apply...");
                        break;
                    }
                    "Show full diff" => show_unified = false,
                    "Show unified diff" => show_unified = true,
                    _ => {
                        println!("Deployment cancelled. No changes made.");
                        return Ok(());
                    }
                }
            }

            if dry_run {
                println!(
                    "Dry-run: kubectl --context {} apply -f {}",
                    selected_env.kubectl_context,
                    yaml_path.display()
                );
            } else {
                let output = Command::new("kubectl")
                    .args([
                        "--context",
                        &selected_env.kubectl_context,
                        "apply",
                        "-f",
                        yaml_path.to_str().unwrap(),
                    ])
                    .output()
                    .context("Failed to execute kubectl apply")?;

                if !output.status.success() {
                    let stderr = String::from_utf8_lossy(&output.stderr);
                    println!("❌ kubectl apply failed: {}", stderr);
                    if !auto_continue {
                        if Confirm::new("Revert local YAML changes?")
                            .with_default(true)
                            .prompt()?
                        {
                            fs::write(&yaml_path, &original_content)?;
                            println!("YAML reverted.");
                        }
                    }
                    return Err(anyhow::anyhow!("kubectl apply failed"));
                }
            }

            println!("Deployment applied. Starting dashboard...");

            let mut dashboard = Dashboard::new(
                selected_service.name.clone(),
                selected_service.kind.clone(),
                selected_env.name.clone(),
                selected_tag.clone(),
                selected_env.kubectl_context.clone(),
                selected_service.namespace.clone(),
                selected_service.selector.clone(),
                selected_service.container_name.clone(),
                auto_continue,
            );
            let res = dashboard.run().await;

            match res {
                Err(e) => {
                    println!("❌ Dashboard error or aborted: {}", e);
                    if !auto_continue {
                        if Confirm::new("Revert local YAML changes?")
                            .with_default(true)
                            .prompt()?
                        {
                            fs::write(&yaml_path, &original_content)?;
                            println!("YAML reverted.");
                        }
                    }
                    return Err(e);
                }
                Ok(DashboardExit::UserQuit) => {
                    if auto_continue {
                        return Err(anyhow::anyhow!(
                            "Dashboard closed before rollout completion in auto-continue mode"
                        ));
                    }
                    println!("Dashboard closed before rollout completion check.");
                }
                Ok(DashboardExit::RolloutCompleted) => {
                    println!("Rollout completed. Continuing to the Git step...");
                }
            }

            // 6.1 Git Automation
            println!("\n🚀 Deployment successful. Preparing to commit changes...");
            let commit_msg = format!(
                "deploy({}): update {} to {}",
                selected_env.name, selected_service.name, selected_tag
            );

            println!("\n--- Commit Recap ---");
            println!("File to commit:   {}", yaml_path.display());
            println!("Commit message:   {}", commit_msg);
            Blueprint::show_diff(&original_content, &updated_content, filename, true);
            println!("--------------------\n");

            if auto_continue {
                Git::commit_and_push(
                    &selected_service.source_root,
                    &commit_msg,
                    &yaml_path,
                    dry_run,
                )?;
                if !dry_run {
                    println!("✅ Changes committed and pushed to Git.");
                }
            } else {
                if Confirm::new("Do you want to commit and push these changes?")
                    .with_default(true)
                    .prompt()?
                {
                    if let Err(e) = Git::commit_and_push(
                        &selected_service.source_root,
                        &commit_msg,
                        &yaml_path,
                        dry_run,
                    ) {
                        println!("⚠️  Failed to commit/push changes: {}", e);
                    } else if !dry_run {
                        println!("✅ Changes committed and pushed to Git.");
                    }
                } else {
                    println!("Committing skipped by user.");
                }
            }
        }
        Commands::Info {
            env,
            namespace,
            service,
        } => {
            let selected_env = resolve_environment(&config, env)?;

            pull_yaml_sources(&selected_env, false, "info")?;

            let selected_service =
                resolve_service_with_ns_filter(&selected_env, service, namespace)?;
            info::show_info(&selected_env, &selected_service).await?;
        }
        Commands::Config { command } => match command {
            ConfigCommands::Show => {
                println!("{:#?}", config);
            }
            ConfigCommands::Path => {
                let path = Config::get_config_path()?;
                println!("{}", path.display());
            }
        },
    }

    Ok(())
}

fn resolve_environment(config: &Config, input: Option<String>) -> Result<Environment> {
    let env_names: Vec<String> = config.environments.iter().map(|e| e.name.clone()).collect();

    let name = match input {
        Some(val) => resolve_from_list("Environment", &env_names, val)?,
        None => Select::new("Select Environment:", env_names.clone())
            .prompt()
            .context("Environment selection was cancelled")?,
    };

    config
        .environments
        .iter()
        .find(|e| e.name == name)
        .cloned()
        .context("Environment not found in config")
}

fn get_service_display_name(
    s: &ServiceSource,
    all_services: &[ServiceSource],
) -> String {
    let duplicates: Vec<&ServiceSource> = all_services
        .iter()
        .filter(|&other| other.name == s.name)
        .collect();

    if duplicates.len() <= 1 {
        return s.name.clone();
    }

    // Multiple services with same name, check namespace
    let same_namespace: Vec<&&ServiceSource> = duplicates
        .iter()
        .filter(|&other| other.namespace == s.namespace)
        .collect();

    if same_namespace.len() <= 1 {
        return format!(
            "{} ({})",
            s.name,
            s.namespace.as_deref().unwrap_or("no-namespace")
        );
    }

    // Multiple services with same name and same namespace, use relative path
    let relative_path = get_service_source_display_path(s);

    format!(
        "{} ({}) {}",
        s.name,
        s.namespace.as_deref().unwrap_or("no-namespace"),
        relative_path
    )
}

fn resolve_service(env: &Environment, input: Option<String>) -> Result<ServiceSource> {
    let services = env
        .list_services()
        .context("Failed to list services in repo_root")?;
    resolve_service_from_list(services, env, input)
}

fn resolve_service_with_ns_filter(
    env: &Environment,
    input: Option<String>,
    namespace: Option<String>,
) -> Result<ServiceSource> {
    let all_services = env.list_services().context("Failed to list services")?;

    let services = match namespace {
        Some(ref ns) => {
            let filtered: Vec<ServiceSource> = all_services
                .into_iter()
                .filter(|s| s.namespace.as_deref() == Some(ns.as_str()))
                .collect();
            if filtered.is_empty() {
                return Err(anyhow::anyhow!("No services found in namespace '{}'", ns));
            }
            filtered
        }
        None => all_services,
    };

    resolve_service_from_list(services, env, input)
}

fn resolve_service_from_list(
    services: Vec<ServiceSource>,
    env: &Environment,
    input: Option<String>,
) -> Result<ServiceSource> {
    if services.is_empty() {
        return Err(anyhow::anyhow!(
            "No services found in configured YAML sources for {}",
            env.name
        ));
    }

    let service_map: Vec<(String, ServiceSource)> = services
        .iter()
        .cloned()
        .map(|s| (get_service_display_name(&s, &services), s))
        .collect();

    let display_names: Vec<String> = service_map.iter().map(|(n, _)| n.clone()).collect();

    let selected_name = match input {
        Some(val) => resolve_from_list("Service", &display_names, val)?,
        None => Select::new("Select Service:", display_names.clone())
            .prompt()
            .context("Service selection was cancelled")?,
    };

    service_map
        .into_iter()
        .find(|(n, _)| n == &selected_name)
        .map(|(_, s)| s)
        .context("Resolved service not found in list")
}

fn get_service_source_display_path(service: &ServiceSource) -> String {
    let relative_path = pathdiff::diff_paths(&service.yaml_path, &service.source_root)
        .unwrap_or_else(|| service.yaml_path.clone());
    format!("[{}]/{}", service.source_name, relative_path.display())
}

fn pull_yaml_sources(env: &Environment, dry_run: bool, action: &str) -> Result<()> {
    let sources = unique_yaml_sources(env);

    if sources.is_empty() {
        return Ok(());
    }

    println!("🔄 Checking for updates in configured YAML sources...");
    if sources.len() > 1 {
        println!(
            "Running up to {} git pulls in parallel and showing each repository output sequentially.",
            MAX_PARALLEL_PULLS
        );
    }

    let mut failures = Vec::new();
    for (source, result) in collect_parallel_pull_results(&sources, MAX_PARALLEL_PULLS, move |source| {
        Git::pull(&source.root, dry_run)
    }) {
        println!("  - [{}] {}", source.name, source.root.display());
        match result {
            Ok(report) => {
                print_git_pull_report(&report.stdout, false);
                print_git_pull_report(&report.stderr, true);

                if !report.success {
                    failures.push((source, "git pull failed".to_string()));
                }
            }
            Err(error) => failures.push((source, error.to_string())),
        }
    }

    if failures.is_empty() {
        return Ok(());
    }

    println!("⚠️  Some YAML sources could not be updated:");
    for (source, error) in &failures {
        println!("  - [{}] {}: {}", source.name, source.root.display(), error);
    }

    if !Confirm::new(&format!("Do you want to continue with {} anyway?", action))
        .with_default(false)
        .prompt()?
    {
        return Err(anyhow::anyhow!(
            "{} aborted by user after git pull failure.",
            capitalize_action(action)
        ));
    }

    Ok(())
}

fn collect_parallel_pull_results<T, F>(
    sources: &[YamlSource],
    max_parallel: usize,
    pull: F,
) -> Vec<(YamlSource, Result<T>)>
where
    T: Send + 'static,
    F: Fn(&YamlSource) -> Result<T> + Send + Sync + 'static,
{
    let max_parallel = max_parallel.max(1);
    let pull = Arc::new(pull);
    let mut results = Vec::with_capacity(sources.len());

    for batch in sources.chunks(max_parallel) {
        let mut handles = Vec::with_capacity(batch.len());

        for source in batch {
            let source = source.clone();
            let pull = Arc::clone(&pull);
            handles.push(thread::spawn(move || {
                let result = pull(&source);
                (source, result)
            }));
        }

        for handle in handles {
            results.push(handle.join().unwrap_or_else(|_| {
                (
                    YamlSource {
                        name: "unknown".to_string(),
                        root: std::path::PathBuf::new(),
                    },
                    Err(anyhow::anyhow!("git pull worker thread panicked")),
                )
            }));
        }
    }

    results
}

fn print_git_pull_report(output: &str, is_stderr: bool) {
    for line in output.lines() {
        if is_stderr {
            eprintln!("    {}", line);
        } else {
            println!("    {}", line);
        }
    }
}

fn unique_yaml_sources(env: &Environment) -> Vec<YamlSource> {
    let mut seen = HashSet::new();
    let mut unique = Vec::new();

    for source in env.yaml_sources() {
        let key = source
            .root
            .canonicalize()
            .unwrap_or_else(|_| source.root.clone());
        if seen.insert(key) {
            unique.push(source);
        }
    }

    unique
}

fn capitalize_action(action: &str) -> String {
    let mut chars = action.chars();
    match chars.next() {
        Some(first) => format!("{}{}", first.to_uppercase(), chars.as_str()),
        None => String::new(),
    }
}

fn resolve_tag(
    env: &Environment,
    service: &ServiceSource,
    input: Option<String>,
    wait_for_tag: Option<String>,
) -> Result<String> {
    if let Some(tag) = wait_for_tag {
        return wait_for_exact_tag(env, service, tag);
    }

    if let Some(tag) = input {
        let images = fetch_service_images(env, service, true)?;
        let available_tags = collect_available_tags(&images);

        if available_tags.is_empty() {
            return Err(anyhow::anyhow!(
                "No images found for service {}",
                service.name
            ));
        }

        return resolve_from_list("Image tag", &available_tags, tag);
    }

    let images = fetch_service_images(env, service, true)?;
    let available_tags = collect_available_tags(&images);

    if available_tags.is_empty() {
        return Err(anyhow::anyhow!(
            "No images found for service {}",
            service.name
        ));
    }

    let options: Vec<String> = images
        .iter()
        .map(|img| {
            format!(
                "{:<15} ({}) [{}]",
                img.display_tag(),
                img.age_string(),
                img.short_hash()
            )
        })
        .collect();

    let selection = Select::new("Select Image Tag:", options)
        .prompt()
        .context("Image selection was cancelled")?;

    let tag = selection
        .split_whitespace()
        .next()
        .unwrap_or("")
        .trim_end_matches(',')
        .to_string();
    Ok(tag)
}

fn fetch_service_images(
    env: &Environment,
    service: &ServiceSource,
    announce: bool,
) -> Result<Vec<ImageMetadata>> {
    let project = env.gcp_project.as_deref().unwrap_or("MOCK_PROJECT");

    if announce {
        println!(
            "Fetching images for {} using path {}...",
            service.name, service.image_path
        );
    }

    let images = match Registry::fetch_images(&service.image_path) {
        Ok(imgs) => imgs,
        Err(e) => {
            if project == "MOCK_PROJECT" {
                mock_images()
            } else {
                return Err(e).context("Failed to fetch images from Artifact Registry");
            }
        }
    };

    Ok(images)
}

fn collect_available_tags(images: &[ImageMetadata]) -> Vec<String> {
    let mut seen = HashSet::new();
    let mut tags = Vec::new();

    for image in images {
        for tag in &image.tags {
            if seen.insert(tag.clone()) {
                tags.push(tag.clone());
            }
        }
    }

    tags
}

fn wait_for_exact_tag(env: &Environment, service: &ServiceSource, tag: String) -> Result<String> {
    let mut attempt = 1;

    println!(
        "Waiting for image tag '{}' for service '{}'.",
        tag, service.name
    );
    println!("Registry image: {}", service.image_path);
    println!(
        "Polling every {} seconds. Press 'q' to cancel.",
        TAG_RETRY_INTERVAL.as_secs()
    );

    loop {
        render_tag_wait_status(
            &tag,
            &service.name,
            attempt,
            "Checking registry",
            None,
            '.',
        )?;

        let images = fetch_service_images(env, service, false)?;
        let available_tags = collect_available_tags(&images);

        if available_tags.iter().any(|available| available == &tag) {
            clear_tag_wait_status_line()?;
            if attempt == 1 {
                println!("Found image tag '{}'.", tag);
            } else {
                println!(
                    "Found image tag '{}' after {} checks. Continuing deployment.",
                    tag, attempt
                );
            }
            return Ok(tag);
        }

        wait_for_next_tag_check(service, &tag, attempt)?;
        attempt += 1;
    }
}

fn wait_for_next_tag_check(service: &ServiceSource, tag: &str, attempt: usize) -> Result<()> {
    let _raw_mode = RawModeGuard::new()?;
    let spinner = ['|', '/', '-', '\\'];
    let start = Instant::now();
    let mut frame = 0usize;

    loop {
        let elapsed = start.elapsed();
        if elapsed >= TAG_RETRY_INTERVAL {
            break;
        }

        let remaining = TAG_RETRY_INTERVAL.saturating_sub(elapsed).as_secs();
        let minutes = remaining / 60;
        let seconds = remaining % 60;

        render_tag_wait_status(
            tag,
            &service.name,
            attempt,
            "Tag not available yet",
            Some((minutes, seconds)),
            spinner[frame % spinner.len()],
        )?;

        if event::poll(TAG_WAIT_POLL_INTERVAL)? {
            loop {
                if let Event::Key(key) = event::read()?
                    && matches!(key.kind, KeyEventKind::Press | KeyEventKind::Repeat)
                {
                    match key.code {
                        KeyCode::Char('q') | KeyCode::Esc => {
                            clear_tag_wait_status_line()?;
                            return Err(anyhow::anyhow!(TAG_WAIT_CANCELLED_MESSAGE));
                        }
                        _ => {}
                    }
                }

                if !event::poll(Duration::from_millis(0))? {
                    break;
                }
            }
        }

        frame += 1;
    }

    clear_tag_wait_status_line()?;
    Ok(())
}

fn render_tag_wait_status(
    tag: &str,
    service_name: &str,
    attempt: usize,
    phase: &str,
    remaining: Option<(u64, u64)>,
    marker: char,
) -> Result<()> {
    clear_tag_wait_status_line()?;

    match remaining {
        Some((minutes, seconds)) => {
            print!(
                "{} {} for '{}' on {}. Checks: {}. Next check in {:02}:{:02}. Press 'q' to cancel.",
                marker, phase, tag, service_name, attempt, minutes, seconds
            );
        }
        None => {
            print!(
                "{} {} for '{}' on {}. Checks completed: {}. Press 'q' to cancel.",
                marker, phase, tag, service_name, attempt.saturating_sub(1)
            );
        }
    }

    io::stdout().flush()?;
    Ok(())
}

fn clear_tag_wait_status_line() -> Result<()> {
    crossterm::execute!(io::stdout(), MoveToColumn(0), Clear(ClearType::CurrentLine))
        .context("Failed to refresh tag wait status line")?;
    Ok(())
}

struct RawModeGuard;

impl RawModeGuard {
    fn new() -> Result<Self> {
        enable_raw_mode().context("Failed to enable terminal raw mode while waiting for image tag")?;
        Ok(Self)
    }
}

impl Drop for RawModeGuard {
    fn drop(&mut self) {
        let _ = disable_raw_mode();
    }
}

fn mock_images() -> Vec<ImageMetadata> {
    use chrono::Duration;
    let now = Utc::now();
    vec![
        ImageMetadata {
            tags: vec!["v1.2.3".to_string(), "latest".to_string()],
            update_time: now - Duration::hours(2),
            name: "auth-service@sha256:abcdef123456789".to_string(),
        },
        ImageMetadata {
            tags: vec!["v1.2.2".to_string()],
            update_time: now - Duration::days(1),
            name: "auth-service@sha256:123456789abcdef".to_string(),
        },
        ImageMetadata {
            tags: vec!["v1.1.0".to_string()],
            update_time: now - Duration::days(5),
            name: "auth-service@sha256:987654321fedcba".to_string(),
        },
    ]
}

/// Generic disambiguation logic
fn resolve_from_list(label: &str, items: &[String], input: String) -> Result<String> {
    // 1. Exact match
    if items.contains(&input) {
        return Ok(input);
    }

    // 2. Partial matches
    let matches: Vec<&String> = items.iter().filter(|&i| i.contains(&input)).collect();

    match matches.len() {
        0 => {
            println!("No {} matches '{}'.", label.to_lowercase(), input);
            Select::new(&format!("Select {}:", label), items.to_vec())
                .prompt()
                .context(format!("{} selection was cancelled", label))
        }
        1 => {
            let suggest = matches[0];
            if Confirm::new(&format!("Did you mean '{}'?", suggest))
                .with_default(true)
                .prompt()?
            {
                Ok(suggest.clone())
            } else {
                Select::new(&format!("Select {}:", label), items.to_vec())
                    .prompt()
                    .context(format!("{} selection was cancelled", label))
            }
        }
        _ => Select::new(
            &format!(
                "Multiple matches for '{}'. Select {}:",
                input,
                label.to_lowercase()
            ),
            matches.into_iter().cloned().collect(),
        )
        .prompt()
        .context(format!("{} selection was cancelled", label)),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::PathBuf;
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::time::Duration;

    #[test]
    fn test_service_display_name_unique() {
        let s = ServiceSource {
            name: "service1".to_string(),
            kind: "Deployment".to_string(),
            image_path: "img1".to_string(),
            container_name: "c1".to_string(),
            source_name: "main".to_string(),
            source_root: PathBuf::from("/root"),
            yaml_path: PathBuf::from("/root/dir1/deploy.yaml"),
            namespace: Some("ns1".to_string()),
            selector: None,
        };
        let all = vec![s.clone()];
        assert_eq!(get_service_display_name(&s, &all), "service1");
    }

    #[test]
    fn test_service_display_name_duplicate_name() {
        let s1 = ServiceSource {
            name: "service1".to_string(),
            kind: "Deployment".to_string(),
            image_path: "img1".to_string(),
            container_name: "c1".to_string(),
            source_name: "main".to_string(),
            source_root: PathBuf::from("/root"),
            yaml_path: PathBuf::from("/root/dir1/deploy.yaml"),
            namespace: Some("ns1".to_string()),
            selector: None,
        };
        let s2 = ServiceSource {
            name: "service1".to_string(),
            kind: "Deployment".to_string(),
            image_path: "img2".to_string(),
            container_name: "c2".to_string(),
            source_name: "main".to_string(),
            source_root: PathBuf::from("/root"),
            yaml_path: PathBuf::from("/root/dir2/deploy.yaml"),
            namespace: Some("ns2".to_string()),
            selector: None,
        };
        let all = vec![s1.clone(), s2.clone()];
        assert_eq!(get_service_display_name(&s1, &all), "service1 (ns1)");
        assert_eq!(get_service_display_name(&s2, &all), "service1 (ns2)");
    }

    #[test]
    fn test_service_display_name_duplicate_name_and_ns() {
        let s1 = ServiceSource {
            name: "service1".to_string(),
            kind: "Deployment".to_string(),
            image_path: "img1".to_string(),
            container_name: "c1".to_string(),
            source_name: "main".to_string(),
            source_root: PathBuf::from("/root"),
            yaml_path: PathBuf::from("/root/dir1/deploy.yaml"),
            namespace: Some("ns1".to_string()),
            selector: None,
        };
        let s2 = ServiceSource {
            name: "service1".to_string(),
            kind: "Deployment".to_string(),
            image_path: "img2".to_string(),
            container_name: "c2".to_string(),
            source_name: "demo".to_string(),
            source_root: PathBuf::from("/root"),
            yaml_path: PathBuf::from("/root/dir2/deploy.yaml"),
            namespace: Some("ns1".to_string()),
            selector: None,
        };
        let all = vec![s1.clone(), s2.clone()];
        assert_eq!(
            get_service_display_name(&s1, &all),
            "service1 (ns1) [main]/dir1/deploy.yaml"
        );
        assert_eq!(
            get_service_display_name(&s2, &all),
            "service1 (ns1) [demo]/dir2/deploy.yaml"
        );
    }

    #[test]
    fn test_collect_available_tags_preserves_order_and_deduplicates() {
        let now = Utc::now();
        let images = vec![
            ImageMetadata {
                tags: vec!["v1.2.3".to_string(), "latest".to_string()],
                update_time: now,
                name: "service@sha256:abcdef1".to_string(),
            },
            ImageMetadata {
                tags: vec!["latest".to_string(), "v1.2.2".to_string()],
                update_time: now,
                name: "service@sha256:abcdef2".to_string(),
            },
        ];

        assert_eq!(
            collect_available_tags(&images),
            vec![
                "v1.2.3".to_string(),
                "latest".to_string(),
                "v1.2.2".to_string()
            ]
        );
    }

    #[test]
    fn test_deploy_wait_for_tag_conflicts_with_tag() {
        let parse = Cli::try_parse_from([
            "davit",
            "deploy",
            "--tag",
            "v1.2.3",
            "--wait-for-tag",
            "v1.2.4",
        ]);
        assert!(parse.is_err());
    }

    #[test]
    fn test_deploy_wait_for_tag_accepts_value() {
        let parse = Cli::try_parse_from(["davit", "deploy", "--wait-for-tag", "v1.2.3"]);
        assert!(parse.is_ok());
    }

    #[test]
    fn test_deploy_auto_apply_accepts_flag() {
        let parse = Cli::try_parse_from(["davit", "deploy", "--auto-apply"]);
        assert!(parse.is_ok());
    }

    #[test]
    fn test_collect_parallel_pull_results_preserves_source_order() {
        let sources = vec![
            YamlSource {
                name: "one".to_string(),
                root: PathBuf::from("/tmp/one"),
            },
            YamlSource {
                name: "two".to_string(),
                root: PathBuf::from("/tmp/two"),
            },
            YamlSource {
                name: "three".to_string(),
                root: PathBuf::from("/tmp/three"),
            },
        ];

        let results = collect_parallel_pull_results(&sources, 2, |source| Ok(source.name.clone()));
        let ordered_names: Vec<String> = results
            .into_iter()
            .map(|(source, result)| {
                assert_eq!(result.as_ref().unwrap(), &source.name);
                source.name
            })
            .collect();

        assert_eq!(ordered_names, vec!["one", "two", "three"]);
    }

    #[test]
    fn test_collect_parallel_pull_results_respects_parallel_limit() {
        let sources: Vec<YamlSource> = (0..12)
            .map(|idx| YamlSource {
                name: format!("repo-{idx}"),
                root: PathBuf::from(format!("/tmp/repo-{idx}")),
            })
            .collect();

        let active = Arc::new(AtomicUsize::new(0));
        let max_seen = Arc::new(AtomicUsize::new(0));

        let active_for_pull = Arc::clone(&active);
        let max_seen_for_pull = Arc::clone(&max_seen);

        let results = collect_parallel_pull_results(&sources, 5, move |_source| {
            let current = active_for_pull.fetch_add(1, Ordering::SeqCst) + 1;
            max_seen_for_pull.fetch_max(current, Ordering::SeqCst);
            thread::sleep(Duration::from_millis(20));
            active_for_pull.fetch_sub(1, Ordering::SeqCst);
            Ok(())
        });

        assert_eq!(results.len(), 12);
        assert_eq!(max_seen.load(Ordering::SeqCst), 5);
    }
}