name: Auto Tag Release (DISABLED)

# DISABLED: Auto-tagging interferes with manual release process
# Use simple-release.yml instead for controlled releases

on:
  workflow_dispatch:
    inputs:
      version_bump:
        description: 'Version bump type'
        required: true
        default: 'patch'
        type: choice
        options:
          - patch
          - minor
          - major

permissions:
  contents: write

jobs:
  auto-tag:
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
          token: ${{ secrets.GITHUB_TOKEN }}
      
      - name: Install Deno
        uses: denoland/setup-deno@v2
        with:
          deno-version: v2.x
      
      - name: Check if should release
        id: check
        run: |
          # Get last tag
          LAST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "")
          
          # Check for changes since last tag
          if [ -z "$LAST_TAG" ]; then
            echo "No tags found, will create first release"
            echo "should_release=true" >> "$GITHUB_OUTPUT"
          else
            CHANGES=$(git log $LAST_TAG..HEAD --oneline | wc -l)
            if [ "$CHANGES" -gt 0 ] || [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
              echo "Found $CHANGES commits since $LAST_TAG"
              echo "should_release=true" >> "$GITHUB_OUTPUT"
            else
              echo "No changes since $LAST_TAG"
              echo "should_release=false" >> "$GITHUB_OUTPUT"
            fi
          fi
      
      - name: Determine version bump
        id: version
        if: steps.check.outputs.should_release == 'true'
        run: |
          # Debug: Show current directory and check if Cargo.toml exists
          echo "Current directory: $(pwd)"
          echo "Contents: $(ls -la)"
          
          # Get current version from workspace Cargo.toml
          if [ ! -f "Cargo.toml" ]; then
            echo "Error: Cargo.toml not found in $(pwd)"
            exit 1
          fi
          
          # Try different methods to extract version
          echo "Method 1: Using awk"
          CURRENT_VERSION=$(awk -F'"' '/^\[workspace\.package\]/{ws=1} ws && /^version = /{print $2; exit}' Cargo.toml || echo "")
          echo "Awk result: '$CURRENT_VERSION'"
          
          if [ -z "$CURRENT_VERSION" ]; then
            echo "Method 2: Using grep and sed"
            CURRENT_VERSION=$(grep -A10 '^\[workspace\.package\]' Cargo.toml | grep '^version = ' | head -1 | sed 's/version = "\(.*\)"/\1/' || echo "")
            echo "Grep/sed result: '$CURRENT_VERSION'"
          fi
          
          if [ -z "$CURRENT_VERSION" ]; then
            echo "Method 3: Using simple grep"
            CURRENT_VERSION=$(grep '^version = ' Cargo.toml | head -1 | cut -d'"' -f2 || echo "")
            echo "Simple grep result: '$CURRENT_VERSION'"
          fi
          
          if [ -z "$CURRENT_VERSION" ]; then
            echo "Error: Could not extract version from Cargo.toml"
            cat Cargo.toml | head -20
            exit 1
          fi
          
          echo "Current version: $CURRENT_VERSION"
          
          # Determine bump type
          if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
            BUMP_TYPE="${{ github.event.inputs.version_bump }}"
          else
            # Auto-detect from commits
            LAST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "")
            if [ -n "$LAST_TAG" ]; then
              if git log $LAST_TAG..HEAD --pretty=format:"%s" | grep -qE "(BREAKING CHANGE|!:)"; then
                BUMP_TYPE="major"
              elif git log $LAST_TAG..HEAD --pretty=format:"%s" | grep -q "feat:"; then
                BUMP_TYPE="minor"
              else
                BUMP_TYPE="patch"
              fi
            else
              BUMP_TYPE="patch"
            fi
          fi
          
          echo "Bump type: $BUMP_TYPE"
          
          # Calculate new version
          IFS='.' read -r MAJOR MINOR PATCH <<< "$CURRENT_VERSION"
          case $BUMP_TYPE in
            major) MAJOR=$((MAJOR + 1)); MINOR=0; PATCH=0 ;;
            minor) MINOR=$((MINOR + 1)); PATCH=0 ;;
            patch) PATCH=$((PATCH + 1)) ;;
          esac
          NEW_VERSION="$MAJOR.$MINOR.$PATCH"
          
          echo "New version: $NEW_VERSION"
          echo "new_version=$NEW_VERSION" >> "$GITHUB_OUTPUT"
      
      - name: Update version files
        if: steps.check.outputs.should_release == 'true'
        run: |
          NEW_VERSION="${{ steps.version.outputs.new_version }}"
          
          # Use the update-version script to update all files
          deno run --allow-read --allow-write --allow-run scripts/update-version.ts "$NEW_VERSION"
      
      - name: Commit and tag
        if: steps.check.outputs.should_release == 'true'
        run: |
          git config user.name "GitHub Actions"
          git config user.email "actions@github.com"
          
          # Commit version updates
          git add -A
          git commit -m "chore: release v${{ steps.version.outputs.new_version }}"
          
          # Create and push tag
          git tag -a "v${{ steps.version.outputs.new_version }}" -m "Release v${{ steps.version.outputs.new_version }}"
          git push origin HEAD:${GITHUB_REF_NAME}
          git push origin "v${{ steps.version.outputs.new_version }}"
      
      - name: Trigger release workflow
        if: steps.check.outputs.should_release == 'true'
        run: |
          echo "Tag pushed. The release workflow will now handle building and publishing the release."