use gh_workflow::*;
use indoc::{formatdoc, indoc};

use crate::tasks::workflows::{
    extension_bump::compare_versions,
    run_tests::{orchestrate_without_package_filter, tests_pass},
    runners,
    steps::{self, CommonJobConditions, FluentBuilder, NamedJob, named},
    vars::{PathCondition, StepOutput, one_workflow_per_non_main_branch},
};

pub(crate) const ZED_EXTENSION_CLI_SHA: &str = "03d8e9aee95ea6117d75a48bcac2e19241f6e667";

// This should follow the set target in crates/extension/src/extension_builder.rs
const EXTENSION_RUST_TARGET: &str = "wasm32-wasip2";

// This is used by various extensions repos in the zed-extensions org to run automated tests.
pub(crate) fn extension_tests() -> Workflow {
    let should_check_rust = PathCondition::new("check_rust", r"^(Cargo.lock|Cargo.toml|.*\.rs)$");
    let should_check_extension =
        PathCondition::new("check_extension", r"^(extension\.toml|.*\.scm)$");

    let orchestrate =
        orchestrate_without_package_filter(&[&should_check_rust, &should_check_extension]);

    let jobs = [
        orchestrate,
        should_check_rust.guard(check_rust()),
        should_check_extension.guard(check_extension()),
    ];

    let tests_pass = tests_pass(&jobs);

    named::workflow()
        .add_event(Event::default().workflow_call(WorkflowCall::default()))
        .concurrency(one_workflow_per_non_main_branch())
        .add_env(("CARGO_TERM_COLOR", "always"))
        .add_env(("RUST_BACKTRACE", 1))
        .add_env(("CARGO_INCREMENTAL", 0))
        .add_env(("ZED_EXTENSION_CLI_SHA", ZED_EXTENSION_CLI_SHA))
        .add_env(("RUSTUP_TOOLCHAIN", "stable"))
        .add_env(("CARGO_BUILD_TARGET", EXTENSION_RUST_TARGET))
        .map(|workflow| {
            jobs.into_iter()
                .chain([tests_pass])
                .fold(workflow, |workflow, job| {
                    workflow.add_job(job.name, job.job)
                })
        })
}

fn install_rust_target() -> Step<Run> {
    named::bash(format!("rustup target add {EXTENSION_RUST_TARGET}",))
}

fn run_clippy() -> Step<Run> {
    named::bash("cargo clippy --release --all-features -- --deny warnings")
}

fn check_rust() -> NamedJob {
    let job = Job::default()
        .with_repository_owner_guard()
        .runs_on(runners::LINUX_LARGE_RAM)
        .timeout_minutes(6u32)
        .add_step(steps::checkout_repo())
        .add_step(steps::cache_rust_dependencies_namespace())
        .add_step(install_rust_target())
        .add_step(steps::cargo_fmt())
        .add_step(run_clippy())
        .add_step(steps::cargo_install_nextest())
        .add_step(
            steps::cargo_nextest(runners::Platform::Linux)
                // Set the target to the current platform again
                .with_target("$(rustc -vV | sed -n 's|host: ||p')")
                .add_env(("NEXTEST_NO_TESTS", "warn")),
        );

    named::job(job)
}

pub(crate) fn check_extension() -> NamedJob {
    let (cache_download, cache_hit) = cache_zed_extension_cli();
    let (check_version_job, version_changed, _) = compare_versions();

    let job = Job::default()
        .with_repository_owner_guard()
        .runs_on(runners::LINUX_LARGE_RAM)
        .timeout_minutes(4u32)
        .add_step(steps::checkout_repo().with_deep_history_on_non_main())
        .add_step(cache_download)
        .add_step(download_zed_extension_cli(cache_hit))
        .add_step(check())
        .add_step(check_version_job)
        .add_step(verify_version_did_not_change(version_changed));

    named::job(job)
}

pub fn cache_zed_extension_cli() -> (Step<Use>, StepOutput) {
    let step = named::uses(
        "actions",
        "cache",
        "0057852bfaa89a56745cba8c7296529d2fc39830",
    )
    .id("cache-zed-extension-cli")
    .with(
        Input::default()
            .add("path", "zed-extension")
            .add("key", "zed-extension-${{ env.ZED_EXTENSION_CLI_SHA }}"),
    );
    let output = StepOutput::new(&step, "cache-hit");
    (step, output)
}

pub fn download_zed_extension_cli(cache_hit: StepOutput) -> Step<Run> {
    named::bash(
    indoc! {
        r#"
        wget --quiet "https://zed-extension-cli.nyc3.digitaloceanspaces.com/$ZED_EXTENSION_CLI_SHA/x86_64-unknown-linux-gnu/zed-extension"
        chmod +x zed-extension
        "#,
    }
    ).if_condition(Expression::new(format!("{} != 'true'", cache_hit.expr())))
}

pub fn check() -> Step<Run> {
    named::bash(indoc! {
        r#"
        mkdir -p /tmp/ext-scratch
        mkdir -p /tmp/ext-output
        ./zed-extension --source-dir . --scratch-dir /tmp/ext-scratch --output-dir /tmp/ext-output
        "#
    })
}

fn verify_version_did_not_change(version_changed: StepOutput) -> Step<Run> {
    named::bash(formatdoc! {r#"
        if [[ {version_changed} == "true" && "${{{{ github.event_name }}}}" == "pull_request" && "${{{{ github.event.pull_request.user.login }}}}" != "zed-zippy[bot]" ]] ; then
            echo "Version change detected in your change!"
            echo "Version changes happen in separate PRs and will be performed by the zed-zippy bot"
            exit 42
        fi
        "#
    })
}
