String SERVICE_NAME = 'graphql-router'
String INTERNAL_SERVICE_NAME = 'graphql-router-internal'
String MCP_SERVICE_NAME = 'graphql-router-mcp'
String ECR_ACCOUNT_ID = '086679231553'
List<String> AWS_REGIONS = ['us-east-1']
String SLACK_NOTIFICATIONS_CHANNEL = '#graphql'
String QA_ACCOUNT_ID = '437795906767'
String QA_DEPLOYMENT_ROLE = 'prod-jenkins-aws-pipeline-agent'
String QA_INTEGRATION_TEST_ROLE = 'qa-graphql-router-integration-test-role'
String QA_INTEGRATION_TEST_SESSION = 'qa-graphql-router-integration-test'
String UAT_ACCOUNT_ID = '437795906767'
String UAT_DEPLOYMENT_ROLE = 'prod-jenkins-aws-pipeline-agent'
String PROD_ACCOUNT_ID = '437795906767'
String PROD_DEPLOYMENT_ROLE = 'prod-jenkins-aws-pipeline-agent'

List<String> VULNERABILITIES_TO_IGNORE = []

pipeline {
    agent any

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

    parameters {
        booleanParam(name: 'DEPLOY_TO_PROD', defaultValue: true, description: 'Whether or not to deploy to prod.')
        booleanParam(name: 'DEPLOY_TO_UAT', defaultValue: true, description: 'Whether or not to deploy to uat.')
        booleanParam(name: 'DEPLOY_INTERNAL_QA', defaultValue: true, description: 'Whether or not to deploy the internal router to qa.')
        booleanParam(name: 'DEPLOY_MCP_QA', defaultValue: true, description: 'Whether or not to deploy the mcp router to qa.')
        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).')
    }

    triggers {
        issueCommentTrigger('.*retest this please.*')
    }

    stages {
        stage('Load Shared Libraries') {
            steps {
                library "jenkins-global-libraries@${params.SHARED_LIBRARIES_VERSION}"
            }
        }
        stage('Compliance Checks') {
            steps {
                complianceChecks()
            }
        }
        stage('Validate Software Catalog Definition') {
            steps {
                datadogSoftwareCatalogValidate()
            }
        }
        stage('Unit Tests and Style Checks') {
            steps {
                withEcr{
                    sh 'make ci-lint-test'
                }
            }
        }
        stage('Static Application Security Tests') {
            steps {
                sastTests()
            }
        }
        stage('Sonar Scan and Analysis') {
            when {
                branch 'master'
            }
            steps {
                sonarScan project: SERVICE_NAME, language: 'rust'
            }
        }
        stage('Create a Release') {
            when {
                anyOf {
                    branch 'master'
                    expression { env.GITHUB_COMMENT =~ 'build docker' }
                }
            }
            steps {
                dockerToEcr awsRegions: AWS_REGIONS, ecrAccountId: ECR_ACCOUNT_ID, imageName: SERVICE_NAME, imageTag: env.GIT_COMMIT,
                    dockerBuildTarget: 'deploy'
            }
        }
        stage('Deploy to QA') {
            parallel {
                stage('Deploy Public Router to QA') {
                    when {
                        branch 'master'
                    }
                    steps {
                        fargateDeploy environment: 'qa', awsRegions: AWS_REGIONS, gitCommit: env.GIT_COMMIT, serviceName: SERVICE_NAME,
                         ecrRegistryAccountId: ECR_ACCOUNT_ID, awsDeploymentTargetAccountId: QA_ACCOUNT_ID, awsDeploymentRoleName: QA_DEPLOYMENT_ROLE
                    }
                }
                stage('Deploy Internal Router to QA') {
                    when {
                        allOf {
                            branch 'master'
                            expression { params.DEPLOY_INTERNAL_QA }
                        }
                    }
                    steps {
                        fargateDeploy environment: 'qa', awsRegions: AWS_REGIONS, gitCommit: env.GIT_COMMIT, serviceName: INTERNAL_SERVICE_NAME,
                         imageNameOverride: SERVICE_NAME,
                         ecrRegistryAccountId: ECR_ACCOUNT_ID, awsDeploymentTargetAccountId: QA_ACCOUNT_ID, awsDeploymentRoleName: QA_DEPLOYMENT_ROLE
                    }
                }
                stage('Deploy MCP Router to QA') {
                    when {
                        allOf {
                            branch 'master'
                            expression { params.DEPLOY_MCP_QA }
                        }
                    }
                    steps {
                        fargateDeploy environment: 'qa', awsRegions: AWS_REGIONS, gitCommit: env.GIT_COMMIT, serviceName: MCP_SERVICE_NAME,
                         imageNameOverride: SERVICE_NAME,
                         ecrRegistryAccountId: ECR_ACCOUNT_ID, awsDeploymentTargetAccountId: QA_ACCOUNT_ID, awsDeploymentRoleName: QA_DEPLOYMENT_ROLE
                    }
                }
            }
        }

        stage('Integration Tests') {
            when {
                branch 'master'
            }
            steps {
                withEcr {
                    withAWS(role: QA_INTEGRATION_TEST_ROLE, roleAccount: QA_ACCOUNT_ID, roleSessionName: QA_INTEGRATION_TEST_SESSION, useNode: true) {
                        sh 'make ci_test_integration'
                    }
                }
            }
            post {
                cleanup {
                    sh 'make ci_test_integration_clean'
                }
            }
        }
        // stage('E2E Tests') {
        //     when {
        //         branch 'master'
        //     }
        //     environment {
        //         TAGS = "@"
        //     }
        //     steps {
        //         e2eTests tags: env.TAGS
        //     }
        //     post {
        //         regression {
        //             slackNotify channel: '#cypress-test-e2e-results'
        //         }
        //         fixed {
        //             slackNotify channel: '#cypress-test-e2e-results'
        //         }
        //     }
        // }
        stage('Scan Docker Image') {
            when {
                anyOf {
                    branch 'master'
                    expression { env.GITHUB_COMMENT =~ 'build docker' }
                }
            }
            steps {
                dockerScan awsRegion: AWS_REGIONS[0], ecrAccountId: ECR_ACCOUNT_ID, imageName: SERVICE_NAME, imageTag: env.GIT_COMMIT,
                vulnerabilitiesToIgnore: VULNERABILITIES_TO_IGNORE
            }
        }
        stage('Deploy to UAT') {
            when {
                allOf {
                    branch 'master'
                    expression { params.DEPLOY_TO_UAT }
                }
            }
            steps {
                fargateDeploy environment: 'uat', awsRegions: AWS_REGIONS, gitCommit: env.GIT_COMMIT, serviceName: SERVICE_NAME,
                    ecrRegistryAccountId: ECR_ACCOUNT_ID, awsDeploymentTargetAccountId: UAT_ACCOUNT_ID, awsDeploymentRoleName: UAT_DEPLOYMENT_ROLE
            }
        }
        stage('Deploy to Prod') {
            parallel {
                stage('Deploy Public Router to Prod') {
                    when {
                        allOf {
                            branch 'master'
                            expression { params.DEPLOY_TO_PROD }
                        }
                    }
                    steps {
                        fargateDeploy environment: 'prod', awsRegions: AWS_REGIONS, gitCommit: env.GIT_COMMIT, serviceName: SERVICE_NAME,
                            ecrRegistryAccountId: ECR_ACCOUNT_ID, awsDeploymentTargetAccountId: PROD_ACCOUNT_ID, awsDeploymentRoleName: PROD_DEPLOYMENT_ROLE
                        datadogSoftwareCatalogPublish()
                    }
                }
                stage('Deploy Internal Router to Prod') {
                    when {
                        allOf {
                            branch 'master'
                            expression { params.DEPLOY_TO_PROD }
                        }
                    }
                    steps {
                        fargateDeploy environment: 'prod', awsRegions: AWS_REGIONS, gitCommit: env.GIT_COMMIT, serviceName: INTERNAL_SERVICE_NAME,
                            imageNameOverride: SERVICE_NAME,
                            ecrRegistryAccountId: ECR_ACCOUNT_ID, awsDeploymentTargetAccountId: PROD_ACCOUNT_ID, awsDeploymentRoleName: PROD_DEPLOYMENT_ROLE
                    }
                }
            }
        }
    }

    post {
        always {
            script {
                if (env.BRANCH_NAME == 'master') {
                    slackNotify channel: SLACK_NOTIFICATIONS_CHANNEL

                }
            }
        }
        cleanup {
            cleanWs()
        }
    }
}
