#!/usr/bin/env groovy

def PROTECTED_BRANCH

def BRANCH_ENV = [
    test: 'test',
    master: 'production',
    beta: 'production'
]

node {
    stage('Setup') {
        env.FRONT_REPLICAS = 1
        env.NODE_ENV = BRANCH_ENV[env.BRANCH_NAME] ?: 'dev'

        PROTECTED_BRANCH = [
            'dev',
            'test',
            'master',
            'beta'
        ].contains(env.BRANCH_NAME)

        // https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#using-environment-variables
        echo "NODE_NAME: ${env.NODE_NAME}"
        echo "WORKSPACE: ${env.WORKSPACE}"

        configFileProvider([
            configFile(fileId: 'salix-front.properties',
            variable: 'PROPS_FILE')
        ]) {
            def props = readProperties file: PROPS_FILE
            props.each {key, value -> env."${key}" = value }
            props.each {key, value -> echo "${key}: ${value}" }
        }

        if (PROTECTED_BRANCH) {
            configFileProvider([
                configFile(fileId: "salix-front.branch.${env.BRANCH_NAME}",
                variable: 'BRANCH_PROPS_FILE')
            ]) {
                def props = readProperties file: BRANCH_PROPS_FILE
                props.each {key, value -> env."${key}" = value }
                props.each {key, value -> echo "${key}: ${value}" }
            }
        }
    }
}

pipeline {
    agent any
    options {
        disableConcurrentBuilds()
    }
    tools {
        nodejs 'node-v18'
    }
    environment {
        PROJECT_NAME = 'lilium'
    }
    stages {
        stage('Install') {
            environment {
                NODE_ENV = ""
            }
            steps {
                sh 'pnpm install --prefer-offline'
            }
        }
        stage('Test: Unit') {
            when {
                expression { !PROTECTED_BRANCH }
            }
            environment {
                NODE_ENV = ""
            }
            steps {
                sh 'pnpm run test:unit:ci'
            }
            post {
                always {
                    junit(
                        testResults: 'junitresults.xml',
                        allowEmptyResults: true
                    )
                }
            }
        }
        stage('Test: E2E') {
            when {
                expression { !PROTECTED_BRANCH }
            }
            environment {
                NODE_ENV = ""
                CREDENTIALS = credentials('docker-registry')
            }
            script {
                def packageJson = readJSON file: 'package.json'
                env.VERSION = "${packageJson.version}-build${env.BUILD_ID}"
                cleanDockerE2E()
                sh 'rm -rf salix'
                sh 'git clone https://gitea.verdnatura.es/verdnatura/salix.git'
            }
            stages {
                stage('Up') {
                    parallel{
                        stage('Database') {
                            steps {
                                sh 'cd salix && pnpm i --prefer-offline @verdnatura/myt && npx myt run -t -d'
                            }
                        }
                        stage('Backend') {
                            steps {
                                sh 'docker build -f ./salix/back/Dockerfile -t back ./salix'
                                sh 'docker run -d --name salix_e2e --net=host -v $(pwd)/test/cypress/storage:/salix/storage back'
                            }
                        }
                        stage('Frontend') {
                            steps {
                                sh 'docker-compose -f docker-compose.e2e.yml up -d --build front'
                            }
                        }
                    }
                }
                stage('Run E2E') {
                    steps {
                        script {
                            sh 'docker-compose -f docker-compose.e2e.yml up e2e'
                            def result = sh(script: 'docker-compose -f docker-compose.e2e.yml ps -q e2e | xargs docker inspect -f "{{.State.ExitCode}}"', returnStatus: true)
                            sh 'docker cp $(docker-compose -f docker-compose.e2e.yml ps -q e2e):/app/test/cypress/reports ./test/cypress/'
                            if (result != 0) {
                                error("Cypress E2E tests failed with exit code: ${result}")
                            }
                        }
                    }
                }
            }
            post {
                always {
                    publishHTML([
                        allowMissing: true,
                        alwaysLinkToLastBuild: true,
                        keepAll: true,
                        reportDir: 'test/cypress/reports',
                        reportFiles: '*.html',
                        reportName: 'Cypress Mochawesome Report'
                    ])
                    cleanDockerE2E()
                }
            }
        }
        stage('Build') {
            when {
                expression { PROTECTED_BRANCH }
            }
            environment {
                CREDENTIALS = credentials('docker-registry')
            }
            steps {
                sh 'quasar build'
                script {
                    def packageJson = readJSON file: 'package.json'
                    env.VERSION = "${packageJson.version}-build${env.BUILD_ID}"
                }
                dockerBuild()
            }
        }
        stage('Deploy') {
            when {
                expression { PROTECTED_BRANCH }
            }
            steps {
                script {
                    def packageJson = readJSON file: 'package.json'
                    env.VERSION = "${packageJson.version}-build${env.BUILD_ID}"
                }
                withKubeConfig([
                    serverUrl: "$KUBERNETES_API",
                    credentialsId: 'kubernetes',
                    namespace: 'lilium'
                ]) {
                    sh 'kubectl set image deployment/lilium-$BRANCH_NAME lilium-$BRANCH_NAME=$REGISTRY/salix-frontend:$VERSION'
                }
            }
        }
    }
}

def cleanDockerE2E() {
    script {
        sh 'docker rm -f vn-database || true'
        sh 'docker rm -f salix_e2e || true'
        sh 'docker-compose -f docker-compose.e2e.yml down || true'
    }
}