name: Coverage
# based on: https://github.com/marketplace/actions/code-coverage-summary

on:
  push:
    branches: [ "master" ]
    paths: [
      ".github/workflows/check-cov.yml",
      "pyproject.toml",
      "requirements/**",
      "src/**.py",
      "tests/**",
    ]

  pull_request:
    branches: [ "master" ]
    paths: [
      ".github/workflows/check-cov.yml",
      "pyproject.toml",
      "requirements/**",
      "src/**.py",
      "tests/**",
    ]

  pull_request_target:
    branches: [ "master" ]
    paths: [
      ".github/workflows/check-cov.yml",
      "pyproject.toml",
      "requirements/**",
      "src/**.py",
      "tests/**",
    ]

  schedule:
    - cron: "0 7 * * 5"

  workflow_dispatch:

permissions:
  pull-requests: write

jobs:
  coverage:
    runs-on: ubuntu-latest

    strategy:
      fail-fast: false
      matrix:
        python-version: ["3.14"]

    steps:
      - uses: actions/checkout@v6

      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v6
        with:
          python-version: ${{ matrix.python-version }}

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements/requirements_dev.txt

      - name: Install the package (and its dependencies)
        run: pip install -e .

      - name: Run coverage with pytest
        run: coverage run -m pytest

      - name: Debug - Check if .coverage exists
        run: test -f .coverage && echo ".coverage exists" || echo ".coverage does NOT exist"

      - name: Display coverage report
        run: coverage report

      - name: Create coverage xml
        run: coverage xml

      - name: Create Code Coverage Report
        # from: https://github.com/marketplace/actions/code-coverage-summary
        uses: irongut/CodeCoverageSummary@v1.3.0
        with:
          filename: coverage.xml
          badge: true
          fail_below_min: true
          format: markdown
          hide_branch_rate: false
          hide_complexity: true
          indicators: true
          output: both
          thresholds: '80 95'

      - name: Add Coverage PR Comment
        # from: https://github.com/marketplace/actions/comment-pull-request
        if: github.event_name == 'pull_request_target'
        uses: thollander/actions-comment-pull-request@v3
        with:
          file-path: code-coverage-results.md
          comment-tag: code-coverage-results.md  # replaces existing by this name
          mode: recreate
          github-token: ${{ secrets.GITHUB_TOKEN }}
        # or: https://github.com/marketplace/actions/sticky-pull-request-comment
        #        if: github.event_name == 'pull_request'
        #        with:
        #          recreate: true
        #          path: code-coverage-results.md
        # GITHUB_TOKEN is implicitly passed in

#      - name: Create Coverage Badge
#        # from: https://github.com/marketplace/actions/coverage-py-badge
#        uses: tj-actions/coverage-badge-py@v2
#        id: coverage-badge-py
#        with:
#            # Output path to write the
#            # coverage badge.
#            # Type: string
#            # Default: "coverage.svg"
#            output: 'misc/coverage-badge.svg'
#
#            # Overwrite an existing coverage badge.
#            # Type: boolean
#            # Default: "true"
#            overwrite: ''
#
#            # Current working directory
#            # Type: string
#            # Default: "."
#            working-directory: ''

      - run: echo "🍏 This job's status is ${{ job.status }}."
