<workflow>
  <critical>The workflow execution engine is governed by: {project-root}/_bmad/core/tasks/workflow.xml</critical>
  <critical>You MUST have already loaded and processed: {installed_path}/workflow.yaml</critical>
  <critical>Communicate all responses in {communication_language}</critical>

  <step n="1" goal="릴리즈 정보 수집">
    <critical>🏷️ GITHUB RELEASE - 태그 생성 및 릴리즈 등록</critical>

    <action>git repository 확인: git rev-parse --git-dir</action>
    <check if="git repository NOT detected">
      <output>🚫 Git 저장소가 감지되지 않습니다. 릴리즈를 생성할 수 없습니다.</output>
      <action>HALT</action>
    </check>

    <action>gh CLI 인증 확인: gh auth status</action>
    <check if="gh auth 실패">
      <output>🚫 GitHub CLI 인증이 필요합니다.
        `gh auth login`을 실행하여 인증해주세요.
      </output>
      <action>HALT</action>
    </check>

    <action>현재 태그 목록 조회: git tag --list --sort=-v:refname | head -10</action>
    <action>가장 최근 태그 확인</action>

    <output>📋 **릴리즈 정보**

      **최근 태그:** {{latest_tag}} (없으면 "첫 번째 릴리즈")
      **현재 브랜치:** {{current_branch}}
      **마지막 커밋:** {{last_commit_hash}} - {{last_commit_message}}
    </output>

    <ask>새 버전을 입력해주세요 (예: v1.0.0, v1.1.0):
      - Major (v2.0.0): 호환되지 않는 API 변경
      - Minor (v1.1.0): 새로운 기능 추가 (하위 호환)
      - Patch (v1.0.1): 버그 수정
    </ask>

    <action>입력된 버전 유효성 검사 (v로 시작, semantic versioning 형식)</action>
    <check if="유효하지 않은 버전 형식">
      <output>⚠️ 버전 형식이 올바르지 않습니다. 예: v1.0.0</output>
      <ask>다시 입력해주세요:</ask>
    </check>

    <action>{{new_version}} 저장</action>
  </step>

  <step n="2" goal="릴리즈 노트 생성">
    <critical>📝 RELEASE NOTES - 완료된 스토리/PR 기반 자동 생성</critical>

    <check if="{{latest_tag}} 존재">
      <action>이전 태그 이후 커밋 조회:
        git log {{latest_tag}}..HEAD --pretty=format:"- %s" --no-merges
      </action>
    </check>

    <check if="첫 번째 릴리즈">
      <action>모든 커밋 조회:
        git log --pretty=format:"- %s" --no-merges | head -50
      </action>
    </check>

    <action>커밋 메시지를 타입별로 분류:
      - feat: 새로운 기능
      - fix: 버그 수정
      - refactor: 리팩토링
      - docs: 문서
      - test: 테스트
      - chore: 기타
    </action>

    <check if="sprint_status 파일 존재">
      <action>완료된 스토리 목록 추출 (status: done)</action>
    </check>

    <action>릴리즈 노트 초안 생성</action>

    <output>📝 **릴리즈 노트 초안**

      ## {{new_version}}

      ### ✨ New Features
      {{feat_commits}}

      ### 🐛 Bug Fixes
      {{fix_commits}}

      ### 🔧 Improvements
      {{refactor_commits}}

      ### 📚 Documentation
      {{docs_commits}}

      ### Completed Stories
      {{completed_stories}}

      ---
      *Generated by BMAD Method*
    </output>

    <ask>릴리즈 노트를 수정하시겠습니까? [y/n]
      - y: 수정 내용 입력
      - n: 현재 내용으로 진행
    </ask>

    <check if="user wants to edit">
      <ask>수정할 내용을 입력해주세요:</ask>
      <action>릴리즈 노트 업데이트</action>
    </check>
  </step>

  <step n="3" goal="릴리즈 워크플로우 트리거">
    <critical>🚀 RELEASE TRIGGER - GitHub Actions 릴리즈 워크플로우 실행</critical>

    <output>⚠️ **릴리즈 확인**

      **버전:** {{new_version}}
      **대상 브랜치:** master (최신 커밋)
      **릴리즈 노트:** (위 내용 참조)

      GitHub Actions가 다음을 자동으로 수행합니다:
      1. CHANGELOG.md 생성 및 커밋
      2. CHANGELOG 커밋에 태그 생성
      3. GitHub Release 생성
      4. npm 패키지 배포
    </output>

    <ask>릴리즈 워크플로우를 실행하시겠습니까? [y/n]</ask>

    <check if="user confirms">
      <action>GitHub Actions 워크플로우 트리거:
        gh workflow run release-on-tag.yml -f version={{new_version}}
      </action>

      <check if="트리거 성공">
        <output>✅ 릴리즈 워크플로우가 트리거되었습니다.

          워크플로우 실행 상태 확인:
          gh run list --workflow=release-on-tag.yml --limit=1
        </output>
      </check>

      <check if="트리거 실패">
        <output>🚫 워크플로우 트리거 실패: {{error_message}}

          수동으로 실행해주세요:
          GitHub → Actions → Release → Run workflow → version: {{new_version}}
        </output>
        <action>HALT</action>
      </check>
    </check>

    <check if="user cancels">
      <output>❌ 릴리즈 취소됨</output>
      <action>HALT</action>
    </check>
  </step>

  <step n="4" goal="릴리즈 완료 확인">
    <output>🎉 **릴리즈 워크플로우 실행됨!**

      **버전:** {{new_version}}
      **릴리즈 URL:** https://github.com/{{owner}}/{{repo}}/releases/tag/{{new_version}}

      **확인 사항:**
      1. GitHub Actions 실행 완료 확인: gh run list --workflow=release-on-tag.yml --limit=1
      2. 릴리즈 페이지에서 CHANGELOG 및 릴리즈 노트 확인
      3. npm 패키지 배포 확인 (해당하는 경우)
    </output>
  </step>

</workflow>
