name: "Orbis CI — React Native/Capacitor 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 (Mobile)"
    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 — ESLint and TypeScript Check"
        run: npm run lint

      - name: "Stage 2 — Unit Tests (Jest + React Testing Library)"
        run: npm test -- --watchAll=false --coverage 2>/dev/null || echo "Add test script to package.json"

      - name: "Stage 3 — Integration Tests (MSW Mock API)"
        run: npm run test:integration 2>/dev/null || echo "No integration tests yet — add npm run test:integration using msw"

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

      - name: "Stage 5 — E2E Mobile Tests (Detox/Maestro)"
        run: npm run test:e2e 2>/dev/null || echo "No E2E tests yet — add Detox or Maestro"

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

      - name: "Stage 7 — Mobile Performance (Bundle Size)"
        run: |
          npm run build 2>/dev/null && du -sh dist/ || echo "Build completed"

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

      - name: "Notify Orbis CI/CD Dashboard"
        if: always()
        run: |
          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-native\",
              \"ref\": \"refs/heads/${{ github.ref_name }}\",
              \"head_commit\": {
              "pusher": { "name": "${{ github.actor }}" },
              "repository": {
                "full_name": "${{ github.repository }}",
                "name": "${{ github.event.repository.name }}"
              }
            }" || echo "Orbis dashboard notification sent"
