
import csv, json, sys
from pathlib import Path

BASE_DIR   = Path("/var/www/vps2.happyuser.info/AIMA_bot")
DATASET    = "lost_shops_202605191501"
BATCH_KEY  = "v13"

CONTACTS_FIELDS = [
    "pilot_id","pilot_name","lead_id","dataset","segment","variant",
    "hypothesis","gate1_text","phone","first_name","last_name",
    "registered_at","last_activity_at","worker-do-not-send",
    "pre_contact_quality","contamination_status","notes",
]
LOG_FIELDS = [
    "pilot_id","lead_id","dataset","hypothesis","phone",
    "worker-do-not-send","added_to_telegram","telegram_contact_name",
    "gate1_message_sent","replied","reply_text","next_gate","notes",
    "sent_at_utc","recipient_id","message_len","message_preview",
    "chat_started","replied_at_utc","reply_category",
    "pre_contact_quality","contamination_status",
    "qualified_product_reply","next_step_accepted","negative_stop","reply_class",
]

screened = json.loads((BASE_DIR/"data/processed/aima_screened_v13_candidates.json").read_text(encoding="utf-8"))
n = len(screened)
print(f"[build] {n} screened кандидатів з lost_shops")

# Для lost_shops потрібна інша гіпотеза — люди що вже МАЛИ магазин
# Використовуємо v9 тексти як тимчасові (TODO: замінити на re-engagement тексти)
variant_texts = {}
with open(BASE_DIR/"data/processed/aima_far_v9_contacts.csv", encoding="utf-8-sig") as f:
    for row in csv.DictReader(f):
        v = row.get("variant","").strip(); t2 = row.get("gate1_text","").strip()
        if v and t2 and v not in variant_texts: variant_texts[v] = t2

max_id = 120
for p in (BASE_DIR/"data/processed").glob("aima_far_v*_contacts.csv"):
    with p.open(encoding="utf-8-sig") as f:
        for row in csv.DictReader(f):
            try: max_id = max(max_id, int(row.get("pilot_id",0) or 0))
            except: pass
pilot_id_start = max_id + 1
print(f"[build] pilot_id_start={pilot_id_start}")

v_keys = list(variant_texts.keys())
half = n // 2
contacts_csv = BASE_DIR/f"data/processed/aima_far_{BATCH_KEY}_contacts.csv"
log_csv      = BASE_DIR/f"data/processed/aima_far_{BATCH_KEY}_telegram_log.csv"

contact_rows, log_rows = [], []
for i, r in enumerate(screened):
    pid     = pilot_id_start + i
    variant = v_keys[0] if i < half else v_keys[min(1,len(v_keys)-1)]
    gate1   = variant_texts[variant]
    contact_rows.append({"pilot_id":pid,"pilot_name":f"manual_far_{BATCH_KEY}",
        "lead_id":r["lead_id"],"dataset":DATASET,"segment":"lost_shop",
        "variant":variant,"hypothesis":variant,"gate1_text":gate1,
        "phone":r["phone"],"first_name":r.get("first_name",""),
        "last_name":r.get("last_name",""),"registered_at":r.get("registered_at",""),
        "last_activity_at":r.get("last_activity_at",""),"worker-do-not-send":"",
        "pre_contact_quality":"confirmed_tg","contamination_status":"unknown",
        "notes":f"lost_shop batch {BATCH_KEY}"})
    log_row = {k:"" for k in LOG_FIELDS}
    log_row.update({"pilot_id":pid,"lead_id":r["lead_id"],"dataset":DATASET,"hypothesis":variant,
                    "phone":r["phone"],"pre_contact_quality":"confirmed_tg","contamination_status":"unknown"})
    log_rows.append(log_row)

with contacts_csv.open("w",encoding="utf-8",newline="") as f:
    w=csv.DictWriter(f,fieldnames=CONTACTS_FIELDS); w.writeheader(); w.writerows(contact_rows)
with log_csv.open("w",encoding="utf-8",newline="") as f:
    w=csv.DictWriter(f,fieldnames=LOG_FIELDS); w.writeheader(); w.writerows(log_rows)
print(f"[build] {contacts_csv.name}: {n} рядків")

src = (BASE_DIR/f"src/aima_batch_send_v9.py").read_text(encoding="utf-8")
src = src.replace("aima_far_v9_contacts.csv",f"aima_far_{BATCH_KEY}_contacts.csv")
src = src.replace("aima_far_v9_telegram_log.csv",f"aima_far_{BATCH_KEY}_telegram_log.csv")
src = src.replace("batch_send_v9",f"batch_send_{BATCH_KEY}")
src = src.replace("AIMA FAR v9 pilot",f"AIMA FAR {BATCH_KEY} pilot")
(BASE_DIR/f"src/aima_batch_send_{BATCH_KEY}.py").write_text(src,encoding="utf-8")
print(f"[build] aima_batch_send_{BATCH_KEY}.py створено")

state = json.loads((BASE_DIR/"data/processed/aima_bot_state.json").read_text(encoding="utf-8"))
state["proposals"][BATCH_KEY] = {
    "name":f"LOST_SHOPS {BATCH_KEY} - {n} (confirmed TG)",
    "contacts_csv":str(contacts_csv),
    "log_csv":str(log_csv),
    "send_script":str(BASE_DIR/f"src/aima_batch_send_{BATCH_KEY}.py"),
    "gdrive_url":"",
}
state["active_batch"] = BATCH_KEY
state["pending"]      = BATCH_KEY
state["daily"]["status"] = "approved"
state["daily"]["send_triggered"] = False
state["daily"]["sent_count"] = None
state["daily"]["sent_at"] = None
(BASE_DIR/"data/processed/aima_bot_state.json").write_text(
    json.dumps(state,ensure_ascii=False,indent=2),encoding="utf-8")
print(f"[build] bot_state: active_batch={BATCH_KEY}, daily.status=approved")
print(f"[ok] v13 ready: {n} contacts from lost_shops")
