pipeline {
  // Build + deploy stages require Docker CLI/daemon access on the Jenkins node.
  // Ensure this label maps to an agent with Docker installed and usable.
  agent any

  environment {
    REGISTRY_IMAGE = "plesk-agency-portal"
    IMAGE_LATEST = "${REGISTRY_IMAGE}:latest"
    IMAGE_BUILD = "${REGISTRY_IMAGE}:build-${BUILD_NUMBER}"
    DEPLOY_SSH_CREDENTIAL_ID = "${env.DEPLOY_SSH_CREDENTIAL_ID ?: 'plesk-agency-deploy-key'}"
    APP_HOST = "${env.APP_HOST ?: '192.168.68.89'}"
    APP_USER = "${env.APP_USER ?: 'deploy'}"
    DEPLOY_PATH = "${env.DEPLOY_PATH ?: '/opt/plesk-agency-portal'}"
  }

  stages {
    // stage('Preflight') {
    //   steps {
    //     sh '''
    //       set -euo pipefail

    //       if ! command -v docker >/dev/null 2>&1; then
    //         echo "Docker CLI is not available on this Jenkins agent."
    //         echo "Run this pipeline on a Docker-capable node or install Docker on the current agent."
    //         exit 1
    //       fi

    //       if ! docker version >/dev/null 2>&1; then
    //         echo "Docker is installed but not usable by Jenkins (daemon/socket/permissions issue)."
    //         exit 1
    //       fi
    //     '''
    //   }
    // }

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

    stage('Install dependencies') {
      steps {
        sh 'npm ci'
      }
    }

    // stage('Lint') {
    //   steps {
    //     script {
    //       def hasLint = sh(script: "node -e \"const fs=require('fs');const p=require('./package.json');process.exit(p?.scripts?.lint?0:1)\"", returnStatus: true)
    //       if (hasLint == 0) {
    //         sh 'npm run lint'
    //       } else {
    //         echo 'No lint script configured; skipping lint stage.'
    //       }
    //     }
    //   }
    // }

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

    stage('Build Docker image') {
      steps {
        sh 'docker build -t ${IMAGE_LATEST} -t ${IMAGE_BUILD} .'
      }
    }

    stage('Deploy') {
      steps {
        withCredentials([sshUserPrivateKey(
          credentialsId: "${env.DEPLOY_SSH_CREDENTIAL_ID}",
          keyFileVariable: 'SSH_KEY'
        )]) {
          sh '''
bash -eo pipefail << 'EOF'
            set -euo pipefail

            rsync -az \
              docker-compose.prod.yml \
              scripts/ \
              -e "ssh -i $SSH_KEY" \
              $APP_USER@$APP_HOST:$DEPLOY_PATH/

            docker save ${IMAGE_LATEST} ${IMAGE_BUILD} \
              | ssh -i $SSH_KEY $APP_USER@$APP_HOST 'docker load'

            ssh -i $SSH_KEY $APP_USER@$APP_HOST \
              "cd $DEPLOY_PATH && chmod +x ./scripts/deploy-prod.sh ./scripts/smoke-check.sh ./scripts/rollback-prod.sh"

            ssh -i $SSH_KEY $APP_USER@$APP_HOST "cd $DEPLOY_PATH && ./scripts/deploy-prod.sh"
EOF
          '''
        }
      }
    }

    stage('Smoke check') {
      steps {
        sh '''
bash -eo pipefail << EOF
set -euo pipefail
chmod +x scripts/smoke-check.sh
scripts/smoke-check.sh "${APP_BASE_URL}"
EOF
'''
      }
    }
  }
}
