name: Release

on:
  release:
    types: [published]

permissions:
  contents: write

env:
  CARGO_TERM_COLOR: always

jobs:
  # ── Build matrix ─────────────────────────────────────────────────────────────
  build:
    name: Build ${{ matrix.target }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - target: x86_64-unknown-linux-gnu
            runner: ubuntu-latest
            use_cross: false
          - target: aarch64-unknown-linux-gnu
            runner: ubuntu-latest
            use_cross: true
          - target: x86_64-apple-darwin
            runner: macos-latest
            use_cross: false
          - target: aarch64-apple-darwin
            runner: macos-latest
            use_cross: false

    runs-on: ${{ matrix.runner }}

    steps:
      - uses: actions/checkout@v4

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

      - name: Install cross (for aarch64-unknown-linux-gnu)
        if: matrix.use_cross
        run: cargo install cross --git https://github.com/cross-rs/cross

      - name: Cache cargo registry
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry/index
            ~/.cargo/registry/cache
            ~/.cargo/git/db
          key: ${{ runner.os }}-${{ matrix.target }}-cargo-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: ${{ runner.os }}-${{ matrix.target }}-cargo-

      - name: Build binary
        run: |
          if [ "${{ matrix.use_cross }}" = "true" ]; then
            cross build --release --target ${{ matrix.target }}
          else
            cargo build --release --target ${{ matrix.target }}
          fi

      - name: Prepare release asset
        shell: bash
        run: |
          BIN="target/${{ matrix.target }}/release/superharness"
          ASSET="superharness-${{ matrix.target }}"

          cp "$BIN" "$ASSET"

          if command -v sha256sum >/dev/null 2>&1; then
            sha256sum "$ASSET" > "$ASSET.sha256"
          else
            shasum -a 256 "$ASSET" > "$ASSET.sha256"
          fi

          echo "ASSET=${ASSET}" >> "$GITHUB_ENV"

      - name: Build .deb package (Linux only)
        if: contains(matrix.target, 'linux')
        shell: bash
        run: |
          VERSION="${GITHUB_REF_NAME#v}"
          case "${{ matrix.target }}" in
            x86_64-unknown-linux-gnu)  DEB_ARCH="amd64" ;;
            aarch64-unknown-linux-gnu) DEB_ARCH="arm64" ;;
          esac

          PKG_DIR="superharness_${VERSION}_${DEB_ARCH}"
          mkdir -p "${PKG_DIR}/DEBIAN" "${PKG_DIR}/usr/bin"

          cat > "${PKG_DIR}/DEBIAN/control" <<EOF
          Package: superharness
          Version: ${VERSION}
          Architecture: ${DEB_ARCH}
          Maintainer: Nikita Kolmogorov <ubuntu@borodutch.com>
          Description: Autonomous multi-agent orchestrator for AI coding
           CLI tool that orchestrates AI coding agents via tmux panes,
           enabling parallel multi-agent workflows with worktree isolation.
          Homepage: https://github.com/backmeupplz/superharness
          Depends: tmux
          Section: utils
          Priority: optional
          EOF
          sed -i 's/^          //' "${PKG_DIR}/DEBIAN/control"

          cp "target/${{ matrix.target }}/release/superharness" "${PKG_DIR}/usr/bin/superharness"
          dpkg-deb --build "${PKG_DIR}"

      - name: Upload release assets
        uses: softprops/action-gh-release@v2
        with:
          tag_name: ${{ github.ref_name }}
          name: "superharness ${{ github.ref_name }}"
          draft: false
          prerelease: false
          generate_release_notes: true
          files: |
            ${{ env.ASSET }}
            ${{ env.ASSET }}.sha256
            *.deb

  # ── Publish to crates.io (after all builds succeed) ───────────────────────────
  publish-crates:
    name: Publish to crates.io
    needs: build
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable

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