# ─── IliaCloud CI Pipeline ────────────────────────────────────────────────────
# Vérifie le build backend + frontend à chaque push et pull request.

name: CI

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

jobs:
  # ─── Backend ──────────────────────────────────────────────────────────────
  backend:
    name: Backend — Build & Audit
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: ./backend
    steps:
      - uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'npm'
          cache-dependency-path: './backend/package-lock.json'

      - name: Install dependencies
        run: npm ci

      - name: Check for vulnerabilities
        run: npm audit --omit=dev --audit-level=high
        continue-on-error: true

      - name: Verify startup (syntax check)
        run: node -e "require('./src/index.js')" &
        env:
          DATABASE_URL: postgresql://test:test@localhost:5432/test
          JWT_SECRET: test-secret-for-ci-only-not-production
          JWT_REFRESH_SECRET: test-refresh-secret-for-ci-only
          ENCRYPTION_KEY: '0000000000000000000000000000000000000000000000000000000000000000'
          PORT: 3001
        timeout-minutes: 1
        continue-on-error: true

  # ─── Frontend ─────────────────────────────────────────────────────────────
  frontend:
    name: Frontend — Build
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: ./frontend
    steps:
      - uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'npm'
          cache-dependency-path: './frontend/package-lock.json'

      - name: Install dependencies
        run: npm ci

      - name: Build
        run: npm run build

      - name: Check bundle size
        run: |
          echo "── Bundle size ──"
          du -sh dist/
          du -sh dist/assets/*.js | sort -rh | head -5
