xbp 10.57.0

XBP is a zero-config build pack that can also interact with proxies, kafka, sockets, synthetic monitors.
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
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
//! Interactive deploy target / mode / env resolution.
//!
//! Makes `xbp deploy` (and `xbp deploy --plan`) usable without memorizing
//! service names — pick from services[], deploy.groups, or `all`.
//!
//! Also peels reserved positional mode words (`plan`, `run`, `verify`, …)
//! so `xbp deploy plan` is never treated as target=`plan`.

use std::collections::BTreeSet;
use std::io::IsTerminal;

use colored::Colorize;
use dialoguer::Input;
use xbp_deploy::DeployMode;

use crate::cli::interactive::{
    confirm, pad_label, print_picker_header, searchable_multi_select, searchable_select_required,
    select_one, xbp_theme,
};
use crate::strategies::XbpConfig;
use xbp_deploy::{
    filter_deploy_plan, retain_services_in_plan, service_plan_labels, unique_service_names,
    DeployPlan,
};
use std::collections::HashSet;

#[derive(Debug, Clone)]
pub struct ResolvedDeployInvocation {
    pub target: String,
    pub env: Option<String>,
    pub mode: DeployMode,
    /// OCI promotion tag when mode is Promote (from `--promote` or interactive prompt).
    pub promote_tag: Option<String>,
    /// Build into local Docker only (no registry push).
    pub local_image: bool,
    pub skip_build: bool,
    pub dry_run: bool,
    pub skip_doctor: bool,
    pub skip_health: bool,
    pub skip_rollout: bool,
    pub skip_apply: bool,
    pub skip_oci: bool,
    /// Skip post-rollout port-forward / hosts DNS.
    pub skip_expose: bool,
}

/// CLI flag seeds for interactive deploy resolution (TTY can override).
#[derive(Debug, Clone, Default)]
pub struct DeployInvocationOptions {
    pub promote_tag: Option<String>,
    pub local_image: bool,
    pub push_image: bool,
    pub skip_build: bool,
    pub dry_run: bool,
    pub skip_doctor: bool,
    pub skip_health: bool,
    pub skip_rollout: bool,
    pub skip_apply: bool,
    pub skip_oci: bool,
    pub skip_expose: bool,
    /// Explicit `--context` (used for auto local-image default).
    pub kube_context: Option<String>,
}

/// How images / Worker artifacts are produced before apply.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ImageBuildStrategy {
    /// Kubernetes: docker load local. Cloudflare: pre-build container with local Docker, then Wrangler.
    LocalLoad,
    /// Kubernetes: docker build + registry push. Cloudflare: Wrangler/OpenNext owns build (no GHCR).
    PushRegistry,
    /// Reuse existing image / config-only CF re-run.
    SkipBuild,
}

/// Destinations for the resolved deploy target (drives wizard copy + defaults).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum TargetDestProfile {
    CloudflareOnly,
    KubernetesOnly,
    Mixed,
    Unknown,
}

impl TargetDestProfile {
    fn has_cloudflare(self) -> bool {
        matches!(self, Self::CloudflareOnly | Self::Mixed)
    }

    fn has_kubernetes(self) -> bool {
        matches!(self, Self::KubernetesOnly | Self::Mixed)
    }

    fn is_cloudflare_only(self) -> bool {
        matches!(self, Self::CloudflareOnly)
    }
}

/// Mode words accepted as a bare positional (instead of `--plan` / `--run` / …).
pub const DEPLOY_MODE_POSITIONALS: &[&str] = &["plan", "run", "verify", "status", "history"];

/// Result of peeling a reserved mode word from the positional target.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PeeledDeployArgs {
    /// Remaining service/group target (None when the positional was only a mode word).
    pub target: Option<String>,
    pub mode: DeployMode,
    /// True when mode came from a CLI flag or a reserved positional.
    pub mode_explicit: bool,
}

/// Interpret `plan`/`run`/… as modes when used as the positional TARGET.
///
/// Examples:
/// - `xbp deploy plan` → mode=Plan, no target
/// - `xbp deploy run` → mode=Run, no target  
/// - `xbp deploy plan --run` → error (conflicting modes)
/// - `xbp deploy athena --plan` → unchanged (athena stays target)
pub fn peel_deploy_mode_positional(
    target: Option<String>,
    mode: DeployMode,
    mode_explicit: bool,
) -> Result<PeeledDeployArgs, String> {
    let Some(raw) = target
        .as_deref()
        .map(str::trim)
        .filter(|s| !s.is_empty())
        .map(str::to_string)
    else {
        return Ok(PeeledDeployArgs {
            target: None,
            mode,
            mode_explicit,
        });
    };

    let lower = raw.to_ascii_lowercase();
    let positional_mode = match lower.as_str() {
        "plan" => Some(DeployMode::Plan),
        "run" => Some(DeployMode::Run),
        "verify" => Some(DeployMode::Verify),
        "status" => Some(DeployMode::Status),
        "history" => Some(DeployMode::History),
        _ => None,
    };

    let Some(from_positional) = positional_mode else {
        return Ok(PeeledDeployArgs {
            target: Some(raw),
            mode,
            mode_explicit,
        });
    };

    if mode_explicit {
        let flag_label = mode_flag_label(mode);
        let pos_label = mode_flag_label(from_positional);
        if std::mem::discriminant(&mode) != std::mem::discriminant(&from_positional) {
            return Err(format!(
                "`{raw}` is a deploy *mode*, not a service name — and it conflicts with `{flag_label}`.\n\n\
                 Plan only (no mutations):\n  xbp deploy --plan\n  xbp deploy <service> --plan\n\n\
                 Apply after reviewing the plan:\n  xbp deploy --run\n  xbp deploy <service> --run\n\n\
                 Do not combine `{pos_label}` with `{flag_label}`."
            ));
        }
        // Same mode twice (`xbp deploy plan --plan`) — fine, drop positional.
        return Ok(PeeledDeployArgs {
            target: None,
            mode,
            mode_explicit: true,
        });
    }

    Ok(PeeledDeployArgs {
        target: None,
        mode: from_positional,
        mode_explicit: true,
    })
}

