String AWS_ACCOUNT_ID = '437795906767'
String QA_S3_ASSUME_ROLE = 'qa-ddex-generate-s3-assume-role'
String PROD_S3_ASSUME_ROLE = 'prod-ddex-generate-s3-assume-role'
String SLACK_NOTIFICATIONS_CHANNEL = '#collections-qa-alarms'

pipeline {
    agent any

    options {
        ansiColor('xterm')
        disableConcurrentBuilds()
        timestamps()
    }

    parameters {
        choice(
            name: 'ENVIRONMENT',
            description: 'Environment to run the DDEX generation in.',
            choices: ['qa', 'prod']
        )
        string(
            name: 'OSR_ID',
            description: 'OrchardSoundRecording id to generate for'
        )
        string(
            name: 'OSR_VERSION',
            description: 'Optional: Version Id of the OrchardSoundRecording to generate for. If not provided, the latest version will be used.',
        )
        choice(
            name: 'DDEX_VERSION',
            choices: ['3.8', '4.3'],
            description: 'DDEX ENR version to generate.'
        )
        choice(
            name: 'STORE',
            choices: ['tiktok', 'youtube', 'spotify'],
            description: 'Store to generate DDEX for. Spotify not yet implemented.'
        )
        choice(
            name: 'DELIVERY_TYPE',
            choices: ['METADATA_UPDATE', 'FULL_DELIVERY', 'TAKEDOWN_DELIVERY'],
            description: 'Delivery type'
        )
    }

    stages {
        stage('Load Shared Libraries') {
            steps {
                library "jenkins-global-libraries@master"
            }
        }

        stage('Generate DDEX') {
            steps {
                script {
                    S3_ASSUME_ROLE = "${params.ENVIRONMENT == "prod" ? PROD_S3_ASSUME_ROLE : QA_S3_ASSUME_ROLE}"

                    if (!params.OSR_ID?.trim()) {
                        error "You must provide an orchard sound recording id."
                    }

                    if (!params.DDEX_VERSION?.trim()) {
                        error "You must choose a ddex version to generate."
                    }

                    if (!params.STORE?.trim()) {
                        error "We need to know which store to generate DDEX for."
                    }

                    if (!params.DELIVERY_TYPE?.trim()) {
                        error "DELIVERY_TYPE parameter is required."
                    }

                    if (!params.ENVIRONMENT?.trim()) {
                        error "ENVIRONMENT parameter is required."
                    }

                    withEcr {
                        withAWS(
                            roleAccount: AWS_ACCOUNT_ID,
                            role: S3_ASSUME_ROLE,
                            roleSessionName: env.BUILD_TAG,
                            useNode: true
                        ) {
                            withEnv([
                                "ENVIRONMENT=${params.ENVIRONMENT}",
                                "OSR_ID=${params.OSR_ID}",
                                "OSR_VERSION=${params.OSR_VERSION}",
                                "DDEX_VERSION=${params.DDEX_VERSION}",
                                "STORE=${params.STORE}",
                                "DELIVERY_TYPE=${params.DELIVERY_TYPE}",
                                "BUILD_NUMBER=${env.BUILD_NUMBER}",
                            ]) {
                                dir('ddex-generator') {
                                    sh """
                                        mkdir -p output
                                        chmod 777 output
                                        docker compose run --rm --build \
                                            -e OSR_ID \
                                            -e OSR_VERSION \
                                            -e DDEX_VERSION \
                                            -e STORE \
                                            -e DELIVERY_TYPE \
                                            -e ENVIRONMENT \
                                            -e BUILD_NUMBER \
                                            dev
                                    """
                                }
                            }
                        }
                    }
                }
            }
            post {
                success {
                    archiveArtifacts artifacts: "ddex-generator/output/${env.BUILD_NUMBER}_${params.DELIVERY_TYPE}_ddex_output.xml", followSymlinks: true
                }
            }
        }
    }

    post {
        failure {
            script {
                slackNotify channel: SLACK_NOTIFICATIONS_CHANNEL
            }
        }
        cleanup {
            cleanWs()
        }
    }
}