name: Release

on:
  push:
    tags:
      - "v[0-9]+.[0-9]+.[0-9]+*"

env:
  CARGO_TERM_COLOR: always

jobs:
  build-binaries:
    name: Build binaries (${{ matrix.os }})
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            artifact: steam-controller-dsu
          # - os: windows-latest
          #   target: x86_64-pc-windows-msvc
          #   artifact: steam-controller-dsu.exe
    steps:
      - name: Checkout
        uses: actions/checkout@v6

      - name: Install Rust toolchain
        uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          target: ${{ matrix.target }}

      - name: Install native dependencies
        if: runner.os == 'Linux'
        run: sudo apt-get update && sudo apt-get install -y libudev-dev

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

      - name: Upload binary artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.artifact }}-${{ matrix.target }}
          path: target/${{ matrix.target }}/release/${{ matrix.artifact }}

  publish-crate:
    name: Publish to crates.io
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v6

      - name: Install Rust toolchain
        uses: actions-rust-lang/setup-rust-toolchain@v1

      - name: Install native dependencies
        if: runner.os == 'Linux'
        run: sudo apt-get update && sudo apt-get install -y libudev-dev

      - name: Publish scdsu-core
        run: cargo publish -p scdsu-core --token ${{ secrets.CARGO_REGISTRY_TOKEN }}

      - name: Publish scdsu-cli
        run: cargo publish -p scdsu-cli --token ${{ secrets.CARGO_REGISTRY_TOKEN }}

      - name: Publish steam-controller-dsu
        run: cargo publish -p steam-controller-dsu --token ${{ secrets.CARGO_REGISTRY_TOKEN }}

  create-release:
    name: Create GitHub Release
    runs-on: ubuntu-latest
    needs:
      - publish-crate
      - build-binaries
    steps:
      - name: Checkout
        uses: actions/checkout@v6

      - name: Download binary artifacts
        uses: actions/download-artifact@v4
        with:
          path: artifacts
          pattern: "*-*-*"

      - name: Create release
        uses: softprops/action-gh-release@v3
        with:
          generate_release_notes: true
          files: |
            artifacts/**/steam-controller-dsu-*
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
