name: Release

on:
  push:
    tags:
      - "v*"

permissions:
  contents: write

jobs:
  build:
    strategy:
      matrix:
        include:
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
          - target: x86_64-apple-darwin
            os: macos-latest
          - target: aarch64-apple-darwin
            os: macos-latest

    runs-on: ${{ matrix.os }}

    steps:
      - uses: actions/checkout@v4

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

      - name: Build release binaries
        run: cargo build --workspace --release --target ${{ matrix.target }}

      - name: Package binaries
        run: |
          cd target/${{ matrix.target }}/release
          # Package all available binaries (bsv-wallet-mcp may not build on all targets)
          BINS="bsv-wallet"
          if [ -f bsv-wallet-mcp ]; then BINS="$BINS bsv-wallet-mcp"; fi
          tar czf ../../../bsv-wallet-${{ matrix.target }}.tar.gz $BINS
          cd ../../..

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: bsv-wallet-${{ matrix.target }}
          path: bsv-wallet-${{ matrix.target }}.tar.gz

  release:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          merge-multiple: true

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          name: ${{ github.ref_name }}
          generate_release_notes: true
          files: bsv-wallet-*.tar.gz
