name: Build Client for All Platforms

on:
  push:
    branches: [ main, develop ]
    paths:
      - 'Client/**'
  pull_request:
    branches: [ main ]
    paths:
      - 'Client/**'
  workflow_dispatch:

jobs:
  build:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
        
    steps:
    - name: Checkout code
      uses: actions/checkout@v4
      
    - name: Setup Node.js
      uses: actions/setup-node@v4
      with:
        node-version: '18'
        cache: 'npm'
        cache-dependency-path: Client/package-lock.json
        
    - name: Install system dependencies (Ubuntu)
      if: matrix.os == 'ubuntu-latest'
      run: |
        sudo apt-get update
        sudo apt-get install -y libnss3-dev libatk-bridge2.0-dev libdrm2 libxkbcommon-dev libxcomposite-dev libxdamage-dev libxrandr-dev libgbm-dev libxss1 libasound2-dev
        
    - name: Install system dependencies (macOS)
      if: matrix.os == 'macos-latest'
      run: |
        # Install Xcode command line tools if not present
        xcode-select --install 2>/dev/null || true
        
    - name: Install system dependencies (Windows)
      if: matrix.os == 'windows-latest'
      run: |
        # Windows builds typically have required dependencies pre-installed
        # Ensure chocolatey is available for any additional packages
        choco --version || echo "Chocolatey not available"
        
    - name: Install dependencies
      working-directory: ./Client
      run: |
        if [ "${{ matrix.os }}" = "windows-latest" ]; then
          npm ci --include=dev
        else
          npm ci
        fi
      shell: bash
      
    - name: Clean previous builds (Windows)
      if: matrix.os == 'windows-latest'
      working-directory: ./Client
      run: if exist dist rmdir /s /q dist
      shell: cmd
      
    - name: Clean previous builds (Unix)
      if: matrix.os != 'windows-latest'
      working-directory: ./Client
      run: rm -rf dist
      shell: bash
      
    - name: Build for Linux
      if: matrix.os == 'ubuntu-latest'
      working-directory: ./Client
      run: npm run build-linux
      
    - name: Build for Windows
      if: matrix.os == 'windows-latest'
      working-directory: ./Client
      run: npm run build-win
      
    - name: Build for macOS
      if: matrix.os == 'macos-latest'
      working-directory: ./Client
      run: npm run build-mac
      
    - name: Upload artifacts
      uses: actions/upload-artifact@v4
      with:
        name: client-${{ matrix.os }}
        path: |
          Client/dist/*.exe
          Client/dist/*.msi
          Client/dist/*.dmg
          Client/dist/*.zip
          Client/dist/*.AppImage
          Client/dist/*.deb
          Client/dist/*.rpm
          Client/dist/*.app
        retention-days: 30
      continue-on-error: true

  package:
    needs: build
    runs-on: ubuntu-latest
    if: github.event_name == 'push' && github.ref == 'refs/heads/main'
    
    steps:
    - name: Checkout code
      uses: actions/checkout@v4
      
    - name: Download all artifacts
      uses: actions/download-artifact@v4
      with:
        path: artifacts
        
    - name: Create release directory
      run: mkdir -p release
      
    - name: Organize executables by platform
      run: |
        # Create platform directories
        mkdir -p release/Windows
        mkdir -p release/macOS  
        mkdir -p release/Linux
        
        # Windows executables - copy directly from dist
        if [ -d "artifacts/client-windows-latest" ]; then
          cp artifacts/client-windows-latest/*.exe release/Windows/ 2>/dev/null || true
          cp artifacts/client-windows-latest/*.msi release/Windows/ 2>/dev/null || true
        fi
        
        # macOS executables - copy directly from dist
        if [ -d "artifacts/client-macos-latest" ]; then
          cp artifacts/client-macos-latest/*.dmg release/macOS/ 2>/dev/null || true
          cp artifacts/client-macos-latest/*.zip release/macOS/ 2>/dev/null || true
          # Handle .app bundles (directories)
          if [ -d "artifacts/client-macos-latest" ]; then
            find artifacts/client-macos-latest -maxdepth 1 -name "*.app" -type d -exec cp -r {} release/macOS/ \; 2>/dev/null || true
          fi
        fi
        
        # Linux executables - copy directly from dist
        if [ -d "artifacts/client-ubuntu-latest" ]; then
          cp artifacts/client-ubuntu-latest/*.AppImage release/Linux/ 2>/dev/null || true
          cp artifacts/client-ubuntu-latest/*.deb release/Linux/ 2>/dev/null || true
          cp artifacts/client-ubuntu-latest/*.rpm release/Linux/ 2>/dev/null || true
        fi
        
        # List what we actually got
        echo "=== Executables Found ==="
        find release -type f -name "*" -o -name "*.app" | head -20 || true
        
        # Create platform info file
        cat > release/README.txt << EOF
        Galaxy Strike Online - Multi-Platform Client
        ==========================================
        
        Windows:
        - Run the .exe installer or portable executable
        
        macOS:
        - Open the .dmg file and drag to Applications
        - Or extract the .zip and run the .app
        
        Linux:
        - Make the .AppImage executable: chmod +x *.AppImage
        - Or install the .deb package: sudo dpkg -i *.deb
        
        Build Date: $(date -u +"%Y-%m-%d %H:%M:%S UTC")
        Commit: ${{ github.sha }}
        EOF
        
    - name: Create all-in-one zip
      run: |
        cd release
        zip -r ../Galaxy-Strike-Online-Client-${{ github.sha }}.zip .
        
    - name: Upload all-in-one zip
      uses: actions/upload-artifact@v4
      with:
        name: galaxy-strike-online-client-all-platforms
        path: Galaxy-Strike-Online-Client-${{ github.sha }}.zip
        retention-days: 90
        
    - name: Create Release (Main Branch)
      if: github.event_name == 'push' && github.ref == 'refs/heads/main'
      uses: softprops/action-gh-release@v1
      with:
        files: Galaxy-Strike-Online-Client-${{ github.sha }}.zip
        name: Galaxy Strike Online Client - Latest
        body: |
          Latest multi-platform client release for Galaxy Strike Online.
          
          Includes:
          - Windows (NSIS installer + Portable)
          - macOS (DMG + ZIP) 
          - Linux (AppImage + Debian package)
          
          Commit: ${{ github.sha }}
          Build Date: ${{ github.event.head_commit.timestamp }}
          
          **Download the all-in-one zip file below for all platforms.**
        draft: false
        prerelease: false
        tag_name: latest
        token: ${{ secrets.GITHUB_TOKEN }}
        
    - name: Create Release (Tags)
      if: startsWith(github.ref, 'refs/tags/')
      uses: softprops/action-gh-release@v1
      with:
        files: Galaxy-Strike-Online-Client-${{ github.sha }}.zip
        name: Galaxy Strike Online Client - ${{ github.ref_name }}
        body: |
          Multi-platform client release for Galaxy Strike Online.
          
          Includes:
          - Windows (NSIS installer + Portable)
          - macOS (DMG + ZIP) 
          - Linux (AppImage + Debian package)
          
          Commit: ${{ github.sha }}
        draft: false
        prerelease: false
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
