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

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

const STANDARD_VEC_STRING_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_string_import_loads_repo_root_standard_source_when_present() {
    let compiler_root = Path::new(env!("CARGO_MANIFEST_DIR"));
    let project = compiler_root.join("../examples/projects/std-import-vec_string");
    let slovo_vec_string = compiler_root.join("../lib/std/vec_string.slo");

    assert!(
        !project.join("src/vec_string.slo").exists(),
        "std-import-vec_string must not carry a source-root vec_string module copy"
    );
    assert!(
        !project.join("std/vec_string.slo").exists(),
        "std-import-vec_string must not carry a project-local std/vec_string.slo copy"
    );
    let main = read(&project.join("src/main.slo"));
    assert!(
        main.starts_with("(module main)\n\n(import std.vec_string ("),
        "std-import-vec_string must exercise explicit `std.vec_string` import syntax"
    );
    for helper in STANDARD_VEC_STRING_SOURCE_FACADE_ALPHA {
        assert!(
            main.contains(helper),
            "std-import-vec_string 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_string facade source search fmt --check", &fmt);

    if !slovo_vec_string.exists() {
        eprintln!(
            "skipping std vec_string source-search check/test: missing `{}`",
            slovo_vec_string.display()
        );
        return;
    }

    let slovo_source = read(&slovo_vec_string);
    assert!(
        slovo_source.starts_with("(module vec_string (export "),
        "repo-root Slovo std/vec_string.slo must export imported helpers directly"
    );
    assert!(
        !slovo_source.contains("capacity")
            && !slovo_source.contains("reserve")
            && !slovo_source.contains("shrink")
            && !slovo_source.contains("sort")
            && !slovo_source.contains("map")
            && !slovo_source.contains("filter")
            && !slovo_source.contains("concat_all")
            && !slovo_source.contains("(vec i32)")
            && !slovo_source.contains("(vec i64)")
            && !slovo_source.contains("(vec f64)")
            && !slovo_source.contains("(vec bool)")
            && !slovo_source.contains("(option i64)")
            && !slovo_source.contains("(option f64)")
            && !slovo_source.contains("(option bool)")
            && !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"),
        "std/vec_string.slo must stay narrow and explicit within concrete vec string plus option i32/string helpers"
    );
    for helper in STANDARD_VEC_STRING_SOURCE_FACADE_ALPHA {
        assert!(
            slovo_source.contains(&format!("(fn {} ", helper)),
            "Slovo std/vec_string.slo is missing helper `{}`",
            helper
        );
    }

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

    let test = run_glagol([OsStr::new("test"), project.as_os_str()]);
    assert_success_stdout(
        test,
        EXPECTED_STD_VEC_STRING_OUTPUT,
        "std vec_string 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);
}
