String GITHUB_REPOSITORY = 'streamlit-call-delete-smeanalyticsdummy-product'
List<String> AWS_REGIONS = ['us-east-1']
String SLACK_NOTIFICATIONS_CHANNEL = '#data-alerts'
String PROD_ACCOUNT_ID = '437795906767'
String PROD_DEPLOYMENT_ROLE = 'prod-jenkins-aws-pipeline-agent'

pipeline {
    agent any

    options {
        ansiColor('xterm')
        timestamps()
        buildDiscarder logRotator(
          artifactDaysToKeepStr: '30',
          artifactNumToKeepStr: '100',
          daysToKeepStr: '365',
          numToKeepStr: '1000'
        )
    }

    parameters {
        booleanParam(name: 'DEPLOY_TO_PROD', defaultValue: false, description: 'Whether or not to deploy to prod.')
    }

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

        stage('Compliance Checks') {
            steps {
                complianceChecks()
            }
        }

        stage('Deploy QA') {
            steps {
             withSecrets(
                            awsAccountId: PROD_ACCOUNT_ID,
                            awsRole: 'prod-streamlit-call-delete-smeanalyticsdummy-product-role',
                            secrets: [
                                [
                                    id: 'prod/streamlit-call-delete-smeanalyticsdummy-product/SNOWFLAKE_KEY',
                                    environmentVariable: 'PRIVATE_KEY'
                                ],
                                [
                                    id: 'qa/snowflake-streamlit-data-apps-delete-product/M2M_AUTH0_CLIENT_CREDENTIALS',
                                    environmentVariable: 'AUTH0_CREDS'
                                ],
                            ]
                        )
                {
                   sh 'docker compose run --rm --build deploy-qa'
                }
            }
        }

        stage('Deploy Prod') {
            when {
                allOf {
                    expression { params.DEPLOY_TO_PROD }
                }
            }
            steps {
             withSecrets(
                            awsAccountId: PROD_ACCOUNT_ID,
                            awsRole: 'prod-streamlit-call-delete-smeanalyticsdummy-product-role',
                            secrets: [
                                [
                                    id: 'prod/streamlit-call-delete-smeanalyticsdummy-product/SNOWFLAKE_KEY',
                                    environmentVariable: 'PRIVATE_KEY'
                                ],
                                [
                                    id: 'prod/snowflake-streamlit-data-apps-delete-product/M2M_AUTH0_CLIENT_CREDENTIALS',
                                    environmentVariable: 'AUTH0_CREDS'
                                ],
                            ]
                        )
                {
                   sh 'docker compose run --rm --build  deploy-prod'
                }
            }
        }
    }
}
