pipeline {
    agent any

    environment {
        DEPLOY_DIR = "/var/www/staging-floorfactory/floorfactory"
    }

    stages {

        stage('Checkout Source') {
            steps {
                checkout([
                    $class: 'GitSCM',
                    branches: [[name: '*/development']],
                    userRemoteConfigs: [[
                        credentialsId: 'github-ssh',
                        url: 'git@github.com:omsoftware2027/floorfactory.git'
                    ]]
                ])
            }
        }

        stage('Composer Install') {
            steps {
                sh '''
                    composer install \
                        --no-interaction \
                        --prefer-dist \
                        --optimize-autoloader
                '''
            }
        }

        stage('Install Node Dependencies') {
            steps {
                sh '''
                    docker run --rm \
                        -v ${WORKSPACE}:/app \
                        -w /app \
                        node:20 \
                        npm install
                '''
            }
        }

        stage('Build Frontend') {
            steps {
                sh '''
                    docker run --rm \
                        -v ${WORKSPACE}:/app \
                        -w /app \
                        node:20 \
                        npm run production
                '''
            }
        }

        stage('Deploy') {
            steps {
                sh '''
                    echo "========== DEPLOYING =========="

                    rsync -rlDz \
                        --no-owner \
                        --no-group \
                        --exclude='.git/' \
                        --exclude='.github/' \
                        --exclude='.env' \
                        --exclude='storage/' \
                        --exclude='bootstrap/cache/' \
                        --exclude='vendor/' \
                        --exclude='node_modules/' \
                        ${WORKSPACE}/ ${DEPLOY_DIR}/

                    echo "Deployment completed."
                '''
            }
        }

        stage('Composer Install on Server') {
            steps {
                dir("${DEPLOY_DIR}") {
                    sh '''
                        composer install \
                            --no-interaction \
                            --prefer-dist \
                            --optimize-autoloader
                    '''
                }
            }
        }

        stage('Laravel Optimize') {
            steps {
                dir("${DEPLOY_DIR}") {
                    sh '''
                        php artisan optimize:clear
                        php artisan config:cache
                        php artisan route:cache
                        php artisan view:cache
                    '''
                }
            }
        }

        stage('Database Migration') {
            steps {
                script {
                    dir("${DEPLOY_DIR}") {
                        def status = sh(
                            script: 'php artisan migrate --force',
                            returnStatus: true
                        )

                        if (status == 0) {
                            echo 'Database migration completed successfully.'
                        } else {
                            echo 'Migration failed. Continuing deployment.'
                        }
                    }
                }
            }
        }

        stage('Set Permissions') {
            steps {
                sh '''
                    chmod -R 775 ${DEPLOY_DIR}/storage || true
                    chmod -R 775 ${DEPLOY_DIR}/bootstrap/cache || true
                '''
            }
        }
    }

    post {
        success {
            echo 'Deployment completed successfully.'
        }

        failure {
            echo 'Deployment failed.'
        }

        always {
            cleanWs()
        }
    }
}
