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@53b83947a5a98c8d113130e565377fae1a50d02f # v6.3.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@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v4.4.3
              with:
                  name: plugin-package-${{ matrix.id }}
                  path: dist/plugins/${{ matrix.id }}.zip
                  retention-days: 30
