#!/usr/bin/env bash
# Usage: search-history.sh <phone e.g. +19723637754> <query> [--since YYYY-MM-DD] [--until YYYY-MM-DD] [--limit N] [--refresh]
# Searches FULL Sendblue archive (all ~2600+ msgs, paginated via /api/v2/messages) for <query>
# (case-insensitive substring). Emits matches as JSONL {ts,dir,content,handle} on stdout.
# Also merges fetched msgs into $SENDBLUE_HARNESS_DIR/<phone>/history.jsonl (dedup by handle)
# so future searches are cheap. See HARNESS_DIR resolution below; mirrors server.ts.
# OUTPUT IS SENSITIVE — may contain PII/card numbers; do not log or paste into
# untrusted places.

set -euo pipefail

PHONE="${1:-}"
QUERY="${2:-}"
shift 2 2>/dev/null || true

SINCE=""
UNTIL=""
LIMIT=0
REFRESH=0
while [ $# -gt 0 ]; do
  case "$1" in
    --since) SINCE="$2"; shift 2 ;;
    --until) UNTIL="$2"; shift 2 ;;
    --limit) LIMIT="$2"; shift 2 ;;
    --refresh) REFRESH=1; shift ;;
    *) echo "unknown arg: $1" >&2; exit 2 ;;
  esac
done

if [ -z "$PHONE" ] || [ -z "$QUERY" ]; then
  echo "usage: $0 <phone> <query> [--since YYYY-MM-DD] [--until YYYY-MM-DD] [--limit N] [--refresh]" >&2
  exit 2
fi

SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd -- "$SCRIPT_DIR/../.." && pwd)"
ENV_FILE="$SCRIPT_DIR/.env"
if [ -f "$ENV_FILE" ]; then
  set -a
  # shellcheck disable=SC1090,SC1091
  source "$ENV_FILE"
  set +a
fi

: "${SENDBLUE_API_KEY_ID:?SENDBLUE_API_KEY_ID not set}"
: "${SENDBLUE_API_SECRET_KEY:?SENDBLUE_API_SECRET_KEY not set}"

# Resolve harness dir using the same precedence as server.ts:
#   $SENDBLUE_HARNESS_DIR > $SENDBLUE_STATE_DIR/harness > ~/.claude/channels/sendblue/harness
# Falls back to the legacy repo-local path only if explicitly opted in.
HARNESS_DIR="${SENDBLUE_HARNESS_DIR:-${SENDBLUE_STATE_DIR:-$HOME/.claude/channels/sendblue}/harness}"
HIST_DIR="$HARNESS_DIR/$PHONE"
HIST_FILE="$HIST_DIR/history.jsonl"
mkdir -p "$HIST_DIR"
touch "$HIST_FILE"

export PHONE QUERY SINCE UNTIL LIMIT REFRESH HIST_FILE SENDBLUE_API_KEY_ID SENDBLUE_API_SECRET_KEY

python3 <<'PY'
import json
import os
import sys
import urllib.parse
import urllib.request

PHONE = os.environ["PHONE"]
QUERY = os.environ["QUERY"].lower()
SINCE = os.environ.get("SINCE") or ""
UNTIL = os.environ.get("UNTIL") or ""
LIMIT = int(os.environ.get("LIMIT") or 0)
HIST_FILE = os.environ["HIST_FILE"]
KEY = os.environ["SENDBLUE_API_KEY_ID"]
SEC = os.environ["SENDBLUE_API_SECRET_KEY"]

def fetch_page(offset: int, limit: int = 100):
    qs = urllib.parse.urlencode({"limit": limit, "offset": offset})
    url = f"https://api.sendblue.co/api/v2/messages?{qs}"
    req = urllib.request.Request(
        url,
        headers={
            "sb-api-key-id": KEY,
            "sb-api-secret-key": SEC,
            "Content-Type": "application/json",
        },
    )
    with urllib.request.urlopen(req, timeout=30) as resp:
        return json.loads(resp.read())

# Load existing handles for dedup
seen = set()
try:
    with open(HIST_FILE, "r", encoding="utf-8") as f:
        for line in f:
            try:
                r = json.loads(line)
                h = r.get("handle")
                if h:
                    seen.add(h)
            except Exception:
                continue
except FileNotFoundError:
    pass

matches = []
new_rows = []  # appended to history.jsonl
offset = 0
page_size = 100
pages = 0

while True:
    data = fetch_page(offset, page_size)
    msgs = data.get("data", []) or []
    pag = data.get("pagination") or {}
    total = pag.get("total")
    has_more = pag.get("hasMore")
    pages += 1

    if not msgs:
        break

    for m in msgs:
        # Only messages involving PHONE
        from_n = m.get("from_number") or ""
        to_n = m.get("to_number") or ""
        num = m.get("number") or ""
        if PHONE not in (from_n, to_n, num):
            continue

        ts = m.get("date_sent") or m.get("date_updated") or ""
        content = m.get("content") or ""
        handle = m.get("message_handle") or ""
        is_out = bool(m.get("is_outbound"))
        direction = "outbound" if is_out else "inbound"

        # Date filtering (client-side, since API ignores date params)
        if SINCE and ts and ts[:10] < SINCE:
            continue
        if UNTIL and ts and ts[:10] > UNTIL:
            continue

        # Merge into history.jsonl if new
        if handle and handle not in seen:
            seen.add(handle)
            new_rows.append({
                "ts": ts,
                "dir": direction,
                "from": from_n if not is_out else os.environ.get("SENDBLUE_OWN_NUMBER", ""),
                "content": content,
                "handle": handle,
            })

        # Search
        if QUERY in content.lower():
            matches.append({
                "ts": ts,
                "dir": direction,
                "content": content,
                "handle": handle,
            })
            if LIMIT and len(matches) >= LIMIT:
                break

    if LIMIT and len(matches) >= LIMIT:
        break

    offset += page_size
    if has_more is False:
        break
    if total is not None and offset >= total:
        break
    # Safety stop: at most 50 pages (5000 msgs)
    if pages >= 50:
        print(f"# stopped at 50 pages (offset={offset})", file=sys.stderr)
        break

# Append new rows to history.jsonl (oldest first for readability)
if new_rows:
    new_rows.sort(key=lambda r: r.get("ts") or "")
    with open(HIST_FILE, "a", encoding="utf-8") as f:
        for r in new_rows:
            f.write(json.dumps(r, ensure_ascii=False) + "\n")
    print(
        f"# merged {len(new_rows)} new msg(s) into {HIST_FILE}",
        file=sys.stderr,
    )

# Emit matches sorted oldest-first
matches.sort(key=lambda r: r.get("ts") or "")
for m in matches:
    sys.stdout.write(json.dumps(m, ensure_ascii=False) + "\n")

print(
    f"# {len(matches)} match(es) across {pages} page(s), {offset + len(msgs)} msgs scanned",
    file=sys.stderr,
)
PY
