name: Release

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

env:
  CARGO_TERM_COLOR: always

permissions:
  contents: write  # required to create GitHub Releases and upload assets

jobs:
  # ── 1. Validate the tag matches Cargo.toml ─────────────────────────────────
  check-version:
    name: Verify Cargo.toml version matches tag
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Extract versions
        run: |
          TAG="${GITHUB_REF_NAME}"               # e.g. v1.2.3
          CARGO_VER="v$(cargo metadata --no-deps --format-version 1 \
            | jq -r '.packages[0].version')"
          echo "Tag:   $TAG"
          echo "Cargo: $CARGO_VER"
          if [ "$TAG" != "$CARGO_VER" ]; then
            echo "::error::Tag $TAG does not match Cargo.toml version $CARGO_VER"
            exit 1
          fi

  # ── 2. Run the full test suite ─────────────────────────────────────────────
  test:
    name: Tests
    needs: check-version
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - name: Run tests
        run: cargo test --all-features

  # ── 3. Build release binaries for every platform ───────────────────────────
  build-release:
    name: Build — ${{ matrix.target }}
    needs: test
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            binary: pman
            asset_name: pman-linux-x86_64
          - os: macos-latest
            target: x86_64-apple-darwin
            binary: pman
            asset_name: pman-macos-x86_64
          - os: macos-latest
            target: aarch64-apple-darwin
            binary: pman
            asset_name: pman-macos-aarch64
          - os: windows-latest
            target: x86_64-pc-windows-msvc
            binary: pman.exe
            asset_name: pman-windows-x86_64.exe

    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}
      - uses: Swatinem/rust-cache@v2
        with:
          key: ${{ matrix.target }}
      - name: Build
        run: cargo build --release --target ${{ matrix.target }}
      - name: Rename binary
        shell: bash
        run: |
          cp target/${{ matrix.target }}/release/${{ matrix.binary }} \
             ${{ matrix.asset_name }}
      - name: Upload binary artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.asset_name }}
          path: ${{ matrix.asset_name }}
          if-no-files-found: error

  # ── 4. Create the GitHub Release and attach all binaries ───────────────────
  create-release:
    name: Create GitHub Release
    needs: build-release
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Download all binaries
        uses: actions/download-artifact@v4
        with:
          path: release-assets
          merge-multiple: true
      - name: Create release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: ${{ github.ref_name }}
          name: "pman ${{ github.ref_name }}"
          generate_release_notes: true
          files: release-assets/*

