name: Build

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

jobs:
  build:
    runs-on: ubuntu-latest

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

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

      - name: Install dependencies
        run: npm ci

      - name: Lint
        run: npm run lint

      - name: Validate manifest.json version
        run: |
          MANIFEST_VERSION=$(node -p "require('./manifest.json').version || ''")
          PACKAGE_VERSION=$(node -p "require('./package.json').version")
          if [ -z "$MANIFEST_VERSION" ]; then
            echo "::error::manifest.json is missing the 'version' field"
            exit 1
          fi
          if [ "$MANIFEST_VERSION" != "$PACKAGE_VERSION" ]; then
            echo "::error::manifest.json version ($MANIFEST_VERSION) does not match package.json version ($PACKAGE_VERSION)"
            exit 1
          fi

      - name: Build
        run: npm run build

      - name: Upload build artifacts
        uses: actions/upload-artifact@v4
        with:
          name: build
          path: |
            main.js
            manifest.json
            styles.css
