name: Create release # You may choose a different name
run-name: ${{ inputs.releaseversion }} # Enumerates entries in the "workflow runs" view
on:
  workflow_dispatch:
    inputs:
      releaseversion:
        description: 'Release version'
        required: true
        type: string
        default: 'X.Y.Z'

jobs:
  release: # Arbitrarily chosen
    name: Release
    runs-on: ubuntu-latest
    permissions:
      contents: write
      packages: write
      attestations: write
      id-token: write
    steps:
      - uses: actions/create-github-app-token@v3
        id: app-token
        with:
          app-id: ${{ vars.CI_APP_ID }}
          private-key: ${{ secrets.CI_PRIVATE_KEY }}

      - name: Checkout
        uses: actions/checkout@v7
        with:
          fetch-depth: 0
          token: ${{ steps.app-token.outputs.token }}
          ref: ${{ github.head_ref }}

      - name: Get GitHub App User ID
        id: get-user-id
        run: echo "user-id=$(gh api "/users/${{ steps.app-token.outputs.app-slug }}[bot]" --jq .id)" >> "$GITHUB_OUTPUT"
        env:
          GH_TOKEN: ${{ steps.app-token.outputs.token }}

      - name: Configure Git author
        run: |
          git config --global user.name '${{ steps.app-token.outputs.app-slug }}[bot]'
          git config --global user.email '${{ steps.get-user-id.outputs.user-id }}+${{ steps.app-token.outputs.app-slug }}[bot]@users.noreply.github.com>'

      - name: Setup NodeJS
        uses: actions/setup-node@v7
        with:
          node-version: '24'
          registry-url: 'https://registry.npmjs.org'

      - name: Package Application
        run: |
          npm run clean
          npm version --no-git-tag-version ${{ github.event.inputs.releaseversion }} || true
          npm run build

      - name: Generate Changelog
        uses: orhun/git-cliff-action@v4
        with:
          config: cliff.toml
          args: --tag v${{ inputs.releaseversion }} --prepend CHANGELOG.md --unreleased
        env:
          GITHUB_TOKEN: ${{ steps.app-token.outputs.token }}

      - name: Publish npm package
        run: |
          npm publish

      - name: Commit Changelog
        run: |
          git add CHANGELOG.md
          git diff --staged --quiet || git commit -m "chore(release): update changelog for v${{ inputs.releaseversion }}"
          git push

      - name: Create Release on GH
        id: tag-and-release
        uses: avakar/tag-and-release@v1.0.0
        with:
          draft: true
          release_name: ${{ github.event.inputs.releaseversion }}
          tag_name: v${{ github.event.inputs.releaseversion }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
