name: Test and lint

on:
  workflow_dispatch:
  push:
    branches: [master]
  pull_request:
    branches:
      - "**"

env:
  PYTHON_VERSION: "3.10"

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        python-version:
          [
            "pypy-3.10",
            "pypy-3.11",
            "3.10",
            "3.11",
            "3.12",
            "3.13",
            "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"

      - name: Install needed dependencies
        run: |
          pip install tox tox-gh-actions

      - name: Run tests with tox
        run: tox

  lint:
    runs-on: ubuntu-latest
    env:
      TARGETS: src tests

    steps:
      - uses: actions/checkout@v6

      - name: Install Poetry
        run: pipx install poetry

      - name: Set up Python
        uses: actions/setup-python@v6
        with:
          python-version: ${{ env.PYTHON_VERSION }}
          cache: "poetry"

      - name: Install dependencies
        run: |
          poetry config virtualenvs.in-project true
          poetry install

      - name: Run isort
        run: poetry run isort --check-only ${TARGETS}

      - name: Run black
        run: poetry run black --check ${TARGETS}

      - name: Run mypy
        run: poetry run mypy ${TARGETS}

      - name: Run pylint
        run: poetry run pylint ${TARGETS}

  test-docs:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v6

      - name: Install Poetry
        run: pipx install poetry

      - name: Set up Python
        uses: actions/setup-python@v6
        with:
          python-version: ${{ env.PYTHON_VERSION }}
          cache: "poetry"

      - name: Install dependencies
        run: |
          poetry config virtualenvs.in-project true
          poetry install

      - name: Generate man pages with Sphinx
        run: poetry run python setup.py build_sphinx -W --keep-going -n -b man --build-dir build/sphinx

      - name: Build docs with Mkdocs
        run: poetry run make docs
