name: CI

on:
  push:
    branches: [main, master]
  pull_request:
    branches: [main, master]

jobs:
{% if testing.enabled %}
  test:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        python-version: ["{{ project.python_version }}", "3.12"]

    steps:
      - uses: actions/checkout@v4

      - name: Set up Python {% raw %}${{ matrix.python-version }}{% endraw %}

        uses: actions/setup-python@v5
        with:
          python-version: {% raw %}${{ matrix.python-version }}{% endraw %}

      - name: Install Poetry
        uses: snok/install-poetry@v1
        with:
          virtualenvs-create: true
          virtualenvs-in-project: true

      - name: Load cached venv
        id: cached-poetry-dependencies
        uses: actions/cache@v4
        with:
          path: .venv
          key: {% raw %}venv-${{ runner.os }}-${{ matrix.python-version }}-${{ hashFiles('**/poetry.lock') }}{% endraw %}

      - name: Install dependencies
        if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
        run: poetry install --no-interaction --no-root

      - name: Install project
        run: poetry install --no-interaction

{% if testing.framework == "pytest" %}
      - name: Run tests with pytest
{% if testing.coverage %}
        run: poetry run pytest --cov={{ project.package_name }} --cov-report=xml --cov-report=term-missing
{% else %}
        run: poetry run pytest
{% endif %}
{% elif testing.framework == "unittest" %}
      - name: Run tests with unittest
        run: poetry run python -m unittest discover -s tests -v
{% endif %}
{% if testing.coverage %}
      - name: Upload coverage reports
        uses: codecov/codecov-action@v4
        with:
          file: ./coverage.xml
          fail_ci_if_error: false
{% endif %}
{% endif %}
{% if formatting.enabled %}
  lint:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Set up Python {{ project.python_version }}
        uses: actions/setup-python@v5
        with:
          python-version: "{{ project.python_version }}"

      - name: Install Poetry
        uses: snok/install-poetry@v1
        with:
          virtualenvs-create: true
          virtualenvs-in-project: true

      - name: Load cached venv
        id: cached-poetry-dependencies
        uses: actions/cache@v4
        with:
          path: .venv
          key: {% raw %}venv-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }}{% endraw %}

      - name: Install dependencies
        if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
        run: poetry install --no-interaction --no-root

      - name: Install project
        run: poetry install --no-interaction

{% if formatting.tool == "ruff" %}
      - name: Lint with ruff
        run: poetry run ruff check src tests

      - name: Check formatting with ruff
        run: poetry run ruff format --check src tests
{% elif formatting.tool == "black" %}
      - name: Check formatting with black
        run: poetry run black --check src tests
{% endif %}
{% if typing_level in ["basic", "strict"] %}
{% if formatting.type_checker == "mypy" %}
      - name: Type check with mypy
        run: poetry run mypy src
{% elif formatting.type_checker == "pyright" %}
      - name: Type check with pyright
        run: poetry run pyright src
{% elif formatting.type_checker == "ty" %}
      - name: Type check with ty
        run: poetry run ty check src
{% endif %}
{% endif %}
{% if formatting.radon %}
      - name: Complexity check with radon
        run: poetry run radon cc src -n B -s
{% endif %}
{% endif %}
