name: Release

on:
  push:
    tags:
      - '[0-9]*'

permissions:
  id-token: write
  contents: write

jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
        with:
          ref: main

      - uses: actions/setup-node@v6
        with:
          node-version: '24'
          registry-url: 'https://registry.npmjs.org'
          package-manager-cache: false

      - name: Set version from tag
        run: npm version "$GITHUB_REF_NAME" --no-git-tag-version

      - run: npm publish

      - name: Commit version back to repo
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git add package.json
          git commit -m "chore: bump version to $GITHUB_REF_NAME"
          git push origin main

      - name: Create GitHub Release
        run: gh release create "$GITHUB_REF_NAME" --generate-notes --title "$GITHUB_REF_NAME"
        env:
          GH_TOKEN: ${{ github.token }}