---
name: Pull request build

on:
  pull_request:
    branches:
      - dev

env:
  MAIN_PY_VER: "3.13"

jobs:
  lint:
    runs-on: ubuntu-latest
    name: Lint source files
    permissions:
      pull-requests: read
    steps:
      - name: Source checkout
        uses: actions/checkout@v5

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

      - name: Cache pip repository
        uses: actions/cache@v4
        with:
          path: ~/.cache/pip
          key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}-${{ env.MAIN_PY_VER }}

      - name: Prepare python environment
        run: |
          pip install -r requirements.txt
          poetry config virtualenvs.create true
          poetry config virtualenvs.in-project true

      - name: Cache poetry virtual environment
        uses: actions/cache@v4
        with:
          path: .venv
          key: ${{ runner.os }}-poetry-${{ hashFiles('**/pyproject.toml') }}-${{ env.MAIN_PY_VER }}

      - name: Lint the project
        run: |
          poetry lock
          poetry install --no-interaction
          poetry run poe lint

  test:
    runs-on: ubuntu-latest
    needs: [lint]
    strategy:
      matrix:
        python: ["3.12", "3.13"]
    name: Test project with Python ${{ matrix.python }}
    permissions:
      checks: write
      pull-requests: write
    steps:
      - name: Source checkout
        uses: actions/checkout@v5

      - name: Setup timezone
        uses: zcong1993/setup-timezone@v2.0.0
        with:
          timezone: Asia/Jerusalem

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

      - name: Cache pip repository
        uses: actions/cache@v4
        with:
          path: ~/.cache/pip
          key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}-${{ matrix.python }}

      - name: Prepare python environment
        run: |
          pip install -r requirements.txt
          poetry config virtualenvs.create true
          poetry config virtualenvs.in-project true

      - name: Cache poetry virtual environment
        uses: actions/cache@v4
        with:
          path: .venv
          key: ${{ runner.os }}-poetry-${{ hashFiles('**/pyproject.toml') }}-${{ matrix.python }}

      - name: Install project build dependencies
        run: |
          poetry lock
          poetry install --no-interaction

      - name: Test the project
        run: >
          if [ ${{ matrix.python }} == ${{ env.MAIN_PY_VER }} ];
          then poetry run poe test_rep;
          else poetry run poe test; fi

      - name: Report test summary
        uses: EnricoMi/publish-unit-test-result-action@v2
        if: ${{ matrix.python == env.MAIN_PY_VER && always() }}
        with:
          test_changes_limit: 0
          junit_files: ./junit.xml
          report_individual_runs: true

      - name: Push to CodeCov
        uses: codecov/codecov-action@v5
        if: ${{ matrix.python == env.MAIN_PY_VER }}
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          files: ./coverage.xml

  docs:
    runs-on: ubuntu-latest
    needs: [lint]
    name: Verify documentation site
    permissions:
      pull-requests: read
    steps:
      - name: Source checkout
        uses: actions/checkout@v5

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

      - name: Cache pip repository
        uses: actions/cache@v4
        with:
          path: ~/.cache/pip
          key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}-${{ env.MAIN_PY_VER }}

      - name: Prepare python environment
        run: |
          pip install -r requirements.txt
          poetry config virtualenvs.create true
          poetry config virtualenvs.in-project true

      - name: Cache poetry virtual environment
        uses: actions/cache@v4
        with:
          path: .venv
          key: ${{ runner.os }}-poetry-${{ hashFiles('**/pyproject.toml') }}-${{ env.MAIN_PY_VER }}

      - name: Build documentation site
        run: |
          poetry lock
          poetry install --no-interaction
          poetry run poe docs_build
