name: Security Audit

on:
  schedule:
    # Run weekly on Monday at 00:00 UTC
    - cron: '0 0 * * 1'
  workflow_dispatch:
  push:
    branches: [main]
    paths:
      - 'packages/**'
      - 'services/**'
      - 'apps/**'
      - '.github/workflows/security-audit.yml'

jobs:
  security-audit:
    name: Security Audit
    runs-on: ubuntu-latest
    continue-on-error: true
    permissions:
      contents: read
      security-events: write
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Setup pnpm
        uses: pnpm/action-setup@v2
        with:
          version: 8

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '18'
          cache: 'pnpm'

      - name: Install dependencies
        run: pnpm install --frozen-lockfile

      - name: Run security audit script
        run: |
          chmod +x scripts/security-audit.sh
          ./scripts/security-audit.sh

      - name: Upload security audit report
        uses: actions/upload-artifact@v4
        if: always()
        with:
          name: security-audit-report
          path: |
            security-audit-*.md
            security-audit-*.log
          retention-days: 30

      - name: Run Trivy vulnerability scanner
        uses: aquasecurity/trivy-action@master
        continue-on-error: true
        with:
          scan-type: 'fs'
          scan-ref: '.'
          format: 'sarif'
          output: 'trivy-results.sarif'
          severity: 'HIGH,CRITICAL'
          exit-code: '0'

      - name: Upload Trivy results to GitHub Security
        uses: github/codeql-action/upload-sarif@v3
        if: always() && hashFiles('trivy-results.sarif') != ''
        continue-on-error: true
        with:
          sarif_file: 'trivy-results.sarif'
          wait-for-processing: false

      - name: Run Grype scan
        uses: anchore/scan-action@v3
        id: grype
        continue-on-error: true
        with:
          path: '.'
          fail-build: false
          severity-cutoff: high

      - name: Upload Grype results
        uses: github/codeql-action/upload-sarif@v3
        if: always() && steps.grype.outputs.sarif != ''
        continue-on-error: true
        with:
          sarif_file: ${{ steps.grype.outputs.sarif }}
          wait-for-processing: false

      - name: Check for security issues
        run: |
          if [ -f security-audit-*.log ]; then
            echo "Security audit completed. Review logs for details."
          fi

  dependency-review:
    name: Dependency Review
    runs-on: ubuntu-latest
    if: github.event_name == 'pull_request'
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Dependency Review
        uses: actions/dependency-review-action@v3
        with:
          fail-on-severity: moderate

  codeql-analysis:
    name: CodeQL Analysis
    runs-on: ubuntu-latest
    continue-on-error: true
    permissions:
      actions: read
      contents: read
      security-events: write
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Initialize CodeQL
        uses: github/codeql-action/init@v3
        continue-on-error: true
        with:
          languages: javascript,typescript

      - name: Autobuild
        uses: github/codeql-action/autobuild@v3
        continue-on-error: true

      - name: Perform CodeQL Analysis
        uses: github/codeql-action/analyze@v3
        continue-on-error: true
