name: Publish to npm

on:
  release:
    types: [published]

permissions:
  contents: read
  id-token: write

jobs:
  publish:
    runs-on: ubuntu-latest
    environment: NPM_TOKEN
    steps:
      - name: Checkout
        uses: actions/checkout@v6

      - name: Setup Node.js
        uses: actions/setup-node@v6
        with:
          node-version: 22
          registry-url: https://registry.npmjs.org

      - name: Install dependencies
        run: npm ci

      - name: Build standalone app
        run: npm run build:cli

      - name: Sync version from release tag
        run: |
          VERSION="${GITHUB_REF_NAME}"
          # Remove 'v' prefix if present (v0.1.0 -> 0.1.0)
          VERSION="${VERSION#v}"
          npm version "$VERSION" --no-git-tag-version --allow-same-version
          echo "Publishing version: $VERSION"

      - name: Publish to npm
        run: npm publish --access public
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
