railwayapp 4.37.4

Interact with Railway via CLI
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
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
use std::{
    collections::{BTreeMap, HashMap},
    io::IsTerminal,
    path::{Path, PathBuf},
    time::{Duration, Instant},
};

use crate::{
    controllers::{
        config::{ServiceInstance, fetch_environment_config},
        develop::{
            CodeServiceConfig, ComposeServiceStatus, DEFAULT_PORT, DevSession, DockerComposeFile,
            DockerComposeNetwork, DockerComposeNetworks, DockerComposeService, DockerComposeVolume,
            HttpsConfig, HttpsDomainConfig, LocalDevConfig, LocalDevelopContext, NetworkMode,
            PortType, ServiceDomainConfig, ServicePort, ServiceSummary, build_port_infos,
            build_service_endpoints, build_slug_port_mapping, certs_exist,
            check_docker_compose_installed, check_mkcert_installed, ensure_mkcert_ca,
            generate_caddyfile, generate_certs, generate_port, generate_random_port,
            get_compose_path as develop_get_compose_path, get_develop_dir, get_existing_certs,
            is_port_443_available, is_project_proxy_on_443, override_railway_vars,
            print_context_info, print_domain_info, resolve_path, slugify, volume_name,
        },
        project::{self, ensure_project_and_environment_exist},
        variables::get_service_variables,
    },
    util::prompt::{prompt_multi_options, prompt_options, prompt_path_with_default, prompt_text},
};

use clap::Subcommand;

use super::*;

/// Run Railway services locally
#[derive(Debug, Parser)]
pub struct Args {
    #[clap(subcommand)]
    command: Option<DevelopCommand>,

    /// Show verbose domain replacement info (for default 'up' command)
    #[clap(short, long)]
    verbose: bool,
}

#[derive(Debug, Subcommand)]
enum DevelopCommand {
    /// Start services (default when no subcommand provided)
    Up(UpArgs),
    /// Stop services
    Down(DownArgs),
    /// Stop services and remove volumes/data
    Clean(CleanArgs),
    /// Configure local code services
    Configure(ConfigureArgs),
}

#[derive(Debug, Parser)]
struct ConfigureArgs {
    /// Specific service to configure (by name)
    #[clap(long)]
    service: Option<String>,

    /// Remove configuration for a service (optionally specify service name)
    #[clap(long, num_args = 0..=1, default_missing_value = "")]
    remove: Option<String>,
}

#[derive(Debug, Parser, Default)]
struct UpArgs {
    /// Environment to use (defaults to linked environment)
    #[clap(short, long)]
    environment: Option<String>,

    /// Output path for docker-compose.yml (defaults to ~/.railway/develop/<project_id>/docker-compose.yml)
    #[clap(short, long)]
    output: Option<PathBuf>,

    /// Only generate docker-compose.yml, don't run docker compose up
    #[clap(long)]
    dry_run: bool,

    /// Disable HTTPS and pretty URLs (use localhost instead)
    #[clap(long)]
    no_https: bool,

    /// Show verbose domain replacement info
    #[clap(short, long)]
    verbose: bool,

    /// Disable TUI, stream logs to stdout instead
    #[clap(long)]
    no_tui: bool,
}

#[derive(Debug, Parser)]
struct DownArgs {
    /// Output path for docker-compose.yml (defaults to ~/.railway/develop/<project_id>/docker-compose.yml)
    #[clap(short, long)]
    output: Option<PathBuf>,
}

#[derive(Debug, Parser)]
struct CleanArgs {
    /// Output path for docker-compose.yml (defaults to ~/.railway/develop/<project_id>/docker-compose.yml)
    #[clap(short, long)]
    output: Option<PathBuf>,
}

pub async fn command(args: Args) -> Result<()> {
    eprintln!(
        "{}",
        "Experimental feature. API may change without notice.".yellow()
    );

    match args.command {
        Some(DevelopCommand::Up(up_args)) => up_command(up_args).await,
        Some(DevelopCommand::Down(down_args)) => down_command(down_args).await,
        Some(DevelopCommand::Clean(clean_args)) => clean_command(clean_args).await,
        Some(DevelopCommand::Configure(cfg_args)) => configure_command(cfg_args).await,
        None => {
            up_command(UpArgs {
                verbose: args.verbose,
                ..Default::default()
            })
            .await
        }
    }
}

async fn get_compose_path(output: &Option<PathBuf>) -> Result<PathBuf> {
    if let Some(path) = output {
        return Ok(path.clone());
    }

    let configs = Configs::new()?;
    let linked_project = configs.get_linked_project().await?;
    Ok(develop_get_compose_path(&linked_project.project))
}

fn docker_install_url() -> &'static str {
    match std::env::consts::OS {
        "macos" => "https://docs.docker.com/desktop/setup/install/mac-install",
        "windows" => "https://docs.docker.com/desktop/setup/install/windows-install",
        _ => "https://docs.docker.com/desktop/setup/install/linux",
    }
}

fn require_docker_compose() {
    if !check_docker_compose_installed() {
        eprintln!();
        eprintln!("{}", "Docker Compose not found.".yellow());
        eprintln!("Install Docker:");
        eprintln!("  {}", docker_install_url());
        std::process::exit(1);
    }
}

async fn down_command(args: DownArgs) -> Result<()> {
    require_docker_compose();

    let compose_path = get_compose_path(&args.output).await?;

    if !compose_path.exists() {
        println!("{}", "Services already stopped".green());
        return Ok(());
    }

    println!("{}", "Stopping services...".cyan());

    let exit_status = tokio::process::Command::new("docker")
        .args(["compose", "-f", &*compose_path.to_string_lossy(), "down"])
        .status()
        .await?;

    if let Some(code) = exit_status.code() {
        if code != 0 {
            bail!("docker compose down exited with code {}", code);
        }
    }

    println!("{}", "Services stopped".green());
    Ok(())
}

