name: CI

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

env:
  FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true

jobs:
  # ── 1. Code style ──────────────────────────────────────────────────────────
  fmt:
    name: Format check
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt
      - uses: Swatinem/rust-cache@v2.9.0
      - name: cargo fmt --check
        run: cargo fmt --check

  # ── 2. Static analysis / code review ───────────────────────────────────────
  clippy:
    name: Clippy (code review)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy
      - uses: Swatinem/rust-cache@v2.9.0
      - name: cargo clippy
        run: cargo clippy -- -D warnings

  # ── 3. Test suite ──────────────────────────────────────────────────────────
  test:
    name: Tests
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2.9.0
      - name: cargo test
        run: cargo test --all-features

  # ── 4. Regression detection ────────────────────────────────────────────────
  regression:
    name: Regression (test on all targets)
    runs-on: ubuntu-latest
    needs: [fmt, clippy, test]
    strategy:
      matrix:
        target:
          - x86_64-unknown-linux-gnu
          - x86_64-unknown-linux-musl
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}
      - uses: Swatinem/rust-cache@v2.9.0
        with:
          key: ${{ matrix.target }}
      - name: Install musl tools
        if: matrix.target == 'x86_64-unknown-linux-musl'
        run: sudo apt-get install -y musl-tools
      - name: cargo test (${{ matrix.target }})
        run: cargo test --target ${{ matrix.target }}
