String GITHUB_REPOSITORY = 'lambda-product-staging'
String ECR_ACCOUNT_ID = '086679231553'
List<String> AWS_REGIONS = ['us-east-1']
String SLACK_NOTIFICATIONS_CHANNEL = '#content-creation-mgmt-alerts'
String QA_ACCOUNT_ID = '437795906767'
String QA_DEPLOYMENT_ROLE = 'prod-jenkins-aws-pipeline-agent'
String PROD_ACCOUNT_ID = '437795906767'
String PROD_DEPLOYMENT_ROLE = 'prod-jenkins-aws-pipeline-agent'
String SFN_INTEGRATION_TEST_ROLE = 'qa-lambda-product-staging-integration-tests-role'
String SFN_INTEGRATION_TEST_SESSION_NAME = 'jenkins-sfn-integration-test'

List<String> VULNERABILITIES_TO_IGNORE = [
    'CVE-2026-34993', // pkg:pypi/aiohttp@3.13.5 requires fix in upstream
    'CVE-2026-47265'  // pkg:pypi/aiohttp@3.13.5 requires fix in upstream
]

PROJECT_CONFIG = [
    'fargate/dropbox_asset_transfer': [
        imageName: 'dropbox-asset-transfer'
    ],
    'fargate/export_catalog_task': [
        imageName: 'export-catalog-task'
    ],
    'fargate/export_spotify_catalog': [
        imageName: 'export-spotify-catalog'
    ],
    'fargate/google_drive_asset_transfer': [
        imageName: 'google-drive-asset-transfer'
    ],
    'fargate/validate_spreadsheet': [
        imageName: 'validate-spreadsheet'
    ]
]


