pipeline {
    agent any

    environment {
        DOCKER_IMAGE = 'jassimsm/documentation'
        DOCKER_TAG = 'latest'
        DOCKER_USERNAME="jassimsm"
        DOCKER_PASSWORD="dckr_pat_c9HutePXaqreGfKi48H0WPWWfBs"
        SSH_CREDENTIALS = 'documentation'
        REMOTE_SERVER = 'ubuntu@160.187.166.47'
        REMOTE_WORKSPACE = '/home/ubuntu'
    }

    stages {
        stage('Add Host Key') {
            steps {
                script {
                    sshagent(credentials: [SSH_CREDENTIALS]) {
                        sh '''
                        mkdir -p ~/.ssh
                        ssh-keyscan -H 160.187.166.47 >> ~/.ssh/known_hosts
                        '''
                    }
                }
            }   
        }

        stage('Checkout and Install Dependencies on Remote Server') {
            steps {
                script {
                    sshagent(credentials: [SSH_CREDENTIALS]) {
                        sh '''
                        ssh ${REMOTE_SERVER} "mkdir -p ${REMOTE_WORKSPACE} && cd ${REMOTE_WORKSPACE} && rm -rf cicid && git clone https://git.tech4biz.wiki/jassim/cicid.git && cd cicid && git checkout main && yarn install"
                        '''
                    }
                }
            }
        }

        stage('Build Docker Image on Remote Server') {
            steps {
                script {
                    try {
                        sshagent(credentials: [SSH_CREDENTIALS]) {
                            sh '''
                            ssh ${REMOTE_SERVER} "cd ${REMOTE_WORKSPACE}/cicid && docker build -t ${DOCKER_IMAGE}:${DOCKER_TAG} ."
                            '''
                        }
                    } catch (Exception e) {
                        error "Failed to build Docker image on remote server: ${e.message}"
                    }
                }
            }
        }

        stage('Push Docker Image from Remote Server') {
            steps {
                script {
                    try {
                        sshagent(credentials: [SSH_CREDENTIALS]) {
                            sh '''
                            ssh ${REMOTE_SERVER} "echo ${DOCKER_PASSWORD} | docker login -u ${DOCKER_USERNAME} --password-stdin && docker push ${DOCKER_IMAGE}:${DOCKER_TAG}"
                            '''
                        }
                    } catch (Exception e) {
                        error "Failed to push Docker image from remote server: ${e.message}"
                    }
                }
            }
        }

        stage('Deploy') {
            steps {
                script {
                    sshagent(credentials: [SSH_CREDENTIALS]) {
                        sh '''
                        ssh ${REMOTE_SERVER} "cd ${REMOTE_WORKSPACE}/cicid && \
                        docker pull ${DOCKER_IMAGE}:${DOCKER_TAG} && \
                        docker stop documentation-container || true && \
                        docker rm documentation-container || true && \
                        docker run -d -p 3000:3000 --name documentation-container ${DOCKER_IMAGE}:${DOCKER_TAG}"
                        '''
                    }
                }
            }
        }


        stage('Update Nginx Configuration') {
            steps {
                script {
                    sshagent(credentials: [SSH_CREDENTIALS]) {
                        sh '''
                        ssh ${REMOTE_SERVER} "sudo nginx -t && sudo systemctl reload nginx"
                        '''
                    }
                }
            }
        }

    }

    post {
        always {
            cleanWs()
        }
    }
}
