name: Auto-Minify JavaScript

on:
  push:
    branches:
      - main
    paths:
      - '**/*.js'
      - '!**/*.min.js'
  workflow_dispatch: 

permissions:
  contents: write

jobs:
  minify: 
    runs-on: ubuntu-latest
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          token: ${{ secrets.GITHUB_TOKEN }}

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with: 
          node-version: '20'

      - name: Install Terser
        run: npm install -g terser

      - name: Find and minify all JavaScript files
        run: |
          echo "🔍 Finding all .js files (excluding .min.js)..."
          
          # Find all .js files, excluding minified, node_modules, etc.
          find . -type f -name "*.js" \
            -not -name "*.min.js" \
            -not -path "*/node_modules/*" \
            -not -path "*/.git/*" \
            -not -path "*/scripts/*" \
            -not -path "*/.github/*" | while read jsfile; do
            
            # Get directory and filename
            dir=$(dirname "$jsfile")
            filename=$(basename "$jsfile" .js)
            minified="${dir}/${filename}.min.js"
            
            echo "📦 Minifying: $jsfile"
            
            # Minify with Terser
            terser "$jsfile" \
              --compress \
              --mangle keep_classnames=true \
              --comments "/^!/" \
              --output "$minified"
            
            if [ -f "$minified" ]; then
              echo "   ✅ Created: $minified"
            else
              echo "   ⚠️ Failed to create: $minified"
            fi
          done
          
          echo ""
          echo "🎉 Minification complete!"

      - name: Commit and push minified files
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          
          # Pull latest changes
          git pull --rebase origin main || git pull origin main || true
          
          # Stage all minified files
          git add **/*.min.js 2>/dev/null || true
          
          # Check if there are changes
          if git diff --staged --quiet; then
            echo "ℹ️ No new minified files to commit"
            exit 0
          fi
          
          # Show what's being committed
          echo "📝 Files to commit:"
          git diff --staged --name-only
          
          # Commit
          git commit -m "🤖 Auto-minify JavaScript files [skip ci]"
          
          # Push with retry
          for i in {1..3}; do
            echo "📤 Push attempt $i/3..."
            
            if git push origin main; then
              echo "✅ Successfully pushed!"
              exit 0
            fi
            
            echo "⚠️ Retrying..."
            git pull --rebase origin main || git pull origin main
            sleep 2
          done
          
          echo "❌ Failed to push after 3 attempts"
          exit 1