async fn clean_command(args: CleanArgs) -> Result<()> {
    require_docker_compose();

    let compose_path = get_compose_path(&args.output).await?;

    if !compose_path.exists() {
        println!("{}", "Nothing to clean".green());
        return Ok(());
    }

    let confirmed = crate::util::prompt::prompt_confirm_with_default(
        "Stop services and remove volume data?",
        false,
    )?;
    if !confirmed {
        return Ok(());
    }

    println!("{}", "Cleaning up services...".cyan());

    let exit_status = tokio::process::Command::new("docker")
        .args([
            "compose",
            "-f",
            &*compose_path.to_string_lossy(),
            "down",
            "-v",
        ])
        .status()
        .await?;

    if let Some(code) = exit_status.code() {
        if code != 0 {
            bail!("docker compose down exited with code {}", code);
        }
    }

    if let Some(parent) = compose_path.parent() {
        std::fs::remove_dir_all(parent)?;
    }

    println!("{}", "Services cleaned".green());
    Ok(())
}

struct CodeServiceDisplay {
    service_id: String,
    name: String,
    configured: bool,
}

impl std::fmt::Display for CodeServiceDisplay {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if self.configured {
            write!(f, "{} (configured)", self.name)
        } else {
            write!(f, "{}", self.name)
        }
    }
}

