name: Publish to NPM and GitHub

on:
  workflow_dispatch:
    inputs:
      release-type:
        description: What is the type of this release?
        required: true
        type: choice
        options:
          - major
          - minor
          - patch
        default: minor

env:
  GITHUB_AUTH: ${{ secrets.PUSH_TOKEN }}
  PUSH_TOKEN: ${{ secrets.PUSH_TOKEN }}
  GPG_PRIVATE_KEY: ${{ secrets.GPG_PRIVATE_KEY }}
  GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }}
  NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

jobs:
  publish:
    runs-on: ubuntu-latest

    steps:
      - name: Check out Git repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          token: ${{ env.PUSH_TOKEN }}

      - name: Set Git Config
        run: |
          git config user.name "${GITHUB_ACTOR}"
          git config user.email "${GITHUB_ACTOR}@users.noreply.github.com"

      - name: Import GPG key
        uses: crazy-max/ghaction-import-gpg@v6
        with:
          gpg_private_key: ${{ env.GPG_PRIVATE_KEY }}
          passphrase: ${{ env.GPG_PASSPHRASE }}
          git_user_signingkey: true
          git_commit_gpgsign: true

      - uses: pnpm/action-setup@v4

      - name: Setup Node v18
        uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: 'pnpm'

      - name: NPM Setup
        run: pnpm config set //registry.npmjs.org/:_authToken $NPM_TOKEN

      - name: Install dependencies
        run: pnpm install && pnpm build

      - name: Release
        run: pnpm release.${{ github.event.inputs.release-type }}
