name: CI

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

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: ["3.11", "3.12", "3.13"]
    name: Python {{ '${{ matrix.python-version }}' }} Tests

    services:
{%- if database == "postgresql" or event_store == "message-db" %}
      postgres:
        image: postgres:11
        env:
          POSTGRES_PASSWORD: postgres
        ports:
          - 5432:5432
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
{%- endif %}

{%- if broker in ["redis", "redis-pubsub"] or cache == "redis" %}
      redis:
        image: redis
        ports:
          - 6379:6379
        options: >-
          --health-cmd "redis-cli ping"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
{%- endif %}

{%- if event_store == "message-db" %}
      message-db:
        image: ethangarofolo/message-db:1.2.6
        ports:
          - 5433:5432
{%- endif %}

{%- if database == "mssql" %}
      mssql:
        image: mcr.microsoft.com/mssql/server:2022-latest
        env:
          ACCEPT_EULA: Y
          SA_PASSWORD: Protean123!
        ports:
          - 1433:1433
        options: >-
          --health-cmd "/opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P Protean123! -Q 'SELECT 1' -b -C"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
{%- endif %}

    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 1

{%- if database == "elasticsearch" %}
      - name: Configure sysctl limits
        run: |
          sudo swapoff -a
          sudo sysctl -w vm.swappiness=1
          sudo sysctl -w fs.file-max=262144
          sudo sysctl -w vm.max_map_count=262144

      - name: Run Elasticsearch
        uses: elastic/elastic-github-actions/elasticsearch@master
        with:
          stack-version: 7.12.0
{%- endif %}

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

      - uses: actions/cache@v4
        with:
          path: ~/.cache/pip
          key: {{ '${{ runner.os }}' }}-pip-{{ '${{ matrix.python-version }}' }}-{{ '${{ hashFiles(\'**/poetry.lock\') }}' }}
          restore-keys: |
            {{ '${{ runner.os }}' }}-pip-{{ '${{ matrix.python-version }}' }}-

{%- if database == "mssql" %}
      - name: Install Microsoft ODBC Driver 18 and tools
        run: |
          sudo apt-get update
          sudo apt-get install -y curl gnupg apt-transport-https
          curl -sSL https://packages.microsoft.com/keys/microsoft.asc | sudo tee /etc/apt/trusted.gpg.d/microsoft.asc >/dev/null
          curl -sSL https://packages.microsoft.com/config/ubuntu/24.04/prod.list | sudo tee /etc/apt/sources.list.d/microsoft-prod.list >/dev/null
          sudo apt-get update
          sudo ACCEPT_EULA=Y apt-get install -y msodbcsql18 mssql-tools18 unixodbc-dev
{%- endif %}

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

      - name: Install dependencies
        run: poetry install --with dev,test --all-extras

      - name: Run linting
        run: poetry run ruff check src/ tests/

      - name: Run type checking
        run: poetry run mypy src/{{ package_name }}

      - name: Run tests
        run: poetry run pytest --cov=src/{{ package_name }} --cov-report=xml --cov-report=term-missing

      - name: Upload coverage to Codecov
        if: matrix.python-version == '3.11'
        uses: codecov/codecov-action@v5
        with:
          files: coverage.xml
          fail_ci_if_error: false
        continue-on-error: true
