name: Auto Tag on Release PR Merge

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

jobs:
  auto-tag:
    name: Auto Create Tag
    runs-on: ubuntu-latest
    # 只在 PR 被合并且是 release PR 时运行
    if: |
      github.event.pull_request.merged == true && 
      startsWith(github.event.pull_request.head.ref, 'release/') &&
      startsWith(github.event.pull_request.title, '🚀 Release v')
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          token: ${{ secrets.PAT_TOKEN }}
          fetch-depth: 0

      - name: Extract version from branch name
        id: version
        run: |
          # 从分支名提取版本号 (release/1.17.1 -> 1.17.1)
          BRANCH_NAME="${{ github.event.pull_request.head.ref }}"
          VERSION="${BRANCH_NAME#release/}"
          echo "version=$VERSION" >> $GITHUB_OUTPUT
          echo "Extracted version: $VERSION"

      - name: Configure Git
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"

      - name: Create and push tag
        run: |
          VERSION="${{ steps.version.outputs.version }}"
          TAG_NAME="v$VERSION"
          
          # 检查 tag 是否已存在
          if git rev-parse "$TAG_NAME" >/dev/null 2>&1; then
            echo "⚠️ Tag $TAG_NAME already exists, skipping..."
            exit 0
          fi
          
          # 创建带注释的 tag
          git tag -a "$TAG_NAME" -m "Release version $VERSION

          Automated release from PR #${{ github.event.pull_request.number }}
          
          ${{ github.event.pull_request.title }}
          
          PR merged by: ${{ github.event.pull_request.merged_by.login }}
          Merge commit: ${{ github.event.pull_request.merge_commit_sha }}"
          
          # 推送 tag
          git push origin "$TAG_NAME"
          
          echo "✅ Successfully created and pushed tag: $TAG_NAME"
          
      - name: Comment on PR
        uses: actions/github-script@v7
        with:
          github-token: ${{ secrets.PAT_TOKEN }}
          script: |
            const version = '${{ steps.version.outputs.version }}';
            const tagName = `v${version}`;
            
            await github.rest.issues.createComment({
              owner: context.repo.owner,
              repo: context.repo.repo,
              issue_number: context.issue.number,
              body: `## 🎉 Release Tagged Successfully!\n\n` +
                    `Tag \`${tagName}\` has been created and pushed.\n\n` +
                    `### 🚀 Automated Actions Triggered:\n` +
                    `- ✅ NPM packages publishing\n` +
                    `- ✅ Docker image building\n` +
                    `- ✅ Desktop app builds\n` +
                    `- ✅ GitHub release creation\n\n` +
                    `### 📦 Packages:\n` +
                    `Once published, packages will be available at:\n` +
                    `- npm: https://www.npmjs.com/package/@promptx/core/v/${version}\n` +
                    `- Docker: \`docker pull deepracticexs/promptx:${version}\`\n\n` +
                    `[View Release](https://github.com/${context.repo.owner}/${context.repo.repo}/releases/tag/${tagName})`
            });

      - name: Summary
        run: |
          echo "## 🏷️ Tag Created Successfully" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "- **Tag**: v${{ steps.version.outputs.version }}" >> $GITHUB_STEP_SUMMARY
          echo "- **PR**: #${{ github.event.pull_request.number }}" >> $GITHUB_STEP_SUMMARY
          echo "- **Merged by**: ${{ github.event.pull_request.merged_by.login }}" >> $GITHUB_STEP_SUMMARY
          echo "- **Merge commit**: ${{ github.event.pull_request.merge_commit_sha }}" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "The following workflows will be triggered by this tag:" >> $GITHUB_STEP_SUMMARY
          echo "- Publish Release (npm + docker)" >> $GITHUB_STEP_SUMMARY
          echo "- Build Desktop Apps" >> $GITHUB_STEP_SUMMARY