name: Package Extension

on:
  workflow_call:
    inputs:
      version:
        description: "Full version to use (e.g., 1.0.0 or empty to use package.json)"
        required: false
        type: string
        default: ""
      dev-patch-number:
        description: "Dev patch number to append (creates version like 1.0.0-dev.123)"
        required: false
        type: string
        default: ""
      use-changelog:
        description: "Whether to download and use changelog artifact"
        required: false
        type: boolean
        default: false
      channel:
        description: "Release channel (Stable or PreRelease) controlling agent maturity filtering"
        required: false
        type: string
        default: "Stable"
    outputs:
      version:
        description: "Version that was packaged"
        value: ${{ jobs.discover-collections.outputs.version }}
      collections-matrix:
        description: "JSON matrix of collections that were actually packaged (filtered by channel and maturity)"
        value: ${{ jobs.discover-collections.outputs.matrix }}

permissions:
  contents: read

jobs:
  discover-collections:
    name: Discover Collection Manifests
    runs-on: ubuntu-latest
    permissions:
      contents: read
    outputs:
      matrix: ${{ steps.discover.outputs.matrix }}
      version: ${{ steps.read-version.outputs.version }}
    steps:
      - name: Checkout code
        uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3
        with:
          persist-credentials: false

      - name: Setup PowerShell modules
        shell: pwsh
        run: |
          Install-Module -Name PowerShell-Yaml -RequiredVersion 0.4.7 -Force -Scope CurrentUser

      - name: Resolve effective version
        id: read-version
        shell: bash
        run: |
          version="${{ inputs.version }}"
          dev_patch="${{ inputs.dev-patch-number }}"
          if [ -z "$version" ]; then
            version=$(jq -r .version extension/templates/package.template.json)
          fi
          if [ -n "$dev_patch" ]; then
            version="${version}-dev.${dev_patch}"
          fi
          echo "version=$version" >> "$GITHUB_OUTPUT"

      - name: Discover collection manifests
        id: discover
        shell: pwsh
        run: |
          & ./scripts/extension/Find-CollectionManifests.ps1 -Channel '${{ inputs.channel }}'

  package:
    name: Package ${{ matrix.id }}
    needs: discover-collections
    runs-on: ubuntu-latest
    permissions:
      contents: read
    strategy:
      fail-fast: false
      matrix: ${{ fromJson(needs.discover-collections.outputs.matrix) }}
    steps:
      - name: Checkout code
        uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3
        with:
          persist-credentials: false

      - name: Setup Node.js
        uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
        with:
          node-version: "24"

      - name: Install dependencies
        run: npm ci

      - name: Setup PowerShell
        shell: pwsh
        run: |
          Write-Host "PowerShell version: $($PSVersionTable.PSVersion)"
          Install-Module -Name PowerShell-Yaml -RequiredVersion 0.4.7 -Force -Scope CurrentUser

      - name: Download changelog artifact
        if: inputs.use-changelog
        uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
        with:
          name: changelog
          path: ./
        continue-on-error: true

      - name: Prepare extension resources
        id: prepare
        shell: pwsh
        run: |
          $channel = "${{ inputs.channel }}".Trim()
          if (-not $channel) { $channel = 'Stable' }
          $collection = "${{ matrix.manifest }}"

          Write-Host "🔧 Preparing extension for $channel channel (collection: ${{ matrix.id }})..."
          ./scripts/extension/Prepare-Extension.ps1 -Channel $channel -Collection $collection

      - name: Package extension
        id: package
        shell: pwsh
        run: |
          $version = "${{ inputs.version }}".Trim()
          $devPatch = "${{ inputs.dev-patch-number }}".Trim()
          $channel = "${{ inputs.channel }}".Trim()
          if (-not $channel) { $channel = 'Stable' }
          $collection = "${{ matrix.manifest }}"

          Write-Host "📦 Packaging extension for $channel channel (collection: ${{ matrix.id }})..."

          $arguments = @{
            Collection = $collection
          }

          if ($channel -ieq 'PreRelease') {
            $arguments['PreRelease'] = $true
          }

          if ($version) {
            $arguments['Version'] = $version
          }

          if ($devPatch) {
            $arguments['DevPatchNumber'] = $devPatch
          }

          if (Test-Path "./CHANGELOG.md") {
            $arguments['ChangelogPath'] = "./CHANGELOG.md"
          }

          ./scripts/extension/Package-Extension.ps1 @arguments

      - name: Upload VSIX artifact
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: extension-vsix-${{ matrix.id }}
          path: extension/*.vsix
          retention-days: 30
