name: Run Chatwoot CE spec
permissions:
  contents: read
on:
  push:
    branches:
      - develop
      - master
  pull_request:
  workflow_dispatch:

jobs:
  # Separate linting jobs for faster feedback
  lint-backend:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: ruby/setup-ruby@v1
        with:
          bundler-cache: true
      - name: Run Rubocop
        run: bundle exec rubocop --parallel

  lint-frontend:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: pnpm/action-setup@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 24
          cache: 'pnpm'
      - name: Install pnpm dependencies
        run: pnpm i
      - name: Run ESLint
        run: pnpm run eslint

  # Frontend tests run in parallel with backend
  frontend-tests:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: pnpm/action-setup@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 24
          cache: 'pnpm'
      - name: Install pnpm dependencies
        run: pnpm i
      - name: Run frontend tests
        run: pnpm run test:coverage

  # Backend tests with parallelization
  backend-tests:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        ci_node_total: [16]
        ci_node_index: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

    services:
      postgres:
        image: pgvector/pgvector:pg16
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: ''
          POSTGRES_DB: postgres
          POSTGRES_HOST_AUTH_METHOD: trust
        ports:
          - 5432:5432
        options: >-
          --mount type=tmpfs,destination=/var/lib/postgresql/data
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
      redis:
        image: redis:alpine
        ports:
          - 6379:6379
        options: --entrypoint redis-server

    steps:
      - uses: actions/checkout@v4
      - uses: pnpm/action-setup@v4
        with:
          ref: ${{ github.event.pull_request.head.ref }}
          repository: ${{ github.event.pull_request.head.repo.full_name }}

      - uses: ruby/setup-ruby@v1
        with:
          bundler-cache: true

      - uses: actions/setup-node@v4
        with:
          node-version: 24
          cache: 'pnpm'

      - name: Install pnpm dependencies
        run: pnpm i

      - name: Strip enterprise code
        run: |
          rm -rf enterprise
          rm -rf spec/enterprise

      - name: Create database
        run: bundle exec rake db:create

      - name: Seed database
        run: bundle exec rake db:schema:load

      - name: Run backend tests (parallelized)
        run: |
          # Get all spec files and split them using round-robin distribution
          # This ensures slow tests are distributed evenly across all nodes
          SPEC_FILES=($(find spec -name '*_spec.rb' | sort))
          TESTS=""

          for i in "${!SPEC_FILES[@]}"; do
            # Assign spec to this node if: index % total == node_index
            if [ $(( i % ${{ matrix.ci_node_total }} )) -eq ${{ matrix.ci_node_index }} ]; then
              TESTS="$TESTS ${SPEC_FILES[$i]}"
            fi
          done

          if [ -n "$TESTS" ]; then
            bundle exec rspec --profile=10 --format progress --format json --out tmp/rspec_results.json $TESTS
          fi
        env:
          NODE_OPTIONS: --openssl-legacy-provider

      - name: Upload test results
        uses: actions/upload-artifact@v4
        if: always()
        with:
          name: rspec-results-${{ matrix.ci_node_index }}
          path: tmp/rspec_results.json

      - name: Upload rails log folder
        uses: actions/upload-artifact@v4
        if: failure()
        with:
          name: rails-log-folder-${{ matrix.ci_node_index }}
          path: log
