name: Release

on:
  push:
    tags:
      - 'v*'

jobs:
  build:
    name: Build ${{ matrix.target }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
          - os: macos-15-intel
            target: x86_64-apple-darwin
          - os: macos-14
            target: aarch64-apple-darwin
    steps:
      - uses: actions/checkout@v4

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

      - name: Run sccache
        uses: mozilla-actions/sccache-action@v0.0.6

      - name: Build release binaries
        run: RUSTC_WRAPPER=sccache cargo build --release --bins --target "${{ matrix.target }}"

      - name: Stage release assets
        shell: bash
        run: |
          mkdir -p dist
          for bin in kg kg-mcp kg-tui; do
            cp "target/${{ matrix.target }}/release/$bin" "dist/$bin-${{ matrix.target }}"
          done

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

  release:
    needs: build
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: Download release assets
        uses: actions/download-artifact@v4
        with:
          pattern: release-*
          merge-multiple: true
          path: dist

      - name: Create checksums
        run: |
          sha256sum dist/* > checksums.txt

      - name: Create Release
        uses: softprops/action-gh-release@v2
        with:
          generate_release_notes: false
          body: |
            Release ${{ github.ref_name }}
          files: |
            dist/*
            checksums.txt
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
