name: Deploy to OVH (SFTP)

on:
  push:
    branches:
      - main
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    env:
      FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true

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

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 24
          cache: npm

      - name: Install npm dependencies
        run: npm ci

      - name: Clean UTF-8 BOM
        run: |
          sed -i '1s/^\xEF\xBB\xBF//' lang/*.json

      - name: Validate translation JSON files
        run: |
          for file in lang/*.json; do
            echo "Checking $file"
            python -m json.tool "$file" > /dev/null || exit 1
          done

      - name: Build assets
        run: npm run build

      - name: Upload build artifact
        uses: actions/upload-artifact@v4
        with:
          name: build-${{ github.run_id }}
          retention-days: 1
          path: |
            .
            !.git/**
            !.github/**
            !node_modules/**
            !tests/**
            !vendor/**
            !storage/**

  deploy-staging:
    needs: build
    runs-on: ubuntu-latest
    env:
      FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true

    steps:
      - name: Download build artifact
        uses: actions/download-artifact@v4
        with:
          name: build-${{ github.run_id }}

      - name: Deploy via lftp to staging
        env:
          SFTP_HOST: ${{ secrets.SFTP_HOST }}
          SFTP_USER: ${{ secrets.SFTP_USERNAME }}
          SFTP_PASS: ${{ secrets.SFTP_PASSWORD }}
          SFTP_PORT: ${{ secrets.SFTP_PORT }}
        run: |
          sudo apt-get install -y lftp
          lftp -c "
            set sftp:connect-program 'ssh -o StrictHostKeyChecking=no -p $SFTP_PORT';
            open sftp://$SFTP_USER:$SFTP_PASS@$SFTP_HOST;
            mirror --reverse --verbose \
              --exclude=^storage/ \
              --exclude=^vendor/ \
              --exclude=^\.env\$ \
              --exclude=^phpunit\.xml\$ \
              --exclude=^README\.md\$ \
              --exclude=^USER_GUIDE\.md\$ \
              --exclude=^claude\.md\$ \
              . ./www-staging/
          "

      - name: Run composer install and artisan on staging
        env:
          SFTP_USER: ${{ secrets.SFTP_USERNAME }}
          SFTP_PASS: ${{ secrets.SFTP_PASSWORD }}
        run: |
          sudo apt-get install -y sshpass
          sshpass -p "$SFTP_PASS" ssh -o StrictHostKeyChecking=no \
            $SFTP_USER@ssh.cluster100.hosting.ovh.net \
            "cd www-staging && \
             mkdir -p bootstrap/cache storage/framework/sessions storage/framework/views storage/framework/cache storage/logs && \
             chmod -R 775 bootstrap/cache storage && \
             php -r \"copy('https://getcomposer.org/installer', '/tmp/composer-setup.php');\" && \
             php /tmp/composer-setup.php --quiet --install-dir=/tmp --filename=composer && \
             php /tmp/composer install --no-dev --optimize-autoloader --no-interaction && \
             php artisan optimize:clear && \
             ([ -L public/storage ] && rm public/storage || true) && mkdir -p public/storage && chmod 775 public/storage && \
             php artisan migrate --force && \
             php artisan db:seed --class=InterestTagSeeder --force && \
             php artisan interests:migrate-csv && \
             php artisan config:cache && \
             php artisan route:cache && \
             php artisan view:cache"

