name: Test Toolkits

on:
  push:
    branches:
      - main
  pull_request:
    types: [opened, synchronize, reopened, ready_for_review]

jobs:
  setup:
    runs-on: ubuntu-latest
    outputs:
      toolkits_with_gha_secrets: ${{ steps.load_toolkits.outputs.toolkits_with_gha_secrets }}
      toolkits_without_gha_secrets: ${{ steps.load_toolkits.outputs.toolkits_without_gha_secrets }}
    steps:
      - name: Check out
        uses: actions/checkout@v4

      - name: determine toolkits with and without GHA secrets
        id: load_toolkits
        run: |
          # Find all directories in toolkits/ that have a pyproject.toml
          TOOLKITS=$(find toolkits -maxdepth 1 -type d -not -name "toolkits" -exec test -f {}/pyproject.toml \; -exec basename {} \; | jq -R -s -c 'split("\n")[:-1]')
          TOOLKITS_WITH_GHA_SECRETS='["postgres", "clickhouse", "mongodb"]'
          TOOLKITS_WITHOUT_GHA_SECRETS=$(echo "$TOOLKITS" | jq -c --argjson with "$TOOLKITS_WITH_GHA_SECRETS" '[.[] | select(. as $t | $with | index($t) | not)]')
          echo "Found toolkits: $TOOLKITS"
          echo "Found toolkits without GHA secrets: $TOOLKITS_WITHOUT_GHA_SECRETS"
          echo "Found toolkits with GHA secrets: $TOOLKITS_WITH_GHA_SECRETS"
          echo "toolkits_without_gha_secrets=$TOOLKITS_WITHOUT_GHA_SECRETS" >> $GITHUB_OUTPUT
          echo "toolkits_with_gha_secrets=$TOOLKITS_WITH_GHA_SECRETS" >> $GITHUB_OUTPUT

  test-toolkits:
    needs: setup
    runs-on: ubuntu-latest
    strategy:
      matrix:
        toolkit: ${{ fromJson(needs.setup.outputs.toolkits_without_gha_secrets) }}
      fail-fast: true
    steps:
      - name: Check out
        uses: actions/checkout@v4

      - name: Set up the environment
        uses: ./.github/actions/setup-uv-env
        with:
          is-toolkit: "true"
          working-directory: toolkits/${{ matrix.toolkit }}

      - name: Install toolkit dependencies
        working-directory: toolkits/${{ matrix.toolkit }}
        run: uv pip install -e ".[dev]"

      - name: Check toolkit
        working-directory: toolkits/${{ matrix.toolkit }}
        run: |
          uv run --active pre-commit run -a
          uv run --active mypy --config-file=pyproject.toml

      - name: Test stand-alone toolkits (no secrets)
        working-directory: toolkits/${{ matrix.toolkit }}
        run: |
          # Run pytest and capture exit code
          uv run --active pytest -W ignore -v --cov=arcade_${{ matrix.toolkit }} --cov-report=xml || EXIT_CODE=$?

          if [ "${EXIT_CODE:-0}" -eq 5 ]; then
            echo "No tests found for toolkit ${{ matrix.toolkit }}, skipping..."
            exit 0
          elif [ "${EXIT_CODE:-0}" -ne 0 ]; then
            exit ${EXIT_CODE}
          fi

  test-toolkits-with-gha-secrets:
    needs: setup
    runs-on: ubuntu-latest
    strategy:
      matrix:
        toolkit: ${{ fromJson(needs.setup.outputs.toolkits_with_gha_secrets) }}
      fail-fast: true
    steps:
      - name: Check out
        uses: actions/checkout@v4

      - name: Set up the environment
        uses: ./.github/actions/setup-uv-env
        with:
          is-toolkit: "true"
          working-directory: toolkits/${{ matrix.toolkit }}

      - name: Install toolkit dependencies
        working-directory: toolkits/${{ matrix.toolkit }}
        run: uv pip install -e ".[dev]"

      - name: Check toolkit
        working-directory: toolkits/${{ matrix.toolkit }}
        run: |
          uv run --active pre-commit run -a
          uv run --active mypy --config-file=pyproject.toml

      - name: Test stand-alone toolkits (with secrets)
        if: |
          !github.event.pull_request.head.repo.fork
        working-directory: toolkits/${{ matrix.toolkit }}
        env:
          TEST_POSTGRES_DATABASE_CONNECTION_STRING: ${{ secrets.TEST_POSTGRES_DATABASE_CONNECTION_STRING }} # TODO: dynamically only load the `TEST_${{ matrix.toolkit }}_DATABASE_CONNECTION_STRING secret`
          TEST_CLICKHOUSE_DATABASE_CONNECTION_STRING: ${{ secrets.TEST_CLICKHOUSE_DATABASE_CONNECTION_STRING }}
          TEST_MONGODB_CONNECTION_STRING: ${{ secrets.TEST_MONGODB_CONNECTION_STRING }}
        run: |
          # If there's a custom test_setup.sh file, run it
          if [ -f tests/test_setup.sh ]; then
            echo "Running custom test setup for ${{ matrix.toolkit }}..."
            ./tests/test_setup.sh
          fi

          # Run pytest and capture exit code
          uv run --active pytest -W ignore -v --cov=arcade_${{ matrix.toolkit }} --cov-report=xml || EXIT_CODE=$?

          if [ "${EXIT_CODE:-0}" -eq 5 ]; then
            echo "No tests found for toolkit ${{ matrix.toolkit }}, skipping..."
            exit 0
          elif [ "${EXIT_CODE:-0}" -ne 0 ]; then
            exit ${EXIT_CODE}
          fi
