name: Auto Tag Release

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

permissions:
  contents: read

jobs:
  auto-tag:
    # Only run on merged PRs with 'release' label
    if: github.event.pull_request.merged == true && contains(github.event.pull_request.labels.*.name, 'release')
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
        with:
          ref: main
          fetch-depth: 0
          token: ${{ secrets.PAT_TOKEN }}

      - name: Get version from Cargo.toml
        id: version
        run: |
          VERSION=$(grep '^version = ' Cargo.toml | head -1 | cut -d'"' -f2)
          echo "version=$VERSION" >> $GITHUB_OUTPUT
          echo "Found version: $VERSION"

      - name: Check if tag already exists
        id: check_tag
        run: |
          if git rev-parse "v${{ steps.version.outputs.version }}" >/dev/null 2>&1; then
            echo "exists=true" >> $GITHUB_OUTPUT
            echo "Tag v${{ steps.version.outputs.version }} already exists"
          else
            echo "exists=false" >> $GITHUB_OUTPUT
            echo "Tag v${{ steps.version.outputs.version }} does not exist"
          fi

      - name: Create and push tag
        if: steps.check_tag.outputs.exists == 'false'
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git tag -a "v${{ steps.version.outputs.version }}" -m "Release v${{ steps.version.outputs.version }}"
          git push origin "v${{ steps.version.outputs.version }}"
          echo "✅ Created and pushed tag v${{ steps.version.outputs.version }}"

      - name: Tag already exists
        if: steps.check_tag.outputs.exists == 'true'
        run: |
          echo "⚠️ Tag v${{ steps.version.outputs.version }} already exists, skipping tag creation"

      - name: Wait for release workflow to trigger
        if: steps.check_tag.outputs.exists == 'false'
        run: |
          echo "✅ Tag v${{ steps.version.outputs.version }} created"
          echo "The Release workflow will be automatically triggered by the tag push"
          echo "Using PAT_TOKEN ensures the push event triggers workflows"
