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

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

const STANDARD_VEC_F64_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",
    "sum",
    "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_f64_source_helper_project_checks_formats_and_tests() {
    let project =
        Path::new(env!("CARGO_MANIFEST_DIR")).join("../examples/projects/std-layout-local-vec_f64");

    assert_local_vec_f64_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_f64 fmt --check", &fmt);

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

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

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

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

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

    for helper in STANDARD_VEC_F64_SOURCE_FACADE_ALPHA {
        assert!(
            vec_f64.contains(&format!("(fn {} ", helper)),
            "vec_f64.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",
        "sum_loop",
        "concat_loop",
        "take_loop",
        "drop_loop",
        "reverse_loop",
    ] {
        assert!(
            vec_f64.contains(&format!("(fn {} ", helper)),
            "vec_f64.slo is missing recursive source helper `{}`",
            helper
        );
    }

    assert_count_of_cases_are_exercised(&main);
    assert_prefix_suffix_cases_are_exercised(&main);
}

fn assert_count_of_cases_are_exercised(main: &str) {
    assert!(
        main.contains("(count_of (empty)"),
        "main.slo must exercise count_of on an empty vec_f64"
    );
    assert!(
        main.matches("(count_of").count() >= 4,
        "main.slo must exercise repeated, singleton, and absent count_of cases"
    );
}

fn assert_prefix_suffix_cases_are_exercised(main: &str) {
    for case in [
        "(starts_with values (empty))",
        "(starts_with values values)",
        "(ends_with values (empty))",
        "(ends_with values values)",
        "(without_prefix values (empty))",
        "(without_prefix values values)",
        "(without_suffix values (empty))",
        "(without_suffix values values)",
        "mismatched_prefix",
        "mismatched_suffix",
        "longer_prefix",
        "longer_suffix",
    ] {
        assert!(
            main.contains(case),
            "main.slo must exercise vec_f64 prefix/suffix case `{}`",
            case
        );
    }
}

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);
}
