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

const EXPECTED_STD_RESULT_OUTPUT: &str = concat!(
    "test \"explicit std result i32 wrappers\" ... ok\n",
    "test \"explicit std result err wrappers\" ... ok\n",
    "test \"explicit std result unwrap_or i32\" ... ok\n",
    "test \"explicit std result ok_or_none i32 ok\" ... ok\n",
    "test \"explicit std result ok_or_none i32 none\" ... ok\n",
    "test \"explicit std result u32 wrappers\" ... ok\n",
    "test \"explicit std result unwrap_or u32\" ... ok\n",
    "test \"explicit std result ok_or_none u32 ok\" ... ok\n",
    "test \"explicit std result ok_or_none u32 none\" ... ok\n",
    "test \"explicit std result unwrap_or i64\" ... ok\n",
    "test \"explicit std result ok_or_none i64 ok\" ... ok\n",
    "test \"explicit std result ok_or_none i64 none\" ... ok\n",
    "test \"explicit std result u64 wrappers\" ... ok\n",
    "test \"explicit std result unwrap_or u64\" ... ok\n",
    "test \"explicit std result ok_or_none u64 ok\" ... ok\n",
    "test \"explicit std result ok_or_none u64 none\" ... ok\n",
    "test \"explicit std result unwrap_or string\" ... ok\n",
    "test \"explicit std result ok_or_none string ok\" ... ok\n",
    "test \"explicit std result ok_or_none string none\" ... ok\n",
    "test \"explicit std result unwrap_or f64\" ... ok\n",
    "test \"explicit std result ok_or_none f64 ok\" ... ok\n",
    "test \"explicit std result ok_or_none f64 none\" ... ok\n",
    "test \"explicit std result bool helpers\" ... ok\n",
    "test \"explicit std result ok_or_none bool ok\" ... ok\n",
    "test \"explicit std result ok_or_none bool none\" ... ok\n",
    "test \"explicit std result helpers all\" ... ok\n",
    "26 test(s) passed\n",
);

const EXPECTED_STD_OPTION_OUTPUT: &str = concat!(
    "test \"explicit std option i32 observation\" ... ok\n",
    "test \"explicit std option i32 unwrap_some\" ... ok\n",
    "test \"explicit std option i32 unwrap_or\" ... ok\n",
    "test \"explicit std option i32 some_or_err ok\" ... ok\n",
    "test \"explicit std option i32 some_or_err err\" ... ok\n",
    "test \"explicit std option u32 observation\" ... ok\n",
    "test \"explicit std option u32 unwrap_some\" ... ok\n",
    "test \"explicit std option u32 unwrap_or\" ... ok\n",
    "test \"explicit std option u32 some_or_err ok\" ... ok\n",
    "test \"explicit std option u32 some_or_err err\" ... ok\n",
    "test \"explicit std option i64 observation\" ... ok\n",
    "test \"explicit std option i64 unwrap_some\" ... ok\n",
    "test \"explicit std option i64 unwrap_or\" ... ok\n",
    "test \"explicit std option i64 some_or_err ok\" ... ok\n",
    "test \"explicit std option i64 some_or_err err\" ... ok\n",
    "test \"explicit std option u64 observation\" ... ok\n",
    "test \"explicit std option u64 unwrap_some\" ... ok\n",
    "test \"explicit std option u64 unwrap_or\" ... ok\n",
    "test \"explicit std option u64 some_or_err ok\" ... ok\n",
    "test \"explicit std option u64 some_or_err err\" ... ok\n",
    "test \"explicit std option f64 observation\" ... ok\n",
    "test \"explicit std option f64 unwrap_some\" ... ok\n",
    "test \"explicit std option f64 unwrap_or\" ... ok\n",
    "test \"explicit std option f64 some_or_err ok\" ... ok\n",
    "test \"explicit std option f64 some_or_err err\" ... ok\n",
    "test \"explicit std option bool observation\" ... ok\n",
    "test \"explicit std option bool unwrap_some\" ... ok\n",
    "test \"explicit std option bool unwrap_or\" ... ok\n",
    "test \"explicit std option bool some_or_err ok\" ... ok\n",
    "test \"explicit std option bool some_or_err err\" ... ok\n",
    "test \"explicit std option string observation\" ... ok\n",
    "test \"explicit std option string unwrap_some\" ... ok\n",
    "test \"explicit std option string unwrap_or\" ... ok\n",
    "test \"explicit std option string some_or_err ok\" ... ok\n",
    "test \"explicit std option string some_or_err err\" ... ok\n",
    "test \"explicit std option helpers all\" ... ok\n",
    "36 test(s) passed\n",
);

