name: Build

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

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Use Node.js 20.x
        uses: actions/setup-node@v4
        with:
          node-version: 20.x
          cache: "npm"

      - name: Install dependencies
        run: npm ci

      - name: Build client
        run: npm run build
        env:
          VITE_SUPABASE_URL: ${{ secrets.VITE_SUPABASE_URL }}
          VITE_SUPABASE_ANON_KEY: ${{ secrets.VITE_SUPABASE_ANON_KEY }}
          VITE_API_BASE: ${{ secrets.VITE_API_BASE }}
          VITE_DISCORD_CLIENT_ID: ${{ secrets.VITE_DISCORD_CLIENT_ID }}
          VITE_DEVCONNECT_URL: ${{ secrets.VITE_DEVCONNECT_URL }}
          VITE_DEVCONNECT_ANON_KEY: ${{ secrets.VITE_DEVCONNECT_ANON_KEY }}
          VITE_BUILDER_API_KEY: ${{ secrets.VITE_BUILDER_API_KEY }}

      - name: Check bundle size
        run: |
          if [ -d "dist" ]; then
            echo "Build artifacts:"
            du -sh dist/
            find dist -type f -name "*.js" -o -name "*.css" | head -20
          fi

      - name: Upload build artifacts
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: dist
          path: dist/
          retention-days: 5
