name: Build

on:
  push:
    branches: [main, master, develop, feature/*]
  pull_request:
    branches: [main, master, develop, feature/*]

jobs:
  build-api:
    name: Build API (NestJS)
    runs-on: ubuntu-latest
    permissions:
      contents: read
    defaults:
      run:
        working-directory: ./api

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

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

      - name: Cache node modules
        uses: actions/cache@v4
        with:
          path: ./api/node_modules
          key: ${{ runner.os }}-api-node-${{ hashFiles('api/package.json') }}
          restore-keys: |
            ${{ runner.os }}-api-node-

      - name: Install dependencies
        run: npm install --prefer-offline --no-audit

      - name: Generate Prisma client
        run: npm run prisma:generate

      - name: Build application
        run: npm run build

      - name: Upload build artifacts
        uses: actions/upload-artifact@v4
        with:
          name: api-build
          path: ./api/dist
          retention-days: 7

  build-web:
    name: Build Web (Next.js)
    runs-on: ubuntu-latest
    permissions:
      contents: read
    defaults:
      run:
        working-directory: ./web

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

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

      - name: Cache node modules
        uses: actions/cache@v4
        with:
          path: ./web/node_modules
          key: ${{ runner.os }}-web-node-${{ hashFiles('web/package.json') }}
          restore-keys: |
            ${{ runner.os }}-web-node-

      - name: Install dependencies
        run: npm install --prefer-offline --no-audit

      - name: Build application
        run: npm run build

      - name: Upload build artifacts
        uses: actions/upload-artifact@v4
        with:
          name: web-build
          path: ./web/.next
          retention-days: 7
