#!/usr/bin/env bash
# Search Hey tracker for all emails from a specific person
# Usage: contact-lookup <name or email>
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
VAULT_DIR="$(cd "$SCRIPT_DIR/../.." && pwd)"
TRACKER="$VAULT_DIR/Meta/hey-tracker.jsonl"

if [[ ! -f "$TRACKER" ]]; then
    echo "Tracker not found at $TRACKER" >&2
    exit 1
fi

if [[ -z "${1:-}" ]]; then
    echo "Usage: contact-lookup <name or email>" >&2
    exit 1
fi

python3 - "$TRACKER" "$1" <<'PYEOF'
import json, sys

tracker_path, query = sys.argv[1], sys.argv[2].lower()
results = []
with open(tracker_path) as f:
    for line in f:
        try:
            e = json.loads(line)
        except Exception:
            continue
        if query in e.get("sender_name", "").lower() or query in e.get("sender_email", "").lower():
            results.append(e)

# Dedupe by topic_id, keep latest (skip entries with no topic_id)
seen = {}
for e in results:
    tid = e.get("topic_id")
    if not tid:
        continue
    seen[tid] = e
results = sorted(seen.values(), key=lambda x: x.get("active_at", ""), reverse=True)

if not results:
    print(f'No emails found from "{query}"')
else:
    emails = set(e.get("sender_email", "") for e in results if e.get("sender_email"))
    names = set(e.get("sender_name", "") for e in results if e.get("sender_name"))
    print(f'Found {len(results)} threads from: {", ".join(names)}')
    print(f'Email(s): {", ".join(emails)}')
    print()
    for e in results[:20]:
        date = e.get("active_at", "")[:10]
        mb = e.get("mailbox", "")
        subj = e.get("subject", "")[:60]
        tid = e.get("topic_id", "")
        print(f"{date} | {mb:8} | {subj} | {tid}")
    if len(results) > 20:
        print(f"  ... and {len(results) - 20} more")
PYEOF
