name: Build and Package Spacetris

on:
  push:
    branches: [ main, master ]
    tags: [ 'v*' ]
  pull_request:
    branches: [ main, master ]

jobs:
  build-windows:
    runs-on: windows-latest
    
    steps:
    - uses: actions/checkout@v4
      with:
        submodules: recursive
    
    - name: Setup vcpkg
      uses: lukka/run-vcpkg@v11
      with:
        vcpkgGitCommitId: 'latest'
    
    - name: Install dependencies
      run: |
        vcpkg install sdl3 sdl3-image sdl3-ttf --triplet=x64-windows
    
    - name: Configure CMake
      run: |
        cmake -B build -DCMAKE_TOOLCHAIN_FILE=${{ github.workspace }}/vcpkg/scripts/buildsystems/vcpkg.cmake -DCMAKE_BUILD_TYPE=Release
    
    - name: Build
      run: cmake --build build --config Release
    
    - name: Package
      run: cmake --build build --target package
    
    - name: Create distribution package
      run: |
        mkdir dist
        powershell -ExecutionPolicy Bypass -File build-production.ps1 -PackageOnly -OutputDir dist
    
    - name: Upload artifacts
      uses: actions/upload-artifact@v4
      with:
        name: spacetris-windows-x64
        path: dist/SpacetrisGame/
    
    - name: Create Release ZIP
      if: startsWith(github.ref, 'refs/tags/v')
      run: |
        cd dist
        7z a ../SpacetrisGame-Windows-x64.zip SpacetrisGame/
    
    - name: Release
      if: startsWith(github.ref, 'refs/tags/v')
      uses: softprops/action-gh-release@v1
      with:
        files: SpacetrisGame-Windows-x64.zip
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  build-linux:
    runs-on: ubuntu-latest
    
    steps:
    - uses: actions/checkout@v4
      with:
        submodules: recursive
    
    - name: Install dependencies
      run: |
        sudo apt-get update
        sudo apt-get install -y cmake build-essential
        # Install SDL3 from source or package manager
        # This may need adjustment based on SDL3 availability
    
    - name: Configure CMake
      run: cmake -B build -DCMAKE_BUILD_TYPE=Release
    
    - name: Build
      run: cmake --build build
    
    - name: Package
      run: |
        mkdir -p dist/SpacetrisGame-Linux
        cp build/spacetris dist/SpacetrisGame-Linux/
        cp -r assets dist/SpacetrisGame-Linux/
        cp FreeSans.ttf dist/SpacetrisGame-Linux/
        echo '#!/bin/bash' > dist/SpacetrisGame-Linux/launch-spacetris.sh
        echo 'cd "$(dirname "$0")"' >> dist/SpacetrisGame-Linux/launch-spacetris.sh
        echo './spacetris' >> dist/SpacetrisGame-Linux/launch-spacetris.sh
        chmod +x dist/SpacetrisGame-Linux/launch-spacetris.sh
        chmod +x dist/SpacetrisGame-Linux/spacetris
    
    - name: Upload artifacts
      uses: actions/upload-artifact@v4
      with:
        name: spacetris-linux-x64
        path: dist/SpacetrisGame-Linux/
    
    - name: Create Release TAR
      if: startsWith(github.ref, 'refs/tags/v')
      run: |
        cd dist
        tar -czf ../SpacetrisGame-Linux-x64.tar.gz SpacetrisGame-Linux/
    
    - name: Release
      if: startsWith(github.ref, 'refs/tags/v')
      uses: softprops/action-gh-release@v1
      with:
        files: SpacetrisGame-Linux-x64.tar.gz
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