async fn configure_command(args: ConfigureArgs) -> Result<()> {
    let configs = Configs::new()?;
    let client = GQLClient::new_authorized(&configs)?;
    let linked_project = configs.get_linked_project().await?;

    ensure_project_and_environment_exist(&client, &configs, &linked_project).await?;

    let project_data =
        project::get_project(&client, &configs, linked_project.project.clone()).await?;

    let service_names: HashMap<String, String> = project_data
        .services
        .edges
        .iter()
        .map(|e| (e.node.id.clone(), e.node.name.clone()))
        .collect();

    let project_id = linked_project.project.clone();
    let environment_id = linked_project.environment_id()?.to_string();

    let env_response = fetch_environment_config(&client, &configs, &environment_id, false).await?;
    let config = env_response.config;

    if config.services.is_empty() {
        println!(
            "{}",
            "No services in this environment. Add services with 'railway add'.".yellow()
        );
        return Ok(());
    }

    let code_services: Vec<_> = config
        .services
        .iter()
        .filter(|(_, svc)| svc.is_code_based())
        .collect();

    if code_services.is_empty() {
        println!(
            "{}",
            "No code-based services found. This environment only has image-based services."
                .yellow()
        );
        return Ok(());
    }

    let mut local_dev_config = LocalDevConfig::load(&project_id)?;

    if let Some(ref remove_arg) = args.remove {
        let service_to_remove = if !remove_arg.is_empty() {
            // --remove <service_name>
            code_services
                .iter()
                .find(|(id, _)| {
                    service_names
                        .get(*id)
                        .map(|n| n == remove_arg)
                        .unwrap_or(false)
                })
                .map(|(id, _)| (*id).clone())
        } else {
            // --remove (no arg) - prompt for selection
            let configured: Vec<_> = code_services
                .iter()
                .filter(|(id, _)| local_dev_config.services.contains_key(*id))
                .map(|(id, _)| CodeServiceDisplay {
                    service_id: (*id).clone(),
                    name: service_names
                        .get(*id)
                        .cloned()
                        .unwrap_or_else(|| (*id).clone()),
                    configured: true,
                })
                .collect();

            if configured.is_empty() {
                println!("{}", "No configured services to remove".yellow());
                return Ok(());
            }

            let selected = prompt_options("Select service to remove configuration:", configured)?;
            Some(selected.service_id)
        };

        if let Some(service_id) = service_to_remove {
            let name = service_names
                .get(&service_id)
                .cloned()
                .unwrap_or_else(|| service_id.clone());
            if local_dev_config.remove_service(&service_id).is_some() {
                local_dev_config.save(&project_id)?;
                println!("{} Removed configuration for '{}'", "".green(), name);
            } else {
                println!(
                    "{}",
                    format!("Service '{}' is not configured", name).yellow()
                );
            }
        }

        return Ok(());
    }

    // Service list loop
    loop {
        let service_id_to_configure = if let Some(ref name) = args.service {
            code_services
                .iter()
                .find(|(id, _)| service_names.get(*id).map(|n| n == name).unwrap_or(false))
                .map(|(id, _)| (*id).clone())
        } else {
            let options: Vec<_> = code_services
                .iter()
                .map(|(id, _)| CodeServiceDisplay {
                    service_id: (*id).clone(),
                    name: service_names
                        .get(*id)
                        .cloned()
                        .unwrap_or_else(|| (*id).clone()),
                    configured: local_dev_config.services.contains_key(*id),
                })
                .collect();

            let selected = prompt_options("Select service to configure:", options)?;
            Some(selected.service_id)
        };

        let Some(service_id) = service_id_to_configure else {
            return Ok(());
        };

        let svc = config
            .services
            .get(&service_id)
            .context("Service not found")?;
        let name = service_names
            .get(&service_id)
            .cloned()
            .unwrap_or_else(|| service_id.clone());

        // If no existing config, do initial setup first
        if local_dev_config.get_service(&service_id).is_none() {
            let mut new_config = prompt_service_config(&name, svc, None)?;

            if let Some(port) = new_config.port {
                let conflicts: Vec<_> = local_dev_config
                    .services
                    .iter()
                    .filter(|(id, cfg)| *id != &service_id && cfg.port == Some(port))
                    .map(|(id, _)| service_names.get(id).cloned().unwrap_or_else(|| id.clone()))
                    .collect();

                if !conflicts.is_empty() {
                    println!(
                        "\n{} Port {} is already used by: {}",
                        "Warning:".yellow().bold(),
                        port,
                        conflicts.join(", ")
                    );
                    let suggested = generate_random_port();
                    let port_input =
                        prompt_text(&format!("Choose a different port [{}]:", suggested))?;
                    new_config.port = Some(if port_input.is_empty() {
                        suggested
                    } else {
                        port_input.parse().context("Invalid port number")?
                    });
                }
            }

            local_dev_config.set_service(service_id.clone(), new_config);
            local_dev_config.save(&project_id)?;
            println!("{} Configured '{}'", "".green(), name);
        }

        // Service config menu loop
        loop {
            let action = show_service_config_menu(
                &name,
                local_dev_config.get_service(&service_id).unwrap(),
            )?;

            match action {
                ConfigAction::ChangeCommand => {
                    let existing = local_dev_config.get_service(&service_id).unwrap();
                    let new_command = prompt_text(&format!(
                        "Dev command for '{}' [{}]:",
                        name, existing.command
                    ))
                    .map(|s| {
                        if s.is_empty() {
                            existing.command.clone()
                        } else {
                            s
                        }
                    })?;
                    let mut updated = existing.clone();
                    updated.command = new_command;
                    local_dev_config.set_service(service_id.clone(), updated);
                    local_dev_config.save(&project_id)?;
                    println!("{} Updated command for '{}'", "".green(), name);
                }
                ConfigAction::ChangeDirectory => {
                    let existing = local_dev_config.get_service(&service_id).unwrap();
                    let cwd = std::env::current_dir().context("Failed to get current directory")?;
                    let default_dir = PathBuf::from(&existing.directory)
                        .strip_prefix(&cwd)
                        .map(|p| p.to_string_lossy().to_string())
                        .unwrap_or_else(|_| existing.directory.clone());

                    let input_path = prompt_path_with_default(
                        &format!("Directory for '{}' (relative to cwd):", name),
                        &default_dir,
                    )?;

                    let directory = if input_path.is_absolute() {
                        input_path.to_string_lossy().to_string()
                    } else {
                        resolve_path(cwd.join(&input_path))
                            .to_string_lossy()
                            .to_string()
                    };

                    let mut updated = existing.clone();
                    updated.directory = directory;
                    local_dev_config.set_service(service_id.clone(), updated);
                    local_dev_config.save(&project_id)?;
                    println!("{} Updated directory for '{}'", "".green(), name);
                }
                ConfigAction::ChangePort => {
                    let existing = local_dev_config.get_service(&service_id).unwrap();
                    let railway_port = svc.get_ports().first().map(|&p| p as u16);
                    let current_port = existing.port.or(railway_port).unwrap_or(DEFAULT_PORT);

                    let port_input =
                        prompt_text(&format!("Port for '{}' [{}]:", name, current_port))?;

                    let mut new_port = if port_input.is_empty() {
                        current_port
                    } else {
                        port_input.parse().context("Invalid port number")?
                    };

                    let conflicts: Vec<_> = local_dev_config
                        .services
                        .iter()
                        .filter(|(id, cfg)| *id != &service_id && cfg.port == Some(new_port))
                        .map(|(id, _)| service_names.get(id).cloned().unwrap_or_else(|| id.clone()))
                        .collect();

                    if !conflicts.is_empty() {
                        println!(
                            "\n{} Port {} is already used by: {}",
                            "Warning:".yellow().bold(),
                            new_port,
                            conflicts.join(", ")
                        );
                        let suggested = generate_random_port();
                        let port_input =
                            prompt_text(&format!("Choose a different port [{}]:", suggested))?;
                        new_port = if port_input.is_empty() {
                            suggested
                        } else {
                            port_input.parse().context("Invalid port number")?
                        };
                    }

                    let mut updated = existing.clone();
                    updated.port = Some(new_port);
                    local_dev_config.set_service(service_id.clone(), updated);
                    local_dev_config.save(&project_id)?;
                    println!("{} Updated port for '{}'", "".green(), name);
                }
                ConfigAction::Remove => {
                    local_dev_config.remove_service(&service_id);
                    local_dev_config.save(&project_id)?;
                    println!("{} Removed configuration for '{}'", "".green(), name);
                    break; // Back to service list
                }
                ConfigAction::Back => {
                    break; // Back to service list
                }
            }
        }

        // If --service was specified, exit after handling that service
        if args.service.is_some() {
            return Ok(());
        }
    }
}

#[derive(Debug, Clone, Copy)]
enum ConfigAction {
    ChangeCommand,
    ChangeDirectory,
    ChangePort,
    Remove,
    Back,
}

impl std::fmt::Display for ConfigAction {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ConfigAction::ChangeCommand => write!(f, "Change command"),
            ConfigAction::ChangeDirectory => write!(f, "Change directory"),
            ConfigAction::ChangePort => write!(f, "Change port"),
            ConfigAction::Remove => write!(f, "Remove configuration"),
            ConfigAction::Back => write!(f, "← Configure another service"),
        }
    }
}

