name: Coverage

on:
  schedule:
    - cron: '17 3 */2 * *'  # a cada 2 dias às 03:17 UTC
  workflow_dispatch:         # permite rodar manualmente

env:
  CARGO_TERM_COLOR: always

jobs:
  coverage:
    name: Code Coverage
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6

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

      - name: Cache cargo registry
        uses: actions/cache@v5
        with:
          path: |
            ~/.cargo/registry/index
            ~/.cargo/registry/cache
            ~/.cargo/git/db
            target
          key: ${{ runner.os }}-cargo-coverage-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-cargo-coverage-
            ${{ runner.os }}-cargo-

      - name: Install system dependencies (OpenSSL)
        run: sudo apt-get update && sudo apt-get install -y libssl-dev pkg-config

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

      - name: Run tarpaulin
        run: cargo tarpaulin --workspace --timeout 300 --out xml --output-dir coverage

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