fn mode_flag_label(mode: DeployMode) -> &'static str {
    match mode {
        DeployMode::Plan => "--plan",
        DeployMode::Run => "--run",
        DeployMode::Verify => "--verify",
        DeployMode::Status => "--status",
        DeployMode::History => "--history",
        DeployMode::Promote => "--promote",
    }
}

/// Resolve missing target / mode / env / OCI options via TTY prompts when possible.
pub fn resolve_deploy_invocation(
    config: &XbpConfig,
    target: Option<String>,
    env: Option<String>,
    mode: DeployMode,
    mode_explicit: bool,
    yes: bool,
    force_non_interactive: bool,
    options: DeployInvocationOptions,
) -> Result<ResolvedDeployInvocation, String> {
    let interactive = is_interactive() && !force_non_interactive && !yes;
    let target_raw = target
        .as_deref()
        .map(str::trim)
        .filter(|s| !s.is_empty())
        .map(str::to_string);

    let choices = deploy_target_choices(config);
    if choices.is_empty() {
        return Err(
            "No services[] defined in project config. Run `xbp init` or `xbp version discover` first."
                .into(),
        );
    }

    let resolved_target = match target_raw {
        Some(t) => t,
        None if interactive => prompt_target(config, &choices)?,
        None if choices.len() == 1 => {
            let only = choices[0].value.clone();
            println!(
                "{} using sole deploy target `{}`",
                "deploy".bright_cyan().bold(),
                only.bright_white()
            );
            only
        }
        None => {
            return Err(format!(
                "deploy target is required.\n\nAvailable targets:\n{}\n\n\
                 Examples:\n  xbp deploy {}\n  xbp deploy --plan\n  xbp deploy all --env production --plan\n\n\
                 In a TTY, omit <TARGET> to pick interactively.",
                format_target_help(&choices),
                choices[0].value
            ));
        }
    };

    let resolved_mode = if mode_explicit || !interactive {
        mode
    } else {
        prompt_mode(mode)?
    };

    let resolved_env = match env
        .as_deref()
        .map(str::trim)
        .filter(|s| !s.is_empty())
        .map(str::to_string)
    {
        Some(e) => Some(e),
        None if interactive => prompt_env(config, &resolved_target)?,
        None => None, // engine / default_env
    };

    let promote_tag = if matches!(resolved_mode, DeployMode::Promote) {
        match options
            .promote_tag
            .as_deref()
            .map(str::trim)
            .filter(|s| !s.is_empty())
            .map(str::to_string)
        {
            Some(tag) => Some(tag),
            None if interactive => Some(prompt_promote_tag(config, &resolved_target, &resolved_env)?),
            None if yes => {
                return Err(
                    "promote mode requires a tag. Pass `--promote <TAG>` (e.g. `--promote stable`) or run interactively."
                        .into(),
                );
            }
            None => {
                return Err(
                    "promote mode requires `--promote <TAG>`.\n\nExample:\n  xbp deploy athena --promote stable --dry-run\n\nIn a TTY, choose promote mode and enter the tag when prompted."
                        .into(),
                );
            }
        }
    } else {
        None
    };

    // Build / apply options — full TUI when interactive; CLI/auto otherwise.
    // Profile drives CF-vs-k8s wording so Worker targets are not asked k8s OCI questions.
    let dest_profile = target_dest_profile(config, &resolved_target);
    let needs_image_options = matches!(
        resolved_mode,
        DeployMode::Plan | DeployMode::Run | DeployMode::Promote | DeployMode::Verify
    );
    let (
        local_image,
        skip_build,
        dry_run,
        skip_doctor,
        skip_health,
        skip_rollout,
        skip_apply,
        skip_oci,
        skip_expose,
    ) = if interactive && needs_image_options {
        prompt_deploy_build_and_apply_options(
            config,
            &resolved_target,
            &resolved_mode,
            &options,
            dest_profile,
        )?
    } else {
        resolve_image_options_noninteractive(&options, dest_profile)
    };

    if interactive {
        match resolved_mode {
            DeployMode::Plan => {
                let image_note = image_strategy_note(dest_profile, local_image, skip_build);
                println!(
                    "  {} plan only (no mutations) for `{}`{} · {} — re-run with {} to apply",
                    "·".bright_cyan(),
                    resolved_target.bright_white(),
                    resolved_env
                        .as_deref()
                        .map(|e| format!(" (env: {e})"))
                        .unwrap_or_default()
                        .bright_black(),
                    image_note.bright_yellow(),
                    "--run".bright_green().bold()
                );
            }
            DeployMode::Run | DeployMode::Promote => {
                let label = match resolved_mode {
                    DeployMode::Promote => {
                        format!(
                            "promote → :{}",
                            promote_tag.as_deref().unwrap_or("?")
                        )
                    }
                    _ => {
                        if dest_profile.is_cloudflare_only() {
                            "run (Cloudflare Worker deploy)".to_string()
                        } else {
                            "run (apply)".to_string()
                        }
                    }
                };
                let image_note = image_strategy_note(dest_profile, local_image, skip_build);
                let mut extras = Vec::new();
                if dry_run {
                    extras.push("dry-run");
                }
                if skip_doctor {
                    extras.push("skip-doctor");
                }
                if skip_health {
                    extras.push("skip-health");
                }
                if skip_rollout {
                    extras.push("skip-rollout");
                }
                if skip_expose && dest_profile.has_kubernetes() {
                    extras.push("skip-expose");
                }
                if skip_apply {
                    if dest_profile.is_cloudflare_only() && local_image {
                        extras.push("local-build-only (no wrangler deploy)");
                    } else {
                        extras.push("skip-apply");
                    }
                }
                let extras_s = if extras.is_empty() {
                    String::new()
                } else {
                    format!(" · {}", extras.join(", "))
                };
                let ok = confirm(
                    &format!(
                        "Apply deploy `{label}` for target `{}`{} · {}{}?",
                        resolved_target,
                        resolved_env
                            .as_deref()
                            .map(|e| format!(" (env: {e})"))
                            .unwrap_or_default(),
                        image_note,
                        extras_s
                    ),
                    matches!(resolved_mode, DeployMode::Promote),
                )?;
                if !ok {
                    return Err("deploy aborted by user".into());
                }
            }
            _ => {}
        }
    } else if local_image && !skip_build && !options.skip_oci && dest_profile.has_kubernetes() {
        // Non-interactive auto-local: still surface the choice (json/CI may skip).
        if !force_non_interactive {
            println!(
                "{} OCI images: local Docker load only (no registry push). Use {} or the interactive picker to push.",
                "i".bright_blue().bold(),
                "--push-image".bright_yellow()
            );
        }
    }

    Ok(ResolvedDeployInvocation {
        target: resolved_target,
        env: resolved_env,
        mode: resolved_mode,
        promote_tag,
        local_image,
        skip_build,
        dry_run,
        skip_doctor,
        skip_health,
        skip_rollout,
        skip_apply,
        skip_oci,
        skip_expose,
    })
}

