use crate::cli::{err, hint, ok, DEFAULT_PORT};
use anyhow::Result;
use clap::Parser;
use std::time::Duration;

#[derive(Parser)]
pub struct Args {
    #[arg(long, default_value_t = DEFAULT_PORT)]
    pub port: u16,
}

pub fn run(args: Args) -> Result<()> {
    let url = format!("http://127.0.0.1:{}/health", args.port);
    match ureq::get(&url).timeout(Duration::from_secs(2)).call() {
        Ok(resp) => {
            let body: serde_json::Value = resp.into_json()?;
            ok(format!("healthy on port {} — {}", args.port, body));
            Ok(())
        }
        Err(e) => {
            err(format!("not running on port {}: {e}", args.port));
            hint("start it with `adom-step view <file.step>`");
            Err(anyhow::anyhow!("not running"))
        }
    }
}