name: Package Plugins

on:
    workflow_call:
        inputs:
            version:
                description: "Release version (for artifact metadata)"
                required: false
                type: string
                default: ""
        outputs:
            collections-matrix:
                description: "JSON matrix of plugin collections that were packaged"
                value: ${{ jobs.discover-collections.outputs.matrix }}

permissions:
    contents: read

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

            - name: Discover collection manifests
              id: discover
              shell: bash
              run: |
                  matrix_json=$(find collections -name '*.collection.yml' -type f | sort | while IFS= read -r file; do
                    id=$(basename "$file" .collection.yml)
                    echo "{\"id\":\"$id\"}"
                  done | jq -sc '{include: .}')

                  echo "matrix=$matrix_json" >> "$GITHUB_OUTPUT"
                  echo "Discovered plugin collections:"
                  echo "$matrix_json" | jq .

    package:
        name: Package Plugin ${{ 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"
                  cache: "npm"

            - name: Install dependencies
              run: npm ci

            - name: Install PowerShell-Yaml
              shell: pwsh
              run: |
                  if (-not (Get-Module -ListAvailable -Name PowerShell-Yaml | Where-Object { $_.Version -eq '0.4.7' })) {
                    Install-Module -Name PowerShell-Yaml -RequiredVersion 0.4.7 -Force -Scope CurrentUser
                  }

            - name: Generate plugins
              run: |
                  mkdir -p logs
                  npm run plugin:generate

            - name: Package plugin directory
              shell: bash
              run: |
                  plugin_dir="plugins/${{ matrix.id }}"
                  if [ ! -d "$plugin_dir" ]; then
                    echo "::error::Expected plugin directory not found: $plugin_dir"
                    exit 1
                  fi

                  mkdir -p dist/plugins
                  zip -r "dist/plugins/${{ matrix.id }}.zip" "$plugin_dir"

            - name: Upload plugin artifact
              uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
              with:
                  name: plugin-package-${{ matrix.id }}
                  path: dist/plugins/${{ matrix.id }}.zip
                  retention-days: 30
