import json
import csv
from datetime import date
from pathlib import Path

# --- Paths ---
base = Path(__file__).resolve().parent.parent
logs_dir = base / "logs"
output_file = base / "output" / "All_Contacts.csv"

# --- Today's log file ---
today = date.today().strftime("%Y_%m_%d")
log_file = logs_dir / f"webhook_log_{today}.json"

rows = []

def extract_contact(data):
    # Support both formats
    contact = data.get("payload", {}).get("contact", {}) or data.get("contact", {})
    fields = contact.get("fields", {})

    return {
        "id": contact.get("id", ""),
        "email": contact.get("email", "").strip().lower(),
        "first_name": contact.get("first_name", ""),
        "last_name": contact.get("last_name", ""),
        "individual_id": fields.get("individual_id"),
        "members_type": fields.get("members_type", ""),
        "do_id": fields.get("do_id"),
        "lead_status": fields.get("lead_status", ""),
        "new_member_date_2": fields.get("new_member_date_2", ""),
        "paid_thru": fields.get("paid_thru", ""),
        "company_paid_thru": fields.get("company_paid_thru", ""),
        "organization_id": fields.get("organization_id")
    }

# --- Read log ---
if log_file.exists():
    with open(log_file, "r", encoding="utf-8") as f:
        for line in f:
            line = line.strip()
            if not line:
                continue
            try:
                data = json.loads(line)
                row = extract_contact(data)
                rows.append(row)
            except json.JSONDecodeError as e:
                print("JSON decode error:", e)
            except Exception as e:
                print("Error:", e)

# --- Deduplicate by Email ---
unique = {}
for r in rows:
    email = r["email"]
    if not email:
        continue
    # keep the latest occurrence (overwrite older)
    unique[email] = r

# Convert dict back to list
deduped_rows = list(unique.values())

# --- Write CSV ---
output_file.parent.mkdir(parents=True, exist_ok=True)
with open(output_file, "w", newline="", encoding="utf-8-sig") as f:
    writer = csv.DictWriter(f, fieldnames=[
        "ID", "Email", "First Name", "Last Name",
        "*INDIVIDUAL ID", "*Members Type", "*DO ID",
        "*Lead Status", "*New member date (date field type)",
        "*Paid Thru", "*Company Paid Thru", "*Organization ID"
    ])
    writer.writeheader()
    for r in deduped_rows:
        writer.writerow({
            "ID": r["id"],
            "Email": r["email"],
            "First Name": r["first_name"],
            "Last Name": r["last_name"],
            "*INDIVIDUAL ID": r["individual_id"],
            "*Members Type": r["members_type"],
            "*DO ID": r["do_id"],
            "*Lead Status": r["lead_status"],
            "*New member date (date field type)": r["new_member_date_2"],
            "*Paid Thru": r["paid_thru"],
            "*Company Paid Thru": r["company_paid_thru"],
            "*Organization ID": r["organization_id"]
        })

print(f"✅ Created {output_file} with {len(deduped_rows)} unique contacts (removed {len(rows) - len(deduped_rows)} duplicates) from {log_file.name}")