fn show_service_config_menu(name: &str, config: &CodeServiceConfig) -> Result<ConfigAction> {
    let display_dir = match std::env::current_dir() {
        Ok(cwd) => PathBuf::from(&config.directory)
            .strip_prefix(&cwd)
            .map(|p| format!("./{}", p.display()))
            .unwrap_or_else(|_| config.directory.clone()),
        Err(_) => config.directory.clone(),
    };

    println!("\n{}", format!("Service '{}'", name).cyan().bold());
    println!("  {}: {}", "command".dimmed(), config.command);
    println!("  {}: {}", "directory".dimmed(), display_dir);
    if let Some(port) = config.port {
        println!("  {}: {}", "port".dimmed(), port);
    }
    println!();

    let options = vec![
        ConfigAction::ChangeCommand,
        ConfigAction::ChangeDirectory,
        ConfigAction::ChangePort,
        ConfigAction::Remove,
        ConfigAction::Back,
    ];

    prompt_options("", options)
}

fn prompt_service_config(
    name: &str,
    svc: &ServiceInstance,
    existing: Option<&CodeServiceConfig>,
) -> Result<CodeServiceConfig> {
    println!("\n{}", format!("Configure '{}'", name).cyan().bold());

    let default_command = existing.map(|e| e.command.as_str()).unwrap_or("");
    let command = if default_command.is_empty() {
        prompt_text(&format!("Dev command for '{}':", name))?
    } else {
        prompt_text(&format!(
            "Dev command for '{}' [{}]:",
            name, default_command
        ))
        .map(|s| {
            if s.is_empty() {
                default_command.to_string()
            } else {
                s
            }
        })?
    };

    let cwd = std::env::current_dir().context("Failed to get current directory")?;
    let default_dir = existing
        .map(|e| {
            PathBuf::from(&e.directory)
                .strip_prefix(&cwd)
                .map(|p| p.to_string_lossy().to_string())
                .unwrap_or_else(|_| e.directory.clone())
        })
        .unwrap_or_else(|| ".".to_string());

    let input_path = prompt_path_with_default(
        &format!("Directory for '{}' (relative to current directory):", name),
        &default_dir,
    )?;

    let directory = if input_path.is_absolute() {
        input_path.to_string_lossy().to_string()
    } else {
        resolve_path(cwd.join(&input_path))
            .to_string_lossy()
            .to_string()
    };

    // Prompt for port if service has networking config
    let inferred_port = svc.get_ports().first().map(|&p| p as u16);
    let default_port = existing.and_then(|e| e.port).or(inferred_port);

    let port = if let Some(default) = default_port {
        let port_input = prompt_text(&format!("Port for '{}' [{}]:", name, default))?;
        if port_input.is_empty() {
            Some(default)
        } else {
            Some(port_input.parse().context("Invalid port number")?)
        }
    } else {
        None
    };

    Ok(CodeServiceConfig {
        command,
        directory,
        port,
    })
}

/// Prompts user to select and configure multiple services at once
fn prompt_initial_service_setup(
    code_services: &[(&String, &ServiceInstance)],
    service_names: &HashMap<String, String>,
    config: &crate::controllers::config::EnvironmentConfig,
    local_dev_config: &mut LocalDevConfig,
) -> Result<()> {
    println!("\n{}", "Configure local code services".cyan().bold());
    println!("{}", "(Press space to select, enter to confirm)".dimmed());

    let options: Vec<_> = code_services
        .iter()
        .map(|(id, _)| CodeServiceDisplay {
            service_id: (*id).clone(),
            name: service_names
                .get(*id)
                .cloned()
                .unwrap_or_else(|| (*id).clone()),
            configured: false,
        })
        .collect();

    let selected = prompt_multi_options("Select services to configure:", options)?;

    for service_display in &selected {
        let svc = config
            .services
            .get(&service_display.service_id)
            .context("Service not found")?;
        let name = &service_display.name;

        let mut new_config = prompt_service_config(name, svc, None)?;

        // Check for port conflicts with already-configured services
        if let Some(port) = new_config.port {
            let conflicts: Vec<_> = local_dev_config
                .services
                .iter()
                .filter(|(id, cfg)| *id != &service_display.service_id && cfg.port == Some(port))
                .map(|(id, _)| service_names.get(id).cloned().unwrap_or_else(|| id.clone()))
                .collect();

            if !conflicts.is_empty() {
                println!(
                    "\n{} Port {} is already used by: {}",
                    "Warning:".yellow().bold(),
                    port,
                    conflicts.join(", ")
                );
                let suggested = generate_random_port();
                let port_input = prompt_text(&format!("Choose a different port [{}]:", suggested))?;
                new_config.port = Some(if port_input.is_empty() {
                    suggested
                } else {
                    port_input.parse().context("Invalid port number")?
                });
            }
        }

        local_dev_config.set_service(service_display.service_id.clone(), new_config);
    }

    // Show summary if any services were configured
    if !selected.is_empty() {
        println!("\n{}", "Configured services:".green().bold());
        for service_display in &selected {
            if let Some(cfg) = local_dev_config.get_service(&service_display.service_id) {
                let port_str = cfg
                    .port
                    .map(|p| p.to_string())
                    .unwrap_or_else(|| "-".to_string());
                println!(
                    "  {} {} (port {})",
                    "".dimmed(),
                    service_display.name.cyan(),
                    port_str
                );
            }
        }
    }

    Ok(())
}

/// Returns a list of (port, service_names) for ports that have multiple services
fn detect_port_conflicts(
    configs: &HashMap<String, CodeServiceConfig>,
    service_names: &HashMap<String, String>,
) -> Vec<(u16, Vec<String>)> {
    let mut port_to_services: HashMap<u16, Vec<String>> = HashMap::new();

    for (service_id, config) in configs {
        if let Some(port) = config.port {
            let name = service_names
                .get(service_id)
                .cloned()
                .unwrap_or_else(|| service_id.clone());
            port_to_services.entry(port).or_default().push(name);
        }
    }

    port_to_services
        .into_iter()
        .filter(|(_, services)| services.len() > 1)
        .collect()
}

