name: CI

on:
  push:
    branches:
    - master
  pull_request:
  workflow_dispatch:

jobs:
  tests:
    name: Python ${{ matrix.python-version }}
    runs-on: ubuntu-latest

    strategy:
      fail-fast: false
      matrix:
        python-version:
        - '3.10'
        - '3.11'
        - '3.12'
        - '3.13'
        - '3.14'

    steps:
    - uses: actions/checkout@v5

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

    - name: Install dependencies
      run: |
        # build and test dependencies
        python -m pip install --upgrade pip wheel tox
        # runtime dependencies
        python -m pip install -e .

    - name: Run tox targets for ${{ matrix.python-version }}
      run: |
        ENV_SUFFIX=$(echo "${{ matrix.python-version }}" | tr -d '.')
        tox -e py${ENV_SUFFIX}

    - name: Upload coverage data
      if: always()
      uses: actions/upload-artifact@v6
      with:
        name: coverage-data-${{ matrix.python-version }}
        path: .coverage.*
        include-hidden-files: true

  coverage:
    name: Coverage
    runs-on: ubuntu-latest
    needs: tests
    steps:
      - uses: actions/checkout@v5

      - uses: actions/setup-python@v6
        with:
          python-version: '3.14'

      - name: Install dependencies
        run: python -m pip install --upgrade coverage[toml]

      - name: Download data
        uses: actions/download-artifact@v7
        with:
          pattern: coverage-data-*
          merge-multiple: true

      - name: Combine coverage and fail if it's <100%
        run: |
          python -m coverage combine
          python -m coverage html --skip-covered --skip-empty
          python -m coverage report --fail-under=55

      - name: Upload HTML report
        uses: actions/upload-artifact@v6
        with:
          name: html-report
          path: coverage-html
