name: CodeQL Analysis

on:
  push:
    branches: [master]
  pull_request:
    branches: [master]
  schedule:
    # Run weekly on Monday at 06:17 UTC — off-peak to avoid :00/:30 congestion
    - cron: "17 6 * * 1"

concurrency:
  group: codeql-${{ github.ref }}
  cancel-in-progress: true

permissions:
  actions: read
  contents: read
  security-events: write

jobs:
  analyze:
    name: CodeQL (${{ matrix.language }})
    runs-on: ubuntu-latest
    timeout-minutes: 30

    strategy:
      fail-fast: false
      matrix:
        language: [javascript-typescript]

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

      - name: Initialize CodeQL
        uses: github/codeql-action/init@v3
        with:
          languages: ${{ matrix.language }}
          # Use extended security queries for deeper analysis
          queries: security-extended,security-and-quality
          config: |
            paths:
              - apps/
              - libs/
            paths-ignore:
              - node_modules/
              - "**/dist/"
              - "**/*.spec.ts"
              - "**/*.test.ts"
              - "**/__tests__/"

      - name: Autobuild
        uses: github/codeql-action/autobuild@v3

      - name: Perform CodeQL Analysis
        uses: github/codeql-action/analyze@v3
        with:
          category: "/language:${{ matrix.language }}"
          # SARIF results are automatically uploaded to GitHub Security tab
          upload: always
