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@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v4.2.2
        with:
          persist-credentials: false

      - name: Setup PowerShell modules
        shell: pwsh
        run: |
          Install-Module -Name PowerShell-Yaml -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@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v4.2.2
        with:
          persist-credentials: false

      - name: Setup Node.js
        uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f # v6.3.0
        with:
          node-version: "20"

      - name: Install dependencies
        run: npm ci

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

      - name: Download changelog artifact
        if: inputs.use-changelog
        uses: actions/download-artifact@70fc10c6e5e1ce46ad2ea6f2b72d43f7d47b13c3 # v8.0.0
        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@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v4.4.3
        with:
          name: extension-vsix-${{ matrix.id }}
          path: extension/*.vsix
          retention-days: 30
