/*
Config mandatory properties:
name
path
type

Optional properties:
slack_channel
aws_profile
*/
def call(body) {
  def config = [:]
  body.resolveStrategy = Closure.DELEGATE_FIRST
  body.delegate = config
  body()

  pipeline {
    agent {
      label 'linux-agents'
    }
    options { disableConcurrentBuilds() }
    environment {
      AWS_PROFILE             = "${ config.aws_profile != null ? utility.getAccountIdByProfile(config.aws_profile) : "gdb-delphi-dev"}"
      PROJECT_GROUP           = "delphi"
      ECR                     = "${utility.getAccountIdByProfile(env.AWS_PROFILE)}.dkr.ecr.us-east-1.amazonaws.com"
      OCTOPUS_CLI_SERVER      = "https://octopus.delphi.zone"
      OCTOPUS_CLI_API_KEY     = sh(script: "aws secretsmanager get-secret-value --profile gdb-delphi-dev --secret-id 'infra/jenkins/octopus_api_key' --output json | jq -r .SecretString", , returnStdout: true).trim()
      OCTOPUS_CHANNEL         = octopus.setOctoChannel(env.BRANCH_NAME)
      DEPLOY_ENV              = octopus.setDeploymentEnvDelphi(params.DEPLOY_ENV_CHOICE, env.PROJECT_GROUP, env.BRANCH_NAME )
      ZIP_PACKAGE_UPLOAD_MODE = octopus.zipPackageUploadMode(params.ZIP_PACKAGE_UPLOAD_MODE)
      NOTIFICATIONS_CHANNEL   = "${config.slack_channel != null ? config.slack_channel : "delphi-slz-cicd-alerts"}"
    }
    parameters {
      choice(
        choices: [
          'default',
          'dev',
          'qa',
          'stage',
          'skip_deployment'
        ],
        description: 'Set environment for deployment. Skipped by default master/release*, dev by default for all other branches',
        name: 'DEPLOY_ENV_CHOICE'
      )
      choice(
        choices: [
          'error',
          'overwrite',
          'ignore'
        ],
        description: 'Defines whether to raise error, overwrite or ignore if zipPackage of specified version already exists in Octopus.',
        name: 'ZIP_PACKAGE_UPLOAD_MODE'
      )
      choice(
        choices: [
          'yes',
          'no'
        ],
        description: "Don't raise errors in case release already exists",
        name: 'FAIL_IF_RELEASE_EXISTS'
      )
    }
    stages {
      stage("Generic build stage") {
        steps {
          script {
            echo "DEBUG: HAS_LINTER=${env.HAS_LINTER}"
            parameters = zipPackage.setZipPackagePipelineParams(params.FAIL_IF_RELEASE_EXISTS, config.path, env.PROJECT_GROUP, false)
            zipPackage.zipPackagePipeline(parameters)
          }
        }
      }
    }
    post {
      always {
        sh "sudo chown -R ec2-user:ec2-user ${env.WORKSPACE}"
      }
      success {
        script {
          if (currentBuild.getPreviousBuild() &&
              currentBuild.getPreviousBuild().getResult().toString() != "SUCCESS") {
            utility.slackNotifyMainBranches(currentBuild.currentResult, env.NOTIFICATIONS_CHANNEL)
          }
        }
      }
      failure {
        script {
          utility.slackNotifyMainBranches(currentBuild.currentResult, env.NOTIFICATIONS_CHANNEL)
        }
      }
      cleanup {
          deleteDir()
      }
    }
  }
}
