name: Publish Package

on:
  push:
    tags:
      - 'v*.*.*'

jobs:
  publish:
    name: Publish to npm
    runs-on: ubuntu-latest
    if: "github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')"
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Use Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          registry-url: 'https://registry.npmjs.org'

      - name: Install dependencies
        run: npm ci

      - name: Build (TypeScript)
        if: hashFiles('tsconfig.json') != ''
        run: npm run build --if-present

      - name: Run tests
        run: npm test --if-present

      - name: Configure npm auth
        run: |
          echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}" > ~/.npmrc

      - name: Verify package.json not private
        run: |
          node -e "const p=require('./package.json'); if(p.private){console.error('package.json has private: true — aborting publish'); process.exit(1);} console.log('package.json OK');"

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