name: CI

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  ci:
    runs-on: ubuntu-latest
    timeout-minutes: 20

    steps:
      - uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version-file: '.nvmrc'

      - name: Setup pnpm
        uses: pnpm/action-setup@v4

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

      - name: Typecheck
        run: pnpm typecheck

      - name: Lint
        run: pnpm lint

      - name: Test
        run: pnpm test

      - name: Test coverage
        run: pnpm test:coverage

      - name: Build both targets
        run: pnpm build:both

      - name: Bundle size gate
        run: pnpm bundle-size

      - name: Validate MF manifest
        run: |
          MANIFEST=$(find dist/remote -name "mf-manifest.json" | head -1)
          node -e "
            const m = JSON.parse(require('fs').readFileSync('$MANIFEST','utf8'));
            const paths = m.exposes.map(e => e.path);
            const required = ['./OnlineBoard','./Schedule','./FlightsMap','./PopularRequests'];
            const missing = required.filter(r => !paths.includes(r));
            if (missing.length) { console.error('MISSING:', missing); process.exit(1); }
            console.log('All 4 exposes verified:', paths);
          "

      - name: Security audit
        run: pnpm audit 2>/dev/null || echo "Audit endpoint unavailable — manual review required"
        continue-on-error: true