type DeployOptionBundle = (bool, bool, bool, bool, bool, bool, bool, bool, bool);

fn image_strategy_note(profile: TargetDestProfile, local_image: bool, skip_build: bool) -> &'static str {
    if skip_build {
        return if profile.is_cloudflare_only() {
            "reuse existing CF image / config-only"
        } else {
            "skip image build"
        };
    }
    if profile.is_cloudflare_only() {
        return if local_image {
            "local Docker container pre-build + Wrangler"
        } else {
            "OpenNext / Wrangler build + deploy"
        };
    }
    if local_image {
        "local Docker load"
    } else {
        "registry push"
    }
}

fn resolve_image_options_noninteractive(
    options: &DeployInvocationOptions,
    profile: TargetDestProfile,
) -> DeployOptionBundle {
    let local_image = if options.push_image {
        false
    } else if options.local_image {
        true
    } else if profile.is_cloudflare_only() {
        // Never infer CF local-build from kube context (docker-desktop is unrelated).
        false
    } else {
        looks_like_local_kube_context(options.kube_context.as_deref())
    };
    (
        local_image,
        options.skip_build,
        options.dry_run,
        options.skip_doctor,
        options.skip_health,
        options.skip_rollout,
        options.skip_apply,
        options.skip_oci,
        options.skip_expose,
    )
}

/// Interactive: build strategy + optional apply toggles (CF-aware when target is Workers-only).
fn prompt_deploy_build_and_apply_options(
    config: &XbpConfig,
    target: &str,
    mode: &DeployMode,
    options: &DeployInvocationOptions,
    profile: TargetDestProfile,
) -> Result<DeployOptionBundle, String> {
    let has_dockerfile = target_has_dockerfile(config, target);
    // Only auto-prefer local Docker load for k8s (or mixed) local clusters.
    let local_default = profile.has_kubernetes()
        && looks_like_local_kube_context(options.kube_context.as_deref());

    // --- Image / Worker build strategy ---
    let strategy = if options.skip_build {
        ImageBuildStrategy::SkipBuild
    } else if options.push_image {
        ImageBuildStrategy::PushRegistry
    } else if options.local_image {
        ImageBuildStrategy::LocalLoad
    } else if profile.is_cloudflare_only() {
        // CF Workers/OpenNext always "build" via Wrangler path unless user opts into
        // local container pre-build or explicit skip. Dockerfile alone does not imply GHCR.
        if matches!(mode, DeployMode::History | DeployMode::Status) {
            ImageBuildStrategy::SkipBuild
        } else {
            prompt_cloudflare_build_strategy(has_dockerfile)?
        }
    } else if !has_dockerfile {
        ImageBuildStrategy::SkipBuild
    } else if matches!(mode, DeployMode::History | DeployMode::Status) {
        ImageBuildStrategy::SkipBuild
    } else {
        prompt_image_build_strategy(local_default, has_dockerfile, profile)?
    };

    let (local_image, skip_build) = match strategy {
        ImageBuildStrategy::LocalLoad => (true, false),
        ImageBuildStrategy::PushRegistry => (false, false),
        ImageBuildStrategy::SkipBuild => (false, true),
    };

    // Plan / verify: image choice only (no apply toggles).
    if matches!(mode, DeployMode::Plan | DeployMode::Verify) {
        return Ok((
            local_image,
            skip_build,
            options.dry_run,
            options.skip_doctor,
            options.skip_health,
            options.skip_rollout,
            options.skip_apply,
            options.skip_oci,
            options.skip_expose,
        ));
    }

    if matches!(mode, DeployMode::History | DeployMode::Status) {
        return Ok(resolve_image_options_noninteractive(options, profile));
    }

    // --- Apply options multi-select ---
    let apply = prompt_apply_options(options, profile)?;

    Ok((
        local_image,
        skip_build,
        apply.dry_run,
        apply.skip_doctor,
        apply.skip_health,
        apply.skip_rollout,
        apply.skip_apply,
        apply.skip_oci,
        apply.skip_expose,
    ))
}

