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

jobs:
  prepare-changelog:
    name: Prepare Changelog
    runs-on: ubuntu-latest
    outputs:
      changelog-path: ${{ steps.create-changelog.outputs.path }}
    steps:
      - name: Checkout code
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v4.2.2
        with:
          persist-credentials: false

      - name: Create changelog file
        id: create-changelog
        run: |
          if [ "${{ github.event_name }}" == "release" ]; then
            cat > CHANGELOG.md << 'EOF'
          ${{ github.event.release.body }}
          EOF
            echo "path=CHANGELOG.md" >> "$GITHUB_OUTPUT"
          elif [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
            echo "path=" >> "$GITHUB_OUTPUT"
          else
            echo "path=" >> "$GITHUB_OUTPUT"
          fi

      - name: Upload changelog
        if: steps.create-changelog.outputs.path != ''
        uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v4.4.3
        with:
          name: changelog
          path: CHANGELOG.md
          retention-days: 1

  normalize-version:
    name: Normalize Version
    runs-on: ubuntu-latest
    outputs:
      version: ${{ steps.normalize.outputs.version }}
    steps:
      - name: Normalize version string
        id: normalize
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          if [ "${{ github.event_name }}" == "release" ]; then
            VERSION="${{ github.event.release.tag_name }}"
          else
            VERSION="${{ inputs.version }}"
          fi
          # 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 view --json tagName -q '.tagName' -R "${{ github.repository }}")
            VERSION="${TAG#hve-core-v}"
            echo "📦 Auto-detected version from latest release: $VERSION"
          fi

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

  package:
    name: Package Extensions
    needs: [prepare-changelog, normalize-version]
    uses: ./.github/workflows/extension-package.yml
    with:
      version: ${{ needs.normalize-version.outputs.version }}
      use-changelog: ${{ needs.prepare-changelog.outputs.changelog-path != '' }}
    permissions:
      contents: read

  publish:
    name: Publish ${{ matrix.id }}
    needs: [package]
    if: ${{ !inputs.dry-run }}
    runs-on: ubuntu-latest
    environment: marketplace
    strategy:
      fail-fast: false
      matrix: ${{ fromJson(needs.package.outputs.collections-matrix) }}
    permissions:
      contents: read
      id-token: write
    steps:
      - name: Checkout code
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v4.2.2
        with:
          persist-credentials: false

      - name: Azure Login (OIDC)
        uses: azure/login@a457da9ea143d694b1b9c7c869ebb04ebe844ef5 # v2.3.0
        with:
          client-id: ${{ secrets.AZURE_CLIENT_ID }}
          tenant-id: ${{ secrets.AZURE_TENANT_ID }}
          subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}

      - name: Setup Node.js
        uses: actions/setup-node@6044e13b5dc448c55e2357c09f80417699197238 # v4.1.0
        with:
          node-version: '20'

      - name: Install VSCE
        run: npm install -g @vscode/vsce@3.7.1

      - name: Download VSIX artifact
        uses: actions/download-artifact@37930b1c2abaa49bbe596cd826c3c89aef350131 # v7.0.0
        with:
          name: extension-vsix-${{ matrix.id }}
          path: ./dist

      - name: Publish to VS Code Marketplace
        id: publish
        run: |
          VSIX_FILE=$(find dist -name '*.vsix' -printf '%T@ %p\n' | sort -rn | head -1 | cut -d' ' -f2)
          if [ -z "$VSIX_FILE" ]; then
            echo "::error::No VSIX file found for collection ${{ matrix.id }}"
            exit 1
          fi
          # Extract version from VSIX filename to avoid depending on matrix job output
          VSIX_VERSION=$(basename "$VSIX_FILE" .vsix | grep -oE '[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9.]+)?$')
          echo "version=$VSIX_VERSION" >> "$GITHUB_OUTPUT"
          echo "📦 Publishing ${{ matrix.id }}: $VSIX_FILE (v$VSIX_VERSION)"
          vsce publish --packagePath "$VSIX_FILE" --azure-credential

      - name: Summary
        run: |
          {
            echo "## 🎉 Extension Published: ${{ matrix.name }}"
            echo ""
            echo "**Collection:** ${{ matrix.id }}"
            echo "**Version:** ${{ steps.publish.outputs.version }}"
            echo ""
            echo "View on [VS Code Marketplace](https://marketplace.visualstudio.com/items?itemName=ise-hve-essentials.${{ matrix.name }})"
          } >> "$GITHUB_STEP_SUMMARY"
