# GitHub Actions CI — Build & type-check on every push/PR
# Docs: https://docs.github.com/en/actions/use-cases-and-examples/building-and-testing/building-and-testing-nodejs

name: CI

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

jobs:
  build:
    name: Build & Type Check
    runs-on: ubuntu-latest

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

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: npm

      - name: Install dependencies
        run: npm ci

      - name: Build shared package
        run: cd packages/shared && npm run build

      - name: Type-check web
        run: cd apps/web && npx tsc --noEmit

      - name: Build web
        run: cd apps/web && npm run build
        env:
          # Required by Next.js build — use dummy values for CI
          POSTGRES_URL: postgresql://user:pass@localhost:5432/slackhive
          REDIS_URL: redis://localhost:6379
          SESSION_SECRET: ci-secret-not-used
          ADMIN_USERNAME: admin
          ADMIN_PASSWORD: admin

      - name: Type-check runner
        run: cd apps/runner && npx tsc --noEmit

      - name: Type-check cli
        run: cd cli && npx tsc --noEmit

  test:
    name: Unit Tests
    runs-on: ubuntu-latest
    needs: build

    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: npm
      - run: npm ci
      - run: cd packages/shared && npm run build
      - name: Run web unit tests
        run: cd apps/web && npm test
      - name: Run runner unit tests
        run: cd apps/runner && npm test

  check-tests:
    name: Test Coverage Check
    runs-on: ubuntu-latest
    if: github.event_name == 'pull_request'

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

      - name: Check that source changes include tests
        run: |
          BASE=${{ github.event.pull_request.base.sha }}
          HEAD=${{ github.event.pull_request.head.sha }}

          # Files changed in this PR
          CHANGED=$(git diff --name-only "$BASE" "$HEAD")

          # Source files changed (lib/ and api routes, excluding test files)
          SRC_CHANGED=$(echo "$CHANGED" | grep -E '^apps/web/src/(lib|app/api)/' | grep -v '__tests__' || true)

          # Test files changed
          TEST_CHANGED=$(echo "$CHANGED" | grep '__tests__' || true)

          echo "=== Source files changed ==="
          echo "${SRC_CHANGED:-none}"
          echo ""
          echo "=== Test files changed ==="
          echo "${TEST_CHANGED:-none}"

          if [ -n "$SRC_CHANGED" ] && [ -z "$TEST_CHANGED" ]; then
            echo ""
            echo "⚠️  WARNING: Source files were changed but no test files were added or updated."
            echo "Please add tests for new logic in apps/web/src/lib/__tests__/"
            echo ""
            echo "If tests are genuinely not applicable (docs-only, pure UI tweak, config change),"
            echo "add a note in the PR description under the 'Tests' section."
            # Exit 1 to fail the check — reviewers can override by merging anyway
            exit 1
          fi

          echo "✅ Test files present for source changes."

  lint:
    name: Lint
    runs-on: ubuntu-latest
    continue-on-error: true

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

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: npm

      - name: Install dependencies
        run: npm ci

      - name: Lint web
        run: cd apps/web && npm run lint || true

      - name: Lint runner
        run: cd apps/runner && npm run lint || true
