name: Test

on:
  pull_request:
    branches: [main]

# Minimal permissions for security
permissions:
  contents: read

jobs:
  # =============================================================================
  # Detect which paths changed to skip unnecessary jobs
  # =============================================================================
  changes:
    name: Detect Changes
    runs-on: ubuntu-latest
    permissions:
      contents: read
      pull-requests: read
    outputs:
      backend: ${{ steps.filter.outputs.backend }}
      frontend: ${{ steps.filter.outputs.frontend }}
    steps:
      - uses: dorny/paths-filter@v3
        id: filter
        with:
          filters: |
            backend:
              - 'backend/**'
              - 'biome.json'
              - '.github/workflows/test.yml'
            frontend:
              - 'frontend/**'
              - 'biome.json'
              - '.github/workflows/test.yml'

  # =============================================================================
  # Backend Tests (skipped if no backend-related files changed)
  # =============================================================================
  backend-test:
    name: Backend Tests
    needs: changes
    if: needs.changes.outputs.backend == 'true'
    runs-on: ubuntu-latest
    permissions:
      contents: read
    defaults:
      run:
        working-directory: backend

    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

      - name: Setup Node.js
        uses: actions/setup-node@v6
        with:
          node-version: '22'
          cache: 'npm'
          cache-dependency-path: backend/package-lock.json

      - name: Install dependencies
        run: npm ci

      - name: Lint
        run: npm run lint

      - name: TypeScript type check
        run: npx tsc --noEmit

      - name: Run tests with coverage
        run: npm run test:coverage

      - name: Upload coverage report
        uses: actions/upload-artifact@v7
        if: always()
        with:
          name: backend-coverage
          path: backend/coverage/
          retention-days: 7

  # =============================================================================
  # Frontend Tests & Build (skipped if no frontend-related files changed)
  # =============================================================================
  frontend-build:
    name: Frontend Build
    needs: changes
    if: needs.changes.outputs.frontend == 'true'
    runs-on: ubuntu-latest
    permissions:
      contents: read
    defaults:
      run:
        working-directory: frontend

    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

      - name: Setup Node.js
        uses: actions/setup-node@v6
        with:
          node-version: '22'
          cache: 'npm'
          cache-dependency-path: frontend/package-lock.json

      - name: Install dependencies
        run: npm ci

      - name: Lint
        run: npm run lint

      - name: Run tests with coverage
        run: npm run test:coverage

      - name: TypeScript type check & build
        run: npm run build

      - name: Upload coverage report
        uses: actions/upload-artifact@v7
        if: always()
        with:
          name: frontend-coverage
          path: frontend/coverage/
          retention-days: 7