#[derive(Debug, Clone, Copy)]
struct ApplyOptionToggles {
    dry_run: bool,
    skip_doctor: bool,
    skip_health: bool,
    skip_rollout: bool,
    skip_apply: bool,
    skip_oci: bool,
    skip_expose: bool,
}

/// Cloudflare-only: build path is Worker/OpenNext/containers — not GHCR/k8s.
fn prompt_cloudflare_build_strategy(has_dockerfile: bool) -> Result<ImageBuildStrategy, String> {
    print_picker_header(
        "xbp deploy  ·  Cloudflare build",
        if has_dockerfile {
            "Worker + Container: OpenNext/Wrangler deploys by default; optional local Docker pre-build"
        } else {
            "Cloudflare Worker: OpenNext install/build + wrangler deploy (no cluster / GHCR)"
        },
    );

    let options = [
        (
            ImageBuildStrategy::PushRegistry, // reuse flag: means "remote CF build path"
            "full CF deploy   ·  OpenNext/Wrangler build + deploy Worker (and containers if configured)",
        ),
        (
            ImageBuildStrategy::LocalLoad,
            "local container  ·  docker build container image locally, then Wrangler deploy/push",
        ),
        (
            ImageBuildStrategy::SkipBuild,
            "config-only      ·  skip local pre-build; allow unchanged container digest if present",
        ),
    ];
    let labels: Vec<&str> = options.iter().map(|(_, l)| *l).collect();
    // Default: full CF deploy (not local kube "load").
    let idx = select_one("Cloudflare build strategy", &labels, 0)?
        .ok_or_else(|| "Selection cancelled".to_string())?;
    Ok(options[idx].0)
}

fn prompt_image_build_strategy(
    local_default: bool,
    has_dockerfile: bool,
    profile: TargetDestProfile,
) -> Result<ImageBuildStrategy, String> {
    let subtitle = if profile == TargetDestProfile::Mixed {
        "Mixed k8s + Cloudflare target — OCI options apply to cluster half; CF still builds via Wrangler"
    } else if local_default {
        "Local kube context detected — default is load into Docker (no ghcr push)"
    } else if has_dockerfile {
        "Services declare oci.dockerfile — choose build + load or push to registry"
    } else {
        "No oci.dockerfile on selected services — skip build uses existing images"
    };
    print_picker_header("xbp deploy  ·  OCI images", subtitle);

    let options = [
        (
            ImageBuildStrategy::LocalLoad,
            "local load     ·  docker build into local engine (no registry push)  ← docker-desktop / kind",
        ),
        (
            ImageBuildStrategy::PushRegistry,
            "registry push  ·  docker build + push (needs docker login for ghcr/…)",
        ),
        (
            ImageBuildStrategy::SkipBuild,
            "skip build     ·  use existing image ref (no docker build)",
        ),
    ];
    let default = if !has_dockerfile {
        2
    } else if local_default {
        0
    } else {
        1
    };
    let labels: Vec<&str> = options.iter().map(|(_, l)| *l).collect();
    let idx = select_one("Image build strategy", &labels, default)?
        .ok_or_else(|| "Selection cancelled".to_string())?;
    Ok(options[idx].0)
}

fn prompt_apply_options(
    seed: &DeployInvocationOptions,
    profile: TargetDestProfile,
) -> Result<ApplyOptionToggles, String> {
    let header = if profile.is_cloudflare_only() {
        "Space toggles  ·  Enter confirms  ·  leave all off for a normal Worker deploy"
    } else {
        "Space toggles  ·  Enter confirms  ·  leave all off for a normal apply"
    };
    print_picker_header("xbp deploy  ·  apply options", header);

    // Build destination-aware labels (same indices for mapping).
    let skip_apply_label = if profile.is_cloudflare_only() {
        "skip apply        ·  skip Wrangler/Worker deploy (with local container strategy → build-only)"
    } else if profile == TargetDestProfile::Mixed {
        "skip apply        ·  skip kubectl + Wrangler apply (still may build)"
    } else {
        "skip apply        ·  skip kubectl apply (still may build/verify)"
    };
    let skip_doctor_label = if profile.is_cloudflare_only() {
        "skip doctor       ·  skip Cloudflare preflight (Wrangler/account)"
    } else if profile.has_cloudflare() {
        "skip doctor       ·  skip cluster + Cloudflare preflight"
    } else {
        "skip doctor       ·  skip cluster preflight"
    };
    let dry_run_label = if profile.is_cloudflare_only() {
        "dry-run           ·  print CF actions only (no Wrangler mutations)"
    } else {
        "dry-run           ·  print actions only (no registry/cluster mutations)"
    };

    let mut items: Vec<(&str, bool)> = vec![
        (dry_run_label, seed.dry_run),
        (skip_doctor_label, seed.skip_doctor),
        (
            "skip health       ·  skip HTTP health probes after rollout",
            seed.skip_health,
        ),
        (
            "skip rollout wait ·  do not wait for workload / container ready",
            seed.skip_rollout,
        ),
        (skip_apply_label, seed.skip_apply),
    ];
    if profile.has_kubernetes() {
        items.push((
            "skip OCI resolve  ·  skip registry digest lookups",
            seed.skip_oci,
        ));
        items.push((
            "skip expose       ·  skip port-forward / hosts DNS after rollout",
            seed.skip_expose,
        ));
    }

    let labels: Vec<&str> = items.iter().map(|(l, _)| *l).collect();
    let defaults: Vec<bool> = items.iter().map(|(_, d)| *d).collect();
    let selected = searchable_multi_select("Apply toggles", &labels, &defaults)?;

    let on = |i: usize| selected.contains(&i);
    // Indices 0..4 always present; 5/6 only when k8s is in the plan.
    Ok(ApplyOptionToggles {
        dry_run: on(0),
        skip_doctor: on(1),
        skip_health: on(2),
        skip_rollout: on(3),
        skip_apply: on(4),
        skip_oci: profile.has_kubernetes() && on(5),
        skip_expose: profile.has_kubernetes() && on(6),
    })
}

