name: CI

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

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

env:
  CARGO_TERM_COLOR: always
  RUST_BACKTRACE: 1
  FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true

jobs:
  # ── Fast lint gate (runs in ~15s) ────────────────────────────────────────────
  lint:
    name: Lint (fmt + clippy)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

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

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

      - name: Check formatting
        run: cargo fmt --check

      - name: Clippy (all targets)
        run: cargo clippy --all-targets -- -D warnings

  # ── Tests (run in parallel with lint) ────────────────────────────────────────
  test:
    name: Test (${{ matrix.rust }})
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        rust: [stable, beta]

    steps:
      - uses: actions/checkout@v4

      - name: Install Rust (${{ matrix.rust }})
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: ${{ matrix.rust }}

      - name: Cache Rust dependencies
        uses: Swatinem/rust-cache@v2
        with:
          key: ${{ matrix.rust }}

      - name: Install cargo-nextest
        uses: taiki-e/install-action@cargo-nextest

      - name: Test (default features)
        run: cargo nextest run --workspace

      - name: Build (no_std check — no default features)
        run: cargo build --no-default-features

  # ── MSRV ─────────────────────────────────────────────────────────────────────
  msrv:
    name: MSRV (1.88)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install Rust 1.88
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: "1.88"

      - name: Cache Rust dependencies
        uses: Swatinem/rust-cache@v2
        with:
          key: msrv-1.88

      - name: Build
        run: cargo build

  # ── Licenses & audit ─────────────────────────────────────────────────────────
  deny:
    name: Licenses & advisories (cargo deny)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Install cargo-deny
        uses: EmbarkStudios/cargo-deny-action@v2

  audit:
    name: Security audit (cargo audit)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install cargo-audit
        uses: taiki-e/install-action@cargo-audit

      - name: Audit dependencies
        run: cargo audit
