use std::{fs, path::Path, process::Command};

#[test]
fn if_fixture_emits_llvm_branch_shape() {
    let output = run_glagol(["../examples/if.slo"]);
    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);

    assert!(
        output.status.success(),
        "compiler rejected if fixture\nstdout:\n{}\nstderr:\n{}",
        stdout,
        stderr
    );
    assert!(
        stdout.contains("define i32 @choose(i32 %value)")
            && stdout.contains("if.then")
            && stdout.contains("if.else")
            && stdout.contains("if.end")
            && stdout.contains("br i1")
            && stdout.contains(" phi i32 "),
        "LLVM output did not contain expected if shape\nstdout:\n{}",
        stdout
    );
    assert!(
        !stdout.contains("if chooses"),
        "compiler emitted test metadata into LLVM output\nstdout:\n{}",
        stdout
    );
    assert!(stderr.is_empty(), "compiler wrote stderr:\n{}", stderr);
}

#[test]
fn if_fixture_runs_top_level_tests() {
    let output = run_glagol(["--run-tests", "../examples/if.slo"]);
    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);

    assert!(
        output.status.success(),
        "test runner rejected if fixture\nstdout:\n{}\nstderr:\n{}",
        stdout,
        stderr
    );
    assert_eq!(
        stdout,
        concat!(
            "test \"if chooses then\" ... ok\n",
            "test \"if chooses else\" ... ok\n",
            "test \"if returns bool\" ... ok\n",
            "3 test(s) passed\n",
        ),
        "test runner output drifted"
    );
    assert!(stderr.is_empty(), "test runner wrote stderr:\n{}", stderr);
}

#[test]
fn if_fixture_is_formatter_stable() {
    let expected = fs::read_to_string("../tests/if.slo").expect("read fixture");
    let output = run_glagol(["--format", "../tests/if.slo"]);
    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);

    assert!(
        output.status.success(),
        "formatter rejected if fixture\nstdout:\n{}\nstderr:\n{}",
        stdout,
        stderr
    );
    assert_eq!(stdout, expected, "formatter output drifted");
    assert!(stderr.is_empty(), "formatter wrote stderr:\n{}", stderr);
}

#[test]
fn if_fixture_prints_lowered_shape() {
    let surface = run_glagol(["--inspect-lowering=surface", "../examples/if.slo"]);
    let surface_stdout = String::from_utf8_lossy(&surface.stdout);
    let surface_stderr = String::from_utf8_lossy(&surface.stderr);
    assert!(
        surface.status.success(),
        "surface lowering rejected if fixture\nstdout:\n{}\nstderr:\n{}",
        surface_stdout,
        surface_stderr
    );
    assert!(
        surface_stdout.contains("if\n")
            && surface_stdout.contains("binary <")
            && surface_stdout.contains("int 10")
            && surface_stdout.contains("int 20"),
        "surface lowering output lost if shape\nstdout:\n{}",
        surface_stdout
    );
    assert!(
        surface_stderr.is_empty(),
        "surface lowering wrote stderr:\n{}",
        surface_stderr
    );

    let checked = run_glagol(["--inspect-lowering=checked", "../examples/if.slo"]);
    let checked_stdout = String::from_utf8_lossy(&checked.stdout);
    let checked_stderr = String::from_utf8_lossy(&checked.stderr);
    assert!(
        checked.status.success(),
        "checked lowering rejected if fixture\nstdout:\n{}\nstderr:\n{}",
        checked_stdout,
        checked_stderr
    );
    assert!(
        checked_stdout.contains("if : i32")
            && checked_stdout.contains("binary < : bool")
            && checked_stdout.contains("int 10 : i32")
            && checked_stdout.contains("int 20 : i32"),
        "checked lowering output lost typed if shape\nstdout:\n{}",
        checked_stdout
    );
    assert!(
        checked_stderr.is_empty(),
        "checked lowering wrote stderr:\n{}",
        checked_stderr
    );
}

fn run_glagol<const N: usize>(args: [&str; N]) -> std::process::Output {
    Command::new(env!("CARGO_BIN_EXE_glagol"))
        .args(args)
        .current_dir(Path::new(env!("CARGO_MANIFEST_DIR")))
        .output()
        .expect("run glagol")
}
