name: Check

on:
  push:
    branches:
      - main
  pull_request:
    types: ["opened", "reopened", "synchronize", "ready_for_review"]

concurrency:
  # Allow only one workflow per any non-`main` branch.
  group: ${{ github.workflow }}-${{ github.ref_name }}-${{ github.ref_name == 'main' && github.sha || 'anysha' }}
  cancel-in-progress: true

env:
  CARGO_TERM_COLOR: always
  CARGO_INCREMENTAL: 0
  RUST_BACKTRACE: 1

jobs:
  check-backend:
    name: Run backend checks
    runs-on: ubuntu-24.04
    if: ${{ github.event_name == 'push' || !github.event.pull_request.draft }}
    steps:
      - name: Checkout repo
        uses: actions/checkout@v4

      - run: rustup default stable && rustup component add clippy && rustup component add rustfmt --toolchain nightly

      - name: Cache dependencies
        uses: swatinem/rust-cache@v2
        with:
          save-if: ${{ github.ref == 'refs/heads/main' }}

      - name: cargo fmt check
        run: cargo +nightly fmt --check

      - name: cargo clippy
        run: cargo clippy

      - name: cargo test
        run: cargo test
  check-frontend:
    name: Run frontend checks
    runs-on: ubuntu-24.04
    if: ${{ github.event_name == 'push' || !github.event.pull_request.draft }}
    defaults:
      run:
        working-directory: web
    steps:
      - name: Checkout repo
        uses: actions/checkout@v4
      - name: Setup bun
        uses: oven-sh/setup-bun@v2
        with:
          bun-version: latest
      - name: Install dependencies
        run: bun install --frozen-lockfile
      - name: Cache dependencies
        uses: actions/cache@v4
        with:
          path: ~/.bun/install/cache
          key: ${{ runner.os }}-bun-${{ hashFiles('docs/bun.lockb') }}
          restore-keys: |
            ${{ runner.os }}-bun-
      - name: Check Frontend
        run: bun run lint
