name: Docs Acknowledgement

on:
  pull_request:
    types: [opened, edited, synchronize]

permissions:
  contents: read
  pull-requests: read

jobs:
  docs-ack:
    name: Require docs PR URL or explicit "not needed"
    runs-on: ubuntu-latest

    steps:
      - name: Read PR body
        id: body
        shell: bash
        run: |
          set -euo pipefail
          BODY_B64=$(jq -r '.pull_request.body // "" | @base64' "$GITHUB_EVENT_PATH")
          {
            echo "body_b64=$BODY_B64"
          } >> "$GITHUB_OUTPUT"

      - name: Validate checkbox selection
        id: validate
        shell: bash
        env:
          BODY_B64: ${{ steps.body.outputs.body_b64 }}
        run: |
          set -euo pipefail
          if ! body="$(printf '%s' "$BODY_B64" | base64 -d)"; then
            echo "::error::Failed to decode PR body from base64. Data may be corrupted or missing."
            exit 1
          fi

          added_checked=$(printf '%s' "$body" | grep -Ei '^[[:space:]]*-\s*\[x\]\s*I added/updated documentation' | wc -l | tr -d '[:space:]' || true)
          noneed_checked=$(printf '%s' "$body" | grep -Ei '^[[:space:]]*-\s*\[x\]\s*Documentation is \*\*not needed\*\*' | wc -l | tr -d '[:space:]' || true)

          total=$((added_checked + noneed_checked))
          if [ "$total" -ne 1 ]; then
            echo "::error::You must check exactly one docs option in the PR template (either 'docs added' OR 'not needed')."
            exit 1
          fi

          if [ "$added_checked" -eq 1 ]; then
            echo "mode=added" >> "$GITHUB_OUTPUT"
          else
            echo "mode=noneed" >> "$GITHUB_OUTPUT"
          fi

      - name: Extract docs PR URL (when 'docs added')
        if: steps.validate.outputs.mode == 'added'
        id: extract
        shell: bash
        env:
          BODY_B64: ${{ steps.body.outputs.body_b64 }}
        run: |
          set -euo pipefail
          body="$(printf '%s' "$BODY_B64" | base64 -d)"

          # Strictly require HTTPS and that it's a PR in netbirdio/docs
          # e.g., https://github.com/netbirdio/docs/pull/1234
          url="$(printf '%s' "$body" | grep -Eo 'https://github\.com/netbirdio/docs/pull/[0-9]+' | head -n1 || true)"

          if [ -z "${url:-}" ]; then
            echo "::error::You checked 'docs added' but didn't include a valid HTTPS PR link to netbirdio/docs (e.g., https://github.com/netbirdio/docs/pull/1234)."
            exit 1
          fi

          pr_number="$(printf '%s' "$url" | sed -E 's#.*/pull/([0-9]+)$#\1#')"
          {
            echo "url=$url"
            echo "pr_number=$pr_number"
          } >> "$GITHUB_OUTPUT"

      - name: Verify docs PR exists (and is open or merged)
        if: steps.validate.outputs.mode == 'added'
        uses: actions/github-script@v7
        id: verify
        env:
          PR_NUMBER: ${{ steps.extract.outputs.pr_number }}
        with:
          script: |
            const prNumber = parseInt(process.env.PR_NUMBER, 10);
            const { data } = await github.rest.pulls.get({
              owner: 'netbirdio',
              repo: 'docs',
              pull_number: prNumber
            });

            // Allow open or merged PRs
            const ok = data.state === 'open' || data.merged === true;
            core.setOutput('state', data.state);
            core.setOutput('merged', String(!!data.merged));
            if (!ok) {
              core.setFailed(`Docs PR #${prNumber} exists but is neither open nor merged (state=${data.state}, merged=${data.merged}).`);
            }
          result-encoding: string
          github-token: ${{ secrets.GITHUB_TOKEN }}

      - name: All good
        run: echo "Documentation requirement satisfied ✅"