/// Detects and resolves port conflicts. Returns true if conflicts were resolved, false if none.
fn resolve_port_conflicts(
    local_dev_config: &mut LocalDevConfig,
    service_names: &HashMap<String, String>,
    project_id: &str,
) -> Result<bool> {
    let conflicts = detect_port_conflicts(&local_dev_config.services, service_names);
    if conflicts.is_empty() {
        return Ok(false);
    }

    if !std::io::stdout().is_terminal() {
        for (port, services) in &conflicts {
            eprintln!(
                "{} Port {} is used by multiple services: {}",
                "Error:".red().bold(),
                port,
                services.join(", ")
            );
        }
        anyhow::bail!("Port conflicts detected. Run 'railway develop configure' to resolve.");
    }

    println!("\n{} Port conflicts detected:", "Warning:".yellow().bold());
    for (port, services) in &conflicts {
        println!("  Port {}: {}", port, services.join(", "));
    }
    println!();

    // Prompt to resolve each conflict - skip first service, reconfigure the rest
    for (port, conflicting_services) in conflicts {
        for service_name in conflicting_services.iter().skip(1) {
            let service_id = service_names
                .iter()
                .find(|(_, name)| *name == service_name)
                .map(|(id, _)| id.clone());

            if let Some(service_id) = service_id {
                let suggested = generate_random_port();
                let port_input = prompt_text(&format!(
                    "New port for '{}' (currently {}) [{}]:",
                    service_name, port, suggested
                ))?;

                let new_port = if port_input.is_empty() {
                    suggested
                } else {
                    port_input.parse().context("Invalid port number")?
                };

                if let Some(mut cfg) = local_dev_config.get_service(&service_id).cloned() {
                    cfg.port = Some(new_port);
                    local_dev_config.set_service(service_id, cfg);
                }
            }
        }
    }
    local_dev_config.save(project_id)?;
    println!();
    Ok(true)
}

