name: Plugin Validation

on:
  workflow_call:
    inputs:
      soft-fail:
        description: "Whether to continue on validation errors"
        required: false
        type: boolean
        default: false

permissions:
  contents: read

jobs:
  validate:
    name: Validate Plugins
    runs-on: ubuntu-latest
    permissions:
      contents: read
    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: Validate collection metadata
        run: npm run lint:collections-metadata

      - name: Validate marketplace manifest
        run: npm run lint:marketplace

      - name: Check plugin freshness
        run: |
          npm run plugin:generate
          if ! git diff --quiet plugins/; then
            echo "::error::Plugins out of date. Run 'npm run plugin:generate' and commit."
            if [ "${{ inputs.soft-fail }}" != "true" ]; then
              exit 1
            fi
          fi
