use gh_workflow::{
    Event, Expression, Input, Job, Level, Permissions, PullRequest, PullRequestType, Push, Run,
    Step, UsesJob, Workflow, WorkflowDispatch,
};
use indoc::indoc;

use crate::tasks::workflows::{
    GenerateWorkflowArgs, GitSha,
    extensions::WithAppSecrets,
    runners,
    steps::{CommonJobConditions, NamedJob, named},
    vars::{JobOutput, StepOutput, one_workflow_per_non_main_branch_and_token},
};

pub(crate) fn bump_version(args: &GenerateWorkflowArgs) -> Workflow {
    let (determine_bump_type, bump_type) = determine_bump_type();
    let bump_type = bump_type.as_job_output(&determine_bump_type);

    let call_bump_version = call_bump_version(args.sha.as_ref(), &determine_bump_type, bump_type);

    named::workflow()
        .on(Event::default()
            .push(
                Push::default()
                    .add_branch("main")
                    .add_ignored_path(".github/**"),
            )
            .pull_request(PullRequest::default().add_type(PullRequestType::Labeled))
            .workflow_dispatch(WorkflowDispatch::default()))
        .concurrency(one_workflow_per_non_main_branch_and_token("labels"))
        .add_job(determine_bump_type.name, determine_bump_type.job)
        .add_job(call_bump_version.name, call_bump_version.job)
}

pub(crate) fn call_bump_version(
    target_ref: Option<&GitSha>,
    depending_job: &NamedJob,
    bump_type: JobOutput,
) -> NamedJob<UsesJob> {
    let job = Job::default()
        .cond(Expression::new(format!(
            "github.event.action != 'labeled' || {} != 'patch'",
            bump_type.expr()
        )))
        .permissions(
            Permissions::default()
                .contents(Level::Write)
                .issues(Level::Write)
                .pull_requests(Level::Write)
                .actions(Level::Write),
        )
        .uses(
            "zed-industries",
            "zed",
            ".github/workflows/extension_bump.yml",
            target_ref.map_or("main", AsRef::as_ref),
        )
        .add_need(depending_job.name.clone())
        .with(
            Input::default()
                .add("bump-type", bump_type.to_string())
                .add("force-bump", "${{ github.event_name != 'push' }}"),
        )
        .with_app_secrets();

    named::job(job)
}

fn determine_bump_type() -> (NamedJob, StepOutput) {
    let (get_bump_type, output) = get_bump_type();
    let job = Job::default()
        .with_repository_owner_guard()
        .permissions(Permissions::default())
        .runs_on(runners::LINUX_SMALL)
        .add_step(get_bump_type)
        .outputs([(output.name.to_owned(), output.to_string())]);
    (named::job(job), output)
}

fn get_bump_type() -> (Step<Run>, StepOutput) {
    let step = named::bash(
        indoc! {r#"
            if [ "$HAS_MAJOR_LABEL" = "true" ]; then
                bump_type="major"
            elif [ "$HAS_MINOR_LABEL" = "true" ]; then
                bump_type="minor"
            else
                bump_type="patch"
            fi
            echo "bump_type=$bump_type" >> $GITHUB_OUTPUT
        "#},
    )
    .add_env(("HAS_MAJOR_LABEL",
        indoc!{
            "${{ (github.event.action == 'labeled' && github.event.label.name == 'major') ||
            (github.event.action == 'synchronize' && contains(github.event.pull_request.labels.*.name, 'major')) }}"
        }))
    .add_env(("HAS_MINOR_LABEL",
        indoc!{
            "${{ (github.event.action == 'labeled' && github.event.label.name == 'minor') ||
            (github.event.action == 'synchronize' && contains(github.event.pull_request.labels.*.name, 'minor')) }}"
        }))
    .id("get-bump-type");

    let step_output = StepOutput::new(&step, "bump_type");

    (step, step_output)
}
