package com.freeleaps.devops

class ArgoApplicationVersionUpdater {
  def steps
  def workspace
  def configurations

  ArgoApplicationVersionUpdater(steps, configurations) {
    this.steps = steps
    this.configurations = configurations
    this.workspace = steps.env.WORKSPACE
  }

  def update(environmentSlug, component) {
    steps.log.info("ArgoApplicationVersionUpdater", "[${environmentSlug}] Update Argo application image version to ${steps.env.BUILD_IMAGE_VERSION} for ${component.name}...")
    steps.log.info("ArgoApplicationVersionUpdater", "[${environmentSlug}] Pull freeleaps-ops repository to workspace...")

    steps.dir("${workspace}") {
      steps.git branch: 'master', credentialsId: 'freeleaps-ops-credentials', url: 'https://freeleaps@dev.azure.com/freeleaps/freeleaps-ops/_git/freeleaps-ops'
    }

    steps.dir("${workspace}") {
      def valuesFile = "./${configurations.serviceName}/helm-pkg/${component.name}/values.${environmentSlug}.yaml"
      steps.log.info("ArgoApplicationVersionUpdater", "[${environmentSlug}] Update ${valuesFile}...")
      def data = steps.readYaml(file: valuesFile)
      data[component.name].image.registry = steps.env.BUILD_IMAGE_REGISTRY
      data[component.name].image.repository = steps.env.BUILD_IMAGE_REPOSITORY
      data[component.name].image.tag = steps.env.BUILD_IMAGE_VERSION
      data[component.name].image.name = steps.env.BUILD_IMAGE_NAME
      steps.writeYaml(file: valuesFile, data: data, overwrite: true)

      steps.withCredentials([steps.usernamePassword(credentialsId: 'freeleaps-ops-credentials', passwordVariable: 'OPS_GIT_PASSWORD', usernameVariable: 'OPS_GIT_USERNAME')]) {
        // steps.sh """
        //   echo "Install required tools for git..."
        //   apt-get -y update && apt-get install -y --no-install-recommends git apt-transport-https ca-certificates gnupg
        //   echo "Set ${workspace} as a safe directory..."
        //   git config --global --add safe.directory ${workspace}
        //   echo "Configure git user..."
        //   git config user.name "freeleaps-gitops-bot"
        //   git config user.email "gitops@mathmast.com"
        //   echo "Add and commit changes..."
        //   git remote add ci_origin https://${steps.env.OPS_GIT_USERNAME}:${steps.env.OPS_GIT_PASSWORD}@dev.azure.com/freeleaps/freeleaps-ops/_git/freeleaps-ops
        //   git add ${valuesFile}
        //   git commit -m "ci(bump): bump ${component.name} image version for ${environmentSlug} to ${steps.env.BUILD_IMAGE_VERSION}"
        //   echo "Push changes to freeleaps-ops repository..."
        //   git push ci_origin HEAD:master
        //   echo "Done."
        // """
        steps.sh """
          #!/bin/bash

          echo "Set ${workspace} as a safe directory..."
          git config --global --add safe.directory ${workspace}

          echo "Configure git user..."
          git config user.name "freeleaps-gitops-bot"
          git config user.email "gitops@mathmast.com"

          echo "Add and commit changes..."
          git remote add ci_origin https://${steps.env.OPS_GIT_USERNAME}:${steps.env.OPS_GIT_PASSWORD}@dev.azure.com/freeleaps/freeleaps-ops/_git/freeleaps-ops

          git add ${valuesFile}

          if ! git diff --cached --quiet; then
            git commit -m "ci(bump): bump ${component.name} image version for ${environmentSlug} to ${steps.env.BUILD_IMAGE_VERSION}"
            echo "Detected changes, commit created."
          else
            echo "No changes detected, skipping commit."
          fi

          echo "Push changes to freeleaps-ops repository..."
          git push ci_origin HEAD:master

          echo "Done."
        """
        steps.log.info("ArgoApplicationVersionUpdater", "[${environmentSlug}] ${component.name} image version bump to ${steps.env.BUILD_IMAGE_VERSION}")
      }
    }
  }
}