name: Release CLI Tool

on:
  push:
    tags:
      - 'v*' # Trigger a release on any tag starting with 'v'

jobs:
  build:
    name: Build Binary
    if: startsWith(github.ref, 'refs/tags/')
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest]
        include:
          - os: ubuntu-latest
            artifact_name: bee-linux
            binary_name: bee_hive
            target: x86_64-unknown-linux-gnu
          - os: windows-latest
            artifact_name: bee-windows.exe
            binary_name: bee_hive.exe
            target: x86_64-pc-windows-msvc

    steps:
    - uses: actions/checkout@v4

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

    - name: Build release binary
      run: |
        cargo build --release --target ${{ matrix.target }}
        # Copy binary to dist directory with proper name
        mkdir -p dist
        cp target/${{ matrix.target }}/release/${{ matrix.binary_name }} dist/${{ matrix.artifact_name }}

    - name: Upload build artifact
      uses: actions/upload-artifact@v4
      with:
        name: ${{ matrix.artifact_name }}
        path: dist/${{ matrix.artifact_name }}

  release:
    name: Create Release
    if: startsWith(github.ref, 'refs/tags/')
    runs-on: ubuntu-latest
    needs: [build]  # Wait for all build jobs to complete
    permissions:
      contents: write  # Required for creating releases

    steps:
    - name: Download all build artifacts
      uses: actions/download-artifact@v4
      with:
        path: dist/

    - name: List downloaded artifacts
      run: ls -la dist/

    - name: Create GitHub Release
      uses: softprops/action-gh-release@v1
      with:
        tag_name: ${{ github.ref_name }}
        name: Release ${{ github.ref_name }}
        generate_release_notes: true
        files: |
          dist/**/*