#!/bin/bash
# Check real recipe database status and control the mass scraper
# Usage: ./check-real-recipes.sh [status|report|start|stop]
#   status  — quick count + log tail
#   report  — count, delta, target met, and whether scraper is running
#   start   — start v4 scraper if below target and not already running
#   stop    — stop any running scraper process

set -euo pipefail

LOG="${HOME}/.hermes/profiles/nutrition-coach/scraper.log"
OUT="${HOME}/.hermes/profiles/nutrition-coach/scraper_v4.out"
DB="${HOME}/.hermes/profiles/nutrition-coach/real_recipes.jsonl"
TARGET=25000

count=$(wc -l < "$DB" 2>/dev/null || echo 0)
delta=$((TARGET - count))
if [ "$count" -ge "$TARGET" ]; then
  met="YES"
else
  met="NO"
fi
pct=$(( count * 100 / TARGET ))
is_running=$(pgrep -f chefkoch_scraper_v4.py >/dev/null && echo "yes" || echo "no")

case "${1:-status}" in
  status|report)
    printf "Real recipe DB: %d / %d recipes (%d%%)\n" "$count" "$TARGET" "$pct"
    printf "Delta: %d  |  Target met: %s  |  Scraper running: %s\n" "$delta" "$met" "$is_running"
    if [ -f "$OUT" ]; then
      printf "\n--- Scraper out tail ---\n"
      tail -n 5 "$OUT"
    fi
    if [ -f "$LOG" ]; then
      printf "\n--- Log tail ---\n"
      tail -n 5 "$LOG"
    fi
    ;;
  count)
    echo "$count"
    ;;
  start)
    if [ "$count" -ge "$TARGET" ]; then
      echo "Target $TARGET already reached."
      exit 0
    fi
    if [ "$is_running" = "yes" ]; then
      echo "Scraper already running."
      exit 0
    fi
    echo "Starting v4 mass scraper..."
    cd "${HOME}/.hermes/profiles/nutrition-coach" || exit 1
    export PYTHONUNBUFFERED=1
    export PLAYWRIGHT_BROWSERS_PATH="${HOME}/.cache/ms-playwright"
    nohup python3 scripts/chefkoch_scraper_v4.py --no-agent >> "$OUT" 2>&1 &
    pid=$!
    echo "PID: $pid"
    ;;
  stop)
    pkill -f chefkoch_scraper_v4.py && echo "Stopped scraper" || echo "Scraper not running"
    ;;
esac
