
import csv, json
from pathlib import Path

BASE_DIR = Path("/var/www/vps2.happyuser.info/AIMA_bot")

# Для кожного батчу: скільки contacts_csv рядків є, скільки в log мають gate1_message_sent=yes
results = []
for batch in ["v6","v7","v8","v9","v10","v11"]:
    contacts_csv = BASE_DIR / f"data/processed/aima_far_{batch}_contacts.csv"
    log_csv      = BASE_DIR / f"data/processed/aima_far_{batch}_telegram_log.csv"
    if not contacts_csv.exists():
        continue

    with contacts_csv.open(encoding="utf-8-sig") as f:
        contacts = list(csv.DictReader(f))
    total = len(contacts)

    sent_ids = set()
    if log_csv.exists():
        with log_csv.open(encoding="utf-8-sig") as f:
            for row in csv.DictReader(f):
                if row.get("gate1_message_sent","").lower() == "yes":
                    sent_ids.add(str(row.get("pilot_id","")))

    not_sent = [c for c in contacts if str(c["pilot_id"]) not in sent_ids]
    results.append({
        "batch": batch,
        "total": total,
        "sent": len(sent_ids),
        "not_sent": len(not_sent),
        "not_sent_phones": [c["phone"] for c in not_sent]
    })

print("=== Аудит батчів ===")
total_not_sent = 0
for r in results:
    print(f"{r['batch']}: total={r['total']} sent={r['sent']} not_sent={r['not_sent']}")
    total_not_sent += r["not_sent"]
print(f"\nВсього НЕ відправлених: {total_not_sent}")

# Зберегти список not_sent phones для перевірки
all_not_sent = []
for r in results:
    if r["batch"] not in ("v11",):  # v11 щойно запустили
        # Знайти повний рядок з contacts
        contacts_csv = BASE_DIR / f"data/processed/aima_far_{r['batch']}_contacts.csv"
        with contacts_csv.open(encoding="utf-8-sig") as f:
            for row in csv.DictReader(f):
                log_csv = BASE_DIR / f"data/processed/aima_far_{r['batch']}_telegram_log.csv"
                sent_ids = set()
                if log_csv.exists():
                    with log_csv.open(encoding="utf-8-sig") as f2:
                        for lr in csv.DictReader(f2):
                            if lr.get("gate1_message_sent","").lower() == "yes":
                                sent_ids.add(str(lr["pilot_id"]))
                if str(row["pilot_id"]) not in sent_ids:
                    all_not_sent.append({
                        "batch": r["batch"],
                        "pilot_id": row["pilot_id"],
                        "phone": row["phone"],
                        "variant": row["variant"],
                        "gate1_text": row["gate1_text"],
                        "first_name": row.get("first_name",""),
                        "last_name": row.get("last_name",""),
                    })

# Зберегти
out_path = BASE_DIR / "data/processed/aima_not_sent_candidates.json"
import json
out_path.write_text(json.dumps(all_not_sent, ensure_ascii=False, indent=2), encoding="utf-8")
print(f"\nЗбережено {len(all_not_sent)} not_sent контактів -> {out_path.name}")
print("Перші 10:")
for r in all_not_sent[:10]:
    print(f"  [{r['batch']}] pilot={r['pilot_id']} phone={r['phone']} {r['first_name']}")
