pipeline {
    agent any

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

    environment {
        BUILD_DIR = 'dist'
        SENTRY_AUTH_TOKEN = credentials('2b685078-e5d8-49a7-af60-b8ec44bc2734')
    }

    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 {
                        def props = readJSON file: 'dist/.vite/manifest.json'
                        def jsFile = props["src/main.tsx"]["file"]

                        dir(env.BUILD_DIR) {
                            def minSize = 750 * 1024
                            def size = sh(script: "stat -c%s ${jsFile}", 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') {
            steps {
                sh "/usr/bin/rsync -avz ${WORKSPACE}/jb-ui/${env.BUILD_DIR}/ cdn-jb:/var/www/amt-jb-cdn/"

                // This already gets moved over in a hidden folder, but let's 
                // make it more clear because dev builds would overwrite it
                sh "/usr/bin/rsync -avz ${WORKSPACE}/jb-ui/${env.BUILD_DIR}/.vite/manifest.json cdn-jb:/var/www/amt-jb-cdn/manifest-${env.BRANCH_NAME}.json"
            }
        }

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

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

                // This already gets moved over in a hidden folder, but let's 
                // make it more clear because dev builds would overwrite it
                sh "/usr/bin/rsync -avz ${WORKSPACE}/jb-ui/${env.BUILD_DIR}/.vite/manifest.json cdn-jb:/var/www/amt-jb-cdn/manifest-${env.BRANCH_NAME}-${env.TAG_NAME}.json"
            }
        }
    }
}
