name: Release

on:
  push:
    branches:
      - main

jobs:
  release:
    name: Semantic Release
    runs-on: ubuntu-latest

    permissions:
      contents: write
      issues: write
      pull-requests: write

    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20.19.0
          registry-url: https://registry.npmjs.org/
          always-auth: true
        
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

      - name: Install dependencies
        run: npm ci
      - name: Configure npm authentication
        run: |
          echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > ~/.npmrc
        env:
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
      - name: Run Semantic Release
      
        env:
          GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
        run: npx semantic-release

      - name: Move major version tag (vX) to latest
        if: success()   # only run if release succeeded
        run: |
          # Get the latest released version
          VERSION=$(node -p "require('./package.json').version")
          MAJOR=$(echo $VERSION | cut -d. -f1)

          echo "Latest release version: $VERSION"
          echo "Updating tag v$MAJOR -> $VERSION"

          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"

          # Move the lightweight tag (force update to latest major)
          git tag -f v$MAJOR
          git push origin -f v$MAJOR
