
import csv, json, sys
from pathlib import Path

BASE_DIR = Path("/var/www/vps2.happyuser.info/AIMA_bot")
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",
]

reachable = json.loads(
    (BASE_DIR/"data/processed/aima_reachable_from_not_sent.json").read_text(encoding="utf-8")
)
print(f"[build] {len(reachable)} реально досяжних not_sent контактів")

# Знайти оригінальні рядки з contacts CSV (для gate1_text та інших полів)
contacts_lookup = {}
for batch in ["v6","v7","v8","v9","v10"]:
    p = BASE_DIR / f"data/processed/aima_far_{batch}_contacts.csv"
    if not p.exists(): continue
    with p.open(encoding="utf-8-sig") as f:
        for row in csv.DictReader(f):
            contacts_lookup[str(row["pilot_id"])] = row

# pilot_id_start
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}")

batch_key = "v12"
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(reachable):
    pid = pilot_id_start + i
    orig = contacts_lookup.get(str(r["pilot_id"]), {})
    gate1 = orig.get("gate1_text","") or r.get("gate1_text","")
    variant = orig.get("variant","") or r.get("variant","")
    dataset = orig.get("dataset","users_without_shop_202605191510")
    contact_rows.append({
        "pilot_id": pid,
        "pilot_name": f"retry_{r['batch']}_{r['pilot_id']}",
        "lead_id": orig.get("lead_id",""),
        "dataset": dataset,
        "segment": "opened_no_store",
        "variant": variant,
        "hypothesis": variant,
        "gate1_text": gate1,
        "phone": r["phone"],
        "first_name": r.get("first_name",""),
        "last_name": r.get("last_name",""),
        "registered_at": orig.get("registered_at",""),
        "last_activity_at": orig.get("last_activity_at",""),
        "worker-do-not-send": "",
        "pre_contact_quality": "confirmed_tg",
        "contamination_status": "unknown",
        "notes": f"retry from {r['batch']} pilot={r['pilot_id']}",
    })
    log_row = {k: "" for k in LOG_FIELDS}
    log_row.update({"pilot_id": pid, "lead_id": orig.get("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}: {len(contact_rows)} рядків")

# Send script (v9 base + fix)
v9_path = BASE_DIR / "src/aima_batch_send_v9.py"
v12_path = BASE_DIR / f"src/aima_batch_send_{batch_key}.py"
src = v9_path.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")
v12_path.write_text(src, encoding="utf-8")
print(f"[build] {v12_path.name} створено")

# Bot state
state = json.loads((BASE_DIR/"data/processed/aima_bot_state.json").read_text(encoding="utf-8"))
state["proposals"][batch_key] = {
    "name": f"FAR {batch_key} - {len(contact_rows)} retry not-sent (confirmed TG)",
    "contacts_csv": str(contacts_csv),
    "log_csv": str(log_csv),
    "send_script": str(v12_path),
    "gdrive_url": "",
}
state["active_batch"] = batch_key
state["pending"]      = batch_key
(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}")
print(f"[ok] v12 готово: {len(contact_rows)} контактів")
for r in contact_rows:
    print(f"  {r['phone']} {r['first_name']} [{r['notes']}] -> {r['variant'][:30]}")
