name: Code Coverage

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

env:
  CARGO_TERM_COLOR: always

jobs:
  coverage:
    runs-on: ubuntu-latest
    
    steps:
    - uses: actions/checkout@v5
      with:
        submodules: recursive
    
    - name: Install LLVM and Clang
      run: |
        sudo apt-get update
        sudo apt-get install -y llvm clang
    
    - name: Install Rust
      uses: dtolnay/rust-toolchain@stable
      with:
        components: llvm-tools-preview
    
    - name: Install cargo-llvm-cov
      uses: taiki-e/install-action@cargo-llvm-cov
    
    - name: Generate code coverage
      run: |
        export CC=clang
        export CXX=clang++
        export LLVM_PROFILE_FILE="pxhist-%p-%m.profraw"
        cargo llvm-cov test --all-features --workspace --include-ffi --lcov --output-path lcov.info
    
    - name: Upload coverage to Codecov
      uses: codecov/codecov-action@v5
      with:
        files: ./lcov.info
        fail_ci_if_error: false
        verbose: true
        token: ${{ secrets.CODECOV_TOKEN }}
    
    - name: Archive code coverage results
      uses: actions/upload-artifact@v4
      with:
        name: code-coverage-report
        path: lcov.info
        retention-days: 30