name: Affn CI

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

permissions:
  contents: read
  checks: write
  pull-requests: write   # needed to post PR comments

concurrency:
  group: coverage-pr-${{ github.event.pull_request.number }}
  cancel-in-progress: true

env:
  CARGO_TERM_COLOR: always

jobs:
  check:
    name: Check
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - run: cargo check --all-targets --all-features

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

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

  test:
    name: Test
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - run: cargo test --all-targets --all-features
      - run: cargo test --doc --all-features

  coverage:
    name: Test & Coverage
    if: ${{ github.event_name == 'push' || github.event.pull_request.draft == false }}
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Set up Rust (nightly + llvm-tools)
        uses: dtolnay/rust-toolchain@v1
        with:
          toolchain: nightly
          components: llvm-tools-preview

      - name: Install cargo-llvm-cov
        uses: taiki-e/install-action@v2
        with:
          tool: cargo-llvm-cov

      # Run tests once and collect coverage data (no report yet)
      - name: Coverage (run, no report)
        run: cargo llvm-cov --workspace --all-features --doctests --no-report

      # Produce machine-readable + human-readable reports
      - name: Coverage (Cobertura XML)
        run: cargo llvm-cov report --cobertura --output-path coverage.xml

      - name: Coverage (HTML)
        run: cargo llvm-cov report --html --output-dir coverage_html

      # Put a nice table + badge into the PR Checks "Summary" tab
      - name: Build coverage summary (Markdown)
        uses: irongut/CodeCoverageSummary@v1.3.0
        with:
          filename: coverage.xml
          badge: true
          format: markdown
          output: file

      - name: Publish to Job Summary
        run: cat code-coverage-results.md >> "$GITHUB_STEP_SUMMARY"

      - name: Post coverage as PR comment
        if: github.event_name == 'pull_request'
        uses: mshick/add-pr-comment@v2
        with:
          message-path: code-coverage-results.md
          repo-token: ${{ secrets.GITHUB_TOKEN }}

      # Gate: fail if total LINE coverage < 85% (no extra test run needed)
      - name: Coverage gate (≥85% lines)
        run: cargo llvm-cov --workspace --all-features --doctests --no-run --fail-under-lines 85

      - name: Upload coverage HTML
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: coverage-html
          path: coverage_html
          retention-days: 7
