name: Build and Release

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

jobs:
  build:
    name: Build for ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - os: ubuntu-latest
            goos: linux
            goarch: amd64
            binary_suffix: ""
            archive_type: tar.gz
          - os: windows-latest
            goos: windows
            goarch: amd64
            binary_suffix: ".exe"
            archive_type: zip
          - os: macos-latest
            goos: darwin
            goarch: amd64
            binary_suffix: ""
            archive_type: tar.gz
          - os: macos-latest
            goos: darwin
            goarch: arm64
            binary_suffix: ""
            archive_type: tar.gz

    steps:
    - uses: actions/checkout@v4

    - name: Set up Go
      uses: actions/setup-go@v4
      with:
        go-version: '1.21'

    - name: Install dependencies (Linux)
      if: matrix.os == 'ubuntu-latest'
      run: |
        sudo apt-get update
        sudo apt-get install -y libc6-dev libglu1-mesa-dev libgl1-mesa-dev libxcursor-dev libxi-dev libxinerama-dev libxrandr-dev libxxf86vm-dev libasound2-dev pkg-config

    - name: Download dependencies
      run: go mod download

    - name: Build
      env:
        GOOS: ${{ matrix.goos }}
        GOARCH: ${{ matrix.goarch }}
      run: |
        mkdir -p build
        go build -ldflags "-s -w" -o build/tetris${{ matrix.binary_suffix }} .

    - name: Create archive (tar.gz)
      if: matrix.archive_type == 'tar.gz'
      run: |
        cd build
        tar -czf tetris-${{ matrix.goos }}-${{ matrix.goarch }}.tar.gz tetris${{ matrix.binary_suffix }}

    - name: Create archive (zip)
      if: matrix.archive_type == 'zip'
      run: |
        cd build
        powershell Compress-Archive -Path tetris${{ matrix.binary_suffix }} -DestinationPath tetris-${{ matrix.goos }}-${{ matrix.goarch }}.zip

    - name: Upload build artifacts
      uses: actions/upload-artifact@v4
      with:
        name: tetris-${{ matrix.goos }}-${{ matrix.goarch }}
        path: build/tetris-${{ matrix.goos }}-${{ matrix.goarch }}.*

  test:
    name: Test
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4

    - name: Set up Go
      uses: actions/setup-go@v4
      with:
        go-version: '1.21'

    - name: Install dependencies
      run: |
        sudo apt-get update
        sudo apt-get install -y libc6-dev libglu1-mesa-dev libgl1-mesa-dev libxcursor-dev libxi-dev libxinerama-dev libxrandr-dev libxxf86vm-dev libasound2-dev pkg-config

    - name: Download dependencies
      run: go mod download

    - name: Run tests
      run: go test -v ./...

    - name: Run go vet
      run: go vet ./...

    - name: Check formatting
      run: |
        if [ "$(gofmt -s -l . | wc -l)" -gt 0 ]; then
          echo "The following files are not formatted:"
          gofmt -s -l .
          exit 1
        fi

  release:
    name: Create Release
    needs: [build, test]
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/v')
    
    steps:
    - name: Download all artifacts
      uses: actions/download-artifact@v4

    - name: Create Release
      id: create_release
      uses: actions/create-release@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        tag_name: ${{ github.ref }}
        release_name: Tetris ${{ github.ref }}
        draft: false
        prerelease: false

    - name: Upload Linux Release Asset
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ steps.create_release.outputs.upload_url }}
        asset_path: ./tetris-linux-amd64/tetris-linux-amd64.tar.gz
        asset_name: tetris-linux-amd64.tar.gz
        asset_content_type: application/gzip

    - name: Upload Windows Release Asset
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ steps.create_release.outputs.upload_url }}
        asset_path: ./tetris-windows-amd64/tetris-windows-amd64.zip
        asset_name: tetris-windows-amd64.zip
        asset_content_type: application/zip

    - name: Upload macOS Intel Release Asset
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ steps.create_release.outputs.upload_url }}
        asset_path: ./tetris-darwin-amd64/tetris-darwin-amd64.tar.gz
        asset_name: tetris-darwin-amd64.tar.gz
        asset_content_type: application/gzip

    - name: Upload macOS ARM Release Asset
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ steps.create_release.outputs.upload_url }}
        asset_path: ./tetris-darwin-arm64/tetris-darwin-arm64.tar.gz
        asset_name: tetris-darwin-arm64.tar.gz
        asset_content_type: application/gzip 