async fn up_command(args: UpArgs) -> Result<()> {
    require_docker_compose();

    let configs = Configs::new()?;
    let client = GQLClient::new_authorized(&configs)?;
    let linked_project = configs.get_linked_project().await?;

    ensure_project_and_environment_exist(&client, &configs, &linked_project).await?;

    let project_data =
        project::get_project(&client, &configs, linked_project.project.clone()).await?;

    let service_names: HashMap<String, String> = project_data
        .services
        .edges
        .iter()
        .map(|e| (e.node.id.clone(), e.node.name.clone()))
        .collect();

    let project_id = linked_project.project.clone();
    let environment_id = match args.environment.clone() {
        Some(env) => env,
        None => linked_project.environment_id()?.to_string(),
    };

    let env_response = fetch_environment_config(&client, &configs, &environment_id, true).await?;
    let env_name = env_response.name;
    let config = env_response.config;

    let service_slugs = build_service_endpoints(&service_names, &config);

    let image_services: Vec<_> = config
        .services
        .iter()
        .filter(|(_, svc)| svc.is_image_based())
        .collect();

    let code_services: Vec<_> = config
        .services
        .iter()
        .filter(|(_, svc)| svc.is_code_based())
        .collect();

    let mut local_dev_config = LocalDevConfig::load(&project_id)?;
    let config_file_exists = LocalDevConfig::path(&project_id).exists();

    // Only prompt for first-time setup (no local-dev.json file yet)
    if !config_file_exists && !code_services.is_empty() && std::io::stdout().is_terminal() {
        prompt_initial_service_setup(
            &code_services,
            &service_names,
            &config,
            &mut local_dev_config,
        )?;
        local_dev_config.save(&project_id)?;
        println!();
    }

    let configured_code_services: Vec<_> = code_services
        .iter()
        .filter(|(id, _)| local_dev_config.services.contains_key(*id))
        .collect();

    // Check for and resolve port conflicts among configured code services
    resolve_port_conflicts(&mut local_dev_config, &service_names, &project_id)?;

    if image_services.is_empty() && configured_code_services.is_empty() {
        if config.services.is_empty() {
            println!();
            println!("No services in environment {}", env_name.blue().bold());
            println!("Add services with {}", "railway add".cyan());
        } else {
            println!();
            println!(
                "No services to run in environment {}",
                env_name.blue().bold()
            );
            println!(
                "Use {} to set up code services",
                "railway develop configure".cyan()
            );
        }
        println!();
        return Ok(());
    }

    let https_config = if args.no_https {
        None
    } else {
        setup_https(&project_data.name, &project_id)?
    };

    // Build LocalDevelopContext with all service domain info
    let mut ctx = LocalDevelopContext::new(NetworkMode::Docker);
    ctx.https_config = https_config.as_ref().map(|c| HttpsDomainConfig {
        base_domain: c.base_domain.clone(),
        use_port_443: c.use_port_443,
    });

    // Add image services to context (public_domain_prod populated after fetching vars)
    for (service_id, svc) in &image_services {
        let slug = service_slugs.get(*service_id).cloned().unwrap_or_default();
        let port_mapping = build_slug_port_mapping(service_id, svc);
        ctx.services.insert(
            (*service_id).clone(),
            ServiceDomainConfig {
                slug,
                port_mapping,
                public_domain_prod: None,
                https_proxy_port: None, // port_mapping already has generated ports
            },
        );
    }

    // Add configured code services to context
    for (service_id, svc) in &configured_code_services {
        let slug = service_slugs
            .get(*service_id)
            .cloned()
            .unwrap_or_else(|| slugify(service_id));
        if let Some(dev_config) = local_dev_config.get_service(service_id) {
            let internal_port = dev_config
                .port
                .map(|p| p as i64)
                .or_else(|| svc.get_ports().first().copied())
                .unwrap_or(DEFAULT_PORT as i64);
            let mut port_mapping = HashMap::new();
            for port in svc.get_ports() {
                port_mapping.insert(port, internal_port as u16);
            }
            port_mapping.insert(internal_port, internal_port as u16);
            let https_proxy_port = Some(generate_port(service_id, internal_port));
            ctx.services.insert(
                (*service_id).clone(),
                ServiceDomainConfig {
                    slug,
                    port_mapping,
                    public_domain_prod: None,
                    https_proxy_port,
                },
            );
        }
    }

    let variable_futures: Vec<_> = image_services
        .iter()
        .map(|(service_id, _)| {
            get_service_variables(
                &client,
                &configs,
                linked_project.project.clone(),
                environment_id.clone(),
                (*service_id).clone(),
            )
        })
        .collect();

    let variable_results = futures::future::join_all(variable_futures).await;

    let resolved_vars: HashMap<String, BTreeMap<String, String>> = image_services
        .iter()
        .zip(variable_results.into_iter())
        .filter_map(|((service_id, _), result)| {
            result.ok().map(|vars| ((*service_id).clone(), vars))
        })
        .collect();

    // Update context with public domain info from resolved variables
    for (service_id, vars) in &resolved_vars {
        if let Some(prod_domain) = vars.get("RAILWAY_PUBLIC_DOMAIN") {
            if let Some(config) = ctx.services.get_mut(service_id) {
                config.public_domain_prod = Some(prod_domain.clone());
            }
        }
    }

    let compose_result = build_image_service_compose(
        &image_services,
        &service_names,
        &resolved_vars,
        &environment_id,
        &ctx,
    );

    let mut compose_services = compose_result.services;
    let compose_volumes = compose_result.volumes;
    let service_summaries = compose_result.summaries;
    let service_count = compose_services.len();

    // Print verbose domain info for image services
    if args.verbose {
        for (service_id, _) in &image_services {
            let name = service_names
                .get(*service_id)
                .cloned()
                .unwrap_or_else(|| (*service_id).clone());
            if let Some(domains) = ctx.for_service(service_id) {
                print_domain_info(&name, &domains);
            }
        }
        print_context_info(&ctx);
    }

    if let Some(ref config) = https_config {
        setup_caddy_proxy(
            &mut compose_services,
            &service_summaries,
            &configured_code_services,
            &local_dev_config,
            &service_slugs,
            config,
            &project_id,
        )?;
    }

    let compose = DockerComposeFile {
        services: compose_services,
        networks: Some(DockerComposeNetworks {
            railway: DockerComposeNetwork {
                driver: "bridge".to_string(),
            },
        }),
        volumes: compose_volumes,
    };

    let output_path = args
        .output
        .unwrap_or_else(|| get_develop_dir(&project_id).join("docker-compose.yml"));

    if let Some(parent) = output_path.parent() {
        std::fs::create_dir_all(parent)?;
    }

    let yaml = serde_yaml::to_string(&compose)?;

    let tmp_path = output_path.with_extension("yml.tmp");
    std::fs::write(&tmp_path, &yaml)?;
    std::fs::rename(&tmp_path, &output_path)?;

    if args.dry_run {
        println!("\n{} {}", "Generated".green(), output_path.display());
        println!("\n{}", "Dry run mode, not starting services".yellow());
        println!("\nTo start manually:");
        println!("  docker compose -f {} up", output_path.display());
        return Ok(());
    }

    if !image_services.is_empty() {
        println!("{}", "Starting image services...".cyan());

        let output_path_str = output_path.to_string_lossy();
        let exit_status = tokio::process::Command::new("docker")
            .args(["compose", "-f", &*output_path_str, "up", "-d"])
            .status()
            .await?;

        if let Some(code) = exit_status.code() {
            if code != 0 {
                bail!("docker compose exited with code {}", code);
            }
        }

        // Wait for containers before starting code services that depend on them
        if !configured_code_services.is_empty() {
            println!("\n{}", "Waiting for services to be ready...".dimmed());
            wait_for_services(&output_path, Duration::from_secs(60)).await?;
        }
    }

    if !service_summaries.is_empty() {
        let svc_word = if service_count == 1 {
            "service"
        } else {
            "services"
        };
        println!(
            " {} Started {} image {}",
            "".green(),
            service_count,
            svc_word
        );
        println!();

        let use_tui =
            !args.no_tui && std::io::stdout().is_terminal() && !configured_code_services.is_empty();
        if !use_tui {
            for summary in &service_summaries {
                print_image_service_summary(summary, &https_config);
            }
        }
    }

    if configured_code_services.is_empty() {
        print_next_steps(&code_services, &service_names);
        return Ok(());
    }

    // Fetch variables for code services
    let code_var_futures: Vec<_> = configured_code_services
        .iter()
        .map(|(service_id, _)| {
            get_service_variables(
                &client,
                &configs,
                linked_project.project.clone(),
                environment_id.clone(),
                (*service_id).clone(),
            )
        })
        .collect();

    let code_var_results = futures::future::join_all(code_var_futures).await;

    let code_resolved_vars: HashMap<String, BTreeMap<String, String>> = configured_code_services
        .iter()
        .zip(code_var_results.into_iter())
        .filter_map(|((service_id, _), result)| {
            result.ok().map(|vars| ((*service_id).clone(), vars))
        })
        .collect();

    // Update context with code services' public domain info
    for (service_id, vars) in &code_resolved_vars {
        if let Some(prod_domain) = vars.get("RAILWAY_PUBLIC_DOMAIN") {
            if let Some(config) = ctx.services.get_mut(service_id) {
                config.public_domain_prod = Some(prod_domain.clone());
            }
        }
    }

    // Switch to host network mode for code services
    ctx.mode = NetworkMode::Host;

    let use_tui = !args.no_tui && std::io::stdout().is_terminal();

    let mut session = DevSession::start(
        &project_id,
        &configured_code_services,
        &service_names,
        &local_dev_config,
        &code_resolved_vars,
        &ctx,
        &https_config,
        &service_summaries,
        output_path,
        !image_services.is_empty(),
        use_tui,
        args.verbose,
    )
    .await?;

    session.run(use_tui).await?;
    session.shutdown().await;

    Ok(())
}

