name: Deploy to Production

on:
  push:
    branches:
      - main
      - master

jobs:
  deploy:
    runs-on: ubuntu-latest
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
      
      - name: Deploy to server
        run: |
          echo "🚀 Deploying to /var/www/html/games"
          echo "📁 Current directory: $(pwd)"
          echo "📁 GITHUB_WORKSPACE: $GITHUB_WORKSPACE"
          
          # Read game info from readme.md
          if [ -f "$GITHUB_WORKSPACE/readme.md" ]; then
            GAME_TITLE=$(sed -n '1p' "$GITHUB_WORKSPACE/readme.md")
            GAME_DESC=$(sed -n '2p' "$GITHUB_WORKSPACE/readme.md")
            GAME_TYPE=$(sed -n '3p' "$GITHUB_WORKSPACE/readme.md")
          else
            GAME_TITLE="Untitled Game"
            GAME_DESC="No description"
            GAME_TYPE="quiz"
          fi
          
          # Create base directory
          mkdir -p /var/www/html/games/
          
          # Find and deploy all game folders (exclude source, .git, .gitea, etc.)
          DEPLOYED_FOLDERS=""
          DEPLOYED_URLS=""
          for folder in $GITHUB_WORKSPACE/*/; do
            folder_name=$(basename "$folder")
            
            # Skip excluded folders
            if [[ "$folder_name" == "source" ]] || \
               [[ "$folder_name" == ".git" ]] || \
               [[ "$folder_name" == ".gitea" ]] || \
               [[ "$folder_name" == "node_modules" ]] || \
               [[ "$folder_name" == "logs" ]] || \
               [[ "$folder_name" == "uploads" ]]; then
              echo "⏭️  Skipping: $folder_name"
              continue
            fi
            
            # Deploy the folder
            echo "📦 Deploying: $folder_name"
            rsync -av --delete "$folder" "/var/www/html/games/$folder_name/"
            DEPLOYED_FOLDERS="$DEPLOYED_FOLDERS $folder_name"
            GAME_URL="https://senaai.tech/games/$folder_name/"
            DEPLOYED_URLS="$DEPLOYED_URLS\n  🔗 $GAME_URL"
            
            # Get thumbnail (first image found or default)
            THUMBNAIL=$(find "$folder" -type f \( -name "*.png" -o -name "*.jpg" -o -name "*.jpeg" \) 2>/dev/null | head -n 1 || true)
            if [ -z "$THUMBNAIL" ]; then
              THUMBNAIL="https://senaai.tech/games/$folder_name/images/default.png"
            else
              THUMBNAIL="https://senaai.tech/games/$folder_name/$(basename "$THUMBNAIL")"
            fi
            
            # Submit game info to API
            echo "📤 Submitting game info to API..."
            curl --location 'http://senaai.tech:10000/api/games/save-with-check' \
              --header 'Content-Type: application/x-www-form-urlencoded' \
              --data-urlencode "title=$GAME_TITLE" \
              --data-urlencode "description=$GAME_DESC" \
              --data-urlencode "url=$GAME_URL" \
              --data-urlencode "thumbnail=$THUMBNAIL" \
              --data-urlencode "type=$GAME_TYPE" || true
            echo ""
          done
          
          # Show deployment summary
          echo ""
          echo "=========================================="
          echo "🎉 Deployment Completed Successfully!"
          echo "=========================================="
          echo ""
          echo "📍 Deployed URLs:"
          echo -e "$DEPLOYED_URLS"
          echo ""
          echo "=========================================="