name: PR Verification

on:
  push:
    branches: [ "main" ]
  pull_request:
    types: [opened, synchronize, reopened]

permissions:
  contents: read

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

env:
  CARGO_TERM_COLOR: always

jobs:
  build:
    name: build and test

    strategy:
      matrix:
        os: [windows-latest, macos-latest, ubuntu-latest]

    runs-on: ${{matrix.os}}

    steps:
      - name: Checkout repository
        uses: actions/checkout@v7

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

      - name: Run tests
        if: ${{ vars.ACTIONS_STEP_DEBUG != 'true' }}
        run: cargo test

      - name: Run tests (debug)
        if: ${{ vars.ACTIONS_STEP_DEBUG == 'true' }}
        run: cargo test --verbose

  quality:
    name: quality checks
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v7
        with:
          fetch-depth: 0

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

      - name: Install Tarpaulin
        uses: taiki-e/install-action@v2
        with:
          tool: cargo-tarpaulin

      - name: Rust version
        run: |
          rustup --version && cargo --version && cargo fmt --version && cargo clippy --version && \
            cargo tarpaulin --version

      - name: Lint and format
        run: |
          cargo fmt --check --verbose
          cargo clippy --all-targets --all-features -- -D warnings

      - name: Analyze code coverage
        run: cargo tarpaulin --out XML

      - name: Upload coverage to codecov.io
        uses: codecov/codecov-action@v7
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          fail_ci_if_error: true
          verbose: ${{ vars.ACTIONS_STEP_DEBUG == 'true' }}

      - name: Archive code coverage results
        uses: actions/upload-artifact@v7
        with:
          name: code-coverage-report
          path: target/tarpaulin/clio-auth-coverage.json
