name: PR manual decline (label or button)

on:
  pull_request_target:
    types: [labeled]

  workflow_dispatch:
    inputs:
      pr_number:
        description: "PR number to act on"
        required: true
      reason:
        description: "Reason to use"
        required: true
        type: choice
        options: [Out of scope, Low info, Duplicate, Spam]

permissions:
  pull-requests: write
  contents: read

env:
  REPO: ${{ github.repository }}
  IDEAS_URL: https://github.com/buildermethods/design-os/discussions/categories/ideas
  CONTRIBUTING_URL: https://github.com/buildermethods/design-os/blob/main/.github/CONTRIBUTING.md

jobs:
  decline:
    runs-on: ubuntu-latest

    steps:
      - name: Determine PR number and reason (from label or dispatch)
        id: vars
        uses: actions/github-script@v7
        with:
          script: |
            let prNumber = "";
            let reason = "";
            let skip = false;

            if (context.eventName === "pull_request_target" && context.payload.action === "labeled") {
              prNumber = String(context.payload.pull_request.number);
              const label = (context.payload.label?.name || "").trim();

              const map = {
                "Close PR: Out of scope": "Out of scope",
                "Close PR: Low info": "Low info",
                "Close PR: Duplicate": "Duplicate",
                "Close PR: Spam": "Spam",
              };

              if (map[label]) {
                reason = map[label];
              } else {
                skip = true; // not one of your decline labels
              }
            }

            if (context.eventName === "workflow_dispatch") {
              prNumber = (core.getInput("pr_number") || "").trim();
              reason   = (core.getInput("reason") || "").trim();
            }

            core.setOutput("skip", skip ? "true" : "false");
            core.setOutput("pr", prNumber);
            core.setOutput("reason", reason);

      - name: Stop if not a decline event
        if: steps.vars.outputs.skip == 'true' || steps.vars.outputs.pr == '' || steps.vars.outputs.reason == ''
        run: echo "No decline action to run."

      - name: Build canned message
        if: steps.vars.outputs.skip != 'true' && steps.vars.outputs.pr != '' && steps.vars.outputs.reason != ''
        id: msg
        uses: actions/github-script@v7
        env:
          REASON: ${{ steps.vars.outputs.reason }}          # <-- pass raw string
          IDEAS_URL: ${{ env.IDEAS_URL }}
          CONTRIBUTING_URL: ${{ env.CONTRIBUTING_URL }}
        with:
          script: |
            const reason = process.env.REASON;              // <-- read raw string
            const IDEAS = process.env.IDEAS_URL;
            const CONTRIB = process.env.CONTRIBUTING_URL;

            let body = "";
            switch (reason) {
              case "Out of scope":
                body =
                  "Thanks for the PR! After review, this change isn’t on the current roadmap for Design OS. " +
                  "We keep core focused to manage long-term maintenance and compatibility.\n\n" +
                  `If you'd like to continue the conversation, please start a proposal in **Ideas**: ${IDEAS}\n` +
                  "If you publish a fork/plugin/example, feel free to share it in **Show & Tell** so others can try it.\n\n" +
                  "_Closing to keep the backlog focused._";
                break;

              case "Low info":
                body =
                  "Thanks for the PR! We’re missing required details for review.\n\n" +
                  "Please update the PR with:\n" +
                  "• **Summary**\n" +
                  "• **Checklist**\n" +
                  "• **Documented steps to test**\n\n" +
                  `Guidelines: ${CONTRIB}\n\n` +
                  "Once updated, you can open a new PR or ask a maintainer to reopen.";
                break;

              case "Duplicate":
                body =
                  "Thanks for the PR! This appears to duplicate existing work or discussion. " +
                  "We’ll consolidate on the canonical thread/PR to reduce churn.\n\n" +
                  "_Closing this one to keep things tidy._";
                break;

              case "Spam":
                body = "Closing this PR. It doesn’t meet our contribution policy.";
                break;

              default:
                body = "Thanks for the PR! Closing per maintainer review.";
            }

            core.setOutput("body", body);

      - name: Comment and close PR
        if: steps.vars.outputs.skip != 'true' && steps.vars.outputs.pr != '' && steps.msg.outputs.body != ''
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          REPO: ${{ env.REPO }}
        run: |
          PR="${{ steps.vars.outputs.pr }}"
          BODY="${{ steps.msg.outputs.body }}"

          gh pr comment "$PR" --repo "$REPO" --body "$BODY"
          gh pr close   "$PR" --repo "$REPO"
