name: CI

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]
  release:
    types: [published]

env:
  CARGO_TERM_COLOR: always
  CARGO_INCREMENTAL: 0

jobs:
  # ──────── Format & Lint ────────
  check:
    name: Check (clippy)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install Rust toolchain
        run: |
          rustup set profile minimal
          rustup toolchain install stable --component rustfmt --component clippy
          rustup default stable
          sudo apt update
          sudo apt install -y protobuf-compiler

      - name: Cache dependencies
        uses: Swatinem/rust-cache@v2

      - name: cargo clippy
        run: cargo clippy --all-targets --all-features -- -D warnings

  # ──────── Test ────────
  test:
    name: Test
    runs-on: ubuntu-latest
    needs: check
    strategy:
      matrix:
        features:
          - --features "full"
          - --features "memory,macro"
          - --features "memory,macro,persistent"
          - --features "memory,macro,persistent,audit"
      fail-fast: false
    steps:
      - uses: actions/checkout@v4

      - name: Install Rust toolchain
        run: |
          rustup set profile minimal
          rustup toolchain install stable
          rustup default stable
          sudo apt update
          sudo apt install -y protobuf-compiler

      - name: Cache dependencies
        uses: Swatinem/rust-cache@v2

      - name: cargo test (${{ matrix.features }})
        run: cargo test ${{ matrix.features }} --workspace -- --nocapture

  # ──────── Benchmark ────────
  bench:
    name: Benchmark
    runs-on: ubuntu-latest
    needs: check
    steps:
      - uses: actions/checkout@v4

      - name: Install Rust toolchain
        run: |
          rustup set profile minimal
          rustup toolchain install stable
          rustup default stable
          sudo apt update
          sudo apt install -y protobuf-compiler


      - name: Cache dependencies
        uses: Swatinem/rust-cache@v2


      - name: cargo bench
        run: cargo bench --all-features --workspace

      - name: Commit benchmark report
        if: github.event_name == 'release'
        run: |
          rm -rf docs/benchmark/
          cp -r target/criterion/ docs/benchmark/
          git config user.name "github-actions[bot]"
          git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git add docs/benchmark/
          if git diff --cached --quiet; then
            echo "No benchmark changes to commit"
          else
            git commit -m "chore: update benchmark report for ${{ github.ref_name }}"
            git push
          fi

  # ──────── Publish to crates.io ────────
  publish:
    name: Publish
    runs-on: ubuntu-latest
    needs: [check, test, bench]
    if: github.event_name == 'release'
    steps:
      - uses: actions/checkout@v4

      - name: Install Rust toolchain
        run: |
          rustup set profile minimal
          rustup toolchain install stable
          rustup default stable
          sudo apt update
          sudo apt install -y protobuf-compiler

      - name: Cache dependencies
        uses: Swatinem/rust-cache@v2

      - uses: cargo-bins/cargo-binstall@main

      - name: Install cargo-release
        run: cargo binstall cargo-release -y

      - name: cargo login
        run: cargo login "${{ secrets.CARGO_REGISTRY_TOKEN }}"

      - name: Publish all crates
        run: cargo release publish --workspace --no-verify --execute
