name: Tests

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

jobs:
  backend-tests:
    name: Backend Tests
    runs-on: ubuntu-latest
    
    steps:
      - uses: actions/checkout@v4
      
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'npm'
          cache-dependency-path: backend/package-lock.json
      
      - name: Install backend dependencies
        run: |
          cd backend
          npm ci
      
      - name: Generate Prisma client
        run: |
          cd backend
          npx prisma generate
      
      - name: Run backend tests
        run: |
          cd backend
          npm test

  frontend-unit-tests:
    name: Frontend Unit Tests
    runs-on: ubuntu-latest
    
    steps:
      - uses: actions/checkout@v4
      
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'npm'
          cache-dependency-path: frontend/package-lock.json
      
      - name: Install frontend dependencies
        run: |
          cd frontend
          npm ci
      
      - name: Run frontend tests
        run: |
          cd frontend
          npm test

  e2e-tests:
    name: E2E Browser Tests
    runs-on: ubuntu-latest
    
    steps:
      - uses: actions/checkout@v4
      
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
      
      - name: Install backend dependencies
        run: |
          cd backend
          npm ci
      
      - name: Generate Prisma client
        run: |
          cd backend
          npx prisma generate
      
      - name: Setup backend database
        run: |
          cd backend
          npx prisma db push
        env:
          DATABASE_URL: file:${{ github.workspace }}/backend/prisma/e2e-test.db
      
      - name: Install frontend dependencies
        run: |
          cd frontend
          npm ci
      
      - name: Install E2E test dependencies
        run: |
          cd e2e
          npm ci
      
      - name: Install Playwright browsers
        run: |
          cd e2e
          npx playwright install chromium --with-deps
      
      - name: Start servers and run E2E tests
        run: |
          # Start backend server in background
          cd backend
          DATABASE_URL="file:${{ github.workspace }}/backend/prisma/e2e-test.db" FRONTEND_URL="http://localhost:5173" npm run dev &
          BACKEND_PID=$!
          cd ..
          
          # Wait for backend to be ready
          echo "Waiting for backend server..."
          for i in {1..30}; do
            if curl -s http://localhost:8000/health > /dev/null; then
              echo "Backend is ready!"
              break
            fi
            echo "Attempt $i: Backend not ready yet..."
            sleep 2
          done
          
          # Start frontend server in background
          cd frontend
          npm run dev -- --host &
          FRONTEND_PID=$!
          cd ..
          
          # Wait for frontend to be ready
          echo "Waiting for frontend server..."
          for i in {1..30}; do
            if curl -s http://localhost:5173 > /dev/null; then
              echo "Frontend is ready!"
              break
            fi
            echo "Attempt $i: Frontend not ready yet..."
            sleep 2
          done
          
          # Run E2E tests
          cd e2e
          NO_SERVER=true CI=true npx playwright test
          TEST_EXIT_CODE=$?
          
          # Cleanup
          kill $BACKEND_PID $FRONTEND_PID 2>/dev/null || true
          
          exit $TEST_EXIT_CODE
        env:
          DATABASE_URL: file:${{ github.workspace }}/backend/prisma/e2e-test.db
      
      - name: Upload Playwright report
        uses: actions/upload-artifact@v4
        if: always()
        with:
          name: playwright-report
          path: e2e/playwright-report/
          retention-days: 7
      
      - name: Upload test results
        uses: actions/upload-artifact@v4
        if: failure()
        with:
          name: test-results
          path: e2e/test-results/
          retention-days: 7

  # Security tests for data sanitization
  security-tests:
    name: Security Sanitization Tests
    runs-on: ubuntu-latest
    
    steps:
      - uses: actions/checkout@v4
      
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'npm'
          cache-dependency-path: backend/package-lock.json
      
      - name: Install backend dependencies
        run: |
          cd backend
          npm ci
      
      - name: Generate Prisma client
        run: |
          cd backend
          npx prisma generate
      
      - name: Run security tests
        run: |
          cd backend
          npx ts-node src/securityTest.ts
