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

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

const STANDARD_RESULT_SOURCE_HELPERS_ALPHA: &[&str] = &[
    "ok_i32",
    "err_i32",
    "is_ok_i32",
    "is_err_i32",
    "unwrap_ok_i32",
    "unwrap_err_i32",
    "unwrap_or_i32",
    "ok_u32",
    "err_u32",
    "is_ok_u32",
    "is_err_u32",
    "unwrap_ok_u32",
    "unwrap_err_u32",
    "unwrap_or_u32",
    "ok_i64",
    "err_i64",
    "is_err_i64",
    "unwrap_err_i64",
    "unwrap_or_i64",
    "ok_u64",
    "err_u64",
    "is_ok_u64",
    "is_err_u64",
    "unwrap_ok_u64",
    "unwrap_err_u64",
    "unwrap_or_u64",
    "ok_string",
    "err_string",
    "is_err_string",
    "unwrap_err_string",
    "unwrap_or_string",
    "ok_f64",
    "err_f64",
    "is_err_f64",
    "unwrap_err_f64",
    "unwrap_or_f64",
    "ok_bool",
    "err_bool",
    "is_ok_bool",
    "is_err_bool",
    "unwrap_ok_bool",
    "unwrap_err_bool",
    "unwrap_or_bool",
];

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 standard_result_source_helper_project_checks_formats_and_tests() {
    let project =
        Path::new(env!("CARGO_MANIFEST_DIR")).join("../examples/projects/std-layout-local-result");

    assert_local_result_fixture_is_source_authored(&project);

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

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

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

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

    assert!(
        result.starts_with("(module result (export "),
        "result.slo must stay an explicit local module export"
    );
    assert!(
        main.starts_with("(module main)\n\n(import result ("),
        "main.slo must stay an explicit local result import"
    );
    assert!(
        !main.contains("(import std") && !main.contains("(import slovo.std"),
        "exp-33 fixture must not depend on automatic or package std imports"
    );
    assert!(
        !result.contains("std.result.unwrap_or")
            && !result.contains("std.result.map")
            && !result.contains("std.result.and_then")
            && !main.contains("std.result.unwrap_or")
            && !main.contains("std.result.map")
            && !main.contains("std.result.and_then"),
        "exp-33 fixture must keep deferred generic result helpers out of source"
    );

    let mut non_result_std = result.clone();
    for allowed in [
        "std.result.is_ok",
        "std.result.is_err",
        "std.result.unwrap_ok",
        "std.result.unwrap_err",
    ] {
        non_result_std = non_result_std.replace(allowed, "");
    }
    assert!(
        !non_result_std.contains("std."),
        "result.slo helpers must use only existing std.result compiler-known names"
    );
    assert!(
        !result.contains("std.option.")
            && !main.contains("std.option.")
            && !main.contains("(import option"),
        "exp-109 fixture must bridge through raw concrete option forms, not compiler-known std.option names or a separate local option module"
    );

    for helper in STANDARD_RESULT_SOURCE_HELPERS_ALPHA {
        assert!(
            result.contains(&format!("(fn {} ", helper)),
            "result.slo is missing source helper `{}`",
            helper
        );
        assert!(
            main.contains(helper),
            "main.slo does not explicitly import/use `{}`",
            helper
        );
    }

    for helper in STANDARD_RESULT_OPTION_BRIDGE_HELPERS_ALPHA {
        assert!(
            result.contains(&format!("(fn {} ", helper)),
            "result.slo is missing bridge helper `{}`",
            helper
        );
        assert!(
            main.contains(helper),
            "main.slo does not explicitly import/use bridge 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);
}
