pipeline {
    agent any
    stages {
        stage('Build backend') {
            environment {
                PATH = "/var/lib/jenkins/.cargo/bin:${env.PATH}"
            }
            steps {
                dir("backend") {
                    sh "touch .env"
                    sh "echo DATABASE_URL=mysql://educa7ls_user:123456789a*@md-89.webhostbox.net:3306/educa7ls_plataforma >> .env"
                    sh "echo RENIEC_API=apis-token-1.aTSI1U7KEuT-6bbbCguH-4Y8TI6KS73N >> .env"
                    sh "cargo build --release"
                }
            }
        }
        stage('Build frontend') {
            environment {
                PATH = "/var/lib/jenkins/.nvm/versions/node/v18.18.0/bin:${env.PATH}"
            }
            steps {
                dir("frontend") {
                    sh "echo VITE_BACKEND_URL=https://system.eegsac.com >> .env.production"
                    sh "pnpm i"
                    sh "pnpm build"
                }
            }
        }
        stage("Create ENV file") {
            steps {
                sh "touch .env"
                sh "echo DATABASE_URL=mysql://educa7ls_user:123456789a*@md-89.webhostbox.net:3306/educa7ls_plataforma >> .env"
                sh "echo RENIEC_API=apis-token-1.aTSI1U7KEuT-6bbbCguH-4Y8TI6KS73N >> .env"
                sh "echo CLASSROOM_URL=https://aulavirtual.eegsac.com  >> .env"
                sh "echo CLASSROOM_USER=admin >> .env"
                sh '''echo CLASSROOM_PASSWORD=YVL1@N4_PaL0-93\\$ >> .env'''
                sh "echo RUST_LOG=info >> .env"
            }
        }
        stage("Stop old PM2 processes") {
            environment {
                PATH = "/var/lib/jenkins/.local/share/pnpm:${env.PATH}"
            }
            steps {
                sh "pm2 stop ${env.JOB_BASE_NAME} || true"
                sh "pm2 delete ${env.JOB_BASE_NAME} || true"
            }
        }
        stage("Clean deploy folder") {
            steps {
                sh "rm -rf /var/deploys/${env.JOB_BASE_NAME}/* || true"
            }
        }
        stage("Copy deploy files") {
            steps {
                sh "mkdir -p /var/deploys/${env.JOB_BASE_NAME}/static"
                // Copy backend binary
                sh "cp ${env.WORKSPACE}/backend/target/release/backend /var/deploys/${env.JOB_BASE_NAME}/"
                // Copy frontend static files
                sh "cp -r ${env.WORKSPACE}/frontend/dist/* /var/deploys/${env.JOB_BASE_NAME}/static/"
                // Copy ENV file for RUST
                sh "cp ${env.WORKSPACE}/.env /var/deploys/${env.JOB_BASE_NAME}/"
                // Make log dir
                sh "mkdir -p /var/deploys/${env.JOB_BASE_NAME}/request-logs/"
            }
        }
        stage('Start app with PM2') {
            environment {
                PATH = "/var/lib/jenkins/.local/share/pnpm:/var/lib/jenkins/.nvm/versions/node/v18.18.0/bin:${env.PATH}"
            }
            steps {
                dir("/var/deploys/${env.JOB_BASE_NAME}/") {
                    sh "ROCKET_PORT=3333 pm2 start backend --name ${env.JOB_BASE_NAME}"
                    sh "pm2 save"
                }
            }
        }
    }
}
