pipeline {
    agent any

    environment {
        APP_ENV = "production"
    }

    stages {

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

        // =========================
        // PHP DEPENDENCIES
        // =========================
        stage('Install PHP Dependencies') {
            steps {
                bat 'composer install --no-interaction --prefer-dist --optimize-autoloader --no-dev'
            }
        }

        // =========================
        // NODE DEPENDENCIES
        // =========================
        stage('Install Node Dependencies') {
            steps {
                bat 'npm install'
            }
        }

        stage('Build Frontend (Vite)') {
            steps {
                bat 'npm run build'
            }
        }

        // =========================
        // ENV SETUP
        // =========================
        stage('Setup Environment') {
            steps {
                bat '''
                if not exist .env (
                    copy .env.example .env
                )

                php artisan key:generate --force
                '''
            }
        }

        // =========================
        // CLEAN CACHE
        // =========================
        stage('Clear Cache') {
            steps {
                bat '''
                php artisan config:clear
                php artisan cache:clear
                php artisan route:clear
                php artisan view:clear
                '''
            }
        }

        // =========================
        // TESTS (OPTIONAL)
        // =========================
        stage('Run Tests') {
            steps {
                bat '''
                set APP_ENV=testing

                if exist vendor\\bin\\pest (
                    vendor\\bin\\pest --stop-on-failure
                ) else (
                    echo Pest not installed. Skipping tests.
                )
                '''
            }
        }

        // =========================
        // OPTIMIZE FOR PRODUCTION
        // =========================
        stage('Prepare Build') {
            steps {
                bat '''
                php artisan config:cache
                php artisan route:cache
                php artisan view:cache
                '''
            }
        }

        // =========================
        // LOCAL DEPLOY (XAMPP)
        // =========================
        stage('Deploy to XAMPP') {

            when {
                branch 'dev'
            }

            steps {
                bat '''
                echo Deploying to XAMPP...

                robocopy . C:\\xampp\\htdocs\\laravel-app /MIR /XD node_modules .git storage\\logs

                set RC=%ERRORLEVEL%

                if %RC% LSS 8 exit 0
                exit %RC%
                '''
            }
        }

        // =========================
        // PRODUCTION DEPLOY (cPanel FTP)
        // =========================
        stage('Deploy to cPanel via FTP') {

            when {
                branch 'main'
            }

            steps {

                withCredentials([usernamePassword(
                    credentialsId: 'ftp-creds',
                    usernameVariable: 'FTP_USER',
                    passwordVariable: 'FTP_PASS'
                )]) {

                    bat '''
                    echo Deploying to cPanel...

                    echo open ftp://%FTP_USER%:%FTP_PASS%@anysales.lk > winscp.txt
                    echo option batch abort >> winscp.txt
                    echo option confirm off >> winscp.txt
                    echo synchronize remote . . >> winscp.txt
                    echo exit >> winscp.txt

                    "C:\\Program Files (x86)\\WinSCP\\WinSCP.com" /script=winscp.txt
                    del winscp.txt
                    '''
                }
            }
        }
    }

    post {
        success {
            echo 'Build SUCCESS'
        }

        failure {
            echo 'Build FAILED'
        }
    }
}