name: Checks and tests

on:
  push:
    branches: main
  pull_request:
  workflow_dispatch:

env:
  CARGO_TERM_COLOR: always

jobs:
  lint:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v6
      - name: Setup environment
        uses: ./.github/actions/setup-env

      - name: Linting and formatting
        run: mise run precommit

  unit-tests:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v6
      - name: Setup environment
        uses: ./.github/actions/setup-env

      - name: Unit tests
        run: mise run test:unit

  wasm-tests:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v6
      - name: Setup environment
        uses: ./.github/actions/setup-env

      - name: Install wasm target
        run: rustup target add wasm32-unknown-unknown

      - name: WASM tests
        run: mise run test:wasm
        env:
          RUSTFLAGS: '--cfg getrandom_backend="wasm_js"'

  hyperfine-smoke:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v6
      - name: Setup environment
        uses: ./.github/actions/setup-env

      - name: Check single run for hyperfine
        run: mise run test:hyperfine

  examples:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v6
      - name: Setup environment
        uses: ./.github/actions/setup-env

      - name: Run examples
        run: mise run examples

  book-example:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v6
      - name: Setup environment
        uses: ./.github/actions/setup-env

      - name: Build book example
        run: mise run test:book

  docs:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v6
      - name: Setup environment
        uses: ./.github/actions/setup-env

      - name: Build docs
        run: mise run docs

  setup-new-project:
    uses: ./.github/workflows/test-setup-new-project.yaml

  all-green:
    runs-on: ubuntu-latest
    needs:
      - lint
      - unit-tests
      - wasm-tests
      - hyperfine-smoke
      - examples
      - book-example
      - docs
      - setup-new-project
    if: ${{ always() }}
    steps:
      - name: Verify all checks passed
        run: |
          set -euo pipefail

          results=(
            lint:${{ needs.lint.result }}
            unit-tests:${{ needs.unit-tests.result }}
            wasm-tests:${{ needs.wasm-tests.result }}
            hyperfine-smoke:${{ needs.hyperfine-smoke.result }}
            examples:${{ needs.examples.result }}
            book-example:${{ needs.book-example.result }}
            docs:${{ needs.docs.result }}
            setup-new-project:${{ needs.setup-new-project.result }}
          )

          failed=0
          for pair in "${results[@]}"; do
            job="${pair%%:*}"
            result="${pair#*:}"
            echo "${job}: ${result}"
            if [ "${result}" != "success" ]; then
              failed=1
            fi
          done

          if [ "${failed}" -ne 0 ]; then
            echo "One or more checks failed."
            exit 1
          fi
