name: CI

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - "**" # run on all branches

jobs:
  build:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - python-version: "3.10"
            os: ubuntu-latest
            envlist: "py310"
          - python-version: "3.11"
            os: ubuntu-latest
            envlist: "py311"
          - python-version: "3.12"
            os: ubuntu-latest
            envlist: "py312"
          - python-version: "3.13"
            os: ubuntu-latest
            envlist: "py313"
          - python-version: "3.14"
            os: ubuntu-latest
            envlist: "py314,ruff,typing,lint,pylint"

          - python-version: "3.13"
            os: macos-14
            envlist: "py313"

          - python-version: "3.13"
            os: windows-latest
            envlist: "py313"
    steps:
      - uses: actions/checkout@v6
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v6
        with:
          python-version: ${{ matrix.python-version }}
          cache: "pip"
          cache-dependency-path: |
            requirements/production.txt
            requirements/testing.txt
            .pre-commit-config.yaml
      - name: Install dependencies
        run: |
          pip install -r requirements/testing.txt
      - name: CI
        run: |
          tox run -e "${{ matrix.envlist }}"
      - name: Upload coverage artifact
        uses: actions/upload-artifact@v6
        with:
          name: coverage-${{ matrix.os }}-${{ matrix.python-version }}
          path: .coverage
          include-hidden-files: true

  coverage:
    name: Process test coverage
    runs-on: ubuntu-latest
    needs: ["build"]
    strategy:
      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 }}
          cache: "pip"
          cache-dependency-path: |
            requirements/production.txt
            requirements/testing.txt
            .pre-commit-config.yaml
      - name: Install coverage.py
        run: pip install coverage
      - name: Download all coverage artifacts
        uses: actions/download-artifact@v7
      - name: Create coverage report
        run: |
          coverage combine coverage-*/.coverage
          coverage report --fail-under=94
          coverage xml
      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v5
        with:
          fail_ci_if_error: true
          token: ${{ secrets.CODECOV_TOKEN }}