fn print_next_steps(
    unconfigured_code_services: &[(&String, &ServiceInstance)],
    service_names: &HashMap<String, String>,
) {
    println!("{}", "Next steps".cyan().bold());
    println!();

    println!(
        "  {} Run a command with access to these services:",
        "".dimmed()
    );
    println!("    {}", "railway run <command>".cyan());
    println!();

    if !unconfigured_code_services.is_empty() {
        println!("  {} Configure code services to run locally:", "".dimmed());
        println!("    {}", "railway dev configure".cyan());
        println!();
        println!("    {}", "Available:".dimmed());
        for (id, _) in unconfigured_code_services {
            if let Some(name) = service_names.get(*id) {
                println!("      {} {}", "·".dimmed(), name);
            }
        }
        println!();
    }
}

fn print_image_service_summary(summary: &ServiceSummary, https_config: &Option<HttpsConfig>) {
    println!("{}", summary.name.green().bold());
    println!("  {}: {}", "Image".dimmed(), summary.image);
    println!(
        "  {}: {} variables",
        "Variables".dimmed(),
        summary.var_count
    );
    if !summary.ports.is_empty() {
        println!("  {}:", "Networking".dimmed());
        let slug = slugify(&summary.name);
        for p in &summary.ports {
            match (https_config, &p.port_type) {
                (Some(config), PortType::Http) => {
                    println!(
                        "    {}: http://localhost:{}",
                        "Private".dimmed(),
                        p.external
                    );
                    if config.use_port_443 {
                        println!(
                            "    {}:  https://{}.{}",
                            "Public".dimmed(),
                            slug,
                            config.base_domain
                        );
                    } else {
                        println!(
                            "    {}:  https://{}:{}",
                            "Public".dimmed(),
                            config.base_domain,
                            p.public_port
                        );
                    }
                }
                (_, PortType::Tcp) => {
                    println!("    {}:     localhost:{}", "TCP".dimmed(), p.external);
                }
                (None, PortType::Http) => {
                    println!("    http://localhost:{}", p.external);
                }
            }
        }
    }
    if !summary.volumes.is_empty() {
        let label = if summary.volumes.len() == 1 {
            "Volume"
        } else {
            "Volumes"
        };
        println!("  {}: {}", label.dimmed(), summary.volumes.join(", "));
    }
    println!();
}

struct ImageServiceComposeResult {
    services: BTreeMap<String, DockerComposeService>,
    volumes: BTreeMap<String, DockerComposeVolume>,
    summaries: Vec<ServiceSummary>,
}

fn build_image_service_compose(
    image_services: &[(&String, &ServiceInstance)],
    service_names: &HashMap<String, String>,
    resolved_vars: &HashMap<String, BTreeMap<String, String>>,
    environment_id: &str,
    ctx: &LocalDevelopContext,
) -> ImageServiceComposeResult {
    let mut compose_services = BTreeMap::new();
    let mut compose_volumes = BTreeMap::new();
    let mut service_summaries = Vec::new();

    for (service_id, svc) in image_services {
        let service_name = service_names
            .get(*service_id)
            .cloned()
            .unwrap_or_else(|| (*service_id).clone());
        let slug = slugify(&service_name);

        let image = svc.source.as_ref().unwrap().image.clone().unwrap();

        let port_infos = build_port_infos(service_id, svc);

        let raw_vars = resolved_vars.get(*service_id).cloned().unwrap_or_default();

        let service_domains = ctx
            .for_service(service_id)
            .expect("image services added to ctx before calling this fn");

        let environment = override_railway_vars(raw_vars, Some(&service_domains), ctx);

        let ports: Vec<String> = port_infos
            .iter()
            .map(|p| format!("{}:{}", p.external, p.internal))
            .collect();

        let mut service_volumes = Vec::new();
        for (vol_id, vol_mount) in &svc.volume_mounts {
            if let Some(mount_path) = &vol_mount.mount_path {
                let vol_name = volume_name(environment_id, vol_id);
                service_volumes.push(format!("{}:{}", vol_name, mount_path));
                compose_volumes.insert(vol_name, DockerComposeVolume {});
            }
        }

        let start_command = svc
            .deploy
            .as_ref()
            .and_then(|d| d.start_command.clone())
            .map(|cmd| cmd.replace('$', "$$"));

        let volume_paths: Vec<String> = svc
            .volume_mounts
            .values()
            .filter_map(|v| v.mount_path.clone())
            .collect();

        service_summaries.push(ServiceSummary {
            name: service_name,
            image: image.clone(),
            var_count: environment.len(),
            ports: port_infos,
            volumes: volume_paths,
        });

        compose_services.insert(
            slug,
            DockerComposeService {
                image,
                command: start_command,
                restart: Some("on-failure".to_string()),
                environment,
                ports,
                volumes: service_volumes,
                networks: vec!["railway".to_string()],
                extra_hosts: Vec::new(),
            },
        );
    }

    ImageServiceComposeResult {
        services: compose_services,
        volumes: compose_volumes,
        summaries: service_summaries,
    }
}

