name: CI

on:
  push:
    branches: [develop]
  pull_request:
    branches: [main, develop]

permissions:
  contents: write

env:
  CARGO_TERM_COLOR: always
  RUSTFLAGS: "-D warnings"

jobs:
  fmt:
    name: Format
    runs-on: ubuntu-latest
    if: github.event_name == 'push'
    steps:
      # fetch-depth: 0 so the Vibe Index step below sees the full git history;
      # a shallow clone would understate the score.
      - uses: actions/checkout@v5
        with:
          fetch-depth: 0

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt

      # Stage any formatting changes, but don't commit here — the Vibe Index
      # step below does a single commit + push for everything (cargo fmt + the
      # refreshed badge), so there's exactly one auto-commit.
      - name: cargo fmt
        run: |
          cargo fmt --all
          git add -A

      # Refresh the badge and commit & push it together with the staged fmt
      # changes in one go (the action's commit picks up everything staged).
      # [skip ci] keeps the push from retriggering the pipeline.
      - name: Auto-format & Vibe Index badge
        uses: roxblnfk/action-vibe-index@v1
        with:
          update-files: README.md
          commit: true
          push: true
          commit-message: "chore: auto-format & update Vibe Index badge [skip ci]"

  check:
    name: Check + Lint
    runs-on: ubuntu-latest
    needs: fmt
    if: always() && !failure()
    steps:
      - uses: actions/checkout@v5
        with:
          ref: ${{ github.ref }}

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy

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

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

      - name: cargo check
        run: cargo check --workspace --all-targets

  test:
    name: Test
    runs-on: ubuntu-latest
    needs: check
    steps:
      - uses: actions/checkout@v5

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable

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

      - name: cargo test
        run: cargo test --workspace --all-targets

  doc:
    name: Docs
    runs-on: ubuntu-latest
    needs: test
    steps:
      - uses: actions/checkout@v5

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable

      - name: cargo doc
        run: cargo doc --workspace --no-deps
        env:
          RUSTDOCFLAGS: "-D warnings"