/// Classify destinations for services in `target` (group / single / all).
fn target_dest_profile(config: &XbpConfig, target: &str) -> TargetDestProfile {
    let names = services_for_target(config, target);
    let services = config.services.as_deref().unwrap_or(&[]);
    let selected: Vec<&crate::strategies::ServiceConfig> = if names.is_empty() {
        services.iter().collect()
    } else {
        services
            .iter()
            .filter(|s| names.iter().any(|n| n == &s.name))
            .collect()
    };
    if selected.is_empty() {
        return TargetDestProfile::Unknown;
    }

    let mut any_cf = false;
    let mut any_k8s = false;
    for svc in selected {
        let Some(deploy) = svc.deploy.as_ref() else {
            continue;
        };
        if !deploy.destinations.is_empty() {
            for (name, dest) in &deploy.destinations {
                if dest.enabled == Some(false) {
                    continue;
                }
                let provider = dest
                    .provider
                    .as_deref()
                    .unwrap_or(name.as_str())
                    .to_ascii_lowercase();
                let key = name.to_ascii_lowercase();
                if provider.contains("cloudflare")
                    || provider.contains("worker")
                    || key.contains("cloudflare")
                    || key == "worker"
                    || key == "cf"
                {
                    any_cf = true;
                }
                if provider.contains("kubernetes")
                    || provider.contains("k8s")
                    || key.contains("kubernetes")
                    || key == "k8s"
                {
                    any_k8s = true;
                }
            }
        } else if let Some(provider) = deploy.provider.as_deref() {
            let p = provider.to_ascii_lowercase();
            if p.contains("cloudflare") || p.contains("worker") {
                any_cf = true;
            }
            if p.contains("kubernetes") || p.contains("k8s") {
                any_k8s = true;
            }
        }
    }

    match (any_cf, any_k8s) {
        (true, false) => TargetDestProfile::CloudflareOnly,
        (false, true) => TargetDestProfile::KubernetesOnly,
        (true, true) => TargetDestProfile::Mixed,
        (false, false) => TargetDestProfile::Unknown,
    }
}

fn target_has_dockerfile(config: &XbpConfig, target: &str) -> bool {
    let names = services_for_target(config, target);
    let services = config.services.as_deref().unwrap_or(&[]);
    if names.is_empty() {
        // "all" or unknown — any service with dockerfile
        return services.iter().any(|s| {
            s.oci
                .as_ref()
                .and_then(|o| o.dockerfile.as_ref())
                .is_some_and(|d| !d.trim().is_empty())
        });
    }
    names.iter().any(|name| {
        services.iter().any(|s| {
            s.name == *name
                && s.oci
                    .as_ref()
                    .and_then(|o| o.dockerfile.as_ref())
                    .is_some_and(|d| !d.trim().is_empty())
        })
    })
}

/// True when the active/requested kube context is a local cluster that shares Docker.
pub fn looks_like_local_kube_context(explicit: Option<&str>) -> bool {
    let ctx = explicit
        .map(str::trim)
        .filter(|s| !s.is_empty())
        .map(str::to_string)
        .or_else(current_kubectl_context_name);
    let Some(ctx) = ctx else {
        return false;
    };
    let c = ctx.to_ascii_lowercase();
    c == "docker-desktop"
        || c == "docker-for-desktop"
        || c == "rancher-desktop"
        || c == "minikube"
        || c.starts_with("kind-")
        || c.starts_with("k3d-")
        || c.contains("desktop")
}

fn current_kubectl_context_name() -> Option<String> {
    let output = std::process::Command::new("kubectl")
        .args(["config", "current-context"])
        .output()
        .ok()?;
    if !output.status.success() {
        return None;
    }
    let s = String::from_utf8_lossy(&output.stdout).trim().to_string();
    if s.is_empty() {
        None
    } else {
        Some(s)
    }
}

#[derive(Debug, Clone)]
struct TargetChoice {
    /// Value passed to the engine.
    value: String,
    /// Human label for the picker.
    label: String,
}

