# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
# For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages

name: Node.js Package

on:
  push:
    tags:
      - v*

permissions:
  contents: read
  id-token: write

jobs:
  publish-npm:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 24
          registry-url: https://registry.npmjs.org/

      - run: npm ci

      - name: Determine npm tag
        id: npm_tag
        run: |
          TAG_NAME="${GITHUB_REF_NAME}"
          echo "Detected tag: $TAG_NAME"

          if [[ "$TAG_NAME" =~ -alpha[0-9]*$ ]]; then
            echo "tag=alpha" >> $GITHUB_OUTPUT
          elif [[ "$TAG_NAME" =~ -beta[0-9]*$ ]]; then
            echo "tag=beta" >> $GITHUB_OUTPUT
          elif [[ "$TAG_NAME" =~ -rc[0-9]*$ ]]; then
            echo "tag=rc" >> $GITHUB_OUTPUT
          else
            echo "tag=latest" >> $GITHUB_OUTPUT
          fi

      - name: Publish to npm (Trusted Publishing)
        run: npm publish --access public --tag ${{ steps.npm_tag.outputs.tag }}

  gh-release:
    runs-on: ubuntu-latest
    permissions: write-all
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: debug
        run: |
          echo "${{ contains(github.ref_name, 'dev') }}"
      - name: Release
        uses: softprops/action-gh-release@v2
        # if: "!startsWith(github.ref_name, 'v-test-')"
        with:
          generate_release_notes: true
          prerelease: ${{ contains(github.ref_name, 'dev') }}
