# Bitbucket Pipelines — Laravel API → FTP
#
# Repository variables (Settings → Pipelines → Repository variables):
#   Secured: FTP_PASSWORD
#   Plain:   FTP_HOST, FTP_USERNAME
#
# Optional: override remote folder if your cPanel path differs
#   FTP_SERVER_DIR  default: /remindersapi.vivafresh-rks.com/ 

# Minimal image; apk installs lftp in seconds (avoids slow Ubuntu apt on CI).
image: alpine:3.20

definitions:
  steps:
    - step: &deploy-api
        name: Deploy reminders API (FTP)
        deployment: production
        script:
          - apk add --no-cache bash lftp curl git
          - command -v lftp && lftp --version
          - |
            REMOTE_DIR="${FTP_SERVER_DIR:-/remindersapi.vivafresh-rks.com/}"
            CLONE_DIR="${BITBUCKET_CLONE_DIR:-$(pwd)}"
            lftp -c "
            set cmd:fail-exit yes
            set ftp:passive-mode true
            set ftp:ssl-allow true
            set ssl:verify-certificate no
            set net:max-retries 3
            set net:reconnect-interval-base 5
            open -u \"${FTP_USERNAME},${FTP_PASSWORD}\" -p ${FTP_PORT:-21} ${FTP_HOST}
            pwd
            cd ${REMOTE_DIR}
            pwd
            lcd ${CLONE_DIR}
            mirror -R --only-newer --parallel=3 --verbose \
              --exclude-glob '.git*' \
              --exclude-glob '.git*/**' \
              --exclude-glob '.env' \
              --exclude-glob '.env.*' \
              --exclude-glob 'node_modules/**' \
              --exclude-glob 'vendor/**' \
              --exclude-glob 'storage/**' \
              --exclude-glob 'bootstrap/cache/**' \
              --exclude-glob '.idea/**' \
              --exclude-glob '.vscode/**' \
              --exclude-glob '*.log' \
              --exclude-glob 'tests/**' \
              --exclude-glob 'phpunit.xml' \
              --exclude-glob '.gitignore' \
              --exclude-glob '.gitattributes' \
              --exclude-glob 'README.md' \
              --exclude-glob 'DEPLOYMENT*.md' \
              --exclude-glob '.styleci.yml' \
              --exclude-glob '.editorconfig' \
              .
            "
          - |
            MSG=$(printf '%s\n' "✅ Deployed: remindersapi.vivafresh-rks.com" "🔀 Branch: ${BITBUCKET_BRANCH}" "🧾 Commit: ${BITBUCKET_COMMIT}" "✏️ $(git log -1 --pretty=format:%s)" "👤 $(git log -1 --pretty=format:'%an <%ae>')")
            curl -s -S -X POST "https://api.telegram.org/bot8646743659:AAHFsLx4KG0rhDyIGQZQfxzRV2gELwZmqWc/sendMessage" \
              --data-urlencode "chat_id=-1003813191929" \
              --data-urlencode "text=${MSG}" > /dev/null || true

pipelines:
  default:
    - step: *deploy-api
