#!/usr/bin/env bash
# Show recent Hey tracker entries (last N hours, default 24)
# Usage: tracker-recent [hours] [--mailbox imbox] [--json]
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

HOURS=24
MAILBOX=""
JSON=false

while [[ $# -gt 0 ]]; do
    case "$1" in
        --mailbox)
            if [[ $# -lt 2 || "$2" == --* ]]; then
                echo "Usage: tracker-recent [hours] [--mailbox imbox] [--json]" >&2
                exit 1
            fi
            MAILBOX="$2"; shift 2 ;;
        --json) JSON=true; shift ;;
        [0-9]*)
            if ! [[ "$1" =~ ^[0-9]+$ ]]; then
                echo "Error: hours must be a positive integer" >&2
                exit 1
            fi
            HOURS="$1"; shift ;;
        *) shift ;;
    esac
done

AS_JSON=$([[ "$JSON" == "true" ]] && echo "1" || echo "0")

python3 - "$TRACKER" "$HOURS" "$MAILBOX" "$AS_JSON" <<'PYEOF'
import json, sys
from datetime import datetime, timedelta, timezone

tracker_path, hours, mailbox_filter, as_json_flag = sys.argv[1], int(sys.argv[2]), sys.argv[3], sys.argv[4] == "1"
cutoff = (datetime.now(timezone.utc) - timedelta(hours=hours)).strftime("%Y-%m-%dT%H:%M:%S")
results = []
with open(tracker_path) as f:
    for line in f:
        try:
            e = json.loads(line)
        except Exception:
            continue
        if e.get("active_at", "")[:19] < cutoff:
            continue
        if mailbox_filter and e.get("mailbox") != mailbox_filter:
            continue
        results.append(e)

if as_json_flag:
    print(json.dumps(results, indent=2))
else:
    for e in results:
        date = e.get("active_at", "")[:16]
        mb = e.get("mailbox", "")
        sender = e.get("sender_name", "")[:30]
        subj = e.get("subject", "")[:60]
        tid = e.get("topic_id", "")
        print(f"{date} | {mb:8} | {sender:30} | {subj} | {tid}")
PYEOF
