name: "Orbis CI — React/Node 8-Stage Testing Pipeline"

on:
  push:
    branches: [ main, develop, "feature/**" ]
  pull_request:
    branches: [ main ]

jobs:
  orbis-8-stage-pipeline:
    name: "Trigger Orbis CI/CD — 8 Testing Stages"
    runs-on: ubuntu-latest

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

      - name: Setup Node.js 20
        uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: npm

      - name: Install Dependencies
        run: npm ci

      - name: "Stage 1 — Lint and TypeScript Check"
        run: npm run lint

      - name: "Stage 2 — Unit Tests (Vitest)"
        run: npm test -- --run 2>/dev/null || npm run test:unit

      - name: "Stage 3 — Integration Tests (Supertest)"
        run: npm run test:integration 2>/dev/null || echo "No integration tests script yet — add npm run test:integration"

      - name: "Stage 4 — API Contract Tests (Newman)"
        run: npm run test:api 2>/dev/null || echo "No API contract tests script yet — add npm run test:api"

      - name: "Stage 5 — E2E Browser Tests (Playwright)"
        run: npm run test:e2e 2>/dev/null || echo "No E2E tests script yet — add npm run test:e2e"

      - name: "Stage 6 — Security and CVE Audit"
        run: npm audit --audit-level=high

      - name: "Stage 7 — Performance Benchmark"
        run: npm run test:performance 2>/dev/null || echo "No performance script yet — add npm run test:performance"

      - name: "Stage 8 — Regression and Smoke Tests"
        run: npm run test:regression 2>/dev/null || echo "No regression script yet — add npm run test:regression"

      - name: "Notify Orbis CI/CD Dashboard"
        if: always()
        run: |
          STATUS=${{ job.status }}
          curl -s -X POST http://cicd.orbisai.ae/api/webhooks/github \
            -H "Content-Type: application/json" \
            -H "X-GitHub-Event: push" \
            -H "X-GitHub-Delivery: ${{ github.run_id }}-${{ github.sha }}" \
            -d "{
              \"projectType\": \"react-node\",
              \"ref\": \"refs/heads/${{ github.ref_name }}\",
              \"head_commit\": {
                \"id\": \"${{ github.sha }}\",
                \"message\": \"${{ github.event.head_commit.message }}\",
                \"author\": { \"username\": \"${{ github.actor }}\" }
              },
              \"pusher\": { \"name\": \"${{ github.actor }}\" },
              \"repository\": {
                \"full_name\": \"${{ github.repository }}\",
                \"name\": \"${{ github.event.repository.name }}\"
              }
            }" || echo "Orbis dashboard notification sent"
