#!/home/agent/venv/bin/python3
"""Wave C — launch-day blast (24.06.2026). Sends a short "it's live" follow-up to
all press targets already contacted/replied. Triggered by cron on launch morning.

Launch URL read from data/launch-url.txt (put the live Kickstarter link there before
launch); defaults to https://helloplappi.com. Safe to run --dry.
"""
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parent))
from marketing_lib import send_email, wa_send, insert_item, set_status, db, log

LAUNCH_URL_FILE = Path("/home/agent/plappi-marketing/data/launch-url.txt")


def launch_url() -> str:
    if LAUNCH_URL_FILE.exists():
        u = LAUNCH_URL_FILE.read_text().strip()
        if u:
            return u
    return "https://helloplappi.com"


def run(dry: bool = False):
    url = launch_url()
    conn = db()
    rows = conn.execute(
        "SELECT * FROM targets WHERE channel='press' AND status IN ('contacted','replied')"
    ).fetchall()
    conn.close()
    subj = "Plappi ist jetzt LIVE auf Kickstarter 🚀"
    sent = forms = 0
    for t in rows:
        c = t["contact"] or ""
        body = (
            f"Hallo,\n\nkurzes Update: Plappi ist seit heute live auf Kickstarter — {url}\n\n"
            f"Der screen-freie, datenschutz-erste Sprachbegleiter für Kinder (2–15 J., 27 Sprachen) "
            f"aus der Wiener BReact GmbH. Über Berichterstattung heute zum Start freuen wir uns sehr.\n\n"
            f"Demo, Pressebilder, O-Töne und ein Interview gern auf Zuruf.\n\n"
            f"Kontakt:\nNemanja Klincov — Gründer & CEO, Plappi (BReact GmbH)\n"
            f"nemanja@breact.ai · https://helloplappi.com"
        )
        if "@" not in c:
            forms += 1
            continue
        if dry:
            sent += 1
            continue
        if send_email(c, subj, body):
            iid = insert_item(t["id"], "press", "press_followup", t["platform"], body,
                              subject=subj, thread_ref="launch")
            set_status(iid, "sent")
            sent += 1
    msg = (f"🚀 Wave-C Launch-Blast: {sent} Mails an warme Kontakte raus "
           f"({forms} Formulare übersprungen). Link: {url}")
    if not dry:
        wa_send(msg)
    log(msg, "wave-c")
    print(msg)


if __name__ == "__main__":
    run(dry="--dry" in sys.argv)
