pipeline {
    agent any
    tools {
        nodejs '22.12.0'
    }

    stages {
        stage('Deploy Locally') {
            steps {
                dir ("${env.API_DIRECTORY_STAGE}") {
                    sh 'git reset --hard'
                    sh 'git clean -fd'
                    sh 'sudo git pull https://ketanmodha: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@github.com/ketanmodha/rrb-exam.git staging'
                    sh 'npm install'
                    sh 'npx sequelize-cli db:migrate --env staging'
                    sh 'pm2 status'
                    sh 'pm2 restart auth-microservice-stage'
                }
            }
        }
    }

    post {
        success {
            script {
                def startTime = new Date(currentBuild.startTimeInMillis).format("yyyy-MM-dd HH:mm:ss")
                def endTime = new Date(currentBuild.startTimeInMillis + currentBuild.duration).format("yyyy-MM-dd HH:mm:ss")

                emailext(
                    to: "${env.EMAIL_STAGE}",
                    subject: "🚀 RRB HUB | Build Successful: ${env.JOB_NAME} #${env.BUILD_NUMBER}",
                    body: """<p>Dear Team,</p>
                        <p>I’m pleased to inform you that the <strong>RRB Hub API</strong> pipeline has successfully completed its run.</p>
                        <p><strong>Pipeline Details:</strong></p>
                        <ul>
                          <li><strong>Pipeline Name:</strong> ${env.JOB_NAME}</li>
                          <li><strong>Branch:</strong> staging</li>
                          <li><strong>Build Number:</strong> ${env.BUILD_NUMBER}</li>
                          <li><strong>Start Time:</strong> ${startTime}</li>
                          <li><strong>End Time:</strong> ${endTime}</li>
                        </ul>
                        <p>The build is now live and ready for testing on the staging environment. Please review the deployment and confirm if everything is functioning as expected.</p>
                        <p>Check the console output for detailed logs: <a href="${env.BUILD_URL}">View Logs</a></p>
                        <p>Thank you for your continued collaboration!</p>
                        <p>Best Regards,<br/>RRB Hub Automated Notifications<br/></p>""",
                    mimeType: 'text/html'
                )
            }
        }
        failure {
            script {
                def startTime = new Date(currentBuild.startTimeInMillis).format("yyyy-MM-dd HH:mm:ss")
                def endTime = new Date(currentBuild.startTimeInMillis + currentBuild.duration).format("yyyy-MM-dd HH:mm:ss")

                emailext(
                    to: "${env.EMAIL_STAGE}",
                    subject: "❌ RRB HUB | Build Failed: ${env.JOB_NAME} #${env.BUILD_NUMBER}",
                    body: """<p>Dear Team,</p>
                        <p>The pipeline <strong>${env.JOB_NAME}</strong> has failed.</p>
                        <ul>
                            <li><strong>Build Number:</strong> ${env.BUILD_NUMBER}</li>
                            <li><strong>Start Time:</strong> ${startTime}</li>
                            <li><strong>Build URL:</strong> <a href="${env.BUILD_URL}">${env.BUILD_URL}</a></li>
                        </ul>
                        <p>Check the console output for more details: <a href="${env.BUILD_URL}console">${env.BUILD_URL}console</a></p>""",
                    mimeType: 'text/html'
                )
            }
        }
        unstable {
            emailext(
                to: "${env.EMAIL_STAGE}",
                subject: "Build ${currentBuild.fullDisplayName} - UNSTABLE",
                body: """<p>Build ${currentBuild.fullDisplayName} is unstable.</p>
                        <p>Check console output at ${env.BUILD_URL} to view the results.</p>""",
                mimeType: 'text/html'
            )
        }
    }
}
