import re, time
name = str(input.get("name") or "").strip()[:80]
email = str(input.get("email") or "").strip()[:120]
note = str(input.get("note") or "").strip()[:2000]
biz = str(input.get("business") or "").strip()[:80]
visitor = re.sub(r"[^a-z0-9-]", "", str(input.get("visitor") or "").lower())[:40] or "anon"
kind = str(input.get("kind") or "operator").strip()[:16]
if not note:
    raise ValueError("note is required")
if email and not re.match(r"^[^@\s]+@[^@\s]+\.[^@\s]+$", email):
    raise ValueError("email looks wrong")
now = int(time.time())
output = {
    "code": "lead-" + visitor + "-" + str(now % 100000000),
    "label": ((name or "someone") + (" — " + email if email else ""))[:120],
    "meta": {"name": name, "email": email, "note": note, "business": biz,
             "visitor": visitor, "kind": kind, "created": now},
}
