import re, time
visitor = re.sub(r"[^a-z0-9-]", "", str(input.get("visitor") or "").lower())[:40] or "anon"
values = input.get("values") if isinstance(input.get("values"), dict) else {}
clean = {}
for k, v in list(values.items())[:8]:
    key = re.sub(r"[^a-z0-9_]", "", str(k).lower())[:24]
    if key:
        clean[key] = str(v).strip()[:120]
noun = re.sub(r"[^a-z]", "", str(input.get("noun") or "").lower())[:16] or "record"
now = int(time.time() * 1000)
first = next(iter(clean.values()), "") or noun
output = {"rows": [{
    "code": "r-" + visitor + "-" + str(now % 100000000),
    "label": first[:60],
    "ordinal": int(now / 1000),
    "meta": {"visitor": visitor, "noun": noun, "values": clean, "created": int(now / 1000)},
}]}
