name: Trivy Security Scan

on:
  push:
    branches: ['**']
  pull_request:
    branches: [main]
  schedule:
    - cron: '0 3 * * *'  # Daily at 3 AM UTC
  workflow_dispatch:

permissions:
  contents: read
  security-events: write

jobs:
  scan:
    name: Trivy vulnerability scan
    runs-on: ubuntu-latest

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

      - name: Run Trivy vulnerability scanner (filesystem)
        uses: aquasecurity/trivy-action@0.34.0
        with:
          scan-type: 'fs'
          scan-ref: '.'
          format: 'sarif'
          output: 'trivy-results.sarif'
          severity: 'CRITICAL,HIGH,MEDIUM'
          ignore-unfixed: false

      - name: Upload Trivy results to GitHub Security
        uses: github/codeql-action/upload-sarif@v3
        if: always()
        with:
          sarif_file: 'trivy-results.sarif'

      - name: Run Trivy for npm dependencies
        uses: aquasecurity/trivy-action@0.34.0
        with:
          scan-type: 'fs'
          scan-ref: '.'
          scanners: 'vuln'
          format: 'table'
          exit-code: 0
          severity: 'CRITICAL,HIGH'