fn setup_caddy_proxy(
    compose_services: &mut BTreeMap<String, DockerComposeService>,
    service_summaries: &[ServiceSummary],
    configured_code_services: &[&(&String, &ServiceInstance)],
    local_dev_config: &LocalDevConfig,
    service_slugs: &HashMap<String, String>,
    https_config: &HttpsConfig,
    project_id: &str,
) -> Result<()> {
    let mut service_ports: Vec<ServicePort> = service_summaries
        .iter()
        .flat_map(|s| {
            s.ports.iter().map(|p| ServicePort {
                slug: slugify(&s.name),
                internal_port: p.internal,
                external_port: p.public_port,
                is_http: matches!(p.port_type, PortType::Http),
                is_code_service: false,
            })
        })
        .collect();

    for &(service_id, svc) in configured_code_services {
        if let Some(dev_config) = local_dev_config.get_service(service_id) {
            let slug = service_slugs
                .get(*service_id)
                .cloned()
                .unwrap_or_else(|| slugify(service_id));
            let internal_port = dev_config
                .port
                .map(|p| p as i64)
                .or_else(|| svc.get_ports().first().copied())
                .unwrap_or(DEFAULT_PORT as i64);
            let proxy_port = generate_port(service_id, internal_port);

            service_ports.push(ServicePort {
                slug,
                internal_port,
                external_port: proxy_port,
                is_http: true,
                is_code_service: true,
            });
        }
    }

    let proxy_ports: Vec<String> = if https_config.use_port_443 {
        vec!["443:443".to_string()]
    } else {
        service_ports
            .iter()
            .filter(|p| p.is_http)
            .map(|p| format!("{}:{}", p.external_port, p.external_port))
            .collect()
    };

    if !proxy_ports.is_empty() {
        compose_services.insert(
            "railway-proxy".to_string(),
            DockerComposeService {
                image: "caddy:2-alpine".to_string(),
                command: None,
                restart: Some("on-failure".to_string()),
                environment: BTreeMap::new(),
                ports: proxy_ports,
                volumes: vec![
                    "./Caddyfile:/etc/caddy/Caddyfile:ro".to_string(),
                    "./certs:/certs:ro".to_string(),
                ],
                networks: vec!["railway".to_string()],
                extra_hosts: vec!["host.docker.internal:host-gateway".to_string()],
            },
        );
    }

    let develop_dir = get_develop_dir(project_id);
    std::fs::create_dir_all(&develop_dir)?;

    let caddyfile = generate_caddyfile(&service_ports, https_config);
    std::fs::write(develop_dir.join("Caddyfile"), caddyfile)?;
    std::fs::write(develop_dir.join("https_domain"), &https_config.base_domain)?;

    Ok(())
}

async fn wait_for_services(compose_path: &Path, timeout: Duration) -> Result<()> {
    let start = Instant::now();

    loop {
        if start.elapsed() > timeout {
            bail!("Timeout waiting for services to be ready");
        }

        let output = tokio::process::Command::new("docker")
            .args([
                "compose",
                "-f",
                &*compose_path.to_string_lossy(),
                "ps",
                "--format",
                "json",
            ])
            .output()
            .await?;

        let services: Vec<ComposeServiceStatus> = String::from_utf8_lossy(&output.stdout)
            .lines()
            .filter_map(|line| serde_json::from_str(line).ok())
            .collect();

        for s in &services {
            if s.state == "exited" && s.exit_code != 0 {
                bail!("Service '{}' exited with code {}", s.service, s.exit_code);
            }
        }

        let all_ready = services.iter().all(|s| {
            if !s.health.is_empty() {
                s.health == "healthy"
            } else {
                s.state == "running" || (s.state == "exited" && s.exit_code == 0)
            }
        });

        if all_ready {
            return Ok(());
        }

        tokio::time::sleep(Duration::from_millis(500)).await;
    }
}

fn setup_https(project_name: &str, project_id: &str) -> Result<Option<HttpsConfig>> {
    use colored::Colorize;

    if !check_mkcert_installed() {
        println!("{}", "mkcert not found, falling back to HTTP mode".yellow());
        println!("Install mkcert for HTTPS support: https://github.com/FiloSottile/mkcert");
        return Ok(None);
    }

    // Determine if we can use port 443
    let use_port_443 = if is_port_443_available() {
        true
    } else if is_project_proxy_on_443(project_id) {
        // Our proxy already has 443, we can reuse it
        true
    } else {
        // Something else has 443, fallback to per-service ports
        println!(
            "{}",
            "Port 443 in use by another process, using per-service ports".yellow()
        );
        false
    };

    let project_slug = slugify(project_name);
    let certs_dir = get_develop_dir(project_id).join("certs");

    // Check if certs already exist with the right mode
    let config = if certs_exist(&certs_dir, use_port_443) {
        get_existing_certs(&project_slug, &certs_dir, use_port_443)
    } else {
        println!("{}", "Setting up local HTTPS...".cyan());

        // Ensure CA is installed
        if let Err(e) = ensure_mkcert_ca() {
            println!("{}: {}", "Warning: Failed to install mkcert CA".yellow(), e);
            println!("Run 'mkcert -install' manually to trust local certificates");
        }

        match generate_certs(&project_slug, &certs_dir, use_port_443) {
            Ok(config) => {
                if use_port_443 {
                    println!(
                        "  {} Generated wildcard certs for *.{}",
                        "".green(),
                        config.base_domain
                    );
                } else {
                    println!(
                        "  {} Generated certs for {}",
                        "".green(),
                        config.base_domain
                    );
                }
                config
            }
            Err(e) => {
                println!(
                    "{}: {}",
                    "Warning: Failed to generate certificates".yellow(),
                    e
                );
                println!("Falling back to HTTP mode");
                return Ok(None);
            }
        }
    };

    Ok(Some(config))
}