name: Update Manifest on Dispatch

on:
  repository_dispatch:
    types: [update-manifest-node] # Or your chosen event type

jobs:
  update_manifest_file:
    runs-on: ubuntu-latest
    permissions:
      contents: write
      pull-requests: write

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
      
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "lts/*"

      - name: Update manifest.json using Node.js script
        id: update_script # Give this step an id to access its outputs
        run: |
          node .github/scripts/update-manifest.js
        env:
          CLIENT_PAYLOAD_JSON: ${{ toJSON(github.event.client_payload) }}
          
      - name: Validate JSON files
        run: python .github/scripts/validate-json.py

      - name: Create Pull Request
        if: success()
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          git config --global user.name "github-actions[bot]"
          git config --global user.email "github-actions[bot]@users.noreply.github.com"

          # Create a new branch name (make it unique)
          # Using plugin name and version for uniqueness, sanitizing for branch name
          SANITIZED_PLUGIN_NAME=$(echo "${{ github.event.client_payload.pluginName }}" | tr -cd '[:alnum:]_-')
          BRANCH_NAME="update-${SANITIZED_PLUGIN_NAME}-${{ github.event.client_payload.version }}"
          echo "Creating new branch: $BRANCH_NAME"
          
          # Check if branch already exists locally (e.g. from a failed previous run on self-hosted runner)
          # and switch to main to ensure clean state before creating new branch
          # For GitHub-hosted runners, this is less of an issue but good practice.
          current_branch=$(git rev-parse --abbrev-ref HEAD)
          if [ "$current_branch" != "main" ]; then
             git checkout main
             git pull origin main
          fi
          
          # Delete the branch if it exists locally (to ensure clean state for this run)
          if git show-ref --quiet refs/heads/$BRANCH_NAME; then
            echo "Branch $BRANCH_NAME already exists locally. Deleting it."
            git branch -D $BRANCH_NAME
          fi
          git checkout -b "$BRANCH_NAME"
          git add *

          # Check if there are changes staged for commit
          if git diff --staged --quiet; then
            echo "No changes staged for commit. Working tree clean or no changes to specified file."
            exit 0 
          else
            COMMIT_TITLE="Update ${{ github.event.client_payload.pluginName }} to v${{ github.event.client_payload.version }}"
            COMMIT_BODY="Automated update for ${{ github.event.client_payload.pluginName }} to version ${{ github.event.client_payload.version }}"
            echo "Changes are staged. Proceeding with commit."
            git commit -m "$COMMIT_TITLE" -m "$COMMIT_BODY"
            echo "Pushing branch $BRANCH_NAME to origin..."
            git push -u origin "$BRANCH_NAME"
            
            echo "Creating Pull Request..."
            gh pr create \
              --base main \
              --head "$BRANCH_NAME" \
              --title "$COMMIT_TITLE" \
              --body "$COMMIT_BODY"
            
            echo "Pull Request created (or attempted)."
          fi
