#!/usr/bin/env bash
set -euo pipefail

if [[ $# -lt 1 ]]; then
  cat >&2 <<'USAGE'
Usage:
  jarvis_code_review.sh <repo-path> [task]

Examples:
  jarvis_code_review.sh /home/agent/projects/FamilyDashboard "Review current git diff"
  jarvis_code_review.sh . "Review last commit for regressions"
USAGE
  exit 2
fi

REPO="$1"
TASK="${2:-Review the current git diff for bugs, regressions, missing tests, security issues, and maintainability problems. Do not modify files. Return a compact German review.}"

cd "$REPO"

# Keep context compact: provide status + bounded diff stats/full diff when reasonable.
STATUS="$(git status --short 2>/dev/null || true)"
DIFF_STAT="$(git diff --stat 2>/dev/null || true)"
DIFF_CHARS="$(git diff --no-ext-diff 2>/dev/null | wc -c | tr -d ' ')"

if [[ "${DIFF_CHARS:-0}" -le 30000 ]]; then
  DIFF="$(git diff --no-ext-diff 2>/dev/null || true)"
else
  DIFF="[DIFF_TOO_LARGE: ${DIFF_CHARS} chars. Use git diff --stat and targeted file inspection only.]"
fi

PROMPT=$(cat <<EOF
Du bist der JARVIS Code-Review-Worker (read-only).

Repo: $REPO
Aufgabe: $TASK

Regeln:
- Keine Dateien ändern, keine Commits, kein Push, kein Deploy.
- Keine Secrets ausgeben.
- Review kompakt auf Deutsch.
- Priorisiere echte Bugs/Regressionen/Sicherheitsprobleme vor Stil.

Git status:
$STATUS

Diff stat:
$DIFF_STAT

Diff/context:
$DIFF
EOF
)

hermes --profile code-review chat --toolsets terminal,file --max-turns 40 -q "$PROMPT"
