name: Docs preview cleanup

on:
  pull_request:
    types: [closed]
    branches:
      - develop

permissions:
  contents: read

# Pages deploys must be serialized across this workflow, ci.yml's docs-deploy
# and ci.yml's docs-preview — concurrent deploys race the atomic site swap
# and the loser's content wipes the others' sibling subtrees.
concurrency:
  group: pages-deploy
  cancel-in-progress: false

jobs:
  cleanup:
    name: Remove docs preview
    runs-on: ubuntu-latest
    environment:
      name: github-pages
    permissions:
      actions: read
      contents: read
      id-token: write
      pages: write
      pull-requests: write
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

      - name: Seed Pages site with current live state
        uses: ./.github/actions/seed-pages-site
        with:
          output-dir: docs-preview
          github-token: ${{ secrets.GITHUB_TOKEN }}

      - name: Drop this PR's preview
        id: seed
        shell: bash
        run: |
          set -euo pipefail
          pr_number="${{ github.event.pull_request.number }}"
          if [[ -d "docs-preview/preview/pr-${pr_number}" ]]; then
            echo "Removing docs-preview/preview/pr-${pr_number}"
            rm -rf "docs-preview/preview/pr-${pr_number}"
            echo "removed=true" >> "$GITHUB_OUTPUT"
          else
            echo "No preview folder for PR ${pr_number}; nothing to redeploy."
            echo "removed=false" >> "$GITHUB_OUTPUT"
          fi

      - name: Configure GitHub Pages
        if: steps.seed.outputs.removed == 'true'
        uses: actions/configure-pages@v5

      - name: Upload docs artifact
        if: steps.seed.outputs.removed == 'true'
        uses: actions/upload-pages-artifact@v4
        with:
          path: docs-preview

      - name: Deploy docs to GitHub Pages
        if: steps.seed.outputs.removed == 'true'
        uses: actions/deploy-pages@v4

      - name: Update PR comment
        if: steps.seed.outputs.removed == 'true'
        uses: actions/github-script@v8
        with:
          script: |
            const marker = '<!-- rise-docs-preview -->';
            const body = [
              marker,
              'Docs preview removed (PR closed).',
            ].join('\n');

            const issue_number = context.payload.pull_request.number;
            const { owner, repo } = context.repo;
            try {
              const comments = await github.paginate(github.rest.issues.listComments, {
                owner,
                repo,
                issue_number,
                per_page: 100,
              });
              const existing = comments.find((comment) => comment.body?.includes(marker));
              if (existing) {
                await github.rest.issues.updateComment({
                  owner,
                  repo,
                  comment_id: existing.id,
                  body,
                });
              }
            } catch (error) {
              core.warning(`Unable to update docs preview comment: ${error.message}`);
            }
