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

# ──────────────────────────────────────────────────────────────
# qa-score.sh — Run the quality-scorer agent against this repo
#
# Usage:
#   ./scripts/qa-score.sh              # default: sonnet, headless
#   ./scripts/qa-score.sh --model opus # use opus
#   ./scripts/qa-score.sh --interactive # open in interactive session
# ──────────────────────────────────────────────────────────────

PROJECT_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
AGENT="quality-scorer"
MODEL="sonnet"
INTERACTIVE=false
BUDGET="1.00"

while [[ $# -gt 0 ]]; do
  case "$1" in
    --model)    MODEL="$2"; shift 2 ;;
    --budget)   BUDGET="$2"; shift 2 ;;
    --interactive) INTERACTIVE=true; shift ;;
    --help|-h)
      echo "Usage: $0 [--model sonnet|opus|haiku] [--budget USD] [--interactive]"
      echo ""
      echo "Runs the quality-scorer agent to produce a QA report in docs/."
      echo ""
      echo "Options:"
      echo "  --model MODEL    Claude model to use (default: sonnet)"
      echo "  --budget USD     Max spend in USD (default: 1.00, headless only)"
      echo "  --interactive    Open interactive session instead of headless"
      exit 0
      ;;
    *) echo "Unknown option: $1"; exit 1 ;;
  esac
done

cd "$PROJECT_ROOT"

# Verify prerequisites
if ! command -v claude &>/dev/null; then
  echo "Error: claude CLI not found. Install from https://claude.ai/code" >&2
  exit 1
fi

if ! command -v uv &>/dev/null; then
  echo "Error: uv not found. Install from https://docs.astral.sh/uv/" >&2
  exit 1
fi

if [[ ! -f "docs/QA-INSTRUCTIONS.md" ]]; then
  echo "Error: docs/QA-INSTRUCTIONS.md not found. Run from the project root." >&2
  exit 1
fi

# Ensure dev dependencies are available
uv sync --dev --quiet

PROMPT="Run a full codebase quality assessment. \
Read docs/QA-INSTRUCTIONS.md for the methodology and rubrics. \
Read docs/QA-TEMPLATE.md for the report structure. \
Check docs/ for previous QA-*.md reports and compute deltas if any exist. \
Collect all raw metrics by running every command in Step 1. \
Score each dimension using the Step 2 rubrics. \
Compute the composite score using the Step 3 formula. \
Write the completed report to docs/QA-<datetime>.md. \
Print the composite score, grade, per-dimension scores, and top 3 actions."

if [[ "$INTERACTIVE" == true ]]; then
  echo "Starting interactive QA session (model: $MODEL)..."
  exec claude \
    --agent "$AGENT" \
    --model "$MODEL" \
    "$PROMPT"
else
  echo "Running quality assessment (model: $MODEL, budget: \$$BUDGET)..."
  echo ""

  claude \
    -p \
    --agent "$AGENT" \
    --model "$MODEL" \
    --max-budget-usd "$BUDGET" \
    --allowedTools "Bash Read Write Grep Glob" \
    --output-format text \
    "$PROMPT"

  echo ""
  echo "──────────────────────────────────────────"

  # Show the report that was just written
  LATEST=$(ls -t docs/QA-2*.md 2>/dev/null | head -1)
  if [[ -n "$LATEST" ]]; then
    echo "Report written: $LATEST"
  else
    echo "Warning: no QA report file found in docs/"
  fi
fi
