#!/usr/bin/env bash
# Start a Hubertino scrape, poll until it finishes, download the CSV.
#
#   export HUBERTINO_API_KEY=hub_live_...        # Dashboard -> Settings
#   ./scrape.sh "dentist" "Austin, TX, United States" 50 leads.csv
#
# Needs: bash, curl, jq.
# Optional env: HUBERTINO_BASE_URL (default https://hubertino.com/api/v1),
#               HUBERTINO_POLL_SECONDS (default 5).
set -euo pipefail

CATEGORY="${1:?usage: scrape.sh <category> <location> [max_results] [out.csv]}"
LOCATION="${2:?usage: scrape.sh <category> <location> [max_results] [out.csv]}"
MAX_RESULTS="${3:-50}"
OUT="${4:-leads.csv}"
BASE="${HUBERTINO_BASE_URL:-https://hubertino.com/api/v1}"
POLL="${HUBERTINO_POLL_SECONDS:-5}"
: "${HUBERTINO_API_KEY:?Set HUBERTINO_API_KEY to your hub_live_... key}"

AUTH="Authorization: Bearer ${HUBERTINO_API_KEY}"

# Every error is JSON: {"error": "..."}; print it and stop.
api() {
  local body status
  body=$(mktemp)
  status=$(curl -sS -o "$body" -w '%{http_code}' -H "$AUTH" "$@")
  if [[ "$status" -ge 400 ]]; then
    echo "HTTP $status: $(jq -r '.error // .' < "$body" 2>/dev/null || cat "$body")" >&2
    rm -f "$body"
    exit 1
  fi
  cat "$body"
  rm -f "$body"
}

# 1. Start. jq builds the JSON, so quotes and commas in the location are safe.
PAYLOAD=$(jq -n --arg c "$CATEGORY" --arg l "$LOCATION" --argjson m "$MAX_RESULTS" \
  '{categories: [$c], locations: [$l], maxResults: $m}')
ID=$(api -X POST "$BASE/scrapes" -H 'content-type: application/json' -d "$PAYLOAD" | jq -r .scrape.id)
echo "Started scrape $ID"

# 2. Poll. status: queued -> running -> done | error.
while true; do
  SCRAPE=$(api "$BASE/scrapes/$ID")
  STATUS=$(jq -r .scrape.status <<<"$SCRAPE")
  case "$STATUS" in
    done) break ;;
    error) echo "Scrape failed: $(jq -r .scrape.error <<<"$SCRAPE")" >&2; exit 1 ;;
    *) echo "  $STATUS $(jq -r '(.scrape.progress.percent // 0|tostring) + "% · " + (.scrape.resultCount|tostring) + " places"' <<<"$SCRAPE")"
       sleep "$POLL" ;;
  esac
done
echo "Done: $(jq -r .scrape.resultCount <<<"$SCRAPE") places, $(jq -r .scrape.creditsCharged <<<"$SCRAPE") credits charged"

# 3. Download the CSV (only available once status is done).
STATUS=$(curl -sS -o "$OUT" -w '%{http_code}' -H "$AUTH" "$BASE/scrapes/$ID/export?format=csv")
if [[ "$STATUS" != "200" ]]; then
  echo "Export failed (HTTP $STATUS): $(jq -r '.error // .' < "$OUT" 2>/dev/null)" >&2
  rm -f "$OUT"
  exit 1
fi
echo "Saved $OUT"
