name: Deploy

on:
  push:
    branches:
      - main
      - develop

env:
  BASE_DIRS: "src public api partials tools"
  CONFIG_BASE_DIR: "config"

jobs:
  deploy:
    runs-on: private-server

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Install lftp
        shell: sh
        run: |
          if command -v lftp >/dev/null 2>&1; then
            echo "✅ lftp bereits installiert"
          elif command -v apk >/dev/null 2>&1; then
            apk add --no-cache lftp ca-certificates
          elif command -v apt-get >/dev/null 2>&1; then
            apt-get update
            apt-get install -y lftp ca-certificates
          else
            echo "❌ Kein unterstützter Paketmanager gefunden"
            exit 1
          fi

      - name: Set environment
        run: |
          if [ "${{ gitea.ref_name }}" = "main" ]; then
            echo "TARGET_PATH=${{ vars.FTP_PATH_PROD }}" >> "$GITHUB_ENV"
            echo "CONFIG_ENV_DIR=config/prod" >> "$GITHUB_ENV"
          elif [ "${{ gitea.ref_name }}" = "develop" ]; then
            echo "TARGET_PATH=${{ vars.FTP_PATH_STAGING }}" >> "$GITHUB_ENV"
            echo "CONFIG_ENV_DIR=config/staging" >> "$GITHUB_ENV"
          else
            echo "Unsupported branch"
            exit 1
          fi

      - name: Debug workspace
        run: |
          echo "📂 CI Workspace:"
          pwd
          ls -la

      - name: Deploy via FTPS
        run: |
          set -e

          echo "🚀 Deploy to ${TARGET_PATH}"

          VALID_DIRS=""

          for d in $BASE_DIRS; do
            if [ -d "$d" ]; then
              VALID_DIRS="$VALID_DIRS $d"
            else
              echo "⚠️ Überspringe fehlendes Verzeichnis: $d"
            fi
          done

          if [ -z "$VALID_DIRS" ]; then
            echo "❌ Kein deploybares Verzeichnis gefunden."
            exit 1
          fi

          for d in $VALID_DIRS; do
            echo "🔁 ${d}/ → ${TARGET_PATH}${d}/"
            lftp -u "${{ secrets.FTP_USER }}","${{ secrets.FTP_PASSWORD }}" "${{ vars.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 ${d}/ ${TARGET_PATH}${d}/;
              bye
            " || exit 1
          done

          if [ -d "$CONFIG_BASE_DIR" ] && [ -d "$CONFIG_ENV_DIR" ]; then
            echo "🧩 Baue gemischtes Config-Verzeichnis"

            rm -rf .ci_config_deploy
            mkdir -p .ci_config_deploy

            for f in ${CONFIG_BASE_DIR}/*.php; do
              [ -f "$f" ] && cp "$f" .ci_config_deploy/
            done

            cp -R ${CONFIG_ENV_DIR}/. .ci_config_deploy/

            echo "🔁 config → ${TARGET_PATH}${CONFIG_BASE_DIR}/"

            lftp -u "${{ secrets.FTP_USER }}","${{ secrets.FTP_PASSWORD }}" "${{ vars.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 .ci_config_deploy;
              mirror -R --delete --exclude .gitkeep ./ ${TARGET_PATH}${CONFIG_BASE_DIR}/;
              bye
            " || exit 1
          else
            echo "⚠️ Config-Deploy übersprungen: ${CONFIG_BASE_DIR} oder ${CONFIG_ENV_DIR} fehlt"
          fi

          echo "✅ Deploy abgeschlossen"