name: CI

on:
  push:
    branches: [main, develop, release/*]
  pull_request:
    branches: [main, develop, release/*]

env:
  CARGO_TERM_COLOR: always

jobs:
  test:
    name: Test
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest]
        rust: [stable, beta]
    steps:
      - uses: actions/checkout@v5
      - uses: dtolnay/rust-toolchain@master
        with:
          toolchain: ${{ matrix.rust }}
      - uses: Swatinem/rust-cache@v2
      - run: cargo test --all-features -- --test-threads=1

  fmt:
    name: Rustfmt
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt
      - run: cargo fmt --all -- --check

  clippy:
    name: Clippy
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy
      - uses: Swatinem/rust-cache@v2
      - run: cargo clippy --all-targets --all-features -- -D warnings

  security:
    name: Security Audit
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5
      - run: cargo generate-lockfile
      - uses: rustsec/audit-check@v2.0.0
        with:
          token: ${{ secrets.GITHUB_TOKEN }}

  coverage:
    name: Code Coverage
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: llvm-tools-preview
      - uses: Swatinem/rust-cache@v2
      - uses: taiki-e/install-action@cargo-llvm-cov

      - name: Generate coverage report
        run: |
          cargo llvm-cov --all-features --workspace --lcov --output-path lcov.info -- --test-threads=1
          echo "Coverage file generated:"
          ls -la lcov.info
          echo "File size: $(wc -c < lcov.info) bytes"

      - name: Upload to Codecov
        uses: codecov/codecov-action@v5
        with:
          files: ./lcov.info
          flags: unittests
          name: coverage-${{ github.ref_name }}
          token: ${{ secrets.CODECOV_TOKEN }}
          fail_ci_if_error: ${{ github.event_name == 'push' }}
          verbose: true