fn deploy_target_choices(config: &XbpConfig) -> Vec<TargetChoice> {
    let mut out = Vec::new();
    let all_services = config.services.as_deref().unwrap_or(&[]);
    // Prefer runtime deploy candidates (not pure scoped npm libraries).
    let services: Vec<_> = all_services
        .iter()
        .filter(|s| crate::commands::deploy_engine::setup::service_is_deploy_candidate(s))
        .collect();

    // Column widths for aligned, scannable picker rows (plain text — fuzzy match stays clean).
    let name_w = services
        .iter()
        .map(|s| s.name.chars().count())
        .chain(
            config
                .deploy
                .as_ref()
                .map(|d| d.groups.keys().map(|k| k.chars().count()).collect::<Vec<_>>())
                .unwrap_or_default(),
        )
        .max()
        .unwrap_or(8)
        .clamp(8, 28);

    if let Some(deploy) = config.deploy.as_ref() {
        let known: std::collections::BTreeSet<&str> =
            all_services.iter().map(|s| s.name.as_str()).collect();
        let mut group_names: Vec<_> = deploy.groups.keys().cloned().collect();
        group_names.sort();
        for name in group_names {
            let group = &deploy.groups[&name];
            let members_src = if group.order.is_empty() {
                &group.services
            } else {
                &group.order
            };
            let members: Vec<&str> = members_src
                .iter()
                .map(String::as_str)
                .filter(|n| known.contains(*n))
                .collect();
            if members.is_empty() {
                continue;
            }
            let members_preview = if members.len() > 8 {
                format!(
                    "{}, … (+{})",
                    members[..8].join(", "),
                    members.len() - 8
                )
            } else {
                members.join(", ")
            };
            let desc = group
                .description
                .as_deref()
                .filter(|s| !s.is_empty())
                .map(|d| format!("  ·  {d}"))
                .unwrap_or_default();
            out.push(TargetChoice {
                value: name.clone(),
                label: format!(
                    "{}  {}  [{} svc]  [{}]{}",
                    pad_label("GROUP", 7),
                    pad_label(&name, name_w),
                    members.len(),
                    members_preview,
                    desc
                ),
            });
        }
    }

    for svc in &services {
        let provider = svc
            .deploy
            .as_ref()
            .and_then(|d| d.provider.as_deref())
            .filter(|s| !s.is_empty())
            .unwrap_or(svc.target.as_str());
        let envs: Vec<_> = svc
            .deploy
            .as_ref()
            .map(|d| {
                let mut keys: Vec<_> = d.envs.keys().cloned().collect();
                keys.sort();
                keys
            })
            .unwrap_or_default();
        let env_hint = if envs.is_empty() {
            "no deploy.envs".to_string()
        } else {
            format!("envs {}", envs.join(", "))
        };
        out.push(TargetChoice {
            value: svc.name.clone(),
            label: format!(
                "{}  {}  {}  ·  {}  ·  :{}",
                pad_label("SVC", 7),
                pad_label(&svc.name, name_w),
                pad_label(provider, 12),
                env_hint,
                svc.port
            ),
        });
    }

    if !services.is_empty() {
        out.push(TargetChoice {
            value: "all".into(),
            label: format!(
                "{}  {}  every deployable service with deploy.envs ({})",
                pad_label("ALL", 7),
                pad_label("all", name_w),
                services.len()
            ),
        });
    }

    out
}

fn format_target_help(choices: &[TargetChoice]) -> String {
    choices
        .iter()
        .map(|c| format!("{}", c.value))
        .collect::<Vec<_>>()
        .join("\n")
}

fn prompt_target(config: &XbpConfig, choices: &[TargetChoice]) -> Result<String, String> {
    print_picker_header(
        "xbp deploy  ·  pick a target",
        "GROUP / SVC / ALL  ·  type to fuzzy-filter  ·  Enter selects  ·  Esc cancels",
    );

    let default_idx = default_target_index(config, choices);
    let labels: Vec<&str> = choices.iter().map(|c| c.label.as_str()).collect();
    let idx = searchable_select_required("Deploy target", &labels, default_idx)?;
    Ok(choices[idx].value.clone())
}

fn default_target_index(config: &XbpConfig, choices: &[TargetChoice]) -> usize {
    // Prefer sole service, else first group, else first choice.
    let services = config.services.as_deref().unwrap_or(&[]);
    if services.len() == 1 {
        if let Some(i) = choices.iter().position(|c| c.value == services[0].name) {
            return i;
        }
    }
    if let Some(i) = choices.iter().position(|c| c.label.starts_with("GROUP")) {
        return i;
    }
    0
}

fn prompt_mode(current: DeployMode) -> Result<DeployMode, String> {
    print_picker_header(
        "xbp deploy  ·  mode",
        "How should this deploy run? (plan is safe / no mutations)",
    );
    let modes = [
        (DeployMode::Run, "run      ·  apply providers (k8s / worker / …)"),
        (DeployMode::Plan, "plan     ·  print plan only (no mutations)"),
        (DeployMode::Verify, "verify   ·  read-only rollout/health checks"),
        (DeployMode::Status, "status   ·  status snapshot"),
        (DeployMode::History, "history  ·  recent deploy records"),
        (
            DeployMode::Promote,
            "promote  ·  retag OCI images by digest (prompts for tag)",
        ),
    ];
    // Interactive default is `run` (apply). If the caller already set a non-plan mode
    // (e.g. --verify), highlight that entry instead.
    let default = if matches!(current, DeployMode::Plan) {
        0 // run is first in the list
    } else {
        modes
            .iter()
            .position(|(m, _)| std::mem::discriminant(m) == std::mem::discriminant(&current))
            .unwrap_or(0)
    };
    let labels: Vec<&str> = modes.iter().map(|(_, l)| *l).collect();
    let idx = select_one("Deploy mode", &labels, default)?
        .ok_or_else(|| "Selection cancelled".to_string())?;
    Ok(modes[idx].0)
}

