name: PR Build Check

on:
  pull_request:
    branches:
      - main
      - dev
    paths:
      - 'source/js/**'
      - 'source/css/**'
      - 'scripts/**'
      - 'package.json'
      - 'package-lock.json'

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Repository
        uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Check for committed build artifacts
        if: ${{ !(github.base_ref == 'main' && github.head_ref == 'dev' && github.event.pull_request.head.repo.full_name == github.repository) }}
        run: |
          git fetch origin "${{ github.base_ref }}"
          if git diff --name-only "origin/${{ github.base_ref }}"...HEAD | grep -qE '^source/(js/build/|css/build/)'; then
            echo "::error::Build artifacts are not allowed in PRs (except same-repo dev -> main)."
            echo "Remove files under source/js/build or source/css/build from the commit."
            exit 1
          fi

      - name: Set up Node.js
        uses: actions/setup-node@v6
        with:
          node-version: '20.x'

      - name: Install Dependencies
        run: npm install

      - name: Build Project
        run: npm run build
