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

const EXPECTED_TEST_OUTPUT: &str = concat!(
    "test \"explicit local vec_i32 empty len facade\" ... ok\n",
    "test \"explicit local vec_i32 direct at facade\" ... ok\n",
    "test \"explicit local vec_i32 builder helpers\" ... ok\n",
    "test \"explicit local vec_i32 constructor helpers\" ... ok\n",
    "test \"explicit local vec_i32 range helper\" ... ok\n",
    "test \"explicit local vec_i32 query helpers\" ... ok\n",
    "test \"explicit local vec_i32 option query helpers\" ... ok\n",
    "test \"explicit local vec_i32 starts_with helper\" ... ok\n",
    "test \"explicit local vec_i32 ends_with helper\" ... ok\n",
    "test \"explicit local vec_i32 without_suffix helper\" ... ok\n",
    "test \"explicit local vec_i32 without_prefix helper\" ... ok\n",
    "test \"explicit local vec_i32 transform helpers\" ... ok\n",
    "test \"explicit local vec_i32 subvec helper\" ... ok\n",
    "test \"explicit local vec_i32 insert helper\" ... ok\n",
    "test \"explicit local vec_i32 insert range helper\" ... ok\n",
    "test \"explicit local vec_i32 replace helper\" ... ok\n",
    "test \"explicit local vec_i32 replace range helper\" ... ok\n",
    "test \"explicit local vec_i32 remove helper\" ... ok\n",
    "test \"explicit local vec_i32 remove range helper\" ... ok\n",
    "test \"explicit local vec_i32 count_of helper\" ... ok\n",
    "test \"explicit local vec_i32 real program helpers\" ... ok\n",
    "test \"explicit local vec_i32 helpers all\" ... ok\n",
    "22 test(s) passed\n",
);

const STANDARD_VEC_I32_SOURCE_FACADE_ALPHA: &[&str] = &[
    "empty",
    "append",
    "len",
    "at",
    "singleton",
    "append2",
    "append3",
    "pair",
    "triple",
    "repeat",
    "range",
    "range_from_zero",
    "is_empty",
    "index_or",
    "first_or",
    "last_or",
    "index_option",
    "first_option",
    "last_option",
    "index_of_option",
    "last_index_of_option",
    "count_of",
    "contains",
    "sum",
    "concat",
    "take",
    "starts_with",
    "without_prefix",
    "ends_with",
    "without_suffix",
    "drop",
    "reverse",
    "subvec",
    "insert_at",
    "insert_range",
    "replace_at",
    "replace_range",
    "remove_at",
    "remove_range",
];

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

    assert_local_vec_i32_fixture_is_source_authored(&project);

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

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

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

fn assert_local_vec_i32_fixture_is_source_authored(project: &Path) {
    let option = read(&project.join("src/option.slo"));
    let vec_i32 = read(&project.join("src/vec_i32.slo"));
    let main = read(&project.join("src/main.slo"));

    assert!(
        option.starts_with("(module option (export "),
        "option.slo must stay an explicit local module export"
    );
    assert!(
        vec_i32.starts_with("(module vec_i32 (export "),
        "vec_i32.slo must stay an explicit local module export"
    );
    assert!(
        main.starts_with("(module main)\n\n(import vec_i32 ("),
        "main.slo must stay an explicit local vec_i32 import"
    );
    assert!(
        !option.contains("(import std")
            && !vec_i32.contains("(import std")
            && !main.contains("(import std")
            && !option.contains("(import slovo.std")
            && !vec_i32.contains("(import slovo.std")
            && !main.contains("(import slovo.std"),
        "vec_i32 fixture must not depend on automatic or package std imports"
    );
    assert!(
        !option.contains("(result i32")
            && !option.contains("(result i64")
            && !option.contains("(result f64")
            && !option.contains("(result string")
            && !option.contains("(result bool")
            && !option.contains("(option i64)")
            && !option.contains("(option f64)")
            && !option.contains("(option string)")
            && !option.contains("(option bool)")
            && !vec_i32.contains("(result i32")
            && !vec_i32.contains("(result i64")
            && !vec_i32.contains("(result f64")
            && !vec_i32.contains("(result string")
            && !vec_i32.contains("(result bool")
            && !main.contains("(result i32")
            && !main.contains("(result i64")
            && !main.contains("(result f64")
            && !main.contains("(result string")
            && !main.contains("(result bool")
            && !vec_i32.contains("(vec i64)")
            && !main.contains("(vec i64)")
            && !vec_i32.contains("(vec f64)")
            && !main.contains("(vec f64)")
            && !vec_i32.contains("(vec string)")
            && !main.contains("(vec string)")
            && !vec_i32.contains("(vec bool)")
            && !main.contains("(vec bool)")
            && !vec_i32.contains("(option i64)")
            && !main.contains("(option i64)")
            && !vec_i32.contains("(option f64)")
            && !main.contains("(option f64)")
            && !vec_i32.contains("(option string)")
            && !main.contains("(option string)")
            && !vec_i32.contains("(option bool)")
            && !main.contains("(option bool)"),
        "vec_i32 fixture must stay limited to concrete i32 vec and option helpers"
    );

    let mut non_vec_std = vec_i32.clone();
    for allowed in [
        "std.vec.i32.empty",
        "std.vec.i32.append",
        "std.vec.i32.len",
        "std.vec.i32.index",
    ] {
        non_vec_std = non_vec_std.replace(allowed, "");
    }
    assert!(
        !option.contains("std.") && !non_vec_std.contains("std.") && !main.contains("std."),
        "vec_i32 fixture must use only the existing promoted std.vec.i32 runtime names"
    );
    assert!(
        !vec_i32.contains("push")
            && !vec_i32.contains("capacity")
            && !vec_i32.contains("reserve")
            && !vec_i32.contains("shrink")
            && !vec_i32.contains("sort")
            && !vec_i32.contains("map")
            && !vec_i32.contains("filter"),
        "vec_i32 fixture must not claim deferred generic or mutation-heavy collection APIs"
    );

    for helper in STANDARD_VEC_I32_SOURCE_FACADE_ALPHA {
        assert!(
            vec_i32.contains(&format!("(fn {} ", helper)),
            "vec_i32.slo is missing source helper `{}`",
            helper
        );
        assert!(
            main.contains(helper),
            "main.slo does not explicitly import/use `{}`",
            helper
        );
    }
    for helper in [
        "repeat_loop",
        "range_from_zero_loop",
        "range_loop",
        "concat_loop",
        "take_loop",
        "drop_loop",
        "reverse_loop",
    ] {
        assert!(
            vec_i32.contains(&format!("(fn {} ", helper)),
            "vec_i32.slo is missing recursive source helper `{}`",
            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);
}
