name: Version Bump Desktop UI

on:
  workflow_dispatch:
    inputs:
      version_type:
        description: 'Version increment type'
        required: true
        default: 'patch'
        type: 'choice'
        options: [patch, minor, major, prepatch, preminor, premajor, prerelease]
      pre_release:
        description: Pre-release ID (suffix)
        required: false
        default: ''
        type: string
      branch:
        description: 'Base branch to bump (e.g., main, core/1.29, core/1.30)'
        required: true
        default: 'main'
        type: string

jobs:
  bump-version-desktop-ui:
    runs-on: ubuntu-latest
    permissions:
      contents: write
      pull-requests: write

    steps:
      - name: Checkout repository
        uses: actions/checkout@v6
        with:
          ref: ${{ github.event.inputs.branch }}
          fetch-depth: 0
          persist-credentials: false

      - name: Validate branch exists
        run: |
          BRANCH="${{ github.event.inputs.branch }}"
          if ! git show-ref --verify --quiet "refs/heads/$BRANCH" && ! git show-ref --verify --quiet "refs/remotes/origin/$BRANCH"; then
            echo "❌ Branch '$BRANCH' does not exist"
            echo ""
            echo "Available core branches:"
            git branch -r | grep 'origin/core/' | sed 's/.*origin\//  - /' || echo "  (none found)"
            echo ""
            echo "Main branch:"
            echo "  - main"
            exit 1
          fi
          echo "✅ Branch '$BRANCH' exists"

      - name: Install pnpm
        uses: pnpm/action-setup@41ff72655975bd51cab0327fa583b6e92b6d3061 # v4.2.0
        with:
          version: 10

      - name: Setup Node.js
        uses: actions/setup-node@v6
        with:
          node-version: '24.x'
          cache: 'pnpm'

      - name: Bump desktop-ui version
        id: bump-version
        env:
          VERSION_TYPE: ${{ github.event.inputs.version_type }}
          PRE_RELEASE: ${{ github.event.inputs.pre_release }}
        run: |
          pnpm -C apps/desktop-ui version "$VERSION_TYPE" --preid "$PRE_RELEASE" --no-git-tag-version
          NEW_VERSION=$(node -p "require('./apps/desktop-ui/package.json').version")
          echo "NEW_VERSION=$NEW_VERSION" >> $GITHUB_OUTPUT

      - name: Format PR string
        id: capitalised
        env:
          VERSION_TYPE: ${{ github.event.inputs.version_type }}
        run: |
          echo "capitalised=${VERSION_TYPE@u}" >> $GITHUB_OUTPUT

      - name: Create Pull Request
        uses: peter-evans/create-pull-request@c0f553fe549906ede9cf27b5156039d195d2ece0 # v8.1.0
        with:
          token: ${{ secrets.PR_GH_TOKEN }}
          commit-message: '[release] Increment desktop-ui to ${{ steps.bump-version.outputs.NEW_VERSION }}'
          title: desktop-ui ${{ steps.bump-version.outputs.NEW_VERSION }}
          body: |
            ${{ steps.capitalised.outputs.capitalised }} version increment for @comfyorg/desktop-ui to ${{ steps.bump-version.outputs.NEW_VERSION }}

            **Base branch:** `${{ github.event.inputs.branch }}`
          branch: desktop-ui-version-bump-${{ steps.bump-version.outputs.NEW_VERSION }}
          base: ${{ github.event.inputs.branch }}
          labels: |
            Release
