stages:
  - deploy

# -----------------------------------------
# Deployment auf STAGING
# -----------------------------------------
deploy:staging:
  stage: deploy
  image: alpine:3.20

  before_script:
    - echo "📡 Installing lftp..."
    - apk add --no-cache lftp

    - echo "📂 CI Workspace:"
    - pwd
    - ls -la

    - echo "📂 Inhalt von src/:"
    - ls -la src/ || echo "⚠️ src/ existiert nicht!"
    - echo "📂 Inhalt von public/:"
    - ls -la public/ || echo "⚠️ public/ existiert nicht!"
    - echo "📂 Inhalt von config/:"
    - ls -la config/ || echo "⚠️ config/ existiert nicht!"
    - echo "📂 Inhalt von config/staging/:"
    - ls -la config/staging/ || echo "⚠️ config/staging/ existiert nicht!"

  script:
    - |
      set -e

      # Sicherstellen, dass Basisverzeichnisse existieren
      for d in src public config config/staging; do
        if [ ! -d "$d" ]; then
          echo "❌ Verzeichnis '$d/' fehlt im Repo! Abbruch."
          exit 1
        fi
      done

      echo "🚀 Deploy STAGING → ${FTP_HOST}:${FTP_PATH_STAGING}"

      echo "🔁 Sync src/ → ${FTP_PATH_STAGING}src/"
      lftp -u "${FTP_USER}","${FTP_PASSWORD}" "${FTP_HOST}" -e "
        set ftp:ssl-force true;
        set ftp:passive-mode true;
        set ftp:ssl-protect-data true;
        set ssl:verify-certificate no;
        mirror -R --delete --exclude .gitkeep src/ ${FTP_PATH_STAGING}src/;
        bye
      "

      echo "🔁 Sync public/ → ${FTP_PATH_STAGING}public/"
      lftp -u "${FTP_USER}","${FTP_PASSWORD}" "${FTP_HOST}" -e "
        set ftp:ssl-force true;
        set ftp:passive-mode true;
        set ftp:ssl-protect-data true;
        set ssl:verify-certificate no;
        mirror -R --delete --exclude .gitkeep public/ ${FTP_PATH_STAGING}public/;
        bye
      "

      echo "🔁 Sync config/staging/ (nur Inhalt) → ${FTP_PATH_STAGING}config/"
      lftp -u "${FTP_USER}","${FTP_PASSWORD}" "${FTP_HOST}" -e "
        set ftp:ssl-force true;
        set ftp:passive-mode true;
        set ftp:ssl-protect-data true;
        set ssl:verify-certificate no;
        lcd config/staging;
        mirror -R --delete --exclude .gitkeep ./ ${FTP_PATH_STAGING}config/;
        bye
      "

      echo "✅ Deploy STAGING abgeschlossen."

  environment:
    name: staging
    url: https://staging.usbcheck.it

  only:
    - develop
    - merge_requests


# -----------------------------------------
# Deployment auf PRODUCTION
# -----------------------------------------
deploy:production:
  stage: deploy
  image: alpine:3.20

  before_script:
    - echo "📡 Installing lftp..."
    - apk add --no-cache lftp

    - echo "📂 CI Workspace:"
    - pwd
    - ls -la

    - echo "📂 Inhalt von src/:"
    - ls -la src/ || echo "⚠️ src/ existiert nicht!"
    - echo "📂 Inhalt von public/:"
    - ls -la public/ || echo "⚠️ public/ existiert nicht!"
    - echo "📂 Inhalt von config/:"
    - ls -la config/ || echo "⚠️ config/ existiert nicht!"
    - echo "📂 Inhalt von config/prod/:"
    - ls -la config/prod/ || echo "⚠️ config/prod/ existiert nicht!"

  script:
    - |
      set -e

      # Sicherstellen, dass Basisverzeichnisse existieren
      for d in src public config config/prod; do
        if [ ! -d "$d" ]; then
          echo "❌ Verzeichnis '$d/' fehlt im Repo! Abbruch."
          exit 1
        fi
      done

      echo "🚀 Deploy PRODUCTION → ${FTP_HOST}:${FTP_PATH_PROD}"

      echo "🔁 Sync src/ → ${FTP_PATH_PROD}src/"
      lftp -u "${FTP_USER}","${FTP_PASSWORD}" "${FTP_HOST}" -e "
        set ftp:ssl-force true;
        set ftp:passive-mode true;
        set ftp:ssl-protect-data true;
        set ssl:verify-certificate no;
        mirror -R --delete --exclude .gitkeep src/ ${FTP_PATH_PROD}src/;
        bye
      "

      echo "🔁 Sync public/ → ${FTP_PATH_PROD}public/"
      lftp -u "${FTP_USER}","${FTP_PASSWORD}" "${FTP_HOST}" -e "
        set ftp:ssl-force true;
        set ftp:passive-mode true;
        set ftp:ssl-protect-data true;
        set ssl:verify-certificate no;
        mirror -R --delete --exclude .gitkeep public/ ${FTP_PATH_PROD}public/;
        bye
      "

      echo "🔁 Sync config/prod/ (nur Inhalt) → ${FTP_PATH_PROD}config/"
      lftp -u "${FTP_USER}","${FTP_PASSWORD}" "${FTP_HOST}" -e "
        set ftp:ssl-force true;
        set ftp:passive-mode true;
        set ftp:ssl-protect-data true;
        set ssl:verify-certificate no;
        lcd config/prod;
        mirror -R --delete --exclude .gitkeep ./ ${FTP_PATH_PROD}config/;
        bye
      "

      echo "✅ Deploy PRODUCTION abgeschlossen."

  environment:
    name: production
    url: https://www.usbcheck.it

  only:
    - main
#  when: manual
