name: CI

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

env:
  CARGO_TERM_COLOR: always
  RUST_BACKTRACE: 1

jobs:
  test:
    name: Test
    runs-on: blacksmith-2vcpu-ubuntu-2404
    steps:
      - uses: actions/checkout@v6

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

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

      - name: Build
        run: cargo build --features "bytecode,taylor,laurent,stde,serde,faer,nalgebra,ndarray,parallel,diffop"

      - name: Run tests (echidna with all features)
        run: cargo test -p echidna --features "bytecode,taylor,laurent,stde,serde,faer,nalgebra,ndarray,parallel,diffop"

      - name: Run tests (echidna-optim)
        run: cargo test -p echidna-optim

      - name: Run tests (echidna-optim with sparse-implicit)
        run: cargo test -p echidna-optim --features sparse-implicit

  lint:
    name: Lint
    runs-on: blacksmith-2vcpu-ubuntu-2404
    steps:
      - uses: actions/checkout@v6

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

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

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

      - name: Clippy (all non-CUDA features)
        run: cargo clippy --all-targets --features "bytecode,taylor,laurent,stde,serde,faer,nalgebra,ndarray,parallel,diffop,gpu-wgpu" -- -D warnings

      - name: Clippy (echidna-optim)
        run: cargo clippy --all-targets -p echidna-optim --all-features -- -D warnings


  docs:
    name: Documentation
    runs-on: blacksmith-2vcpu-ubuntu-2404
    steps:
      - uses: actions/checkout@v6

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

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

      - name: Build docs (echidna)
        run: cargo doc --no-deps --features "bytecode,taylor,laurent,stde,serde,faer,nalgebra,ndarray"
        env:
          RUSTDOCFLAGS: -D warnings

      - name: Build docs (echidna-optim)
        run: cargo doc -p echidna-optim --no-deps --all-features
        env:
          RUSTDOCFLAGS: -D warnings

  msrv:
    name: MSRV (1.93)
    runs-on: blacksmith-2vcpu-ubuntu-2404
    steps:
      - uses: actions/checkout@v6

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

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

      - name: Build (default features)
        run: cargo build

      - name: Test (default features)
        run: cargo test

      - name: Build (bytecode)
        run: cargo build --features bytecode

      - name: Test (bytecode)
        run: cargo test --features bytecode

      - name: Build (taylor)
        run: cargo build --features taylor

      - name: Test (taylor)
        run: cargo test --features taylor

      - name: Build (stde)
        run: cargo build --features stde

      - name: Test (stde)
        run: cargo test --features stde

      - name: Build (bytecode,taylor)
        run: cargo build --features bytecode,taylor

      - name: Test (bytecode,taylor)
        run: cargo test --features bytecode,taylor

      - name: Build (bytecode,stde)
        run: cargo build --features bytecode,stde

      - name: Test (bytecode,stde)
        run: cargo test --features bytecode,stde

      - name: Build (taylor,stde)
        run: cargo build --features taylor,stde

      - name: Test (taylor,stde)
        run: cargo test --features taylor,stde

      - name: Build (bytecode,taylor,stde)
        run: cargo build --features bytecode,taylor,stde

      - name: Test (bytecode,taylor,stde)
        run: cargo test --features bytecode,taylor,stde

  coverage:
    name: Coverage
    runs-on: blacksmith-2vcpu-ubuntu-2404
    steps:
      - uses: actions/checkout@v6

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

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

      - name: Install cargo-tarpaulin
        run: cargo install cargo-tarpaulin

      - name: Generate coverage
        run: cargo tarpaulin --out xml --timeout 300 --features "bytecode,taylor,laurent,stde,serde,faer,nalgebra,ndarray,parallel"
        continue-on-error: true

      - name: Upload coverage
        uses: codecov/codecov-action@v6
        with:
          files: cobertura.xml
          token: ${{ secrets.CODECOV_TOKEN }}
          fail_ci_if_error: false

  security:
    name: Security Audit
    runs-on: blacksmith-2vcpu-ubuntu-2404
    steps:
      - uses: actions/checkout@v6

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

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

      - name: Install cargo-audit
        run: cargo install cargo-audit --locked

      - name: Audit dependencies
        run: cargo audit

      - name: Install cargo-deny
        run: cargo install cargo-deny --locked

      - name: Check dependency policies
        run: cargo deny check

