name: Release

on:
  push:
    tags:
      - 'v*'

permissions:
  contents: write

jobs:
  build:
    name: Build ${{ matrix.target }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - target: aarch64-apple-darwin
            os: macos-latest
            artifact: mochify-macos-arm64
          - target: x86_64-apple-darwin
            os: macos-latest
            artifact: mochify-macos-x86_64
          - target: x86_64-unknown-linux-musl
            os: ubuntu-latest
            artifact: mochify-linux-x86_64
          - target: aarch64-unknown-linux-musl
            os: ubuntu-latest
            artifact: mochify-linux-arm64

    steps:
      - uses: actions/checkout@v4

      - uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}

      - name: Install musl tools
        if: contains(matrix.target, 'musl')
        run: |
          sudo apt-get update -q
          sudo apt-get install -y musl-tools
          if [ "${{ matrix.target }}" = "aarch64-unknown-linux-musl" ]; then
            sudo apt-get install -y gcc-aarch64-linux-gnu
          fi

      - name: Build
        env:
          CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_LINKER: aarch64-linux-gnu-gcc
        run: cargo build --release --target ${{ matrix.target }}

      - name: Rename binary
        run: cp target/${{ matrix.target }}/release/mochify ${{ matrix.artifact }}

      - name: Upload to release
        uses: softprops/action-gh-release@v2
        with:
          files: ${{ matrix.artifact }}

  homebrew:
    name: Update Homebrew Formula
    runs-on: ubuntu-latest
    needs: build
    steps:
      - name: Checkout homebrew tap
        uses: actions/checkout@v4
        with:
          repository: getmochify/homebrew-mochify
          token: ${{ secrets.HOMEBREW_TAP_TOKEN }}

      - name: Compute SHA256 hashes
        id: sha
        run: |
          VERSION="${{ github.ref_name }}"
          BASE="https://github.com/getmochify/mochify-cli/releases/download/${VERSION}"
          ARM64_SHA=$(curl -sL "${BASE}/mochify-macos-arm64" | sha256sum | awk '{print $1}')
          X86_SHA=$(curl -sL "${BASE}/mochify-macos-x86_64" | sha256sum | awk '{print $1}')
          echo "arm64_sha=${ARM64_SHA}" >> "$GITHUB_OUTPUT"
          echo "x86_sha=${X86_SHA}" >> "$GITHUB_OUTPUT"
          echo "version=${VERSION#v}" >> "$GITHUB_OUTPUT"

      - name: Write formula
        run: |
          VERSION="${{ steps.sha.outputs.version }}"
          ARM64_SHA="${{ steps.sha.outputs.arm64_sha }}"
          X86_SHA="${{ steps.sha.outputs.x86_sha }}"
          cat > Formula/mochify.rb << EOF
          class Mochify < Formula
            desc "CLI and MCP server for AI-powered image optimization via mochify.app"
            homepage "https://mochify.app"
            version "${VERSION}"
            license "MIT"

            on_macos do
              on_arm do
                url "https://github.com/getmochify/mochify-cli/releases/download/v${VERSION}/mochify-macos-arm64"
                sha256 "${ARM64_SHA}"
              end
              on_intel do
                url "https://github.com/getmochify/mochify-cli/releases/download/v${VERSION}/mochify-macos-x86_64"
                sha256 "${X86_SHA}"
              end
            end

            def install
              bin.install stable.url.split("/").last => "mochify"
            end

            test do
              assert_match "mochify", shell_output("#{bin}/mochify --version")
            end
          end
          EOF

      - name: Commit and push
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git add Formula/mochify.rb
          git commit -m "mochify ${{ steps.sha.outputs.version }}"
          git push
