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

const EXPECTED_TEST_OUTPUT: &str = concat!(
    "test \"local random i32 non-negative facade\" ... ok\n",
    "test \"explicit local random i32 non-negative facade\" ... ok\n",
    "test \"explicit local random facade all\" ... ok\n",
    "3 test(s) passed\n",
);

const STANDARD_RANDOM_SOURCE_FACADE_ALPHA: &[&str] = &["random_i32", "random_i32_non_negative"];

#[test]
fn standard_random_source_facade_project_checks_formats_and_tests() {
    let project =
        Path::new(env!("CARGO_MANIFEST_DIR")).join("../examples/projects/std-layout-local-random");

    assert_local_random_fixture_is_source_authored(&project);

    let fmt = run_glagol([
        OsStr::new("fmt"),
        OsStr::new("--check"),
        project.as_os_str(),
    ]);
    assert_success("std layout local random fmt --check", &fmt);

    let check = run_glagol([OsStr::new("check"), project.as_os_str()]);
    assert_success_stdout(check, "", "std layout local random check");

    let test = run_glagol([OsStr::new("test"), project.as_os_str()]);
    assert_success_stdout(
        test,
        EXPECTED_TEST_OUTPUT,
        "std layout local random test output",
    );
}

fn assert_local_random_fixture_is_source_authored(project: &Path) {
    let random = read(&project.join("src/random.slo"));
    let main = read(&project.join("src/main.slo"));

    assert!(
        random.starts_with("(module random (export "),
        "random.slo must stay an explicit local module export"
    );
    assert!(
        main.starts_with("(module main)\n\n(import random ("),
        "main.slo must stay an explicit local random import"
    );
    assert!(
        !main.contains("(import std") && !main.contains("(import slovo.std"),
        "random source facade fixture must not depend on automatic or package std imports"
    );
    assert!(
        random.contains("(std.random.i32)"),
        "random.slo must use the existing promoted std.random.i32 call"
    );
    let non_random_std = random.replace("std.random.i32", "");
    assert!(
        !non_random_std.contains("std.") && !main.contains("std."),
        "random source facade fixture must not introduce other compiler-known std names"
    );
    assert!(
        !random.contains("seed")
            && !random.contains("range")
            && !random.contains("bytes")
            && !random.contains("float")
            && !random.contains("uuid")
            && !random.contains("crypto"),
        "random source facade fixture must not claim deferred random APIs"
    );
    assert!(
        random.contains("(fn random_i32 () -> i32\n  (std.random.i32))"),
        "random_i32 must stay a flat source facade over std.random.i32"
    );
    assert!(
        random.contains("(fn random_i32_non_negative () -> bool\n  (>= (random_i32) 0))"),
        "random_i32_non_negative must stay a deterministic non-negative check"
    );

    for helper in STANDARD_RANDOM_SOURCE_FACADE_ALPHA {
        assert!(
            random.contains(&format!("(fn {} ", helper)),
            "random.slo is missing source facade `{}`",
            helper
        );
        assert!(
            main.contains(helper),
            "main.slo does not explicitly import/use `{}`",
            helper
        );
    }
}

fn run_glagol<I, S>(args: I) -> Output
where
    I: IntoIterator<Item = S>,
    S: AsRef<std::ffi::OsStr>,
{
    Command::new(env!("CARGO_BIN_EXE_glagol"))
        .args(args)
        .current_dir(Path::new(env!("CARGO_MANIFEST_DIR")))
        .output()
        .expect("run glagol")
}

fn read(path: &Path) -> String {
    fs::read_to_string(path).unwrap_or_else(|err| panic!("read `{}`: {}", path.display(), err))
}

fn assert_success(context: &str, output: &Output) {
    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);

    assert!(
        output.status.success(),
        "{} failed\nstdout:\n{}\nstderr:\n{}",
        context,
        stdout,
        stderr
    );
    assert!(stderr.is_empty(), "{} wrote stderr:\n{}", context, stderr);
}

fn assert_success_stdout(output: Output, expected: &str, context: &str) {
    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);

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