pipeline {
    agent any

    triggers {
        cron('H */6 * * *')
        pollSCM('H/1 * * * *')
    }

    environment {
        BUILD_DIR = 'dist'
        JS_FILENAME = 'james-billings'
        CSS_FILENAME = 'james-has-style'
    }

    options {
        skipDefaultCheckout()
    }

    stages {
        stage('setup') {
            steps {
                cleanWs()
                checkout scmGit(
                    branches: [[name: env.BRANCH_NAME]],
                    extensions: [ cloneOption(shallow: true) ],
                    userRemoteConfigs: [
                        [credentialsId:  'abdeb570-b708-44f3-b857-8a6b06ed9822',
                         url: 'ssh://code.g-r-l.com:6611/panels/amt-jb']
                    ],
                )
            }
        }

        stage('env') {
            steps {
                withCredentials([file(
                    credentialsId: 'amt-jb-ui-env',
                    variable: 'ENV_FILE_PATH')]) {
                    sh "cp \$ENV_FILE_PATH ${WORKSPACE}/jb-ui/.env.production"
                    }
            }
        }

        stage('pnpm.install') {
            steps {
                dir('jb-ui') {
                    sh 'pnpm install'
                }
            }
        }

        stage('pnpm.test') {
            steps {
                dir('jb-ui') {
                    sh 'pnpm run test'
                }
            }
        }

        stage('pnpm.build') {
            steps {
                dir('jb-ui') {
                    sh 'pnpm run build'
                    script {
                        dir(env.BUILD_DIR) {
                            def minSize = 750 * 1024
                            def size = sh(script: "stat -c%s ${env.JS_FILENAME}.js", returnStdout: true).trim().toInteger()

                            if (size < minSize) {
                                error "Build is too small: ${size} bytes"
                            } else {
                                echo "Build size is acceptable: ${size} bytes"
                            }
                        }
                    }
                }
            }
        }

        stage('cdn.deploy.master') {
            when {
                expression { env.BRANCH_NAME == 'master' }
            }
            steps {
                sh "/usr/bin/rsync -avz ${WORKSPACE}/jb-ui/${env.BUILD_DIR}/ thl-cdn:/root/amt-jb-cdn/"
            }
        }

        stage('cdn.deploy.non-master') {
            when {
                expression { env.BRANCH_NAME != 'master' }
            }
            steps {
                dir('jb-ui') {
                    script {
                        echo "Detected branch: ${env.BRANCH_NAME}"
                        def versioned_js = "${env.JS_FILENAME}-${env.BRANCH_NAME}"
                        def versioned_css = "${env.CSS_FILENAME}-${env.BRANCH_NAME}"

                        dir(env.BUILD_DIR) {
                            sh "cp ${env.JS_FILENAME}.js ${versioned_js}.js"
                            sh "cp ${env.CSS_FILENAME}.js ${versioned_css}.js"

                            sh "/usr/bin/rsync -avz ${WORKSPACE}/jb-ui/${env.BUILD_DIR}/ thl-cdn:/root/amt-jb-cdn/"
                        }
                    }
                }
            }
        }

        stage('cdn.deploy.tag') {
            when {
                expression {
                    return env.TAG_NAME?.trim()
                }
            }

            steps {
                dir('jb-ui') {
                    script {
                        echo "Detected tag: ${env.TAG_NAME}"
                        def versioned_js = "${env.JS_FILENAME}-${env.TAG_NAME}"
                        def versioned_css = "${env.CSS_FILENAME}-${env.TAG_NAME}"

                        dir(env.BUILD_DIR) {
                            sh "cp ${env.JS_FILENAME}.js ${versioned}.js"
                            sh "cp ${env.CSS_FILENAME}.js ${versioned}.js"

                            sh "/usr/bin/rsync -avz ${WORKSPACE}/jb-ui/${env.BUILD_DIR}/ thl-cdn:/root/amt-jb-cdn/"
                        }
                    }
                }
            }
        }
    }
}
