"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateE2EYaml = generateE2EYaml;
function generateE2EYaml(context) {
    const { devOps: { database }, } = context;
    // Use 'postgres' as hostname instead of 'localhost' for GitHub Actions service containers
    const connectionString = `postgresql://postgres:postgres@postgres:5432/${context.schema.slug}`;
    return /*yml*/ `
  name: End-to-End Visual Tests

  on:
    push:
      branches:
        - main
    pull_request:

  concurrency:
    group: \${{ github.workflow }}-\${{ github.ref }}
    cancel-in-progress: true

  jobs:
    e2e:
      name: Run e2e test
      runs-on: ubuntu-latest
      
      container:
        image: mcr.microsoft.com/playwright:v1.56.1

      ${database ? postgresContainerSetup(database) : ''}

      steps:
        - name: Checkout Current branch
          uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 #v4.3.1
          with:
            ref: \${{ github.ref }}
            fetch-depth: 0

        - uses: pnpm/action-setup@a7487c7e89a18df4991f7f222e4898a00d66ddda #v4.1.0

        - name: Setup Node Environment
          uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 #v4.4.0
          with:
            node-version: '24'
            cache: 'pnpm'

        - name: Setup Workspace
          run: apt-get update && apt-get -y install unzip && pnpm run setup

        ${database ? databaseSetup(connectionString) : ''}

        - name: Start backend
          run: pnpm e2e:coverage:stateless > backend.log 2>&1 &
          working-directory: backend
          ${database ? databaseConnectionEnv(connectionString) : ''}

        - name: Start frontend
          run: pnpm e2e:ci > frontend.log 2>&1 &
          working-directory: frontend
          env:
            VITE_PUBLIC_API_URL: http://localhost:3001
            VITE_PUBLIC_API_URL_2: http://localhost:3001
            VITE_PUBLIC_BASE_URL: http://localhost:3000
            VITE_PUBLIC_MAPBOX_ACCESS_TOKEN: \${{ secrets.MAPBOX_ACCESS_TOKEN }}

        - name: Check if Backend and Frontend are running and Print Logs
          run: |
            sleep 30

            echo "Backend Logs:"
            cat backend/backend.log

            echo "Frontend Logs:"
            cat frontend/frontend.log

        - name: Wait for Backend to be ready
          run: |
            retry=0
            maxRetries=9
            delay=10

            url="http://localhost:3001/health"

            until output=$(curl --fail $url 2>&1); do
              retry=$((retry+1))

              if [ $retry -eq $maxRetries ]; then
                echo "$url not ready after $maxRetries attempts, failing..."
                echo $output
                exit 1
              fi

              echo "waiting for $url to be ready, attempt $retry..."
              echo $output
              sleep $((delay * retry))
            done

            echo "$url is ready!"

        - name: Wait for Frontend to be ready
          run: |
            retry=0
            maxRetries=9
            delay=10

            url="http://localhost:3000/health"

            until output=$(curl --fail $url 2>&1); do
              retry=$((retry+1))

              if [ $retry -eq $maxRetries ]; then
                echo "$url not ready after $maxRetries attempts, failing..."
                echo $output
                exit 1
              fi

              echo "waiting for $url to be ready, attempt $retry..."
              echo $output
              sleep $((delay * retry))
            done

            echo "$url is ready!"

        - name: Run Backend Unit Tests
          run: pnpm run test:jest
          working-directory: backend

        - name: Run Storybook Vitest Tests
          run: pnpm test:storybook
          working-directory: frontend

        - name: Run Playwright visual Tests
          run: npx playwright test
          working-directory: e2e
          env:
            BASE_URL: 'http://localhost:3000'
            API_URL: 'http://localhost:3001'
            ENABLE_BACKEND_SHUTDOWN: true
            PLAYWRIGHT_WORKERS: 2

        - name: Upload Playwright Report
          uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 #v4.6.2
          if: \${{ !cancelled() }}
          with:
            name: playwright-report
            path: e2e/test-results
            retention-days: 5

        - name: Upload Backend Coverage
          uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 #v4.6.2
          if: \${{ !cancelled() }}
          with:
            name: backend-coverage
            path: backend/coverage
            retention-days: 5

        - name: Upload Frontend Coverage
          uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 #v4.6.2
          if: \${{ !cancelled() }}
          with:
            name: frontend-coverage
            path: frontend/coverage
            retention-days: 5

        - name: SonarQube Scan
          uses: SonarSource/sonarqube-scan-action@1a6d90ebcb0e6a6b1d87e37ba693fe453195ae25 #v5.3.1
          env:
            GITHUB_TOKEN: \${{ secrets.GITHUB_TOKEN }}
            SONAR_TOKEN: \${{ secrets.SONAR_TOKEN }}
`;
}
const postgresContainerSetup = ({ username, password, database }) => `
      services:
        postgres:
          image: postgres:13
          env:
            POSTGRES_USER: ${username}
            POSTGRES_PASSWORD: ${password}
            POSTGRES_DB: ${database}
          ports:
            - 5432:5432
          # Add a health check
          options: >-
            --health-cmd pg_isready
            --health-interval 10s
            --health-timeout 5s
            --health-retries 5
`;
const databaseConnectionEnv = (connectionString) => `
          env:
            # We need to use postgres service name as the host when running in a container!
            DATABASE_CONNECTION: '${connectionString}'
`;
const databaseSetup = (connectionString) => `
        - name: Setup Test Database
          run: pnpm prisma migrate dev
${databaseConnectionEnv(connectionString)}
`;
//# sourceMappingURL=e2e-yml.generator.js.map