pipeline {
    agent any

    options {
        timestamps()
        disableConcurrentBuilds()
    }

    stages {
        stage('Deploy Staging') {
            when {
                branch 'staging'
            }

            agent {
                label 'docker_node_1'
            }

            environment {
                CI_COMMIT_SHA = "${GIT_COMMIT}"
            }

            steps {
                checkout scm

                sh '''
                    set -eu

                    DEPLOY_DIR="/var/www/presale_backend"

                    echo "Checking deployment directory..."
                    test -d "$DEPLOY_DIR"
                    test -f "$DEPLOY_DIR/.env"

                    echo "Syncing staging code to deployment directory..."
                    rsync -a --delete \
                        --exclude='.git/' \
                        --exclude='.env' \
                        --exclude='.env.*' \
                        --exclude='node_modules/' \
                        --exclude='dist/' \
                        --exclude='uploads/' \
                        "$WORKSPACE/" "$DEPLOY_DIR/"

                    cd "$DEPLOY_DIR"

                    echo "Updating Docker image tag..."
                    IMAGE_TAG="$(git -C "$WORKSPACE" rev-parse --short=12 HEAD)"
                    if grep -q '^IMAGE_TAG=' .env; then
                        sed -i "s/^IMAGE_TAG=.*/IMAGE_TAG=$IMAGE_TAG/" .env
                    else
                        printf '\\nIMAGE_TAG=%s\\n' "$IMAGE_TAG" >> .env
                    fi

                    echo "Validating Docker Compose configuration..."
                    docker compose --env-file .env config -q

                    echo "Building Docker image for commit: $CI_COMMIT_SHA"
                    docker compose --env-file .env build

                    echo "Starting containers..."
                    docker compose --env-file .env up -d --no-build

                    echo "Checking container status..."
                    docker compose --env-file .env ps

                    echo "Recent application logs:"
                    docker compose --env-file .env logs --tail=50 app
                '''
            }
        }
          stage('Deploy Staging') {
            when {
                branch 'staging'
            }

            agent {
                label 'docker_node_1'
            }

            environment {
                CI_COMMIT_SHA = "${GIT_COMMIT}"
            }

            steps {
                checkout scm

                sh '''
                    set -eu

                    DEPLOY_DIR="/var/www/presale_backend"

                    echo "Checking deployment directory..."
                    test -d "$DEPLOY_DIR"
                    test -f "$DEPLOY_DIR/.env"

                    echo "Syncing staging code to deployment directory..."
                    rsync -a --delete \
                        --exclude='.git/' \
                        --exclude='.env' \
                        --exclude='.env.*' \
                        --exclude='node_modules/' \
                        --exclude='dist/' \
                        --exclude='uploads/' \
                        "$WORKSPACE/" "$DEPLOY_DIR/"

                    cd "$DEPLOY_DIR"

                    echo "Updating Docker image tag..."
                    IMAGE_TAG="$(git -C "$WORKSPACE" rev-parse --short=12 HEAD)"
                    if grep -q '^IMAGE_TAG=' .env; then
                        sed -i "s/^IMAGE_TAG=.*/IMAGE_TAG=$IMAGE_TAG/" .env
                    else
                        printf '\\nIMAGE_TAG=%s\\n' "$IMAGE_TAG" >> .env
                    fi

                    echo "Validating Docker Compose configuration..."
                    docker compose --env-file .env config -q

                    echo "Building Docker image for commit: $CI_COMMIT_SHA"
                    docker compose --env-file .env build

                    echo "Starting containers..."
                    docker compose --env-file .env up -d --no-build

                    echo "Checking container status..."
                    docker compose --env-file .env ps

                    echo "Recent application logs:"
                    docker compose --env-file .env logs --tail=50 app
                '''
            }
        }
    }

    post {
        success {
            echo 'Staging backend deployment successful.'
        }
        failure {
            echo 'Staging backend deployment failed. Check Jenkins console output.'
        }
    }
}