name: codecov

on:
  push:
    branches: [main, development]
    paths:
      - "**/*.py"
      - "tests/**"
      - ".github/workflows/codecov.yml"
  pull_request:
    branches: [main, development]
    paths:
      - "**/*.py"
      - "tests/**"
      - ".github/workflows/codecov.yml"

jobs:
  coverage:
    name: Code Coverage Report
    runs-on: ubuntu-latest
    env:
      FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true

    steps:
      - name: Checkout code
        uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
        with:
          fetch-depth: 0

      - name: Install uv
        uses: astral-sh/setup-uv@d4b2f3b6ecc6e67c4457f6d3e41ec42d3d0fcb86 # v5

      - name: Set up Python
        run: uv python install 3.12

      - name: Install dependencies
        run: uv sync --group dev

      - name: Run tests with coverage
        run: |
          uv run --with pytest-cov --with coverage \
            pytest tests/ \
              --cov=dracs \
              --cov-report=xml \
              --cov-report=html \
              --cov-report=term-missing

      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@75cd11691c0faa626561e295848008c8a7dddffe # v5
        with:
          files: ./coverage.xml
          flags: unittests
          name: codecov-umbrella
          fail_ci_if_error: true
          verbose: true
          token: ${{ secrets.CODECOV_TOKEN }}

      - name: Archive code coverage results
        uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
        with:
          name: code-coverage-report
          path: |
            coverage.xml
            htmlcov/
          retention-days: 30

      - name: Comment coverage on PR
        if: github.event_name == 'pull_request'
        uses: py-cov-action/python-coverage-comment-action@53ff7553078bad69030786e486677315346c2dd2 # v3
        with:
          GITHUB_TOKEN: ${{ github.token }}
          MINIMUM_GREEN: 80
          MINIMUM_ORANGE: 70
