#!/usr/bin/env bash

set -euo pipefail

# source the common basics
# shellcheck source=_utilities.sh
source "${WORKSPACE}/${PIPELINE_CONFIG_REPO_PATH}/scripts/_utilities.sh"

# shellcheck source=./_github.sh
source "${WORKSPACE}/${PIPELINE_CONFIG_REPO_PATH}/scripts/_github.sh"



function python_release() {
    local version_file="${1:-}"
    local release_type=$(get_env "wai-release-type")

    local build_env_image
    build_env_image=$(get_env "wai-build-env-image")

    # If not default branch, exit
    if ! using_default_branch; then
        if [ "$release_type" = "BETA" ]; then
          local git_token
          git_token=$(get_env git-token)

          install_gh_cli_via_curl
          authenticate_gh_cli "$git_token"
          login_docker_registry "$(get_env "wai-registry-development")"

          # Fetch what the new version will be
          local new_version
          new_version=$(get_env wai-artifact-version)

          # Create staging branch for new version
          local branch
          branch="$(get_default_branch)-release-${new_version}"
          git_create_branch $branch

          # Add changed files
          git add "$version_file"

          # Commit and push changed files
          # Pushing this should trigger another pipeline trigger to start a run that will push the version to artifactory
          git_commit_push "$branch" "Release version ${new_version}" --exclude-pipeline-run-url

          git push origin "${branch}"
          sleep 2m

          git_create_pr --open-from "${branch}" --title "chore(beta-release): Release of ${new_version}" --description "Release of ${new_version} created by pipeline run ${BUILD_NUMBER}" --reviewer "Eric-Marcoux"
        elif [ "$release_type" = "RELEASE" ]; then
          local git_token
          git_token=$(get_env git-token)

          install_gh_cli_via_curl
          authenticate_gh_cli "$git_token"
          login_docker_registry "$(get_env "wai-registry-development")"

          # Fetch what the new version will be
          local new_version
          new_version=$(get_env wai-artifact-version)

          # Create staging release for new version
          local branch
          branch="$(get_default_branch)-release-${new_version}"
          git_create_branch $branch
          git commit --allow-empty -m "autogenerated: Closing release version ${new_version}"
          git push -u origin "${branch}"
          sleep 2m

          git_create_pr --open-from "${branch}" --title "chore(release): Release of ${new_version}" --description "Release of ${new_version} created by pipeline run ${BUILD_NUMBER}" --reviewer "Eric-Marcoux"
        else
          warn "Not on default branch, skipping release"
        fi
    elif ! property_set "wai-release-type"; then
        warn "No new version detected, skipping release"
    else
        local git_token
        git_token=$(get_env git-token)
        
        install_gh_cli_via_curl
        authenticate_gh_cli "$git_token"
        login_docker_registry "$(get_env "wai-registry-development")"

        # Fetch what the new version will be
        local new_version
        new_version=$(get_env wai-artifact-version)

        # Create staging branch for new version
        local branch
        branch="$(get_default_branch)-staging-${new_version}"
        git_create_branch $branch

        # Add changed files
        git add "$version_file"

        # Commit and push changed files
        # Pushing this should trigger another pipeline trigger to start a run that will push the version to artifactory
        git_commit_push "$branch" "Release version ${new_version}" --exclude-pipeline-run-url

        git push origin "${branch}"
        sleep 2m

        git_create_pr --open-from "${branch}" --title "chore(release-candidate): Release Candidate of ${new_version}" --description "Release candidate of ${new_version} created by pipeline run ${BUILD_NUMBER}" --reviewer "Eric-Marcoux"
    fi
}


if property_set "wai-release-type"; then
   warn "Starting auto release"
   # Need to pass in the path of the version file
   python_release src/ibm_watsonx_orchestrate/__init__.py
fi

success "EXIT ${BASH_SOURCE[0]}"
