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:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-musl
            artifact: epic-harness-x86_64-unknown-linux-musl.tar.gz
          - os: ubuntu-latest
            target: aarch64-unknown-linux-musl
            artifact: epic-harness-aarch64-unknown-linux-musl.tar.gz
            cross: true
          - os: macos-latest
            target: aarch64-apple-darwin
            artifact: epic-harness-aarch64-apple-darwin.tar.gz
          - os: macos-latest
            target: x86_64-apple-darwin
            artifact: epic-harness-x86_64-apple-darwin.tar.gz
          - os: windows-latest
            target: x86_64-pc-windows-msvc
            artifact: epic-harness-x86_64-pc-windows-msvc.zip

    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

      - name: Install Rust
        uses: dtolnay/rust-toolchain@3c5f7ea28cd621ae0bf5283f0e981fb97b8a7af9 # stable
        with:
          toolchain: stable
          targets: ${{ matrix.target }}

      - name: Cache cargo
        uses: actions/cache@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ matrix.target }}-cargo-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: ${{ matrix.target }}-cargo-

      - name: Install musl-tools (Linux x86_64)
        if: matrix.target == 'x86_64-unknown-linux-musl'
        run: sudo apt-get install -y musl-tools

      - name: Install cross (Linux ARM64)
        if: matrix.cross
        run: cargo install cross --git https://github.com/cross-rs/cross

      - name: Build (native)
        if: "!matrix.cross"
        env:
          POSTHOG_KEY: ${{ secrets.POSTHOG_KEY }}
          SENTRY_DSN_EPIC_HARNESS: ${{ secrets.SENTRY_DSN_EPIC_HARNESS }}
        run: cargo build --release --target ${{ matrix.target }}

      - name: Build (cross)
        if: matrix.cross
        env:
          POSTHOG_KEY: ${{ secrets.POSTHOG_KEY }}
          SENTRY_DSN_EPIC_HARNESS: ${{ secrets.SENTRY_DSN_EPIC_HARNESS }}
        run: cross build --release --target ${{ matrix.target }}

      - name: Package (Unix)
        if: runner.os != 'Windows'
        run: |
          tar -czf ${{ matrix.artifact }} \
            -C target/${{ matrix.target }}/release epic-harness epic

      - name: Package (Windows)
        if: runner.os == 'Windows'
        shell: pwsh
        run: |
          Compress-Archive `
            -Path @("target/${{ matrix.target }}/release/epic-harness.exe", "target/${{ matrix.target }}/release/epic.exe") `
            -DestinationPath ${{ matrix.artifact }}

      - name: Upload artifact
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: ${{ matrix.artifact }}
          path: ${{ matrix.artifact }}

  release:
    name: Create Release
    needs: build
    runs-on: ubuntu-latest
    permissions:
      contents: write

    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

      - name: Download all artifacts
        uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
        with:
          merge-multiple: true

      - name: Publish Release
        uses: softprops/action-gh-release@b4309332981a82ec1c5618f44dd2e27cc8bfbfda # v3.0.0
        with:
          generate_release_notes: true
          files: |
            epic-harness-*.tar.gz
            epic-harness-*.zip

  publish-crates:
    name: Publish to crates.io
    needs: release
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

      - name: Install Rust
        uses: dtolnay/rust-toolchain@3c5f7ea28cd621ae0bf5283f0e981fb97b8a7af9 # stable
        with:
          toolchain: stable

      - name: Publish
        run: cargo publish --token ${{ secrets.CARGO_REGISTRY_TOKEN }}

  update-homebrew:
    name: Update Homebrew Tap
    needs: release
    runs-on: ubuntu-latest
    if: "!contains(github.ref_name, '-')"
    steps:
      - name: Compute SHA256 for macOS tarballs
        id: sha
        run: |
          BASE="https://github.com/epicsagas/epic-harness/releases/download/${GITHUB_REF_NAME}"
          for i in 1 2 3; do
            curl -sLf "${BASE}/epic-harness-aarch64-apple-darwin.tar.gz" -o arm.tar.gz && break
            sleep 10
          done
          for i in 1 2 3; do
            curl -sLf "${BASE}/epic-harness-x86_64-apple-darwin.tar.gz" -o intel.tar.gz && break
            sleep 10
          done
          echo "arm=$(sha256sum arm.tar.gz | awk '{print $1}')" >> $GITHUB_OUTPUT
          echo "intel=$(sha256sum intel.tar.gz | awk '{print $1}')" >> $GITHUB_OUTPUT

      - name: Checkout homebrew tap
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          repository: epicsagas/homebrew-tap
          token: ${{ secrets.HOMEBREW_TAP_TOKEN }}

      - name: Update formula
        env:
          SHA_ARM: ${{ steps.sha.outputs.arm }}
          SHA_INTEL: ${{ steps.sha.outputs.intel }}
        run: |
          VERSION="${GITHUB_REF_NAME#v}"
          cat > Formula/epic-harness.rb << RUBY
          class EpicHarness < Formula
            desc "Self-evolving Claude Code harness — 6 commands, auto skills"
            homepage "https://github.com/epicsagas/epic-harness"
            version "${VERSION}"
            license "Apache-2.0"

            on_macos do
              if Hardware::CPU.arm?
                url "https://github.com/epicsagas/epic-harness/releases/download/v${VERSION}/epic-harness-aarch64-apple-darwin.tar.gz"
                sha256 "${SHA_ARM}"
              else
                url "https://github.com/epicsagas/epic-harness/releases/download/v${VERSION}/epic-harness-x86_64-apple-darwin.tar.gz"
                sha256 "${SHA_INTEL}"
              end
            end

            def install
              bin.install "epic-harness"
            end

            test do
              assert_match "epic-harness", shell_output("#{bin}/epic-harness version 2>&1")
            end
          end
          RUBY

      - 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/epic-harness.rb
          git diff --cached --quiet || git commit -m "epic-harness ${GITHUB_REF_NAME#v}"
          git push
