name: Publish to npm

on:
  push:
    branches:
      - main

jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0 # Fetch all history for changesets to work correctly

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '22' # Specify the Node.js version you want to use
          registry-url: 'https://registry.npmjs.org'

      - name: Install dependencies
        run: npm ci

      - name: Build the project
        run: npm run build

      - name: Publish to npm
        run: npm run release
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} # Use the NPM_TOKEN from GitHub secrets

      - name: Trigger release workflow
        if: success() # Only trigger if publish succeeds
        run: |
          gh workflow run release.yml -f version=${{ github.sha }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}