name: Publish Extension

on:
  # Release trigger disabled - extension publishing is manual-only
  # Uncomment to enable auto-publish on GitHub releases:
  # release:
  #   types: [published]
  workflow_dispatch:
    inputs:
      version:
        description: "Version to publish (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:
  normalize-version:
    name: Normalize Version
    runs-on: ubuntu-latest
    permissions:
      contents: read
    outputs:
      version: ${{ steps.normalize.outputs.version }}
    steps:
      - name: Normalize version string
        id: normalize
        env:
          GH_TOKEN: ${{ github.token }}
          INPUT_VERSION: ${{ inputs.version }}
        run: |
          VERSION="$INPUT_VERSION"
          # Strip leading 'v' and component 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 list --exclude-drafts --exclude-pre-releases --limit 1 --json tagName -q '.[0].tagName' -R "${{ github.repository }}")
            VERSION="${TAG#hve-core-v}"
            echo "📦 Auto-detected version from latest release: $VERSION"
          fi

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

          # Validate even minor version (stable channel convention)
          MINOR=$(echo "$VERSION" | cut -d. -f2)
          if (( MINOR % 2 != 0 )); then
            echo "::error::Stable channel requires EVEN minor version. Got: $VERSION (minor=$MINOR is ODD)"
            echo "::error::Use extension-publish-prerelease workflow for ODD minor (pre-release) versions"
            exit 1
          fi

          echo "version=$VERSION" >> "$GITHUB_OUTPUT"

  package:
    name: Package Extensions
    needs: [normalize-version]
    uses: ./.github/workflows/extension-package.yml
    with:
      version: ${{ needs.normalize-version.outputs.version }}
      channel: Stable
    permissions:
      contents: read

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