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

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

            - name: Install dependencies
              run: npm ci

            - name: Install PowerShell-Yaml
              shell: pwsh
              run: |
                  if (-not (Get-Module -ListAvailable -Name PowerShell-Yaml)) {
                    Install-Module -Name PowerShell-Yaml -Force -Scope CurrentUser
                  }

            - name: Generate plugins
              run: 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@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v4.4.3
              with:
                  name: plugin-package-${{ matrix.id }}
                  path: dist/plugins/${{ matrix.id }}.zip
                  retention-days: 30
