name: release

on:
  push:
    tags:
      - "v*"

env:
  CARGO_TERM_COLOR: always

jobs:
  all-go:
    name: all systems go
    runs-on: ubuntu-latest
    needs:
      - test
      - cover
      - build
      - release
    steps:
      - run: exit 0

  test:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macOS-latest, windows-latest]
        architecture: [x64, x86]
        toolchain: [stable]
    steps:
      - uses: actions/checkout@v5

      - name: Run Tests
        run: cargo test --verbose -- --nocapture

  cover:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5

      - uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          override: true
          components: llvm-tools-preview

      - name: Install cargo-llvm-cov
        uses: taiki-e/install-action@v2
        with:
          tool: cargo-llvm-cov

      - name: Generate Code Coverage Report
        run: cargo llvm-cov --all-features --workspace --fail-under-functions 80 --fail-under-lines 80 --fail-under-regions 80 --lcov --output-path ./target/lcov.info

      - name: Publish Coverage Report
        uses: codecov/codecov-action@v5
        env:
          CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
        with:
          files: ./target/lcov.info
          verbose: true
          fail_ci_if_error: true

  build:
    needs:
      - test
      - cover
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macOS-latest, windows-latest]
        architecture: [x64, x86]
        toolchain: [stable]
    steps:
      - uses: actions/checkout@v5

      - name: Build
        run: cargo build --release --verbose

      - name: Upload Artifacts
        uses: actions/upload-artifact@v5
        with:
          name: ${{ matrix.os }}-${{ matrix.architecture }}
          path: target/release/

  release:
    needs:
      - test
      - cover
      - build
    runs-on: ubuntu-latest
    strategy:
      matrix:
        os: [ubuntu-latest, macOS-latest, windows-latest]
        architecture: [x64, x86]
        toolchain: [stable]
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v5

      - name: Download Artifacts
        uses: actions/download-artifact@v6

      - name: Set Build File Variables
        id: vars
        run: |
          name=$(awk -F '= ' '/^\[package\]/{found=1} found && /^name/{gsub(/["]/, "", $2); print $2; exit}' Cargo.toml)
          echo "name=$name" >> $GITHUB_OUTPUT
          os="${{ matrix.os }}"
          os_sans_latest=$(echo "$os" | sed 's/-latest//')
          echo "os=$os_sans_latest" >> $GITHUB_OUTPUT
          if [[ "${{ matrix.os }}" == "windows-latest" ]]; then
            echo "ext=.exe" >> $GITHUB_OUTPUT
          else
            echo "ext=" >> $GITHUB_OUTPUT
          fi

      - name: Prepare Artifacts
        run: |
          source=${{ matrix.os }}-${{ matrix.architecture }}/${{ steps.vars.outputs.name }}${{ steps.vars.outputs.ext }}
          binary=paq${{ steps.vars.outputs.ext }}
          archive=${{ steps.vars.outputs.name }}-${{ steps.vars.outputs.os }}-${{ matrix.architecture }}.zip
          mv $source $binary
          zip $archive $binary

      - name: Create Release
        uses: softprops/action-gh-release@v2
        if: startsWith(github.ref, 'refs/tags/')
        with:
          draft: false
          prerelease: false
          token: ${{ secrets.GITHUB_TOKEN }}
          files: |
            ${{ steps.vars.outputs.name }}-${{ steps.vars.outputs.os }}-${{ matrix.architecture }}.zip
