rise-deploy 0.16.4

A simple and powerful CLI for deploying containerized applications
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
use anyhow::{bail, Result};
use reqwest::Client;
use serde::Deserialize;
use std::io::{self, IsTerminal, Write as _};
use std::time::{Duration, Instant};
use tracing::{debug, info};

use crate::api::models::{Deployment, DeploymentStatus};
use crate::config::Config;

use super::core::{fetch_deployment, open_log_stream, parse_duration, LogStreamError};

// Project info for fetching project URL
#[derive(Deserialize)]
struct ProjectInfo {
    #[serde(skip_serializing_if = "Option::is_none")]
    primary_url: Option<String>,
}

// Legacy Docker controller metadata structures (for backward compatibility with old deployments)
#[derive(Deserialize, Debug, Clone, PartialEq)]
struct DockerMetadata {
    #[serde(default)]
    reconcile_phase: ReconcilePhase,
    container_id: Option<String>,
    container_name: Option<String>,
    assigned_port: Option<u16>,
}

#[derive(Deserialize, Debug, Clone, PartialEq, Default)]
enum ReconcilePhase {
    #[default]
    NotStarted,
    CreatingContainer,
    StartingContainer,
    WaitingForHealth,
    Completed,
}

// ANSI escape codes for terminal manipulation
mod ansi {
    pub const CLEAR_LINE: &str = "\x1B[2K";
    pub const HIDE_CURSOR: &str = "\x1B[?25l";
    pub const SHOW_CURSOR: &str = "\x1B[?25h";
    pub const RESET: &str = "\x1B[0m";

    /// Move cursor up n lines
    pub fn move_up(n: usize) -> String {
        format!("\x1B[{}A", n)
    }

    /// Move cursor to beginning of line
    pub const CURSOR_TO_START: &str = "\r";
}

// Spinner animation frames
const SPINNER_FRAMES: &[&str] = &["", "", "", "", "", "", "", "", "", ""];

/// State tracking between polls
struct FollowState {
    last_status: DeploymentStatus,
    last_controller_phase: Option<ReconcilePhase>,
    last_error: Option<String>,
    last_url: Option<String>,
    last_metadata: serde_json::Value,
    spinner_frame: usize,
    is_first_poll: bool,
}

impl FollowState {
    fn new() -> Self {
        Self {
            last_status: DeploymentStatus::Pending,
            last_controller_phase: None,
            last_error: None,
            last_url: None,
            last_metadata: serde_json::Value::Null,
            spinner_frame: 0,
            is_first_poll: true,
        }
    }

    fn should_log_state_change(
        &self,
        deployment: &Deployment,
        controller_phase: &Option<ReconcilePhase>,
    ) -> bool {
        self.is_first_poll
            || self.last_status != deployment.status
            || self.last_controller_phase != *controller_phase
    }

    fn update(&mut self, deployment: &Deployment, controller_phase: Option<ReconcilePhase>) {
        self.last_status = deployment.status.clone();
        self.last_controller_phase = controller_phase;
        self.last_error = deployment.error_message.clone();
        self.last_url = deployment.primary_url.clone();
        self.last_metadata = deployment.controller_metadata.clone();
        self.is_first_poll = false;
    }
}

/// Live status section that gets replaced on each poll
struct LiveStatusSection {
    pub last_line_count: usize,
}

impl LiveStatusSection {
    fn new() -> Self {
        Self { last_line_count: 0 }
    }

    fn clear_previous(&self) {
        if self.last_line_count > 0 {
            // Move cursor up and clear each line
            for _ in 0..self.last_line_count {
                print!(
                    "{}{}{}",
                    ansi::move_up(1),
                    ansi::CURSOR_TO_START,
                    ansi::CLEAR_LINE
                );
            }
            print!("{}", ansi::CURSOR_TO_START);
            io::stdout().flush().unwrap();
        }
    }

