
pipeline {
    agent any
    environment {
        CI_PROJECT_DIR = "${WORKSPACE}/analytics-soa"
        PATH = "${CI_PROJECT_DIR}/node_modules/.bin:$PATH"
    }
    stages {
        stage('Check Node Version') {
            steps {
                sh 'node -v'
                sh 'npm -v'
            }
        }
        stage('Install Dependencies') {
            steps {
                dir("${CI_PROJECT_DIR}") {
                    sh 'npm install --force'
                }
            }
        }
        stage('Build Backend') {
            steps {
                dir("${CI_PROJECT_DIR}") {
                    script {
                        sh '''
                            export PATH="$CI_PROJECT_DIR/node_modules/.bin:$PATH"
                            nx run services-hospitality-analytics:build --skip-nx-cache
                        '''
                    }
                }
            }
        }
        stage('Build Frontend') {
            steps {
                dir("${CI_PROJECT_DIR}") {
                    script {
                        sh '''
                            export PATH="$CI_PROJECT_DIR/node_modules/.bin:$PATH"
                            nx run ui:build --skip-nx-cache
                        '''
                    }
                }
            }
        }
        /*
        stage('Transfer Build Files') {
            steps {
                sshagent(credentials: ['2']) { // Use the correct credentials ID here
                    sh '''
                        ssh root@206.189.138.212 "rm -rf /var/www/html/analytics_ui/*"
                        ssh root@206.189.138.212 "rm -rf /var/www/html/analytics/analytics-soa/dist/packages/services"
                        scp -r $CI_PROJECT_DIR/dist/packages/ui/* root@206.189.138.212:/var/www/html/analytics_ui
                        scp -r $CI_PROJECT_DIR/dist/packages/services root@206.189.138.212:/var/www/html/analytics/analytics-soa/dist/packages
                    '''
                }
            }
        }
        stage('Start Services with PM2') {
            steps {
                sshagent(credentials: ['2']) { // Use the correct credentials ID here
                    sh '''
                        ssh root@206.189.138.212 <<EOF
pm2 start /var/www/html/bom/gtstoinfor/dist/packages/services/common/main.js --name "analytics-hospitality"
pm2 status
pm2 save
EOF
                    '''
                }
            }
        }
        stage('Restart Services with PM2') {
            steps {
                sshagent(credentials: ['2']) { // Use the correct credentials ID here
                    sh '''
                        ssh root@206.189.138.212 <<EOF
pm2 restart analytics-hospitality
pm2 status
pm2 save
EOF
                    '''
                }
            }
        }
        */
    }
    post {
        success {
            script {
                def startTime = currentBuild.getStartTimeInMillis()
                def endTime = System.currentTimeMillis()
                def duration = (endTime - startTime) / 1000
                // Send success email
                emailext(
                    to: 'saikumarvardhi7799@gmail.com, rajud@schemaxtech.com, arunkumars@schemaxtech.com',
                    subject: "SUCCESS: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'",
                    body: """Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' was successful.
Start Time: ${new Date(startTime)}
End Time: ${new Date(endTime)}
Duration: ${duration} seconds
Check console output at ${env.BUILD_URL} to view the results."""
                )
            }
        }
        failure {
            script {
                def logFile = "${WORKSPACE}/log"
                def logContent = ""
                if (fileExists(logFile)) {
                    logContent = readFile(logFile).take(1000)
                } else {
                    logContent = "Log file not found."
                }
                // Send failure email
                emailext(
                    to: 'saikumarvardhi7799@gmail.com, rajud@schemaxtech.com, arunkumars@schemaxtech.com',
                    subject: "FAILURE: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'",
                    body: """Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' failed.
Error Log:
${logContent}
Check console output at ${env.BUILD_URL} to view the results."""
                )
            }
        }
    }
}