const STANDARD_OPTION_RESULT_BRIDGE_HELPERS_ALPHA: &[&str] = &[
    "some_or_err_i32",
    "some_or_err_u32",
    "some_or_err_i64",
    "some_or_err_u64",
    "some_or_err_f64",
    "some_or_err_bool",
    "some_or_err_string",
];

const STANDARD_RESULT_OPTION_BRIDGE_HELPERS_ALPHA: &[&str] = &[
    "ok_or_none_i32",
    "ok_or_none_u32",
    "ok_or_none_i64",
    "ok_or_none_u64",
    "ok_or_none_string",
    "ok_or_none_f64",
    "ok_or_none_bool",
];

#[test]
fn explicit_std_result_import_loads_repo_root_standard_source() {
    let compiler_root = Path::new(env!("CARGO_MANIFEST_DIR"));
    let project = compiler_root.join("../examples/projects/std-import-result");
    let slovo_result = compiler_root.join("../lib/std/result.slo");

    assert!(
        !project.join("src/result.slo").exists(),
        "std-import-result must not carry a local result module copy"
    );
    assert!(
        !project.join("src/bridge.slo").exists(),
        "std-import-result must not depend on a local bridge shim"
    );
    assert!(
        read(&project.join("src/main.slo")).starts_with("(module main)\n\n(import std.result ("),
        "std-import-result must exercise explicit `std.result` import syntax"
    );
    assert!(
        read(&slovo_result).starts_with("(module result (export "),
        "repo-root Slovo std/result.slo must export imported helpers directly"
    );
    for helper in STANDARD_RESULT_OPTION_BRIDGE_HELPERS_ALPHA {
        assert!(
            read(&slovo_result).contains(&format!("(fn {} ", helper)),
            "repo-root Slovo std/result.slo is missing bridge helper `{}`",
            helper
        );
        assert!(
            read(&project.join("src/main.slo")).contains(helper),
            "std-import-result must import/use bridge helper `{}`",
            helper
        );
    }

    assert_project_formats_checks_and_tests(&project, EXPECTED_STD_RESULT_OUTPUT);
}

#[test]
fn explicit_std_option_import_loads_repo_root_standard_source() {
    let compiler_root = Path::new(env!("CARGO_MANIFEST_DIR"));
    let project = compiler_root.join("../examples/projects/std-import-option");
    let slovo_option = compiler_root.join("../lib/std/option.slo");

    assert!(
        !project.join("src/option.slo").exists(),
        "std-import-option must not carry a local option module copy"
    );
    assert!(
        !project.join("src/bridge.slo").exists(),
        "std-import-option must not depend on a local bridge shim"
    );
    assert!(
        read(&project.join("src/main.slo")).starts_with("(module main)\n\n(import std.option ("),
        "std-import-option must exercise explicit `std.option` import syntax"
    );
    assert!(
        read(&slovo_option).starts_with("(module option (export "),
        "repo-root Slovo std/option.slo must export imported helpers directly"
    );
    for helper in STANDARD_OPTION_RESULT_BRIDGE_HELPERS_ALPHA {
        assert!(
            read(&slovo_option).contains(&format!("(fn {} ", helper)),
            "repo-root Slovo std/option.slo is missing bridge helper `{}`",
            helper
        );
        assert!(
            read(&project.join("src/main.slo")).contains(helper),
            "std-import-option must import/use bridge helper `{}`",
            helper
        );
    }

    assert_project_formats_checks_and_tests(&project, EXPECTED_STD_OPTION_OUTPUT);
}

fn assert_project_formats_checks_and_tests(project: &Path, expected: &str) {
    let fmt = run_glagol([
        OsStr::new("fmt"),
        OsStr::new("--check"),
        project.as_os_str(),
    ]);
    assert_success("std source search fmt --check", &fmt);

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

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