name: Release

on:
  push:
    tags:
      - "v*"

permissions:
  contents: read
  id-token: write

jobs:
  publish:
    runs-on: ubuntu-latest

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

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 24
          registry-url: https://registry.npmjs.org
          cache: npm

      - name: Ensure tag matches package version
        run: |
          PACKAGE_VERSION="$(node -p "require('./package.json').version")"
          TAG_VERSION="${GITHUB_REF_NAME#v}"

          if [ "$PACKAGE_VERSION" != "$TAG_VERSION" ]; then
            echo "package.json version ($PACKAGE_VERSION) does not match tag ($TAG_VERSION)"
            exit 1
          fi

      - name: Install dependencies
        run: npm ci

      - name: Build
        run: npm run build

      - name: Verify publish artifact
        run: npm pack --dry-run

      - name: Publish to npm
        run: npm publish --access public