/// Common promotion tags + free-form input.
fn prompt_promote_tag(
    config: &XbpConfig,
    target: &str,
    env: &Option<String>,
) -> Result<String, String> {
    println!();
    println!(
        "{} retag images for `{}` by digest (no rebuild)",
        "promote".bright_cyan().bold(),
        target.bright_white()
    );
    println!(
        "{}",
        "The source digest comes from the plan/lock; the tag is the new mutable pointer (e.g. stable, production)."
            .bright_black()
    );

    let mut suggestions: Vec<String> = vec![
        "stable".into(),
        "latest".into(),
        "production".into(),
        "staging".into(),
        "canary".into(),
    ];
    if let Some(e) = env.as_deref().map(str::trim).filter(|s| !s.is_empty()) {
        if !suggestions.iter().any(|s| s == e) {
            suggestions.insert(0, e.to_string());
        }
    }
    if let Some(default_env) = config
        .deploy
        .as_ref()
        .and_then(|d| d.default_env.as_deref())
        .map(str::trim)
        .filter(|s| !s.is_empty())
    {
        if !suggestions.iter().any(|s| s == default_env) {
            suggestions.insert(0, default_env.to_string());
        }
    }
    // Dedup while preserving order
    let mut seen = BTreeSet::new();
    suggestions.retain(|s| seen.insert(s.clone()));

    let mut labels: Vec<String> = suggestions
        .iter()
        .map(|t| format!(":{t}"))
        .collect();
    labels.push("custom tag…".into());

    let idx = select_one("Promotion tag", &labels, 0)?
        .ok_or_else(|| "Selection cancelled".to_string())?;

    let tag = if idx < suggestions.len() {
        suggestions[idx].clone()
    } else {
        Input::with_theme(&xbp_theme())
            .with_prompt("Custom promotion tag (no leading colon)")
            .validate_with(|input: &String| -> Result<(), &str> {
                let t = input.trim();
                if t.is_empty() {
                    return Err("tag must not be empty");
                }
                if t.contains(char::is_whitespace) {
                    return Err("tag must not contain whitespace");
                }
                if t.starts_with(':') {
                    return Err("omit the leading colon (enter stable, not :stable)");
                }
                if t.contains('/') || t.contains('@') {
                    return Err("enter a bare tag, not a full image reference");
                }
                Ok(())
            })
            .interact_text()
            .map_err(|e| e.to_string())?
            .trim()
            .to_string()
    };

    println!(
        "{} will retag plan digests → `…:{}`",
        "promote".bright_green().bold(),
        tag.bright_white()
    );
    Ok(tag)
}

fn prompt_env(config: &XbpConfig, target: &str) -> Result<Option<String>, String> {
    let mut envs: BTreeSet<String> = BTreeSet::new();
    if let Some(default) = config
        .deploy
        .as_ref()
        .and_then(|d| d.default_env.clone())
        .filter(|s| !s.trim().is_empty())
    {
        envs.insert(default);
    }
    envs.insert("production".into());
    envs.insert("staging".into());
    envs.insert("development".into());

    let services = config.services.as_deref().unwrap_or(&[]);
    let names = services_for_target(config, target);
    for name in names {
        if let Some(svc) = services.iter().find(|s| s.name == name) {
            if let Some(deploy) = &svc.deploy {
                for env in deploy.envs.keys() {
                    envs.insert(env.clone());
                }
            }
        }
    }

    let mut list: Vec<String> = envs.into_iter().collect();
    list.sort();
    // Prefer project default first in the list.
    if let Some(default) = config
        .deploy
        .as_ref()
        .and_then(|d| d.default_env.as_deref())
    {
        if let Some(i) = list.iter().position(|e| e == default) {
            let d = list.remove(i);
            list.insert(0, d);
        }
    } else if let Some(i) = list.iter().position(|e| e == "production") {
        let d = list.remove(i);
        list.insert(0, d);
    }

    if list.len() == 1 {
        return Ok(Some(list[0].clone()));
    }

    let labels: Vec<String> = list
        .iter()
        .map(|e| {
            if config
                .deploy
                .as_ref()
                .and_then(|d| d.default_env.as_deref())
                == Some(e.as_str())
            {
                format!("{e} (project default)")
            } else {
                e.clone()
            }
        })
        .collect();
    print_picker_header(
        "xbp deploy  ·  environment",
        "Environment keys from deploy.default_env + service deploy.envs",
    );
    let idx = select_one("Deploy environment", &labels, 0)?
        .ok_or_else(|| "Selection cancelled".to_string())?;
    Ok(Some(list[idx].clone()))
}

fn services_for_target(config: &XbpConfig, target: &str) -> Vec<String> {
    let raw = target.trim();
    let services = config.services.as_deref().unwrap_or(&[]);
    if raw.eq_ignore_ascii_case("all") {
        return services.iter().map(|s| s.name.clone()).collect();
    }
    if let Some(group) = config.deploy.as_ref().and_then(|d| d.groups.get(raw)) {
        return if group.order.is_empty() {
            group.services.clone()
        } else {
            group.order.clone()
        };
    }
    if services.iter().any(|s| s.name == raw) {
        return vec![raw.to_string()];
    }
    Vec::new()
}

fn is_interactive() -> bool {
    std::io::stdin().is_terminal()
        && std::io::stdout().is_terminal()
        && std::env::var_os("XBP_NON_INTERACTIVE").is_none()
}

