name: Changelog Release Sync

# 仅支持手动触发，用于补充创建或更新已有版本的 Release Notes
# 注意：tag push 时由 ci.yml 的 release job 自动处理，无需此工作流
on:
  workflow_dispatch:
    inputs:
      version:
        description: "Version to sync (e.g., 1.2.0)"
        required: true
        type: string

permissions:
  contents: write

jobs:
  sync-release:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.13"

      - name: Determine version
        id: version
        run: |
          echo "version=${{ inputs.version }}" >> $GITHUB_OUTPUT
          echo "tag=v${{ inputs.version }}" >> $GITHUB_OUTPUT

      - name: Read Changelog (English)
        id: changelog_en
        uses: mindsers/changelog-reader-action@v2
        continue-on-error: true
        with:
          validation_level: none
          version: ${{ steps.version.outputs.version }}
          path: ./CHANGELOG.md

      - name: Read Changelog (Chinese)
        id: changelog_zh
        uses: mindsers/changelog-reader-action@v2
        continue-on-error: true
        with:
          validation_level: none
          version: ${{ steps.version.outputs.version }}
          path: ./CHANGELOG_ZH.md

      - name: Check if release exists
        id: check_release
        run: |
          if gh release view ${{ steps.version.outputs.tag }} &>/dev/null; then
            echo "exists=true" >> $GITHUB_OUTPUT
          else
            echo "exists=false" >> $GITHUB_OUTPUT
          fi
        env:
          GH_TOKEN: ${{ github.token }}

      - name: Create bilingual release notes
        run: |
          cat << 'EOF' > release_notes.md
          ## English

          ${{ steps.changelog_en.outputs.changes }}

          ---

          ## 简体中文

          ${{ steps.changelog_zh.outputs.changes }}
          EOF

      - name: Create or Update GitHub Release
        run: |
          TAG="${{ steps.version.outputs.tag }}"

          if [ "${{ steps.check_release.outputs.exists }}" = "true" ]; then
            echo "Updating existing release $TAG"
            gh release edit "$TAG" \
              --notes-file release_notes.md
          else
            echo "Creating new release $TAG"
            PRERELEASE_FLAG=""
            if [[ "$TAG" == *"alpha"* ]] || [[ "$TAG" == *"beta"* ]] || [[ "$TAG" == *"rc"* ]]; then
              PRERELEASE_FLAG="--prerelease"
            fi

            gh release create "$TAG" \
              --title "$TAG" \
              --notes-file release_notes.md \
              $PRERELEASE_FLAG \
              || echo "Release creation skipped or failed"
          fi
        env:
          GH_TOKEN: ${{ github.token }}

      - name: Summary
        run: |
          echo "## Release Sync Complete" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "- **Version**: ${{ steps.version.outputs.version }}" >> $GITHUB_STEP_SUMMARY
          echo "- **Tag**: ${{ steps.version.outputs.tag }}" >> $GITHUB_STEP_SUMMARY
          echo "- **Action**: ${{ steps.check_release.outputs.exists == 'true' && 'Updated' || 'Created' }}" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "View release: https://github.com/${{ github.repository }}/releases/tag/${{ steps.version.outputs.tag }}" >> $GITHUB_STEP_SUMMARY
