name: Tests

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

jobs:
    ci:
        runs-on: ${{ matrix.os }}
#        services:
#            postgres:
#                image: postgres
#                env:
#                    POSTGRES_PASSWORD: postgres
#                ports:
#                    - 5432:5432
#                options: >-
#                    --health-cmd pg_isready
#                    --health-interval 10s
#                    --health-timeout 5s
#                    --health-retries 5
        strategy:
            matrix:
                os: [ubuntu-latest]
                php: ['8.1', '8.2', '8.3', '8.4']
                dependency-version: [prefer-lowest, prefer-stable]

        name: PHP ${{ matrix.php }} - ${{ matrix.os }} - ${{ matrix.dependency-version }}

        steps:
            - name: Checkout
              uses: actions/checkout@v4

            - name: Login to Docker Hub
              uses: docker/login-action@v3
              with:
                username: ${{ vars.DOCKERHUB_USERNAME }}
                password: ${{ secrets.DOCKERHUB_TOKEN }}

            - name: Setup PHP
              uses: shivammathur/setup-php@v2
              with:
                  php-version: ${{ matrix.php }}
                  coverage: none

            - name: Setup Problem Matches
              run: |
                  echo "::add-matcher::${{ runner.tool_cache }}/php.json"
                  echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

            - name: Configure sysctl limits (for elastic to boot)
              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: Runs Elasticsearch
              uses: elastic/elastic-github-actions/elasticsearch@master
              with:
                  stack-version: 8.17.0
                  security-enabled: false

            - name: Install Typesense Database
              run: |
                  docker run -id \
                  -p 8108:8108 \
                  --name typesense \
                  -v /tmp/typesense-data:/data \
                  -v /tmp/typesense-analytics-data:/analytics-data \
                  typesense/typesense:28.0 \
                  --api-key=xyz \
                  --data-dir=/data \
                  --enable-search-analytics=true \
                  --analytics-dir=/analytics-data  \
                  --analytics-flush-interval=60 \
                  --analytics-minute-rate-limit=100 \
                  --enable-cors

            - name: Wait for Typesense to be healthy
              shell: bash
              run: |
                  start_time=$(date +%s)
                  timeout=60
                  counter=0
                  until curl -s http://localhost:8108/health | grep -q '"ok":true'; do
                    if [ $counter -eq $timeout ]; then
                      echo "Timed out waiting for Typesense to be healthy"
                      exit 1
                    fi
                    echo "Waiting for Typesense to be healthy..."
                    sleep 5
                    counter=$((counter + 1))
                  done
                  end_time=$(date +%s)
                  elapsed=$((end_time - start_time))
                  echo "Typesense healthcheck elapsed: ${elapsed}s"

            - name: Install PHP dependencies
              run: composer update --${{ matrix.dependency-version }} --no-interaction --no-progress --ansi

            - name: Unit Tests
              run: vendor/bin/phpunit --colors=always --verbose
