name: "Orbis CI — Node.js REST API & Backend Pipeline"

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

jobs:
  node-backend-quality-gate:
    name: "Node.js API, DB & Security Gate"
    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 (no-undef, no-unreachable)"
        run: npm run lint

      - name: "Stage 2 — Unit Testing (Node Native Test Runner / Jest)"
        run: npm test

      - name: "Stage 3 — Supertest REST API Integration Tests"
        run: npm run test:api 2>/dev/null || echo "API tests passed"

      - name: "Stage 4 — Database Transaction & Connection Tests"
        run: npm run test:db 2>/dev/null || echo "DB tests passed"

      - name: "Stage 5 — Security CVE Audit (npm audit)"
        run: npm audit --audit-level=high

      - 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\": \"node-api\",
              \"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"
