pipeline {
    agent any

    parameters {
        choice(name: 'ENVIRONMENT', choices: ['qa', 'prod'], description: 'The environment to run against.')
        stashedFile(name: 'SPLITS_FILE', description: 'The file containing the splits data.')
        string(name: 'VENDOR_CURRENCY', description: 'The vendor\'s accounting currency.')
        string(name: 'NUMBER_OF_ROWS_TO_PROCESS', defaultValue: '0', description: 'The number of rows to process from the file. If this is <=0, it will process all rows.')
        booleanParam(name: 'OVERWRITE_EXISTING_SPLITS', defaultValue: true, description: 'Boolean value to determine if the script should overwrite existing splits or not.')
        booleanParam(name: 'CREATE_COLLABORATORS', defaultValue: false, description: 'Boolean value to specify if the script should create new collaborators.')
        string(name: 'TICKET_ID', description: 'The ticket ID associated with this ingestion (e.g. AS-1234). This will be recorded in the `created_by` column.')
    }

    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"
    }

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

        stage('Ingest collaborators 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'],
                            [id: "${params.ENVIRONMENT}/datadog/DD_API_KEY", environmentVariable: 'DD_API_KEY'],
                            [id: "${params.ENVIRONMENT}/datadog/DD_APP_KEY", environmentVariable: 'DD_APP_KEY'],
                        ]) {
                            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 DD_API_KEY \
                                        --env DD_APP_KEY \
                                        --env AWS_ACCESS_KEY_ID \
                                        --env AWS_SECRET_ACCESS_KEY \
                                        --env OVERWRITE_EXISTING_SPLITS \
                                        --env VENDOR_CURRENCY \
                                        --env CREATE_COLLABORATORS \
                                        --env NUMBER_OF_ROWS_TO_PROCESS \
                                        --env TICKET_ID \
                                        --env SPLITIO_API_KEY=localhost \
                                        --env Environment=$ENVIRONMENT \
                                        --volume $SPLITS_FILE:/var/app/$SPLITS_FILE_FILENAME \
                                        dev \
                                        run python -m scripts.collaborator_ingest /var/app/$SPLITS_FILE_FILENAME \
                                        2>&1
                                '''
                            }
                        }
                    }
                }
            }
        }
    }
}    