/// After a multi-service plan is built, optionally let the operator deselect services.
///
/// Skipped when: non-interactive, `--yes`, single service, or plan/verify/run with
/// no TTY. History/status never call this.
pub fn maybe_edit_plan_services(
    plan: DeployPlan,
    yes: bool,
    force_non_interactive: bool,
) -> Result<DeployPlan, String> {
    let names = unique_service_names(&plan);
    if names.len() <= 1 {
        return Ok(plan);
    }
    let interactive = is_interactive() && !force_non_interactive && !yes;
    if !interactive {
        return Ok(plan);
    }

    print_picker_header(
        "xbp deploy  ·  plan services",
        &format!(
            "{} services from target `{}`  ·  adjust before apply",
            names.len(),
            plan.target.label()
        ),
    );

    let actions = [
        format!("Continue with all {} services", names.len()),
        "Edit services…".to_string(),
        "Cancel deploy".to_string(),
    ];
    // Prefer Edit when many services (accidental `all` is common).
    let default = if names.len() > 3 { 1 } else { 0 };
    let idx = select_one("Plan services", &actions, default)?
        .ok_or_else(|| "Selection cancelled".to_string())?;

    match idx {
        0 => Ok(plan),
        2 => Err("deploy cancelled".into()),
        _ => {
            let labeled = service_plan_labels(&plan);
            let labels: Vec<&str> = labeled.iter().map(|(_, l)| l.as_str()).collect();
            let defaults = vec![true; labeled.len()];
            let selected = searchable_multi_select(
                "Services to include in this deploy",
                &labels,
                &defaults,
            )?;
            if selected.is_empty() {
                return Err("deploy cancelled: no services selected".into());
            }
            let keep: HashSet<String> = selected
                .into_iter()
                .filter_map(|i| labeled.get(i).map(|(n, _)| n.clone()))
                .collect();
            let filtered = retain_services_in_plan(plan, &keep).map_err(|e| e.to_string())?;
            println!(
                "{} plan now has {} service(s): {}",
                "deploy".bright_cyan().bold(),
                unique_service_names(&filtered).len(),
                unique_service_names(&filtered).join(", ")
            );
            Ok(filtered)
        }
    }
}

/// Apply CLI `--only` / `--exclude` filters.
pub fn apply_cli_service_filters(
    plan: DeployPlan,
    only: &[String],
    exclude: &[String],
) -> Result<DeployPlan, String> {
    filter_deploy_plan(plan, only, exclude).map_err(|e| e.to_string())
}

/// Confirm apply for multi-service run when interactive.
pub fn maybe_confirm_multi_service_apply(
    plan: &DeployPlan,
    mode: DeployMode,
    yes: bool,
    force_non_interactive: bool,
) -> Result<(), String> {
    if !matches!(mode, DeployMode::Run | DeployMode::Promote) {
        return Ok(());
    }
    if yes || force_non_interactive || !is_interactive() {
        return Ok(());
    }
    let n = unique_service_names(plan).len();
    if n <= 1 {
        return Ok(());
    }
    let ok = confirm(
        &format!("Apply deploy for {} service(s)?", n),
        false,
    )?;
    if !ok {
        return Err("deploy cancelled".into());
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    fn sample_config() -> XbpConfig {
        serde_json::from_value(serde_json::json!({
            "project_name": "demo",
            "version": "1.0.0",
            "port": 8080,
            "build_dir": ".",
            "services": [
                {"name": "api", "target": "rust", "branch": "main", "port": 8080},
                {"name": "web", "target": "nextjs", "branch": "main", "port": 3000}
            ],
            "deploy": {
                "default_env": "production",
                "groups": {
                    "core": {
                        "services": ["api", "web"],
                        "description": "core stack"
                    }
                }
            }
        }))
        .expect("sample config")
    }

    #[test]
    fn target_choices_include_group_service_and_all() {
        let cfg = sample_config();
        let choices = deploy_target_choices(&cfg);
        let values: Vec<_> = choices.iter().map(|c| c.value.as_str()).collect();
        assert!(values.contains(&"core"));
        assert!(values.contains(&"api"));
        assert!(values.contains(&"web"));
        assert!(values.contains(&"all"));
    }

    #[test]
    fn default_target_prefers_group_when_multiple_services() {
        let cfg = sample_config();
        let choices = deploy_target_choices(&cfg);
        let idx = default_target_index(&cfg, &choices);
        assert_eq!(choices[idx].value, "core");
    }

    #[test]
    fn sole_service_is_default_when_no_group() {
        let cfg: XbpConfig = serde_json::from_value(serde_json::json!({
            "project_name": "solo",
            "version": "0.1.0",
            "port": 1,
            "build_dir": ".",
            "services": [
                {"name": "xbp-github-runner", "target": "rust", "branch": "main", "port": 3599}
            ]
        }))
        .expect("solo config");
        let choices = deploy_target_choices(&cfg);
        let idx = default_target_index(&cfg, &choices);
        assert_eq!(choices[idx].value, "xbp-github-runner");
    }

    #[test]
    fn peel_plan_positional_sets_plan_mode() {
        let peeled = peel_deploy_mode_positional(
            Some("plan".into()),
            DeployMode::Plan,
            false,
        )
        .expect("peel");
        assert_eq!(peeled.target, None);
        assert!(matches!(peeled.mode, DeployMode::Plan));
        assert!(peeled.mode_explicit);
    }

    #[test]
    fn peel_run_positional_sets_run_mode() {
        let peeled = peel_deploy_mode_positional(Some("run".into()), DeployMode::Plan, false)
            .expect("peel");
        assert_eq!(peeled.target, None);
        assert!(matches!(peeled.mode, DeployMode::Run));
    }

    #[test]
    fn peel_plan_with_run_flag_errors() {
        let err = peel_deploy_mode_positional(Some("plan".into()), DeployMode::Run, true)
            .expect_err("conflict");
        assert!(err.contains("mode"), "{err}");
        assert!(err.contains("--run") || err.contains("--plan"), "{err}");
    }

    #[test]
    fn peel_service_name_unchanged() {
        let peeled = peel_deploy_mode_positional(
            Some("athena".into()),
            DeployMode::Plan,
            false,
        )
        .expect("peel");
        assert_eq!(peeled.target.as_deref(), Some("athena"));
        assert!(!peeled.mode_explicit);
    }
}