    fn render(
        &mut self,
        deployment: &Deployment,
        state: &FollowState,
        controller_phase: &Option<ReconcilePhase>,
    ) -> String {
        // Clear previous output
        self.clear_previous();

        let mut output = String::new();
        let mut line_count = 0;

        // Status line with icon and color
        let icon = status_icon(&deployment.status);
        let color = status_color(&deployment.status);
        let spinner = if is_in_progress(&deployment.status) {
            format!("{} ", spinner_frame(state.spinner_frame))
        } else {
            String::new()
        };

        // Show deployment status + controller phase if available
        let status_text = if let Some(phase) = controller_phase {
            format!("{} ({})", deployment.status, format_controller_phase(phase))
        } else {
            format!("{}", deployment.status)
        };

        output.push_str(&format!(
            "{}{} Status:    {}{}{}\n",
            spinner,
            icon,
            color,
            status_text,
            ansi::RESET
        ));
        line_count += 1;

        // URL if available
        if let Some(ref url) = deployment.primary_url {
            output.push_str(&format!("   URL:       {}\n", url));
            line_count += 1;
        }

        // Error message if present
        if let Some(ref error) = deployment.error_message {
            output.push_str(&format!(
                "   {}Error:{} {}\n",
                "\x1B[31m",
                ansi::RESET,
                error
            ));
            line_count += 1;
        }

        // Controller metadata summary (container ID if available)
        if let Some(container_id) = extract_container_id(&deployment.controller_metadata) {
            output.push_str(&format!("   Container: {}\n", container_id));
            line_count += 1;
        }

        self.last_line_count = line_count;
        output
    }
}

/// Get status color ANSI code
fn status_color(status: &DeploymentStatus) -> &'static str {
    match status {
        DeploymentStatus::Healthy => "\x1B[32m",   // Green
        DeploymentStatus::Failed => "\x1B[31m",    // Red
        DeploymentStatus::Deploying => "\x1B[33m", // Yellow
        DeploymentStatus::Building => "\x1B[36m",  // Cyan
        DeploymentStatus::Pushing => "\x1B[36m",   // Cyan
        DeploymentStatus::Unhealthy => "\x1B[31m", // Red
        DeploymentStatus::Cancelled => "\x1B[90m", // Gray
        DeploymentStatus::Stopped => "\x1B[90m",   // Gray
        _ => "\x1B[37m",                           // White
    }
}

/// Get status icon
fn status_icon(status: &DeploymentStatus) -> &'static str {
    match status {
        DeploymentStatus::Healthy => "",
        DeploymentStatus::Failed => "",
        DeploymentStatus::Deploying => "",
        DeploymentStatus::Building => "🔨",
        DeploymentStatus::Pushing => "",
        DeploymentStatus::Pushed => "",
        DeploymentStatus::Unhealthy => "",
        DeploymentStatus::Cancelled => "",
        DeploymentStatus::Cancelling => "",
        DeploymentStatus::Terminating => "",
        DeploymentStatus::Stopped => "",
        DeploymentStatus::Superseded => "",
        DeploymentStatus::Expired => "",
        DeploymentStatus::Pending => "",
    }
}

/// Get spinner frame
fn spinner_frame(frame_num: usize) -> &'static str {
    SPINNER_FRAMES[frame_num % SPINNER_FRAMES.len()]
}

/// Check if status is in-progress (should show spinner)
fn is_in_progress(status: &DeploymentStatus) -> bool {
    matches!(
        status,
        DeploymentStatus::Pending
            | DeploymentStatus::Building
            | DeploymentStatus::Pushing
            | DeploymentStatus::Pushed
            | DeploymentStatus::Deploying
            | DeploymentStatus::Cancelling
            | DeploymentStatus::Terminating
    )
}

/// Check if status is terminal
fn is_terminal_state(status: &DeploymentStatus) -> bool {
    matches!(
        status,
        DeploymentStatus::Healthy
            | DeploymentStatus::Failed
            | DeploymentStatus::Cancelled
            | DeploymentStatus::Stopped
            | DeploymentStatus::Superseded
            | DeploymentStatus::Expired
    )
}

/// Parse controller metadata to extract deployment phase info (handles legacy Docker deployments)
fn parse_controller_metadata(metadata: &serde_json::Value) -> Option<DockerMetadata> {
    if metadata.is_null() || metadata == &serde_json::json!({}) {
        return None;
    }

    // Try to parse as Docker metadata (for legacy deployments)
    // For Kubernetes deployments, this will return None, which is fine
    serde_json::from_value::<DockerMetadata>(metadata.clone()).ok()
}

/// Extract container ID from metadata for display
fn extract_container_id(metadata: &serde_json::Value) -> Option<String> {
    parse_controller_metadata(metadata)
        .and_then(|m| m.container_id.map(|id| id[..12.min(id.len())].to_string()))
}

/// Format controller phase for display
fn format_controller_phase(phase: &ReconcilePhase) -> String {
    match phase {
        ReconcilePhase::NotStarted => "not started".to_string(),
        ReconcilePhase::CreatingContainer => "creating container".to_string(),
        ReconcilePhase::StartingContainer => "starting container".to_string(),
        ReconcilePhase::WaitingForHealth => "waiting for health".to_string(),
        ReconcilePhase::Completed => "running".to_string(),
    }
}

