name: Process Documentation

on:
  pull_request:
    types: [opened, synchronize]
    branches: [main]
  workflow_dispatch:

jobs:
  process-docs:
    runs-on: ubuntu-latest
    permissions:
      contents: write

    steps:
      - name: Checkout repository
        uses: actions/checkout@v3
        with:
          ref: ${{ github.event.pull_request.head.sha }}

      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.10'

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install pyyaml

      - name: Run documentation tools
        id: doc-tools
        run: python tools/main_docs_bundle.py

      - name: Display results
        run: |
          echo "Execution results:"
          echo "Successful operations: ${{ steps.doc-tools.outputs.success_count }}"
          echo "Failed operations: ${{ steps.doc-tools.outputs.error_count }}"
          if [ "${{ steps.doc-tools.outputs.detailed_message }}" != "" ]; then
            echo "Details:"
            echo "${{ steps.doc-tools.outputs.detailed_message }}"
          fi

      - name: Commit and Push changes (if applicable)
        run: |
          # Push only if the PR is coming from a branch within the same repository
          # where the workflow is running (i.e., not a PR from an external fork targeting this repo).
          # This covers:
          # 1. PRs within the main repository.
          # 2. PRs within a fork of the main repository (when this workflow runs in that fork).
          if [[ "${{ github.event.pull_request.head.repo.full_name }}" == "${{ github.repository }}" ]]; then
            if [[ -n "$(git status --porcelain)" ]]; then
              FINAL_COMMIT_SUBJECT=""
              if [[ -n "${{ steps.doc-tools.outputs.commit_message }}" ]]; then
                FINAL_COMMIT_SUBJECT="${{ steps.doc-tools.outputs.commit_message }}"
              else
                FINAL_COMMIT_SUBJECT="Docs: Apply automated formatting by CI"
              fi
              
              FINAL_COMMIT_BODY=""
              if [[ -n "${{ steps.doc-tools.outputs.detailed_message }}" ]]; then
                FINAL_COMMIT_BODY="${{ steps.doc-tools.outputs.detailed_message }}"
              else
                FINAL_COMMIT_BODY="Automated changes by CI. This commit was made to the PR branch."
              fi
              
              git config --local user.email "88554920+alterxyz@users.noreply.github.com"
              git config --local user.name "alterxyz"
              git add .
              git commit -m "$FINAL_COMMIT_SUBJECT" -m "$FINAL_COMMIT_BODY"
              # Push to the PR's head ref (the source branch of the PR)
              git push origin HEAD:${{ github.head_ref }}
              
              echo "Formatting changes automatically committed and pushed to PR branch: ${{ github.head_ref }}"
              echo "Commit Subject: $FINAL_COMMIT_SUBJECT"
              echo "Description:"
              echo "$FINAL_COMMIT_BODY"
            else
              echo "No file changes detected by script in this PR (source repo: ${{ github.event.pull_request.head.repo.full_name }}, target repo: ${{ github.repository }}). Nothing to commit or push."
            fi
          else
            # This case covers PRs from truly external forks targeting this repository.
            echo "PR is from an external fork ('${{ github.event.pull_request.head.repo.full_name }}' to '${{ github.repository }}'). Formatting changes will not be pushed to the PR branch."
            if [[ -n "$(git status --porcelain)" ]]; then
                echo "Note: The script identified formatting changes. These will be handled by other processes post-merge if necessary."
            else
                echo "No formatting changes were identified by the script in this externally forked PR."
            fi
          fi