name: E2E Tests

on:
  push:
    branches: [main, develop]
    paths:
      - 'frontend/**'
      - 'src/**'
      - '.github/workflows/e2e.yml'
  pull_request:
    branches: [main, develop]
    paths:
      - 'frontend/**'
      - 'src/**'
      - '.github/workflows/e2e.yml'

jobs:
  e2e-tests:
    name: Run E2E Tests
    runs-on: ubuntu-latest
    timeout-minutes: 30
    
    defaults:
      run:
        working-directory: frontend

    services:
      postgres:
        image: postgres:15
        env:
          POSTGRES_USER: mockupaws
          POSTGRES_PASSWORD: mockupaws
          POSTGRES_DB: mockupaws
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
        ports:
          - 5432:5432

    steps:
      - name: Checkout code
        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: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.11'

      - name: Install Python dependencies
        run: |
          pip install -r requirements.txt
        working-directory: .

      - name: Install Node.js dependencies
        run: npm ci

      - name: Install Playwright browsers
        run: npx playwright install --with-deps chromium firefox webkit

      - name: Wait for PostgreSQL
        run: |
          until pg_isready -h localhost -p 5432 -U mockupaws; do
            echo "Waiting for PostgreSQL..."
            sleep 1
          done

      - name: Run database migrations
        run: |
          alembic upgrade head
        env:
          DATABASE_URL: postgresql://mockupaws:mockupaws@localhost:5432/mockupaws

      - name: Start backend server
        run: |
          uvicorn src.main:app --host 0.0.0.0 --port 8000 &
          echo $! > /tmp/backend.pid
          # Wait for backend to be ready
          npx wait-on http://localhost:8000/health --timeout 60000
        env:
          DATABASE_URL: postgresql://mockupaws:mockupaws@localhost:5432/mockupaws
          CORS_ORIGINS: "[\"http://localhost:5173\"]"

      - name: Run E2E tests
        run: npm run test:e2e:ci
        env:
          VITE_API_URL: http://localhost:8000/api/v1
          CI: true

      - name: Stop backend server
        if: always()
        run: |
          if [ -f /tmp/backend.pid ]; then
            kill $(cat /tmp/backend.pid) || true
          fi

      - name: Upload Playwright report
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: playwright-report
          path: frontend/e2e-report/
          retention-days: 30

      - name: Upload test results
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: test-results
          path: frontend/e2e-results/
          retention-days: 7

      - name: Upload screenshots
        if: failure()
        uses: actions/upload-artifact@v4
        with:
          name: screenshots
          path: frontend/e2e/screenshots/
          retention-days: 7

  visual-regression:
    name: Visual Regression Tests
    runs-on: ubuntu-latest
    timeout-minutes: 20
    needs: e2e-tests
    if: github.event_name == 'pull_request'
    
    defaults:
      run:
        working-directory: frontend

    services:
      postgres:
        image: postgres:15
        env:
          POSTGRES_USER: mockupaws
          POSTGRES_PASSWORD: mockupaws
          POSTGRES_DB: mockupaws
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
        ports:
          - 5432:5432

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

      - name: Checkout baseline screenshots
        uses: actions/checkout@v4
        with:
          ref: ${{ github.base_ref }}
          path: baseline
          sparse-checkout: |
            frontend/e2e/screenshots/baseline/

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

      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.11'

      - name: Install Python dependencies
        run: |
          pip install -r requirements.txt
        working-directory: .

      - name: Install Node.js dependencies
        run: npm ci

      - name: Install Playwright browsers
        run: npx playwright install --with-deps chromium

      - name: Wait for PostgreSQL
        run: |
          until pg_isready -h localhost -p 5432 -U mockupaws; do
            echo "Waiting for PostgreSQL..."
            sleep 1
          done

      - name: Run database migrations
        run: |
          alembic upgrade head
        env:
          DATABASE_URL: postgresql://mockupaws:mockupaws@localhost:5432/mockupaws

      - name: Start backend server
        run: |
          uvicorn src.main:app --host 0.0.0.0 --port 8000 &
          echo $! > /tmp/backend.pid
          npx wait-on http://localhost:8000/health --timeout 60000
        env:
          DATABASE_URL: postgresql://mockupaws:mockupaws@localhost:5432/mockupaws
          CORS_ORIGINS: "[\"http://localhost:5173\"]"

      - name: Copy baseline screenshots
        run: |
          if [ -d "../baseline/frontend/e2e/screenshots/baseline" ]; then
            mkdir -p e2e/screenshots/baseline
            cp -r ../baseline/frontend/e2e/screenshots/baseline/* e2e/screenshots/baseline/
          fi

      - name: Run visual regression tests
        run: npx playwright test visual-regression.spec.ts --project=chromium
        env:
          VITE_API_URL: http://localhost:8000/api/v1
          CI: true

      - name: Stop backend server
        if: always()
        run: |
          if [ -f /tmp/backend.pid ]; then
            kill $(cat /tmp/backend.pid) || true
          fi

      - name: Upload visual regression results
        if: failure()
        uses: actions/upload-artifact@v4
        with:
          name: visual-regression-diff
          path: |
            frontend/e2e/screenshots/actual/
            frontend/e2e/screenshots/diff/
          retention-days: 7

  smoke-tests:
    name: Smoke Tests
    runs-on: ubuntu-latest
    timeout-minutes: 10
    if: github.event_name == 'push'
    
    defaults:
      run:
        working-directory: frontend

    services:
      postgres:
        image: postgres:15
        env:
          POSTGRES_USER: mockupaws
          POSTGRES_PASSWORD: mockupaws
          POSTGRES_DB: mockupaws
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
        ports:
          - 5432:5432

    steps:
      - name: Checkout code
        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: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.11'

      - name: Install Python dependencies
        run: |
          pip install -r requirements.txt
        working-directory: .

      - name: Install Node.js dependencies
        run: npm ci

      - name: Install Playwright browsers
        run: npx playwright install --with-deps chromium

      - name: Wait for PostgreSQL
        run: |
          until pg_isready -h localhost -p 5432 -U mockupaws; do
            echo "Waiting for PostgreSQL..."
            sleep 1
          done

      - name: Run database migrations
        run: |
          alembic upgrade head
        env:
          DATABASE_URL: postgresql://mockupaws:mockupaws@localhost:5432/mockupaws

      - name: Start backend server
        run: |
          uvicorn src.main:app --host 0.0.0.0 --port 8000 &
          echo $! > /tmp/backend.pid
          npx wait-on http://localhost:8000/health --timeout 60000
        env:
          DATABASE_URL: postgresql://mockupaws:mockupaws@localhost:5432/mockupaws
          CORS_ORIGINS: "[\"http://localhost:5173\"]"

      - name: Run smoke tests
        run: npx playwright test navigation.spec.ts --grep "dashboard\|scenarios" --project=chromium
        env:
          VITE_API_URL: http://localhost:8000/api/v1
          CI: true

      - name: Stop backend server
        if: always()
        run: |
          if [ -f /tmp/backend.pid ]; then
            kill $(cat /tmp/backend.pid) || true
          fi