/// Log state change to tracing (appears in history)
fn log_state_change(
    project: &str,
    deployment_id: &str,
    status: &DeploymentStatus,
    controller_phase: &Option<ReconcilePhase>,
) {
    let status_text = if let Some(phase) = controller_phase {
        format!("{} ({})", status, format_controller_phase(phase))
    } else {
        format!("{}", status)
    };

    info!("Deployment {}:{} → {}", project, deployment_id, status_text);
}

/// Check if stdout is a TTY
fn is_tty() -> bool {
    io::stdout().is_terminal()
}

/// Print deployment snapshot (for non-follow mode)
pub fn print_deployment_snapshot(deployment: &Deployment) {
    // Parse controller metadata
    let controller_phase =
        parse_controller_metadata(&deployment.controller_metadata).map(|m| m.reconcile_phase);

    // Status line with icon and color
    let icon = status_icon(&deployment.status);
    let color = status_color(&deployment.status);

    // Show deployment status + controller phase if available
    let status_text = if let Some(phase) = controller_phase {
        format!(
            "{} ({})",
            deployment.status,
            format_controller_phase(&phase)
        )
    } else {
        format!("{}", deployment.status)
    };

    println!(
        "{} Status:         {}{}{}",
        icon,
        color,
        status_text,
        ansi::RESET
    );

    // Deployment ID
    println!("   Deployment ID:  {}", deployment.deployment_id);

    // Deployment group (if not default)
    if deployment.deployment_group != "default" {
        println!("   Group:          {}", deployment.deployment_group);
    }

    // Created by
    println!("   Created by:     {}", deployment.created_by_email);

    // Created/Updated timestamps
    println!("   Created:        {}", deployment.created);
    if deployment.updated != deployment.created {
        println!("   Updated:        {}", deployment.updated);
    }

    // Expires at (if set)
    if let Some(ref expires) = deployment.expires_at {
        println!("   Expires at:     {}", expires);
    }

    // Image and digest (if available)
    if let Some(ref image) = deployment.image {
        println!("   Image:          {}", image);
    }
    if let Some(ref digest) = deployment.image_digest {
        println!("   Image digest:   {}", digest);
    }

    // URL if available
    if let Some(ref url) = deployment.primary_url {
        println!("   URL:            {}", url);
    }

    // Controller metadata summary (container ID if available)
    if let Some(container_id) = extract_container_id(&deployment.controller_metadata) {
        println!("   Container:      {}", container_id);
    }

    // Error message if present
    if let Some(ref error) = deployment.error_message {
        println!("   \x1B[31mError:{} {}", ansi::RESET, error);
    }
}

/// Fetch project info to get project URL
async fn fetch_project_info(
    http_client: &Client,
    backend_url: &str,
    token: &str,
    project: &str,
) -> Result<ProjectInfo> {
    let url = format!("{}/api/v1/projects/{}", backend_url, project);

    let response = http_client.get(&url).bearer_auth(token).send().await?;

    if !response.status().is_success() {
        bail!("Failed to fetch project info");
    }

    let project_info: ProjectInfo = response.json().await?;
    Ok(project_info)
}

/// Check if the deployment status indicates logs should be available for streaming.
fn should_stream_logs(status: &DeploymentStatus) -> bool {
    matches!(
        status,
        DeploymentStatus::Deploying | DeploymentStatus::Unhealthy
    )
}

