pipeline {
    agent any

    triggers {
        // Job di-trigger otomatis oleh webhook GitHub (push ke branch staging).
        githubPush()
    }

    environment {
        COMPOSER_ALLOW_SUPERUSER = '1'
    }

    options {
        timestamps()
        disableConcurrentBuilds()
        buildDiscarder(logRotator(numToKeepStr: '10'))
    }

    stages {
        stage('Checkout') {
            steps {
                checkout scm
            }
        }

        stage('Install Dependencies') {
            parallel {
                stage('Composer') {
                    steps {
                        sh 'composer install --no-interaction --prefer-dist --optimize-autoloader'
                    }
                }
                stage('NPM') {
                    steps {
                        sh 'npm ci'
                    }
                }
            }
        }

        stage('Prepare Environment') {
            steps {
                sh '''
                    if [ ! -f .env ]; then
                        cp .env.example .env
                        php artisan key:generate
                    fi
                    php artisan ziggy:generate
                '''
            }
        }

        stage('Lint') {
            parallel {
                stage('Pint') {
                    steps {
                        sh 'vendor/bin/pint --test'
                    }
                }
                stage('Frontend') {
                    steps {
                        sh 'npm run format:check'
                        sh 'npm run lint'
                    }
                }
            }
        }

        stage('Build Assets') {
            steps {
                sh 'npm run build'
            }
        }

        stage('Test') {
            steps {
                sh './vendor/bin/phpunit'
            }
        }

        stage('Deploy to Staging') {
            steps {
                // TODO: isi step deploy ke server staging (mis. SSH + git pull,
                // atau rsync hasil build) setelah kredensial/host server tersedia
                // di Jenkins credential store. Contoh pola SSH (sshagent + ssh):
                //
                // sshagent(credentials: ['SSH_STAGING_CRED']) {
                //     sh '''
                //         ssh -o StrictHostKeyChecking=no user@staging-host '
                //             cd /path/to/app &&
                //             git pull origin stag-meet-meals-be &&
                //             composer install --no-interaction --prefer-dist --optimize-autoloader &&
                //             npm ci && npm run build &&
                //             php artisan migrate --force &&
                //             php artisan config:cache &&
                //             php artisan route:cache &&
                //             php artisan queue:restart
                //         '
                //     '''
                // }
                echo 'Deploy step belum dikonfigurasi — lengkapi setelah kredensial server staging tersedia.'
            }
        }
    }

    post {
        always {
            cleanWs()
        }
    }
}
