name: Release Preview

on:
  workflow_dispatch:
    inputs:
      title:
        description: "Conventional title to evaluate (defaults to the latest commit title on this ref)"
        required: false
        type: string
      compound_engineering_bump:
        description: "compound-engineering bump override"
        required: false
        type: choice
        options: [auto, patch, minor, major]
        default: auto
      marketplace_bump:
        description: "marketplace bump override"
        required: false
        type: choice
        options: [auto, patch, minor, major]
        default: auto
      cursor_marketplace_bump:
        description: "cursor-marketplace bump override"
        required: false
        type: choice
        options: [auto, patch, minor, major]
        default: auto

jobs:
  preview:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Setup Bun
        uses: oven-sh/setup-bun@v2
        with:
          bun-version: latest

      - name: Install dependencies
        run: bun install --frozen-lockfile

      - name: Determine title and changed files
        id: inputs
        shell: bash
        run: |
          TITLE="${{ github.event.inputs.title }}"
          if [ -z "$TITLE" ]; then
            TITLE="$(git log -1 --pretty=%s)"
          fi

          FILES="$(git diff --name-only HEAD~1...HEAD | tr '\n' ' ')"

          echo "title=$TITLE" >> "$GITHUB_OUTPUT"
          echo "files=$FILES" >> "$GITHUB_OUTPUT"

      - name: Add preview note
        run: |
          echo "This preview currently evaluates the selected ref from its latest commit title and changed files." >> "$GITHUB_STEP_SUMMARY"
          echo "It is side-effect free, but it does not yet reconstruct the full accumulated open release PR state." >> "$GITHUB_STEP_SUMMARY"

      - name: Validate release metadata
        run: bun run release:validate

      - name: Preview release
        shell: bash
        run: |
          TITLE='${{ steps.inputs.outputs.title }}'
          FILES='${{ steps.inputs.outputs.files }}'

          args=(--title "$TITLE" --json)
          for file in $FILES; do
            args+=(--file "$file")
          done

          args+=(--override "compound-engineering=${{ github.event.inputs.compound_engineering_bump || 'auto' }}")
          args+=(--override "marketplace=${{ github.event.inputs.marketplace_bump || 'auto' }}")
          args+=(--override "cursor-marketplace=${{ github.event.inputs.cursor_marketplace_bump || 'auto' }}")

          bun run scripts/release/preview.ts "${args[@]}" | tee /tmp/release-preview.txt

      - name: Publish preview summary
        shell: bash
        run: cat /tmp/release-preview.txt >> "$GITHUB_STEP_SUMMARY"