/// Stream logs from a deployment while monitoring its status.
///
/// Opens an SSE log stream and polls deployment status every 3 seconds.
/// Returns the final deployment when a terminal state is reached.
async fn stream_logs_with_status_polling(
    http_client: &Client,
    backend_url: &str,
    token: &str,
    project: &str,
    deployment_id: &str,
    timeout: Duration,
    start_time: Instant,
) -> Result<Deployment> {
    let mut log_stream = None;
    let mut retry_count: usize = 0;
    const MAX_RETRIES: usize = 10;
    const RETRY_DELAY: Duration = Duration::from_secs(2);

    let mut status_interval = tokio::time::interval(Duration::from_secs(3));
    status_interval.tick().await; // consume first immediate tick

    // Try initial connection
    match open_log_stream(http_client, backend_url, token, project, deployment_id, 100).await {
        Ok(s) => log_stream = Some(s),
        Err(LogStreamError::Gone) => {
            return fetch_deployment(http_client, backend_url, token, project, deployment_id).await;
        }
        Err(e) => {
            debug!("Initial log stream connection failed: {:?}", e);
        }
    }

    loop {
        if start_time.elapsed() >= timeout {
            bail!(
                "Timeout waiting for deployment to complete after {:?}",
                timeout
            );
        }

        if let Some(ref mut stream) = log_stream {
            tokio::select! {
                biased; // prefer draining log lines over status checks
                line = stream.recv() => {
                    match line {
                        Some(Ok(text)) => println!("{}", text),
                        Some(Err(e)) => {
                            debug!("Log stream error: {:?}", e);
                            log_stream = None;
                        }
                        None => {
                            debug!("Log stream ended");
                            log_stream = None;
                        }
                    }
                }
                _ = status_interval.tick() => {
                    let deployment = fetch_deployment(
                        http_client, backend_url, token, project, deployment_id,
                    ).await?;
                    if is_terminal_state(&deployment.status) {
                        drain_log_stream(stream).await;
                        return Ok(deployment);
                    }
                }
            }
        } else {
            // No active log stream - try to reconnect or poll status
            if retry_count >= MAX_RETRIES {
                debug!("Max log stream retries exceeded, falling back to status-only polling");
                return status_only_polling(
                    http_client,
                    backend_url,
                    token,
                    project,
                    deployment_id,
                    timeout,
                    start_time,
                )
                .await;
            }

            tokio::select! {
                _ = tokio::time::sleep(RETRY_DELAY) => {
                    retry_count += 1;
                    match open_log_stream(
                        http_client, backend_url, token, project, deployment_id, 100,
                    ).await {
                        Ok(s) => {
                            log_stream = Some(s);
                            retry_count = 0;
                        }
                        Err(LogStreamError::Gone) => {
                            return fetch_deployment(
                                http_client, backend_url, token, project, deployment_id,
                            ).await;
                        }
                        Err(e) => {
                            debug!("Log stream reconnect failed (attempt {}): {:?}", retry_count, e);
                        }
                    }
                }
                _ = status_interval.tick() => {
                    let deployment = fetch_deployment(
                        http_client, backend_url, token, project, deployment_id,
                    ).await?;
                    if is_terminal_state(&deployment.status) {
                        return Ok(deployment);
                    }
                }
            }
        }
    }
}

/// Drain remaining log lines from the log stream, waiting up to 2 seconds.
async fn drain_log_stream(stream: &mut super::core::LogStream) {
    let deadline = tokio::time::Instant::now() + Duration::from_secs(2);
    loop {
        tokio::select! {
            line = stream.recv() => {
                match line {
                    Some(Ok(text)) => println!("{}", text),
                    _ => break,
                }
            }
            _ = tokio::time::sleep_until(deadline) => break,
        }
    }
}

/// Fall back to status-only polling when log streaming is unavailable.
async fn status_only_polling(
    http_client: &Client,
    backend_url: &str,
    token: &str,
    project: &str,
    deployment_id: &str,
    timeout: Duration,
    start_time: Instant,
) -> Result<Deployment> {
    loop {
        let deployment =
            fetch_deployment(http_client, backend_url, token, project, deployment_id).await?;
        if is_terminal_state(&deployment.status) {
            return Ok(deployment);
        }
        if start_time.elapsed() >= timeout {
            bail!(
                "Timeout waiting for deployment to complete after {:?}",
                timeout
            );
        }
        tokio::time::sleep(Duration::from_secs(3)).await;
    }
}

