# 평일 아침마다 열린 결정 요청(kind:decision)을 모아, 기한이 지난 요청이 있으면 결정 대기 목록을 이슈 댓글로 올린다.
# 저장소의 이슈만 읽고 쓰므로 워크플로의 기본 토큰(GITHUB_TOKEN)으로 동작한다. Project 권한은 필요 없다.
#
# 설치 (D3):
#   1. 이 파일을 기본 브랜치의 .github/workflows/decision-queue.yml로 커밋한다.
#      키트 폴더가 저장소의 github-pm-starter-kit/에 있다고 가정한다.
#   2. 결정 대기 목록을 받을 이슈 번호를 저장소 변수 DECISION_ISSUE로 만든다.
#      gh variable set DECISION_ISSUE -R "$PM_REPO" --body 31
#   3. Actions 탭에서 수동 실행(workflow_dispatch)으로 한 번 확인한다.
name: decision-queue

on:
  schedule:
    # 평일 23:43 UTC = 다음 날 08:43 한국 시각 (월~금 아침). 정각은 실행이 밀리기 쉬워 피한다.
    - cron: "43 23 * * 0-4"
  workflow_dispatch:

permissions:
  contents: read
  issues: write

jobs:
  queue:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: 결정 대기 목록 만들기
        id: queue
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          gh issue list -R "${{ github.repository }}" --label kind:decision --state open --limit 200 \
            --json number,title,createdAt,labels,assignees,body,url > decisions.json
          today=$(TZ=Asia/Seoul date +%F)
          set +e
          bash github-pm-starter-kit/decision-queue.sh decisions.json --today "$today" --fail-overdue > queue.md
          rc=$?
          set -e
          cat queue.md
          if [ "$rc" -eq 2 ]; then echo "overdue=true" >> "$GITHUB_OUTPUT"; elif [ "$rc" -ne 0 ]; then exit "$rc"; fi

      - name: 기한이 지난 요청이 있으면 댓글로 올리기
        if: steps.queue.outputs.overdue == 'true'
        env:
          GH_TOKEN: ${{ github.token }}
          DECISION_ISSUE: ${{ vars.DECISION_ISSUE }}
        run: gh issue comment "$DECISION_ISSUE" -R "${{ github.repository }}" --body-file queue.md
