pipeline {
    agent any

    parameters {
        choice(name: 'ENVIRONMENT', choices: ['qa', 'prod'], description: 'The environment to run against.')
        stashedFile(name: 'SPLITS_FILE', description: 'The XLSX file containing the splits data.')
        string(name: 'TICKET_ID', description: 'The ticket ID associated with this ingestion (e.g. AS-1234). This will be recorded in the `created_by` / `updated_by` columns.')
        booleanParam(name: 'DRY_RUN', defaultValue: true, description: 'Print a summary of changes only; make no DB changes.')
        string(name: 'SHARED_LIBRARIES_VERSION', defaultValue: 'master', description: 'The version of the Jenkins shared libraries to use. Can be a branch, tag or Git revision.')
    }

    environment {
        COLLAB_DB_USER = 'collab_env'
        COLLAB_DB_DBNAME = 'ows_collaborator'
        COLLAB_DB_HOST = "${params.ENVIRONMENT}-ows-collaborator.cluster-cb22xqmk0y0q.us-east-1.rds.amazonaws.com"
        SNOWFLAKE_USER = 'prod_ows_collaborator'
        SNOWFLAKE_ROLE = 'prod_ows_collaborator'
        SNOWFLAKE_WAREHOUSE = 'prod_ows_warehouse'
        SNOWFLAKE_ACCOUNT = 'sme-delphi'
        ENVIRONMENT = "${params.ENVIRONMENT}"
        SNOWFLAKE_SCHEMA_FACTS = 'facts.prod'
        SNOWFLAKE_SCHEMA_ROYALTY_ACCOUNTING = 'orchard_app_reporting_v2.prod_royalty_accounting_royalty_accounting'
    }

    stages {
        stage('Load shared libraries') {
            steps {
                library "jenkins-global-libraries@${params.SHARED_LIBRARIES_VERSION}"
            }
        }

        stage('Ingest splits data') {
            steps {
                withEcr {
                    withCredentials([aws(credentialsId: "${params.ENVIRONMENT}-collaborator-report-trigger")]) {
                        withSecrets(secrets: [
                            [id: "${params.ENVIRONMENT}/ows-collaborator/COLLAB_DB_PASS", environmentVariable: 'COLLAB_DB_PASS'],
                        ]) {
                            withCredentials([aws(credentialsId: 'prod-collaborator-report-trigger')]) {
                                withSecrets(secrets: [
                                    [id: 'prod/ows-collaborator/SNOWFLAKE_PRIVATE_KEY', environmentVariable: 'SNOWFLAKE_PRIVATE_KEY'],
                                    [id: 'prod/ows-collaborator/SNOWFLAKE_KEY_PASSPHRASE', environmentVariable: 'SNOWFLAKE_KEY_PASSPHRASE'],
                                ]) {
                                    withFileParameter('SPLITS_FILE') {
                                        sh '''
                                            docker compose run \
                                                --build \
                                                --entrypoint /poetry/bin/poetry \
                                                --env COLLAB_DB_USER \
                                                --env COLLAB_DB_PASS \
                                                --env COLLAB_DB_HOST \
                                                --env COLLAB_DB_DBNAME \
                                                --env SNOWFLAKE_ACCOUNT \
                                                --env SNOWFLAKE_USER \
                                                --env SNOWFLAKE_ROLE \
                                                --env SNOWFLAKE_WAREHOUSE \
                                                --env SNOWFLAKE_PRIVATE_KEY \
                                                --env SNOWFLAKE_KEY_PASSPHRASE \
                                                --env SNOWFLAKE_SCHEMA_FACTS \
                                                --env SNOWFLAKE_SCHEMA_ROYALTY_ACCOUNTING \
                                                --env Environment=$ENVIRONMENT \
                                                --env "FILE_PATH=/var/app/$SPLITS_FILE_FILENAME" \
                                                --env TICKET_ID \
                                                --env DRY_RUN \
                                                --volume "$SPLITS_FILE:/var/app/$SPLITS_FILE_FILENAME" \
                                                dev \
                                                run python -m scripts.ingest_splits \
                                                2>&1
                                        '''
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    post {
        failure {
            script {
                if (!params.DRY_RUN) {
                    slackSend(
                        channel: '#collaborators-alerts',
                        color: 'danger',
                        message: "Splits ingestion failed in ${params.ENVIRONMENT} (${params.TICKET_ID})")
                }
            }
        }
        success {
            script {
                if (!params.DRY_RUN) {
                    slackSend(
                        channel: '#collaborators-alerts',
                        color: 'good',
                        message: "Splits ingestion succeeded in ${params.ENVIRONMENT} (${params.TICKET_ID})")
                }
            }
        }
    }
}
