//! Env-scoped overlays (§4.1): `pic apply --env <E>` merges the sparse
//! `picloud.<env>.toml` (per-env slug) onto the base and deploys to that
//! environment's app — leaving the base app untouched.

use std::fs;

use tempfile::TempDir;

use crate::common;
use crate::common::cleanup::AppGuard;

fn scripts_ls(env: &common::TestEnv, slug: &str) -> String {
    String::from_utf8(
        common::pic_as(env)
            .args(["scripts", "ls", "--app", slug])
            .output()
            .unwrap()
            .stdout,
    )
    .unwrap()
}

#[ignore = "needs DATABASE_URL pointing at a running Postgres"]
#[test]
fn apply_env_overlay_targets_the_env_app() {
    let Some(fx) = common::fixture_or_skip() else {
        return;
    };
    let env = common::admin_env(fx);
    let base_slug = common::unique_slug("ovl");
    let staging_slug = format!("{base_slug}-staging");
    for s in [&base_slug, &staging_slug] {
        common::pic_as(&env)
            .args(["apps", "create", s])
            .assert()
            .success();
    }
    let _g1 = AppGuard::new(&env.url, &env.token, &base_slug);
    let _g2 = AppGuard::new(&env.url, &env.token, &staging_slug);

    let dir = TempDir::new().unwrap();
    fs::create_dir_all(dir.path().join("scripts")).unwrap();
    fs::write(dir.path().join("scripts/hello.rhai"), "\"hi\"").unwrap();
    let base = dir.path().join("picloud.toml");
    fs::write(
        &base,
        format!(
            "[app]\nslug = \"{base_slug}\"\nname = \"Ovl\"\n\n\
             [[scripts]]\nname = \"hello\"\nfile = \"scripts/hello.rhai\"\n"
        ),
    )
    .unwrap();
    // Overlay redirects to the staging app.
    fs::write(
        dir.path().join("picloud.staging.toml"),
        format!("[app]\nslug = \"{staging_slug}\"\n"),
    )
    .unwrap();

    // Apply to staging only.
    common::pic_as(&env)
        .args(["apply", "--file"])
        .arg(&base)
        .args(["--env", "staging"])
        .assert()
        .success();

    assert!(
        scripts_ls(&env, &staging_slug).contains("hello"),
        "overlay apply must deploy to the staging app"
    );
    assert!(
        !scripts_ls(&env, &base_slug).contains("hello"),
        "the base app must be untouched by an --env apply"
    );
}
