name: Monorepo CI

on:
  push:
    branches:
      - main
    paths:
      - '**/*.ts'
      - '**/*.tsx'
      - '**/*.vue'
      - '**/*.js'
      - 'package.json'
      - 'pnpm-lock.yaml'
      - 'pnpm-workspace.yaml'
      - 'turbo.json'
      - 'apps/**'
      - 'packages/**'
      - '.github/workflows/ci.yml'

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

jobs:
  quality-gate:
    name: Typecheck, Lint, Tests, Build
    runs-on: ubuntu-latest
    timeout-minutes: 30

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

      - name: Setup pnpm
        uses: pnpm/action-setup@v4
        with:
          run_install: false
          package_json_path: package.json

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

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

      - name: Cache turbo
        uses: actions/cache@v4
        with:
          path: |
            .turbo
            node_modules/.cache/turbo
          key: ${{ runner.os }}-turbo-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-turbo-

      - name: Type check (monorepo)
        run: pnpm type-check

      - name: Lint (monorepo)
        run: pnpm lint

      - name: Run unit tests
        run: pnpm test:unit

      - name: Run integration tests
        run: pnpm test:integration

      - name: Build (monorepo)
        run: pnpm build

      - name: Upload coverage artifacts
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: coverage-artifacts
          path: |
            **/coverage/**
          if-no-files-found: ignore

      - name: Secret scan (gitleaks)
        uses: zricethezav/gitleaks-action@v2
        with:
          args: '--redact'
