name: Release

on:
  push:
    tags:
      - "v*"

permissions:
  contents: write

env:
  CARGO_TERM_COLOR: always

jobs:
  ci:
    name: CI
    uses: ./.github/workflows/ci.yml

  build:
    name: Build CLI (${{ matrix.name }})
    needs: ci
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            name: linux-amd64
            artifact: oramacore_fields-linux-amd64
          - os: macos-latest
            target: aarch64-apple-darwin
            name: darwin-arm64
            artifact: oramacore_fields-darwin-arm64
    steps:
      - uses: actions/checkout@v4
      - uses: Swatinem/rust-cache@v2
        with:
          key: ${{ matrix.target }}
      - name: Build release binary
        run: cargo build --release --features cli --target ${{ matrix.target }}
      - name: Prepare artifact
        run: |
          cp target/${{ matrix.target }}/release/oramacore_fields ${{ matrix.artifact }}
          chmod +x ${{ matrix.artifact }}
      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.artifact }}
          path: ${{ matrix.artifact }}

  release:
    name: Create Release
    needs: build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          path: artifacts
      - name: Create GitHub Release
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          gh release create "${{ github.ref_name }}" \
            --title "${{ github.ref_name }}" \
            --generate-notes \
            artifacts/oramacore_fields-linux-amd64/oramacore_fields-linux-amd64 \
            artifacts/oramacore_fields-darwin-arm64/oramacore_fields-darwin-arm64
