#!/usr/bin/env bash
#ddev-generated
## Description: Grade one task against live proving-ground state; emit an every_eval_ever 0.2.2 verdict as JSON on stdout (outside-in seam)
## Usage: ai-eval-grade <task-id> [dataset] [model-id] [--run=<run-id>]
## Example: "ddev ai-eval-grade dc01_canvas_build droost_build_tasks --run=example-run"
set -euo pipefail

APPROOT="${DDEV_APPROOT:-/var/www/html}"
AI_PG_ROOT="${AI_PG_ROOT:-${APPROOT}/recipes/ai_proving_ground}"
RUN_ID=""
POSITIONAL=()
for arg in "$@"; do
  case "${arg}" in
    --run=*) RUN_ID="${arg#*=}" ;;
    *) POSITIONAL+=("${arg}") ;;
  esac
done
set -- "${POSITIONAL[@]}"
TASK_ID="${1:?usage: ddev ai-eval-grade <task-id> [dataset] [model-id] [--run=<run-id>]}"
DATASET="${2:-droost_build_tasks}"
MODEL_ID="${3:-}"

# Exit 0 means "a verdict was emitted" (pass AND fail alike; the verdict is
# in the JSON). Exit 3 means the question has no droost spec so nothing was
# graded (no envelope). Other non-zero means the grade could not run.
# drush collapses every explicit exit() in a php:script to code 1, so the
# skip distinction rides a stable stderr token (APG_GRADE_SKIP) that this
# wrapper maps back to exit 3.
STDERR_FILE="$(mktemp)"
trap 'rm -f "${STDERR_FILE}"' EXIT
if drush php:script --script-path="${AI_PG_ROOT}/scripts" grade.php -- "${TASK_ID}" "${DATASET}" "${MODEL_ID}" "${RUN_ID}" 2>"${STDERR_FILE}"; then
  cat "${STDERR_FILE}" >&2
  exit 0
fi
cat "${STDERR_FILE}" >&2
if grep -q 'APG_GRADE_SKIP' "${STDERR_FILE}"; then
  exit 3
fi
exit 1
