import json, hashlib, datetime

SRC_FILES = ["/tmp/tx_batch_00.json", "/tmp/tx_batch_01.json"]

records = []
for f in SRC_FILES:
    records.extend(json.load(open(f)))

# Dedupe by (out_name, address) - many businesses have both a CIG/TOB and E-CIG permit record
seen = {}
for r in records:
    key = (r["out_name"], r["address"], r["city"])
    if key not in seen:
        seen[key] = {"permit_types": set(), "rec": r}
    seen[key]["permit_types"].add(r["permit_type"])

now = datetime.datetime(2026, 6, 30, 0, 0, 0).isoformat() + "Z"

leads = []
for (out_name, address, city), v in seen.items():
    r = v["rec"]
    permit_types = sorted(v["permit_types"])
    uid = hashlib.md5(f"{out_name}|{address}|{city}|TX".encode()).hexdigest()[:24]
    subtype = "Smoke Shop / Tobacco Retailer"
    if permit_types == ["E-CIG RETAILER"]:
        subtype = "Vape / E-Cig Retailer"
    leads.append({
        "id": uid,
        "category": "smokeshop",
        "business_name": r["out_name"],
        "contact_name": None,
        "contact_title": None,
        "email": None,
        "phone": None,
        "address": r["address"],
        "city": r["city"],
        "state": r["state"],
        "zip": r["zip"],
        "county": r["county"],
        "subtype": subtype,
        "legal_entity_name": r["name"],
        "permit_types": permit_types,
        "revenue_range": None,
        "revenue_est": None,
        "employee_range": None,
        "credit_cost": 1,
        "verified": False,
        "dnc_clean": None,
        "quality_score": None,
        "data_source": "Texas Comptroller - Active Cigarette/Tobacco Retailers (data.texas.gov, dataset n4rp-ar9b)",
        "created_at": now,
        "needs_enrichment": True
    })

out_path = "/home/user/makeleads/data/sources/tx_tobacco_leads_formatted.json"
json.dump(leads, open(out_path, "w"), indent=2)
print(f"Raw records pulled: {len(records)}")
print(f"Unique businesses after de-dup: {len(leads)}")
print(f"Written to: {out_path}")
