name: Release

on:
  push:
    tags:
      - 'v*'

jobs:
  # 1. Build prebuilt binaries for each target and attach to the GitHub release.
  build:
    permissions:
      contents: write
    strategy:
      fail-fast: false
      matrix:
        include:
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
          - target: x86_64-apple-darwin
            os: macos-latest
          - target: aarch64-apple-darwin
            os: macos-latest
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}
      - uses: Swatinem/rust-cache@v2
      - run: cargo build --release --target ${{ matrix.target }}
      - name: Package
        run: |
          mkdir -p dist
          cp target/${{ matrix.target }}/release/cc-session dist/cc-session-${{ matrix.target }}
          tar -czf dist/cc-session-${{ matrix.target }}.tar.gz -C dist cc-session-${{ matrix.target }}
          (cd dist && shasum -a 256 cc-session-${{ matrix.target }}.tar.gz > cc-session-${{ matrix.target }}.tar.gz.sha256)
      - uses: softprops/action-gh-release@v2
        with:
          files: |
            dist/cc-session-${{ matrix.target }}.tar.gz
            dist/cc-session-${{ matrix.target }}.tar.gz.sha256
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  # 2. Publish the crate to crates.io.
  cargo-publish:
    runs-on: ubuntu-latest
    needs: build
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - name: Publish
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
        run: cargo publish --no-verify

  # 3. Update Homebrew formula in this repo (it doubles as the tap).
  homebrew:
    runs-on: ubuntu-latest
    needs: build
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v4
        with:
          ref: main
          token: ${{ secrets.GITHUB_TOKEN }}

      - name: Resolve tag
        id: tag
        run: echo "version=${GITHUB_REF_NAME#v}" >> "$GITHUB_OUTPUT"

      - name: Fetch SHA256s from release
        id: sha
        env:
          VERSION: ${{ steps.tag.outputs.version }}
        run: |
          base="https://github.com/dudegladiator/claude-code-session-editor/releases/download/v${VERSION}"
          for t in aarch64-apple-darwin x86_64-apple-darwin x86_64-unknown-linux-gnu; do
            sha=$(curl -fsSL "$base/cc-session-${t}.tar.gz.sha256" | awk '{print $1}')
            echo "${t}=${sha}" >> "$GITHUB_OUTPUT"
          done

      - name: Write formula
        env:
          VERSION: ${{ steps.tag.outputs.version }}
          SHA_ARM: ${{ steps.sha.outputs.aarch64-apple-darwin }}
          SHA_INTEL: ${{ steps.sha.outputs.x86_64-apple-darwin }}
          SHA_LINUX: ${{ steps.sha.outputs.x86_64-unknown-linux-gnu }}
        run: |
          mkdir -p Formula
          cat > Formula/cc-session.rb <<EOF
          class CcSession < Formula
            desc "Interactive TUI editor for Claude Code session JSONL files"
            homepage "https://github.com/dudegladiator/claude-code-session-editor"
            version "${VERSION}"
            license "MIT"

            on_macos do
              on_arm do
                url "https://github.com/dudegladiator/claude-code-session-editor/releases/download/v#{version}/cc-session-aarch64-apple-darwin.tar.gz"
                sha256 "${SHA_ARM}"
              end
              on_intel do
                url "https://github.com/dudegladiator/claude-code-session-editor/releases/download/v#{version}/cc-session-x86_64-apple-darwin.tar.gz"
                sha256 "${SHA_INTEL}"
              end
            end

            on_linux do
              url "https://github.com/dudegladiator/claude-code-session-editor/releases/download/v#{version}/cc-session-x86_64-unknown-linux-gnu.tar.gz"
              sha256 "${SHA_LINUX}"
            end

            def install
              bin.install Dir["cc-session-*"].first => "cc-session"
            end

            test do
              assert_match version.to_s, shell_output("#{bin}/cc-session --version")
            end
          end
          EOF

      - name: Commit + push formula
        env:
          VERSION: ${{ steps.tag.outputs.version }}
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git add Formula/cc-session.rb
          if git diff --cached --quiet; then
            echo "no formula changes"
            exit 0
          fi
          git commit -m "chore: update Homebrew formula for v${VERSION}"
          git push origin HEAD:main
