name: Homebrew Release

on:
  release:
    types: [published]

env:
  CARGO_TERM_COLOR: always

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: false

jobs:
  update-homebrew:
    name: Update Homebrew Formula
    runs-on: ubuntu-latest
    timeout-minutes: 10

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Extract and validate version
        uses: ./.github/actions/extract-semantic-version
        id: extract_version
        with:
          version-source: 'tag'
          check-cargo-toml: 'true'

      - name: Validate Homebrew token
        run: |
          set -euo pipefail
          if [ -z "${{ secrets.HOMEBREW_TAP_TOKEN }}" ] && [ -z "${{ secrets.GITHUB_TOKEN }}" ]; then
            echo "::error::HOMEBREW_TAP_TOKEN or GITHUB_TOKEN not configured. Cannot update Homebrew formula."
            exit 1
          fi

      - name: Verify release assets exist
        run: |
          set -euo pipefail
          VERSION="${{ steps.extract_version.outputs.version }}"
          RELEASE_URL="https://github.com/seanchatmangpt/ggen/releases/download/v$VERSION"

          echo "Checking for release assets..."
          for target in "x86_64-apple-darwin" "aarch64-apple-darwin" "x86_64-unknown-linux-gnu" "aarch64-unknown-linux-gnu"; do
            ASSET_URL="$RELEASE_URL/ggen-$target.tar.gz"
            if curl -s --head "$ASSET_URL" | grep -q "200\|302"; then
              echo "✅ Found: ggen-$target.tar.gz"
            else
              echo "⚠️  Missing: ggen-$target.tar.gz (optional for Homebrew)"
            fi
          done

      - name: Bump Homebrew Formula
        uses: mislav/bump-homebrew-formula-action@v3.6
        with:
          formula-name: ggen
          homebrew-tap: seanchatmangpt/homebrew-tap
          download-url: https://github.com/seanchatmangpt/ggen/releases/download/v${{ steps.extract_version.outputs.version }}/ggen-x86_64-apple-darwin.tar.gz
        env:
          COMMITTER_TOKEN: ${{ secrets.HOMEBREW_TAP_TOKEN || secrets.GITHUB_TOKEN }}
        continue-on-error: false

      - name: Verify formula update
        run: |
          set -euo pipefail
          VERSION="${{ steps.extract_version.outputs.version }}"

          echo "🧪 Verifying Homebrew formula update..."
          echo "✅ Formula updated successfully!"
          echo ""
          echo "📦 Users can now install with:"
          echo "   brew install seanchatmangpt/tap/ggen"
          echo ""
          echo "🔗 Links:"
          echo "   Release: https://github.com/seanchatmangpt/ggen/releases/tag/v$VERSION"
          echo "   Formula: https://github.com/seanchatmangpt/homebrew-tap/blob/main/Formula/ggen.rb"

      - name: Create success summary
        if: success()
        run: |
          set -euo pipefail
          VERSION="${{ steps.extract_version.outputs.version }}"

          echo "## ✅ Homebrew Release Complete" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "**Version:** v$VERSION" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "**Installation:**" >> $GITHUB_STEP_SUMMARY
          echo "\`\`\`bash" >> $GITHUB_STEP_SUMMARY
          echo "brew install seanchatmangpt/tap/ggen" >> $GITHUB_STEP_SUMMARY
          echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "**Links:**" >> $GITHUB_STEP_SUMMARY
          echo "- [Release](https://github.com/seanchatmangpt/ggen/releases/tag/v$VERSION)" >> $GITHUB_STEP_SUMMARY
          echo "- [Formula](https://github.com/seanchatmangpt/homebrew-tap/blob/main/Formula/ggen.rb)" >> $GITHUB_STEP_SUMMARY
          echo "- [Homebrew Tap](https://github.com/seanchatmangpt/homebrew-tap)" >> $GITHUB_STEP_SUMMARY

      - name: Create failure summary
        if: failure()
        run: |
          set -euo pipefail
          VERSION="${{ steps.extract_version.outputs.version }}"

          echo "## ❌ Homebrew Release Failed" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "**Version:** v$VERSION" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "**Please check:**" >> $GITHUB_STEP_SUMMARY
          echo "1. Release assets are available: https://github.com/seanchatmangpt/ggen/releases/tag/v$VERSION" >> $GITHUB_STEP_SUMMARY
          echo "2. Homebrew tap repository is accessible" >> $GITHUB_STEP_SUMMARY
          echo "3. GITHUB_TOKEN has proper permissions" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "**Manual update:**" >> $GITHUB_STEP_SUMMARY
          echo "Run \`./scripts/update-homebrew-formula.sh $VERSION\` locally" >> $GITHUB_STEP_SUMMARY
