name: E2E Nightly

on:
  schedule:
    - cron: "0 2 * * *"
  workflow_dispatch:

jobs:
  e2e:
    runs-on: ubuntu-latest
    timeout-minutes: 30

    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: "20"
          cache: npm

      - name: Install dependencies
        run: npm ci

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

      - name: E2E tests
        run: npm run test:e2e
        env:
          CI: true
          JWT_SECRET: ci-test-jwt-secret-32chars-min!!
          HOST_SERVICE_TOKENS: '{"demo-host-token":{"customerId":"CUST_001","permissions":["pricing:markup:write"]}}'

      - uses: actions/upload-artifact@v4
        if: always()
        with:
          name: playwright-report
          path: playwright-report/
          retention-days: 7

  go-no-go:
    runs-on: ubuntu-latest
    needs: e2e
    if: always()

    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: "20"
          cache: npm

      - run: npm ci

      - name: Go-No-Go check (mock)
        run: npm run go-no-go -- --mock
        env:
          RPA_MOCK_MODE: "true"

  probe:
    runs-on: ubuntu-latest
    needs: e2e
    if: always()
    timeout-minutes: 45
    continue-on-error: true

    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: "20"
          cache: npm

      - name: Install dependencies
        run: npm ci

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

      - name: RPA probe 3/3 (optional)
        run: bash scripts/ci/run-probe-nightly.sh
        env:
          MOTHERSHIP_USERNAME: ${{ secrets.MOTHERSHIP_USERNAME }}
          MOTHERSHIP_PASSWORD: ${{ secrets.MOTHERSHIP_PASSWORD }}
          MOTHERSHIP_QUOTE_URLS: ${{ secrets.MOTHERSHIP_QUOTE_URLS }}
          PROBE_NIGHTLY_LOG: probe-nightly.log

      - uses: actions/upload-artifact@v4
        if: always()
        with:
          name: probe-nightly-log
          path: probe-nightly.log
          retention-days: 7
          if-no-files-found: ignore

  load:
    runs-on: ubuntu-latest
    needs: e2e
    if: always()

    steps:
      - uses: actions/checkout@v4

      - name: Install k6
        run: |
          sudo gpg -k
          sudo gpg --no-default-keyring --keyring /usr/share/keyrings/k6-archive-keyring.gpg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys C5AD17C747E3415A3642D57D77C6C491D6AC1D69
          echo "deb [signed-by=/usr/share/keyrings/k6-archive-keyring.gpg] https://dl.k6.io/deb stable main" | sudo tee /etc/apt/sources.list.d/k6.list
          sudo apt-get update
          sudo apt-get install k6

      - uses: actions/setup-node@v4
        with:
          node-version: "20"
          cache: npm

      - run: npm ci

      - name: Start app (mock mode)
        run: |
          npm run build
          npm run start &
          sleep 15
        env:
          JWT_SECRET: ci-test-jwt-secret-32chars-min!!
          HOST_SERVICE_TOKENS: '{"demo-host-token":{"customerId":"CUST_001","permissions":["pricing:markup:write"]}}'
          RPA_MOCK_MODE: "true"
          DATABASE_URL: mysql://root:root@127.0.0.1:3306/chajia
          REDIS_URL: redis://127.0.0.1:6379

      - name: k6 rate-limit (smoke)
        continue-on-error: true
        run: k6 run --vus 1 --iterations 5 load-tests/rate-limit.js
        env:
          BASE_URL: http://localhost:3000

      - uses: actions/upload-artifact@v4
        if: always()
        with:
          name: k6-report
          path: load-tests/
          retention-days: 7
