pipeline {
    agent any
    parameters {
        choice(name: 'ENV', choices: ['qa', 'uat', 'prod'], description: 'Environment in which to run the rollback')
        string(name: 'APP_NAME', defaultValue: '', description: 'Name of frontend application, e.g. frontend-insights, frontend-settings')
        string(name: 'NUMBER_OF_DEPLOYMENTS_TO_ROLLBACK', defaultValue: '', description: 'Number of deployments to go back in time. Valid values are 1-10')
        string(name: 'S3_BUCKET', defaultValue: '', description: 'Optional: Custom S3 bucket name (e.g., app.qa.theorchard.io). Leave empty to use default CDN bucket for the environment.')
        string(name: 'SHARED_LIBRARIES_VERSION', defaultValue: 'master', description: 'The version of the Jenkins shared libraries to use. Can be a branch, tag, Git revision or PR ref (e.g. pull/PR_NUMBER/merge).')
    }
    stages {
        stage('Load Shared Libraries') {
            steps {
                library "jenkins-global-libraries@${params.SHARED_LIBRARIES_VERSION}"
            }
        }
        stage('Slack notification') {
            steps {
                script {
                    if(params.ENV == 'prod'){
                        slackSend channel: "#production-outage", message: "Build Started - ${env.JOB_NAME} ${env.BUILD_NUMBER} (<${env.BUILD_URL}|Open>)"
                    }
                }
            }
        }
        stage('Revert files in S3') {
            steps {
                retry(2) {
                    withEcr(registries: [[accountId: '086679231553', region: 'us-east-1']]) {
                        withAWS(
                            region: 'us-east-1',
                            role: 'prod-cdn-deploy-role',
                            roleAccount: '437795906767',
                            roleSessionName: 'prod-cdn-deploy-role',
                            useNode: true
                        ){
                            sh '''
                                set +x
                                cd s3
                                
                                docker compose run --rm frontend-rollback
                            '''
                        }
                    }
                }
            }
        }
        stage('Cloudfront Cache Invalidations') {
            when { expression { !params.S3_BUCKET } }
            steps {
                build job: 'frontend-cdn-cache-invalidation', parameters: [
                    [$class: 'StringParameterValue', name: 'APP_NAME', value: params.APP_NAME],
                    [$class: 'StringParameterValue', name: 'ENV', value: params.ENV],
                    [$class: 'StringParameterValue', name: 'NUMBER_OF_DEPLOYMENTS_TO_ROLLBACK', value: params.NUMBER_OF_DEPLOYMENTS_TO_ROLLBACK]
                ]
            }
        }
        stage('Cloudflare CDN Cache Invalidation') {
            when { expression { !params.S3_BUCKET && params.ENV == 'prod' } }
            steps {
                script {
                    if(params.ENV == 'prod'){
                        build job: 'cloudflare-cache-invalidation'
                    }
                }
            }
        }
    }
}
