name: Cross-platform Release

on:
  push:
    tags:
      - 'v*'

jobs:
  release:
    name: Build and Upload Binaries
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            bin_ext: ""

          - os: windows-latest
            target: x86_64-pc-windows-msvc # NB: on Windows two majors build-chains: x86_64-pc-windows-gnu & x86_64-pc-windows-msvc
            bin_ext: ".exe"  #On github action already windows-latest has Visual Studio Build Tools, so better windows version

          - os: macos-latest
            target: x86_64-apple-darwin
            bin_ext: ""

    steps:
      - name: Checkout
        uses: actions/checkout@v5

      - name: Set up Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          target: ${{ matrix.target }}

      - name: Build release binary
        run: cargo build --release --target=${{ matrix.target }}

      - name: Rename output binary (Unix)
        if: runner.os != 'Windows'
        shell: bash
        run: |
          mkdir -p dist
          cp target/${{ matrix.target }}/release/rsnake${{ matrix.bin_ext }} \
             dist/rsnake-${{ matrix.target }}${{ matrix.bin_ext }}

      - name: Rename output binary (Windows)
        if: runner.os == 'Windows'
        run: |
          mkdir dist
          copy target\${{ matrix.target }}\release\rsnake.exe dist\rsnake-${{ matrix.target }}.exe


      - name: Upload binaries to GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          files: dist/*
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
