name: Publish Pre-Release Extension

on:
  workflow_dispatch:
    inputs:
      version:
        description: "ODD minor version (e.g., 1.1.0). Leave empty to auto-detect from latest release."
        required: false
        type: string
        default: ""
      dry-run:
        description: "Dry run (package only, do not publish)"
        required: false
        type: boolean
        default: false

permissions:
  contents: read

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: false

jobs:
  validate-version:
    name: Validate Pre-Release Version
    runs-on: ubuntu-latest
    permissions: {}
    outputs:
      version: ${{ steps.validate.outputs.version }}
    steps:
      - name: Validate ODD minor version
        id: validate
        env:
          GH_TOKEN: ${{ github.token }}
          INPUT_VERSION: ${{ inputs.version }}
        run: |
          VERSION="$INPUT_VERSION"
          # Strip leading 'v' or 'hve-core-v' prefix if present
          VERSION="${VERSION#v}"
          VERSION="${VERSION#hve-core-v}"

          # Auto-detect from latest release tag when no version specified
          if [ -z "$VERSION" ]; then
            TAG=$(gh release view --json tagName -q '.tagName' -R "${{ github.repository }}")
            VERSION="${TAG#hve-core-v}"

            MAJOR=$(echo "$VERSION" | cut -d. -f1)
            MINOR=$(echo "$VERSION" | cut -d. -f2)
            PATCH=$(echo "$VERSION" | cut -d. -f3)

            # Derive ODD minor: if even, bump minor by 1 and reset patch
            if (( MINOR % 2 == 0 )); then
              MINOR=$((MINOR + 1))
              PATCH=0
            fi
            VERSION="${MAJOR}.${MINOR}.${PATCH}"
            echo "📦 Auto-derived pre-release version from latest release: $VERSION"
          fi

          # Validate format
          if ! [[ "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
            echo "::error::Invalid version format: $VERSION. Expected semantic version (e.g., 1.1.0)"
            exit 1
          fi

          # Extract minor version
          MINOR=$(echo "$VERSION" | cut -d. -f2)

          # Check if ODD (pre-release channel uses ODD minor versions)
          if (( MINOR % 2 == 0 )); then
            echo "::error::Pre-release requires ODD minor version. Got: $VERSION (minor=$MINOR is EVEN)"
            echo "::error::Use version like 1.1.0, 1.3.0, 2.1.0 for pre-release channel"
            exit 1
          fi

          echo "✅ Valid pre-release version: $VERSION (minor=$MINOR is ODD)"
          echo "version=$VERSION" >> "$GITHUB_OUTPUT"

  package:
    name: Package Pre-Release Extensions
    needs: validate-version
    uses: ./.github/workflows/extension-package.yml
    with:
      version: ${{ needs.validate-version.outputs.version }}
      channel: PreRelease
    permissions:
      contents: read

  publish:
    name: Publish Pre-Release Extensions
    needs: [package]
    if: ${{ !inputs.dry-run }}
    uses: ./.github/workflows/extension-publish-marketplace.yml
    with:
      collections-matrix: ${{ needs.package.outputs.collections-matrix }}
      pre-release: true
    permissions:
      contents: read
      id-token: write
    secrets: inherit
