pipeline {
    agent any

    options {
        ansiColor('xterm')
        disableConcurrentBuilds()
        timestamps()
        timeout(time: 20, unit: 'MINUTES')
        buildDiscarder(logRotator(numToKeepStr: '60'))
    }

    parameters {
        choice(name: 'TAGS', choices: [
            '@insights_regression',
            '@insights_account',
            '@insights_song',
            '@insights_artist',
            '@insights_charts',
            '@insights_search',
            '@insights_trending',
            '@insights_market_selector',
            '@insights_my_catalog',
            '@insights_product',
            '@insights_brands',
            '@insights_channel',
            '@insights_video',
            '@insights_sticky_header',
            '@insights_localization',
            '@insights_store_status_popup'
        ], description: 'Playwright tag filter')
    }

    triggers {
        parameterizedCron('''
            H 0 * * 0 %TAGS=@insights_account
            H 1 * * 0 %TAGS=@insights_song
            H 2 * * 0 %TAGS=@insights_artist
            H 3 * * 0 %TAGS=@insights_charts
            H 4 * * 0 %TAGS=@insights_search
            H 5 * * 0 %TAGS=@insights_trending
            H 6 * * 0 %TAGS=@insights_market_selector
            H 7 * * 0 %TAGS=@insights_my_catalog
            H 8 * * 0 %TAGS=@insights_product
            H 9 * * 0 %TAGS=@insights_brands
            H 10 * * 0 %TAGS=@insights_channel
            H 11 * * 0 %TAGS=@insights_video
            H 12 * * 0 %TAGS=@insights_sticky_header
            H 13 * * 0 %TAGS=@insights_localization
            H 14 * * 0 %TAGS=@insights_store_status_popup
        ''')
    }

    stages {
        stage('Set Build Name') {
            steps {
                script {
                    currentBuild.displayName = "#${env.BUILD_NUMBER} - ${params.TAGS}"
                }
            }
        }

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

        stage('Run Playwright E2E Insights Regression Tests') {
            steps {
                script {
                    playwrightTests(
                        tags: params.TAGS,
                        envVars: [USE_DYNAMIC_THROTTLING: 'true', MAX_CONCURRENT_FEATURES: '20'],
                        slackNotificationChannels: ['#insights-regression-reports']
                    )
                }
            }
        }
    }
}