pipeline {
    agent any

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

    parameters {
        booleanParam(name: 'DEPLOY_TO_PROD', defaultValue: true, description: 'Whether or not to deploy to prod.')
        string(name: 'LAMBDA_FUNCTION_NAMES', defaultValue: '', description: 'Comma-separated list of Lambda function directory names to build and deploy')
        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 {
                withModifiedProjects(checkout: true) { project, config ->
                    echo "Running Compliance Checks for ${project}"
                    dir(project) {
                        complianceChecks()
                    }
                }
            }
        }

        stage('Validate Software Catalog Definition') {
            steps {
                withModifiedProjects(checkout: true) { project, config ->
                    echo "Validating Software Catalog Definition for ${project}"
                    datadogSoftwareCatalogValidate(servicePath: project)
                }
            }
        }

        stage('Unit Tests and Style Checks') {
            steps {
                dir('common') {
                    withEcr {
                        script {
                            sh "docker compose run --rm --build lint-and-test"
                        }
                    }
                }
                withModifiedProjects(checkout: true) { project, config ->
                    echo "Running Unit Tests and Style Checks for ${project}"
                    dir(project) {
                        withEcr {
                            script {
                                sh "docker compose run --rm --build lint-and-test"
                            }
                        }
                    }
                }
            }
        }

        stage('Static Application Security Tests') {
            steps {
                withModifiedProjects(checkout: true) { project, config ->
                    echo "Running Static App Security Tests for ${project}"
                    sastTests(projectDir: project)
                }
            }
        }

        stage('Sonar Scan and Analysis') {
            agent any
            when {
                branch 'master'
            }
            steps {
                script {
                    echo "Running Sonar scan for ${GITHUB_REPOSITORY}"
                    sonarScan project: GITHUB_REPOSITORY, language: 'py'
                }
            }
        }

        stage('Create a Release') {
            when {
                anyOf {
                    branch 'master'
                    expression { env.GITHUB_COMMENT =~ 'build docker' }
                }
            }
            steps {
                withModifiedProjects(checkout: true) { project, config ->
                    echo "Building for ${project}"
                    dockerToEcr awsRegions: AWS_REGIONS,
                        ecrAccountId: ECR_ACCOUNT_ID,
                        imageName: config?.imageName ?: "lambda-product-staging-${(project.tokenize('/')[-1]).replaceAll('_', '-')}",
                        imageTag: env.GIT_COMMIT,
                        dockerBuildContext: "../..",
                        dockerBuildFile: "${project}/Dockerfile"
                }
            }
        }

        stage('Deploy to QA') {
            when {
                branch 'master'
            }
            steps {
                script {
                    withModifiedProjects(checkout: true) { project, config ->
                        echo "Deploying to QA for ${project}"
                        dir(project) {
                            if (project.startsWith('lambda/')) {
                                lambdaDeploy environment: 'qa',
                                    awsRegions: AWS_REGIONS,
                                    imageTag: env.GIT_COMMIT,
                                    imageName: config?.imageName ?: "lambda-product-staging-${(project.tokenize('/')[-1]).replaceAll('_', '-')}",
                                    ecrRegistryAccountId: ECR_ACCOUNT_ID,
                                    awsDeploymentTargetAccountId: QA_ACCOUNT_ID,
                                    awsDeploymentRoleName: QA_DEPLOYMENT_ROLE
                            } else if (project.startsWith('fargate/')) {
                                fargateDeploy environment: 'qa',
                                    awsRegions: AWS_REGIONS,
                                    deployType: 'CREATE_TASK_DEFINITION',
                                    gitCommit: env.GIT_COMMIT,
                                    serviceName: config?.imageName,
                                    ecrRegistryAccountId: ECR_ACCOUNT_ID,
                                    awsDeploymentTargetAccountId: QA_ACCOUNT_ID,
                                    awsDeploymentRoleName: QA_DEPLOYMENT_ROLE
                            }
                        }
                    }
                }
            }
        }

        stage('Scan Docker Images') {
            when {
                anyOf {
                    branch 'master'
                    expression { env.GITHUB_COMMENT =~ 'build docker' }
                }
            }
            steps {
                withModifiedProjects { project, config ->
                    dir(project) {
                        dockerScan awsRegion: AWS_REGIONS[0],
                            ecrAccountId: ECR_ACCOUNT_ID,
                            imageName: config?.imageName ?: "lambda-product-staging-${(project.tokenize('/')[-1]).replaceAll('_', '-')}",
                            imageTag: env.GIT_COMMIT,
                            vulnerabilitiesToIgnore: VULNERABILITIES_TO_IGNORE
                    }
                }
            }
        }

        stage('Lambda Integration Tests') {
            agent {
                label 'aws'
            }
            when {
                branch 'master'
            }
            steps {
                script {
                    withModifiedProjects(checkout: true) { project, config ->
                        if (project.startsWith('lambda/')) {
                            dir(project) {
                                withEcr {
                                    withAWS(role: SFN_INTEGRATION_TEST_ROLE, roleAccount: QA_ACCOUNT_ID, roleSessionName: SFN_INTEGRATION_TEST_SESSION_NAME, useNode: true) {
                                        sh """
                                            if [ -d tests/integration ]; then
                                                echo 'Running integration tests for ${project}'
                                                docker compose run --rm --build integration-test
                                            fi
                                        """
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        stage('SFN Integration Tests') {
            agent {
                label 'aws'
            }
            when {
                branch 'master'
            }
            steps {
                withEcr {
                    withAWS(role: SFN_INTEGRATION_TEST_ROLE, roleAccount: QA_ACCOUNT_ID, roleSessionName: SFN_INTEGRATION_TEST_SESSION_NAME, useNode: true) {
                        sh 'make ci_test_sfn_integration'
                    }
                }
            }
            post {
                cleanup {
                    sh 'make ci_test_sfn_integration_clean'
                }
            }
        }

        stage('E2E Playwright Tests') {
            when {
                branch 'master'
            }
            steps {
                playwrightTests (tags: '@bulk_uploader', slackNotificationChannels: [SLACK_NOTIFICATIONS_CHANNEL])
            }
        }

        stage('Deploy to PROD') {
            when {
                allOf {
                    branch 'master'
                    expression { params.DEPLOY_TO_PROD }
                }
            }
            steps {
                script {
                    withModifiedProjects(checkout: true) { project, config ->
                        echo "Deploying to PROD for ${project}"
                        dir(project) {
                            if (project.startsWith('lambda/')) {
                                lambdaDeploy environment: 'prod',
                                    awsRegions: AWS_REGIONS,
                                    imageTag: env.GIT_COMMIT,
                                    imageName: config?.imageName ?: "lambda-product-staging-${(project.tokenize('/')[-1]).replaceAll('_', '-')}",
                                    ecrRegistryAccountId: ECR_ACCOUNT_ID,
                                    awsDeploymentTargetAccountId: PROD_ACCOUNT_ID,
                                    awsDeploymentRoleName: PROD_DEPLOYMENT_ROLE
                            } else if (project.startsWith('fargate/')) {
                                fargateDeploy environment: 'prod',
                                    awsRegions: AWS_REGIONS,
                                    deployType: 'CREATE_TASK_DEFINITION',
                                    gitCommit: env.GIT_COMMIT,
                                    serviceName: config?.imageName,
                                    ecrRegistryAccountId: ECR_ACCOUNT_ID,
                                    awsDeploymentTargetAccountId: PROD_ACCOUNT_ID,
                                    awsDeploymentRoleName: PROD_DEPLOYMENT_ROLE
                            }
                        }
                        datadogSoftwareCatalogPublish(servicePath: project)
                    }
                }
            }
        }
    }

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


def withModifiedProjects(Map args = [:], Closure steps) {
    getMonorepoUtils().withModifiedProjects(args, steps)
}

def getMonorepoUtils() {
    return library("jenkins-global-libraries@${params.SHARED_LIBRARIES_VERSION}").com.sonymusic.MonorepoUtils.getInstance(
        steps: this,
        projectBasePath: 'lambda',
        projectsToBuild: params.LAMBDA_FUNCTION_NAMES ? params.LAMBDA_FUNCTION_NAMES.split(',') : null,
        projectConfig: PROJECT_CONFIG,
        useRelativePaths: false,
        projectDependencies: [
            'common': [/^lambda\/.*$/, /^fargate\/.*$/]
        ]
    )
}