/// Main follow function with enhanced UX
pub async fn follow_deployment_with_ui(
    http_client: &Client,
    backend_url: &str,
    config: &Config,
    project: &str,
    deployment_id: &str,
    timeout_str: &str,
) -> Result<Deployment> {
    let token = config
        .get_token()
        .ok_or_else(|| anyhow::anyhow!("Not authenticated"))?;

    let timeout = parse_duration(timeout_str)?;
    let start_time = Instant::now();

    // Check if we're in a TTY - if not, fall back to simple mode
    if !is_tty() {
        return follow_deployment_simple(
            http_client,
            backend_url,
            &token,
            project,
            deployment_id,
            timeout,
        )
        .await;
    }

    let mut state = FollowState::new();
    let mut live_section = LiveStatusSection::new();

    // Hide cursor for cleaner output
    print!("{}", ansi::HIDE_CURSOR);
    io::stdout().flush().unwrap();

    // Phase 1: Status polling with spinner UI
    // Poll until deployment reaches Deploying state (logs available) or a terminal state.
    let phase1_result: Result<Deployment> = async {
        loop {
            let deployment =
                fetch_deployment(http_client, backend_url, &token, project, deployment_id).await?;

            let controller_phase = parse_controller_metadata(&deployment.controller_metadata)
                .map(|m| m.reconcile_phase);

            if state.should_log_state_change(&deployment, &controller_phase) {
                live_section.clear_previous();
                log_state_change(
                    project,
                    deployment_id,
                    &deployment.status,
                    &controller_phase,
                );
                live_section.last_line_count = 0;
            } else {
                let output = live_section.render(&deployment, &state, &controller_phase);
                print!("{}", output);
                io::stdout().flush().unwrap();
            }

            state.update(&deployment, controller_phase);
            state.spinner_frame = (state.spinner_frame + 1) % SPINNER_FRAMES.len();

            // Terminal state reached before Deploying - skip to Phase 3
            if is_terminal_state(&deployment.status) {
                return Ok(deployment);
            }

            // Deploying (or later) - transition to Phase 2 for log streaming
            if should_stream_logs(&deployment.status) {
                return Ok(deployment);
            }

            if start_time.elapsed() >= timeout {
                bail!(
                    "Timeout waiting for deployment to complete after {:?}",
                    timeout
                );
            }

            tokio::time::sleep(Duration::from_secs(1)).await;
        }
    }
    .await;

    let deployment = match phase1_result {
        Ok(d) => d,
        Err(e) => {
            print!("{}", ansi::SHOW_CURSOR);
            io::stdout().flush().unwrap();
            return Err(e);
        }
    };

    // Phase 2: Log streaming + status monitoring (only if not already terminal)
    let final_deployment = if !is_terminal_state(&deployment.status) {
        // Clear spinner UI and restore cursor
        live_section.clear_previous();
        print!("{}", ansi::SHOW_CURSOR);
        io::stdout().flush().unwrap();

        println!("--- Logs ---");

        stream_logs_with_status_polling(
            http_client,
            backend_url,
            &token,
            project,
            deployment_id,
            timeout,
            start_time,
        )
        .await?
    } else {
        print!("{}", ansi::SHOW_CURSOR);
        io::stdout().flush().unwrap();
        deployment
    };

    // Phase 3: Print project URL if deployment became active (Healthy in default group)
    if final_deployment.status == DeploymentStatus::Healthy
        && final_deployment.deployment_group == "default"
    {
        if let Ok(project_info) =
            fetch_project_info(http_client, backend_url, &token, project).await
        {
            if let Some(url) = project_info.primary_url {
                println!();
                println!("Project URL: {}", url);
            }
        }
    }

    Ok(final_deployment)
}

/// Simple fallback for non-TTY environments (pipes, redirects)
async fn follow_deployment_simple(
    http_client: &Client,
    backend_url: &str,
    token: &str,
    project: &str,
    deployment_id: &str,
    timeout: Duration,
) -> Result<Deployment> {
    let start_time = Instant::now();
    let mut state = FollowState::new();

    // Phase 1: Status polling (print state changes as text lines)
    let deployment = loop {
        let deployment =
            fetch_deployment(http_client, backend_url, token, project, deployment_id).await?;

        let controller_phase =
            parse_controller_metadata(&deployment.controller_metadata).map(|m| m.reconcile_phase);

        if state.should_log_state_change(&deployment, &controller_phase) {
            log_state_change(
                project,
                deployment_id,
                &deployment.status,
                &controller_phase,
            );
        }

        state.update(&deployment, controller_phase);

        if is_terminal_state(&deployment.status) {
            break deployment;
        }

        if should_stream_logs(&deployment.status) {
            break deployment;
        }

        if start_time.elapsed() >= timeout {
            bail!("Timeout waiting for deployment");
        }

        tokio::time::sleep(Duration::from_secs(1)).await;
    };

    // Phase 2: Log streaming + status monitoring (only if not terminal)
    let final_deployment = if !is_terminal_state(&deployment.status) {
        println!("--- Logs ---");

        stream_logs_with_status_polling(
            http_client,
            backend_url,
            token,
            project,
            deployment_id,
            timeout,
            start_time,
        )
        .await?
    } else {
        deployment
    };

    // Phase 3: Print project URL if deployment became active (Healthy in default group)
    if final_deployment.status == DeploymentStatus::Healthy
        && final_deployment.deployment_group == "default"
    {
        if let Ok(project_info) = fetch_project_info(http_client, backend_url, token, project).await
        {
            if let Some(url) = project_info.primary_url {
                println!();
                println!("Project URL: {}", url);
            }
        }
    }

    Ok(final_deployment)
}