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

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

const STANDARD_VEC_BOOL_SOURCE_FACADE_ALPHA: &[&str] = &[
    "empty",
    "append",
    "len",
    "at",
    "singleton",
    "append2",
    "append3",
    "pair",
    "triple",
    "is_empty",
    "index_or",
    "first_or",
    "last_or",
    "index_option",
    "first_option",
    "last_option",
    "index_of_option",
    "last_index_of_option",
    "contains",
    "count_of",
    "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_bool_source_helper_project_checks_formats_and_tests() {
    let project = Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("../examples/projects/std-layout-local-vec_bool");

    assert_local_vec_bool_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_bool fmt --check", &fmt);

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

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

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

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

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

    for helper in STANDARD_VEC_BOOL_SOURCE_FACADE_ALPHA {
        assert!(
            vec_bool.contains(&format!("(fn {} ", helper)),
            "vec_bool.slo is missing source helper `{}`",
            helper
        );
        assert!(
            main.contains(helper),
            "main.slo does not explicitly import/use `{}`",
            helper
        );
    }
    for helper in [
        "index_of_option_loop",
        "last_index_of_option_loop",
        "contains_loop",
        "count_of_loop",
        "concat_loop",
        "take_loop",
        "drop_loop",
        "reverse_loop",
    ] {
        assert!(
            vec_bool.contains(&format!("(fn {} ", helper)),
            "vec_bool.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);
}
