#!/bin/bash
export LC_ALL=C.UTF-8
URL=http://127.0.0.1:5678/webhook/bale-chat
SECRET=2633813394

ask() {
  local q="$1" sid="$2"
  printf '\n>>> %s\n' "$q"
  out=$(curl -s --max-time 120 -X POST "$URL" \
    -H "Authorization: Bearer $SECRET" -H 'Content-Type: application/json' \
    -w '\n__TIME__%{time_total}' \
    -d "{\"chatInput\":\"$q\",\"sessionId\":\"$sid\"}" 2>&1)
  t=${out##*__TIME__}
  body=${out%__TIME__*}
  echo "    time: ${t}s"
  echo "$body" | python3 -c 'import sys,json
raw=sys.stdin.read().strip()
try:
    d=json.loads(raw); print("    reply:", str(d.get("output") or d)[:400])
except Exception:
    print("    raw:", raw[:400])'
}

ask "مدیرعامل فاوا کیه" "probe-$(date +%s)-1"
ask "فاوا چه سالی تاسیس شد" "probe-$(date +%s)-2"
ask "مدیران فاوا چه کسانی بودند" "probe-$(date +%s)-3"
