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

const EXPECTED_STD_VEC_BOOL_OUTPUT: &str = concat!(
    "test \"explicit std vec_bool empty len facade\" ... ok\n",
    "test \"explicit std vec_bool direct at facade\" ... ok\n",
    "test \"explicit std vec_bool builder helpers\" ... ok\n",
    "test \"explicit std vec_bool query helpers\" ... ok\n",
    "test \"explicit std vec_bool option query helpers\" ... ok\n",
    "test \"explicit std vec_bool starts_with helper\" ... ok\n",
    "test \"explicit std vec_bool ends_with helper\" ... ok\n",
    "test \"explicit std vec_bool without_suffix helper\" ... ok\n",
    "test \"explicit std vec_bool without_prefix helper\" ... ok\n",
    "test \"explicit std vec_bool transform helpers\" ... ok\n",
    "test \"explicit std vec_bool subvec helper\" ... ok\n",
    "test \"explicit std vec_bool insert helper\" ... ok\n",
    "test \"explicit std vec_bool insert range helper\" ... ok\n",
    "test \"explicit std vec_bool replace helper\" ... ok\n",
    "test \"explicit std vec_bool replace range helper\" ... ok\n",
    "test \"explicit std vec_bool remove helper\" ... ok\n",
    "test \"explicit std vec_bool remove range helper\" ... ok\n",
    "test \"explicit std vec_bool real program helpers\" ... ok\n",
    "test \"explicit std 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 explicit_std_vec_bool_import_loads_repo_root_standard_source() {
    let compiler_root = Path::new(env!("CARGO_MANIFEST_DIR"));
    let project = compiler_root.join("../examples/projects/std-import-vec_bool");
    let slovo_vec_bool = compiler_root.join("../lib/std/vec_bool.slo");

    assert!(
        !project.join("src/vec_bool.slo").exists(),
        "std-import-vec_bool must not carry a source-root vec_bool module copy"
    );
    assert!(
        !project.join("std/vec_bool.slo").exists(),
        "std-import-vec_bool must not carry a project-local std/vec_bool.slo copy"
    );
    let main = read(&project.join("src/main.slo"));
    assert!(
        main.starts_with("(module main)\n\n(import std.vec_bool ("),
        "std-import-vec_bool must exercise explicit `std.vec_bool` import syntax"
    );

    let slovo_source = read(&slovo_vec_bool);
    assert!(
        slovo_source.starts_with("(module vec_bool (export "),
        "repo-root Slovo std/vec_bool.slo must export imported helpers directly"
    );
    assert!(
        !slovo_source.contains("(vec i32)")
            && !slovo_source.contains("(vec i64)")
            && !slovo_source.contains("(vec string)")
            && !slovo_source.contains("(option i64)")
            && !slovo_source.contains("(option f64)")
            && !slovo_source.contains("(option string)")
            && !slovo_source.contains("(result i32")
            && !slovo_source.contains("(result i64")
            && !slovo_source.contains("(result f64")
            && !slovo_source.contains("(result string")
            && !slovo_source.contains("(result bool")
            && !slovo_source.contains("capacity")
            && !slovo_source.contains("reserve")
            && !slovo_source.contains("shrink")
            && !slovo_source.contains("sort")
            && !slovo_source.contains("map")
            && !slovo_source.contains("filter"),
        "std/vec_bool.slo must stay concrete to vec bool plus option i32/bool helpers"
    );

    let mut non_vec_std = slovo_source.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."),
        "std/vec_bool.slo must use only the existing promoted std.vec.bool runtime names"
    );

    for helper in STANDARD_VEC_BOOL_SOURCE_FACADE_ALPHA {
        assert!(
            slovo_source.contains(&format!("(fn {} ", helper)),
            "Slovo std/vec_bool.slo is missing helper `{}`",
            helper
        );
        assert!(
            main.contains(helper),
            "std-import-vec_bool main fixture import/use is missing helper `{}`",
            helper
        );
    }

    let fmt = run_glagol([
        OsStr::new("fmt"),
        OsStr::new("--check"),
        project.as_os_str(),
    ]);
    assert_success("std vec_bool facade source search fmt --check", &fmt);

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

    let test = run_glagol([OsStr::new("test"), project.as_os_str()]);
    assert_success_stdout(
        test,
        EXPECTED_STD_VEC_BOOL_OUTPUT,
        "std vec_bool facade source search test",
    );
}

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\nstatus: {:?}\nstdout:\n{}\nstderr:\n{}",
        context,
        output.status.code(),
        stdout,
        stderr
    );
}

fn assert_success_stdout(output: Output, expected: &str, context: &str) {
    assert_success(context, &output);
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert_eq!(stdout, expected, "{}", context);
}
