#!/usr/bin/env python3
import json, time, hashlib, pathlib, subprocess

INBOX = pathlib.Path("/var/lib/chronological/runtime/nova/inbox.jsonl")
SEEN  = pathlib.Path("/var/lib/chronological/runtime/nova/seen.json")

seen = set()
if SEEN.exists():
    try:
        seen = set(json.loads(SEEN.read_text()))
    except:
        pass

def send_to_telegram(text):
    cmd = [
        "curl","-sS","-X","POST",
        f"https://api.telegram.org/bot{BOT}/sendMessage",
        "-H","Content-Type: application/json",
        "-d",json.dumps({
            "chat_id": CHAT,
            "text": text
        })
    ]
    subprocess.run(cmd, timeout=5)

BOT  = subprocess.getoutput("printenv TELEGRAM_BOT_TOKEN")
CHAT = "7509024401"

for line in INBOX.read_text().splitlines():
    h = hashlib.sha256(line.encode()).hexdigest()
    if h in seen:
        continue

    e = json.loads(line)
    header = f"🛰️ {e['general']} L{e['lane']} SIGNAL_REPORT"
    send_to_telegram(header)

    seen.add(h)

SEEN.write_text(json.dumps(list(seen)))
print("NOVA_EMIT_OK")
