
import csv, json, sqlite3, sys
from pathlib import Path

BASE_DIR = Path("/var/www/vps2.happyuser.info/AIMA_bot")
DATASET  = "users_without_shop_202605191510"
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 кандидатів
screened_path = BASE_DIR / "data/processed/aima_screened_v11_candidates.json"
if not screened_path.exists():
    print("[error] screened file not found"); sys.exit(1)

screened = json.loads(screened_path.read_text(encoding="utf-8"))
# Дедуплікація по phone
seen_phones = set()
unique_screened = []
for r in screened:
    if r["phone"] not in seen_phones:
        seen_phones.add(r["phone"])
        unique_screened.append(r)

n = len(unique_screened)
print(f"[build] Унікальних кандидатів: {n}")
if n == 0:
    print("[error] 0 кандидатів"); sys.exit(1)

# Взяти variant тексти з v9
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()
        t = row.get("gate1_text","").strip()
        if v and t and v not in variant_texts:
            variant_texts[v] = t
print(f"[build] Варіанти: {list(variant_texts.keys())}")

# 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

# Побудова v11
batch_key = "v11"
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(unique_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": "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": 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"FAR screened {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} рядків")

# Send script (копія v9 з заміною)
v9_script = BASE_DIR / "src/aima_batch_send_v9.py"
v11_script = BASE_DIR / f"src/aima_batch_send_{batch_key}.py"
src = v9_script.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")
v11_script.write_text(src, encoding="utf-8")
print(f"[build] {v11_script.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} - {n} screened (confirmed TG)",
    "contacts_csv": str(contacts_csv),
    "log_csv": str(log_csv),
    "send_script": str(v11_script),
    "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] v11 готово: {n} контактів")
for r in contact_rows:
    print(f"  {r['phone']} {r['first_name']} {r['last_name']} -> {r['variant']}")
