name: Release npm packages (sdk)

on:
  push:
    branches:
      - main
    paths:
      - 'libs/sdk/**'
  workflow_dispatch:
    inputs:
      release_channel:
        description: npm dist-tag and prerelease lane.
        required: true
        type: choice
        default: stable
        options:
          - stable
          - rc
          - beta
          - alpha
      version_specifier:
        description: Nx release specifier. Use auto for conventional commits.
        required: true
        type: choice
        default: auto
        options:
          - auto
          - patch
          - minor
          - major
          - prerelease
          - prepatch
          - preminor
          - premajor
      dry_run:
        description: Preview versioning and npm publish without changing git or npm.
        required: true
        type: boolean
        default: true
      first_release:
        description: Use only for the very first release (no prior nx-managed git tag exists).
        required: true
        type: boolean
        default: false
      allow_major:
        description: Required when intentionally publishing a major release.
        required: true
        type: boolean
        default: false

permissions:
  contents: write
  id-token: write

concurrency:
  # Shared group with release-cli so both workflows serialize on the same branch.
  # cancel-in-progress: false — never cancel a release mid-flight.
  group: release-main
  cancel-in-progress: false

jobs:
  release:
    name: ${{ (inputs.dry_run == 'true' || inputs.dry_run == true) && 'Preview' || 'Publish' }} sdk ${{ inputs.release_channel || 'stable' }}
    runs-on: ubuntu-latest
    environment: npm-release
    # Skip when triggered by a release/skip-ci commit to break the re-release loop.
    # workflow_run.head_commit.message reflects the HEAD at CI completion time; checking
    # it here prevents re-releasing when the contributors sync or a prior release pushed
    # a [skip ci] commit between CI start and finish.
    if: >-
      github.event_name == 'workflow_dispatch' || (
        github.event_name == 'push' &&
        !startsWith(github.event.head_commit.message, 'chore(release):') &&
        !contains(github.event.head_commit.message, '[skip ci]')
      )

    steps:
      - name: Resolve effective inputs
        id: effective
        shell: bash
        run: |
          # push trigger: inputs are empty — use stable/auto/no-dry-run defaults.
          echo "dry_run=${{ inputs.dry_run == '' && 'false' || inputs.dry_run }}" >> "$GITHUB_OUTPUT"
          echo "release_channel=${{ inputs.release_channel == '' && 'stable' || inputs.release_channel }}" >> "$GITHUB_OUTPUT"
          echo "version_specifier=${{ inputs.version_specifier == '' && 'auto' || inputs.version_specifier }}" >> "$GITHUB_OUTPUT"
          echo "first_release=${{ inputs.first_release == '' && 'false' || inputs.first_release }}" >> "$GITHUB_OUTPUT"
          echo "allow_major=${{ inputs.allow_major == '' && 'false' || inputs.allow_major }}" >> "$GITHUB_OUTPUT"

      - name: Checkout
        uses: actions/checkout@08eba0b27e820071cde6df949e0beb9ba4906955 # v4.3.0
        with:
          fetch-depth: 0
          fetch-tags: true
          persist-credentials: true
          token: ${{ secrets.BOT_PAT || secrets.GITHUB_TOKEN }}

      - name: Release prelude
        uses: ./.github/actions/release-prelude
        with:
          dry_run: ${{ steps.effective.outputs.dry_run }}
          bot_pat: ${{ secrets.BOT_PAT }}

      - name: Configure release
        id: release
        uses: ./.github/actions/configure-release-version
        with:
          release_channel: ${{ steps.effective.outputs.release_channel }}
          version_specifier: ${{ steps.effective.outputs.version_specifier }}
          allow_major: ${{ steps.effective.outputs.allow_major }}
          git_ref: ${{ github.ref_name }}

      - name: Preflight tests
        run: pnpm nx run sdk:test

      - name: Preview version + changelog (dry run)
        if: steps.effective.outputs.dry_run == 'true'
        shell: bash
        run: |
          set -euo pipefail
          args=(release --projects=sdk --skip-publish --dry-run --yes)
          specifier='${{ steps.release.outputs.specifier }}'
          preid='${{ steps.release.outputs.preid }}'
          if [[ "$specifier" != 'auto' ]]; then args+=("$specifier"); fi
          if [[ -n "$preid" ]]; then args+=(--preid "$preid"); fi
          if [[ '${{ steps.effective.outputs.first_release }}' = 'true' ]]; then args+=(--first-release); fi
          pnpm nx "${args[@]}"
          npm publish dist/libs/sdk --dry-run --access public --tag '${{ steps.release.outputs.dist_tag }}'

      - name: Release sdk
        if: steps.effective.outputs.dry_run != 'true'
        id: sdk
        uses: ./.github/actions/nx-release-package
        with:
          project: sdk
          package_json_path: libs/sdk/package.json
          dist_path: dist/libs/sdk
          dist_tag: ${{ steps.release.outputs.dist_tag }}
          npm_access: public
          npm_token: ${{ secrets.NPM_TOKEN }}
          first_release: ${{ steps.effective.outputs.first_release }}
          specifier: ${{ steps.release.outputs.specifier }}
          preid: ${{ steps.release.outputs.preid }}
          git_ref: ${{ github.ref_name }}
