name: Release

on:
  workflow_dispatch:
    inputs:
      increment:
        description: 'Version bump: patch, minor, major, or explicit (e.g. 1.27.0)'
        required: true
        default: 'patch'
        type: string
      release_name:
        description: 'Custom release name (optional, defaults to "CloudCLI UI vX.Y.Z")'
        required: false
        type: string

permissions:
  contents: read

# This workflow publishes releases with write credentials, so actions are pinned
# to immutable commit SHAs. The trailing comments keep the original major tag
# visible for maintenance context.
jobs:
  build-macos-semantic-helper:
    strategy:
      fail-fast: false
      matrix:
        include:
          - runs_on: macos-15
            target_dir: darwin-arm64
          - runs_on: macos-15-intel
            target_dir: darwin-x64
    runs-on: ${{ matrix.runs_on }}
    permissions:
      contents: read
    steps:
      - uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6
        with:
          persist-credentials: false
      - uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6
        with:
          node-version: 22
      - name: Build macOS semantic helper
        run: node scripts/build-computer-semantics.mjs
        env:
          CLOUDCLI_SEMANTICS_BUILD_REQUIRED: "1"
      - name: Verify macOS semantic helper target
        run: test -x "server/modules/computer-use/semantics/bin/${{ matrix.target_dir }}/CloudCLISemantics"
      - name: Stage macOS semantic helper artifact
        run: |
          mkdir -p "semantic-helper-artifact/${{ matrix.target_dir }}"
          cp "server/modules/computer-use/semantics/bin/${{ matrix.target_dir }}/CloudCLISemantics" "semantic-helper-artifact/${{ matrix.target_dir }}/"
      - uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6
        with:
          name: semantic-helper-${{ matrix.target_dir }}
          path: semantic-helper-artifact/*
          if-no-files-found: error

  build-windows-semantic-helper:
    strategy:
      fail-fast: false
      matrix:
        include:
          - runs_on: windows-2025
            target_dir: win32-x64
          - runs_on: windows-11-arm
            target_dir: win32-arm64
    runs-on: ${{ matrix.runs_on }}
    permissions:
      contents: read
    steps:
      - uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6
        with:
          persist-credentials: false
      - uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6
        with:
          node-version: 22
      - name: Build Windows semantic helper
        run: node scripts/build-computer-semantics.mjs
        env:
          CLOUDCLI_SEMANTICS_BUILD_REQUIRED: "1"
      - name: Verify Windows semantic helper target
        shell: bash
        run: test -f "server/modules/computer-use/semantics/bin/${{ matrix.target_dir }}/CloudCLISemantics.exe"
      - name: Stage Windows semantic helper artifact
        shell: bash
        run: |
          mkdir -p "semantic-helper-artifact/${{ matrix.target_dir }}"
          cp "server/modules/computer-use/semantics/bin/${{ matrix.target_dir }}/CloudCLISemantics.exe" "semantic-helper-artifact/${{ matrix.target_dir }}/"
      - uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6
        with:
          name: semantic-helper-${{ matrix.target_dir }}
          path: semantic-helper-artifact/*
          if-no-files-found: error

  release:
    needs:
      - build-macos-semantic-helper
      - build-windows-semantic-helper
    runs-on: ubuntu-latest
    permissions:
      contents: write
      id-token: write
    steps:
      - uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6
        with:
          fetch-depth: 0
          token: ${{ secrets.RELEASE_PAT }}

      - uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6
        with:
          node-version: 22
          registry-url: https://registry.npmjs.org

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

      - run: npm ci

      - uses: actions/download-artifact@018cc2cf5baa6db3ef3c5f8a56943fffe632ef53 # v6
        with:
          pattern: semantic-helper-*
          path: server/modules/computer-use/semantics/bin
          merge-multiple: true

      - name: Verify bundled semantic helpers
        run: |
          test -x server/modules/computer-use/semantics/bin/darwin-arm64/CloudCLISemantics
          test -x server/modules/computer-use/semantics/bin/darwin-x64/CloudCLISemantics
          test -f server/modules/computer-use/semantics/bin/win32-x64/CloudCLISemantics.exe
          test -f server/modules/computer-use/semantics/bin/win32-arm64/CloudCLISemantics.exe
          find server/modules/computer-use/semantics/bin -maxdepth 2 -type f -print

      - name: Release
        run: |
          ARGS="--ci --increment=${{ inputs.increment }}"
          if [ -n "${{ inputs.release_name }}" ]; then
            ARGS="$ARGS --github.releaseName=\"${{ inputs.release_name }}\""
          fi
          npx release-it $ARGS
        env:
          GITHUB_TOKEN: ${{ secrets.RELEASE_PAT }}
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
