#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Process and classify CRM contact lists for ONLINE TRADER CRM import."""

from __future__ import annotations

import csv
import json
import os
import re
import shutil
import sys
import time
import urllib.parse
import urllib.request
from collections import Counter, defaultdict
from datetime import datetime, timezone
from pathlib import Path
from typing import Any

# ASCII-safe stdout
if hasattr(sys.stdout, "reconfigure"):
    try:
        sys.stdout.reconfigure(encoding="utf-8", errors="replace")
    except Exception:
        pass

TELEGRAM_DIR = Path(r"C:\Users\Thunderobot\Downloads\Telegram Desktop")
CRM_ROOT = Path(r"C:\Users\Thunderobot\Downloads\CRM")
OUT_DIR = CRM_ROOT / "scripts" / "batch_import"
IMPORTS_DIR = CRM_ROOT / "imports"
CACHE_PATH = CRM_ROOT / "scripts" / "translate_cache_fr.json"

CSV_FILES = [
    "9ReadyFTDS.csv",
    "10france2026.csv",
    "13 fr.csv",
    "20live.csv",
    "27HOT France ALL.csv",
    "70 Warm FR Recovery all.csv",
    "70 WarmFR.csv",
    "70LeadsWarm.csv",
    "90 Forex data.csv",
    "ColdFR.csv",
    "FR FRESH 5.csv",
    "FR NEW.csv",
    "HOTlive.csv",
    "ReadyFTD France.csv",
]

AIRTABLE_URLS = [
    "https://airtable.com/appXrat9RF10N1Uts/shraqf3QIolzGWxnP/tblZ15pvPnWytdvNB",
    "https://airtable.com/appghts5VSzwx6Ma2/shrUpR46idUigKBpK/tblxo3GuSRrVV2kVF",
    "https://airtable.com/appsVNNIhei9ZPx11/shr7r7qpM2b5nVxeX/tblC9MHPaptUENH2J",
    "https://airtable.com/appqTzAhzqLOF9nPt/shrKw01K3BmwkJPN6/tblufAaCfZINvcmV0",
    "https://airtable.com/appG4KnYQvxX05Xw9/shrIGmTaOMYtN4JeJ/tblgibmv2cAHV3K4X",
]

PRIORITY_RANK = {"haute": 3, "moyenne": 2, "basse": 1}
PRIORITY_ORDER = ["basse", "moyenne", "haute"]

FIELD_ALIASES: dict[str, list[str]] = {
    "phone": ["Phone", "phone"],
    "country": ["Country", "country"],
    "first_name": ["First Name", "first_name"],
    "last_name": ["Last Name", "last_name"],
    "name_full": ["Name (Full)", "Name", "name"],
    "status_temp": ["Temp (Lead Status)", "Lead Status", "lead_status"],
    "industry": ["Industry", "industry"],
    "lead_date": ["Date", "date"],
    "email": ["Email", "email"],
    "broker": ["Broker", "broker", "Company"],
    "deposit": ["Deposit", "Deposit Amount", "deposit"],
    "ftd_stage": ["FTD Stage", "FTD", "ftd_stage"],
    "urgency": ["Urgency", "urgency"],
    "budget": ["Budget", "budget"],
    "language": ["Language", "language"],
    "pain_point": ["Pain Point", "pain_point"],
    "objections": ["Objections", "objections"],
    "summary": ["Summary", "summary"],
    "next_step": ["Next Step", "Next step", "next_step"],
    "amount_lost": ["Lost Amount", "Amount Lost", "amount_lost"],
    "scam_type": ["Scam Type", "scam_type"],
    "case_status": ["Case Status", "case_status"],
    "bank": ["Bank Name", "Bank", "bank"],
    "payment": ["Payment", "Payment Method", "payment"],
    "security_code": ["Security Code", "security_code"],
}

TRANSLATE_KEYS = ("summary", "pain_point", "objections", "next_step")


def ascii_print(msg: str) -> None:
    safe = msg.encode("ascii", errors="replace").decode("ascii")
    print(safe)


def normalize_phone(raw: str) -> str:
    if not raw:
        return ""
    digits = re.sub(r"\D", "", str(raw).strip())
    if not digits:
        return ""
    if len(digits) == 10 and digits.startswith("0"):
        digits = "33" + digits[1:]
    elif len(digits) == 9 and digits[0] in "67":
        digits = "33" + digits
    return digits


def pick(row: dict[str, str], key: str) -> str:
    for alias in FIELD_ALIASES.get(key, [key]):
        if alias in row and row[alias] is not None:
            v = str(row[alias]).strip()
            if v:
                return v
    return ""


def classify_type(industry: str, source_file: str) -> str:
    ind = (industry or "").strip().lower()
    if any(x in ind for x in ("recovery", "récupération", "recuperation", "recup")):
        return "recovery"
    if "forex" in ind or "ftd" in ind:
        return "forex"
    fn = source_file.lower()
    if any(x in fn for x in ("recovery", "recup")):
        return "recovery"
    if "forex" in fn or "ftd" in fn:
        return "forex"
    return "other"


def temp_to_priority(status: str) -> str | None:
    s = (status or "").strip().lower()
    if s == "hot":
        return "haute"
    if s == "warm":
        return "moyenne"
    if s == "cold":
        return "basse"
    return None


def filename_priority_hint(filename: str) -> str | None:
    fn = filename.lower()
    if any(x in fn for x in ("hot", "readyftd", "live", "ready")):
        return "haute"
    if "warm" in fn:
        return "moyenne"
    if any(x in fn for x in ("cold", "fresh", "new")):
        return "basse"
    return None


def elevate_priority(p: str) -> str:
    if p == "basse":
        return "moyenne"
    if p == "moyenne":
        return "haute"
    return p


def max_priority(a: str, b: str) -> str:
    return a if PRIORITY_RANK[a] >= PRIORITY_RANK[b] else b


def compute_priority(
    status_temp: str,
    source_file: str,
    urgency: str,
    ftd_stage: str,
) -> str:
    p = temp_to_priority(status_temp)
    if not p:
        p = filename_priority_hint(source_file) or "moyenne"

    urg = (urgency or "").lower()
    if any(x in urg for x in ("high", "immediate", "immediat", "urgent")):
        p = max_priority(p, "haute")

    ftd = (ftd_stage or "").lower().replace(" ", "_")
    if ftd in ("ready_ftd", "pre_ftd", "yes") or "ready" in ftd:
        p = elevate_priority(p)

    fn_hint = filename_priority_hint(source_file)
    if fn_hint and not temp_to_priority(status_temp):
        p = max_priority(p, fn_hint)

    return p


def read_csv_rows(path: Path) -> tuple[list[str], list[dict[str, str]]]:
    for enc in ("utf-8-sig", "utf-8", "cp1252", "latin-1"):
        try:
            with path.open("r", encoding=enc, newline="") as f:
                reader = csv.DictReader(f)
                if not reader.fieldnames:
                    return [], []
                rows = []
                for row in reader:
                    cleaned = {k: (v.strip() if isinstance(v, str) else (v or "")) for k, v in row.items() if k}
                    rows.append(cleaned)
                return list(reader.fieldnames), rows
        except UnicodeDecodeError:
            continue
    raise RuntimeError(f"Cannot decode {path}")


def row_to_contact(row: dict[str, str], source_file: str) -> dict[str, Any] | None:
    phone_raw = pick(row, "phone")
    phone = normalize_phone(phone_raw)
    if not phone:
        return None

    first = pick(row, "first_name")
    last = pick(row, "last_name")
    name_full = pick(row, "name_full") or " ".join(x for x in (first, last) if x).strip()
    industry = pick(row, "industry")
    status_temp = pick(row, "status_temp")
    urgency = pick(row, "urgency")
    ftd_stage = pick(row, "ftd_stage")
    ctype = classify_type(industry, source_file)
    priority = compute_priority(status_temp, source_file, urgency, ftd_stage)
    source_tag = Path(source_file).stem

    tags = [source_tag, ctype, priority]
    if status_temp:
        tags.append(status_temp.lower())

    return {
        "name": name_full,
        "first_name": first,
        "last_name": last,
        "phone": phone,
        "phone_display": phone_raw or phone,
        "email": pick(row, "email"),
        "country": pick(row, "country") or "France",
        "industry": industry,
        "type": ctype,
        "priority": priority,
        "status_temp": status_temp,
        "lead_date": pick(row, "lead_date"),
        "broker": pick(row, "broker"),
        "deposit": pick(row, "deposit"),
        "ftd_stage": ftd_stage,
        "urgency": urgency,
        "budget": pick(row, "budget"),
        "language": pick(row, "language"),
        "pain_point": pick(row, "pain_point"),
        "objections": pick(row, "objections"),
        "summary": pick(row, "summary"),
        "next_step": pick(row, "next_step"),
        "amount_lost": pick(row, "amount_lost"),
        "scam_type": pick(row, "scam_type"),
        "case_status": pick(row, "case_status"),
        "bank": pick(row, "bank"),
        "payment": pick(row, "payment"),
        "security_code": pick(row, "security_code"),
        "source_file": source_file,
        "tags": sorted(set(t for t in tags if t)),
    }


def richness_score(c: dict[str, Any]) -> int:
    text_fields = (
        "summary",
        "pain_point",
        "objections",
        "next_step",
        "email",
        "broker",
        "amount_lost",
    )
    return sum(len(str(c.get(k) or "")) for k in text_fields)


def merge_contacts(existing: dict[str, Any], new: dict[str, Any]) -> dict[str, Any]:
    out = dict(existing)
    if PRIORITY_RANK[new["priority"]] > PRIORITY_RANK[existing["priority"]]:
        out["priority"] = new["priority"]
    for k in TRANSLATE_KEYS + (
        "email",
        "broker",
        "deposit",
        "ftd_stage",
        "urgency",
        "budget",
        "amount_lost",
        "scam_type",
        "case_status",
        "bank",
        "payment",
        "security_code",
        "status_temp",
        "lead_date",
        "name",
        "first_name",
        "last_name",
    ):
        if not out.get(k) and new.get(k):
            out[k] = new[k]
        elif k in ("summary",) and len(str(new.get(k) or "")) > len(str(out.get(k) or "")):
            out[k] = new[k]
    out["tags"] = sorted(set(out.get("tags", []) + new.get("tags", [])))
    if new["source_file"] not in str(out.get("source_file", "")):
        sf = out.get("source_file", "")
        if new["source_file"] not in sf:
            out["source_file"] = f"{sf}; {new['source_file']}" if sf else new["source_file"]
    if richness_score(new) > richness_score(existing) and new.get("summary"):
        if len(str(new.get("summary") or "")) >= len(str(out.get("summary") or "")):
            for k in TRANSLATE_KEYS:
                if new.get(k):
                    out[k] = new[k]
    return out


class Translator:
    def __init__(self, cache_path: Path) -> None:
        self.cache_path = cache_path
        self.cache: dict[str, str] = {}
        if cache_path.is_file():
            try:
                self.cache = json.loads(cache_path.read_text(encoding="utf-8"))
            except Exception:
                self.cache = {}
        self._dirty = False

    def looks_english(self, text: str) -> bool:
        if not text or len(text) < 8:
            return False
        lang_indicators_fr = re.search(
            r"\b(le|la|les|des|une|est|pour|avec|client|appel|demande)\b",
            text.lower(),
        )
        if lang_indicators_fr:
            return False
        ascii_ratio = sum(1 for c in text if ord(c) < 128) / max(len(text), 1)
        return ascii_ratio > 0.85

    def translate(self, text: str) -> str:
        if not text or not self.looks_english(text):
            return text
        key = text.strip()
        if key in self.cache:
            return self.cache[key]
        time.sleep(0.35)
        q = urllib.parse.quote(key[:4500])
        url = f"https://api.mymemory.translated.net/get?q={q}&langpair=en|fr"
        try:
            with urllib.request.urlopen(url, timeout=30) as resp:
                data = json.loads(resp.read().decode("utf-8"))
            translated = (
                data.get("responseData", {}).get("translatedText") or key
            ).strip()
            if translated.upper() == key.upper():
                translated = key
            self.cache[key] = translated
            self._dirty = True
            return translated
        except Exception:
            return text

    def save(self) -> None:
        if self._dirty:
            self.cache_path.parent.mkdir(parents=True, exist_ok=True)
            self.cache_path.write_text(
                json.dumps(self.cache, ensure_ascii=False, indent=2),
                encoding="utf-8",
            )


def field_needs_translation(c: dict[str, Any], field: str, tr: Translator) -> bool:
    val = str(c.get(field) or "").strip()
    if not val:
        return False
    lang = (c.get("language") or "").lower()
    return lang in ("english", "en") or tr.looks_english(val)


def pretranslate_contacts(contacts: list[dict[str, Any]], tr: Translator) -> None:
    unique: set[str] = set()
    for c in contacts:
        for field in TRANSLATE_KEYS:
            if field_needs_translation(c, field, tr):
                unique.add(str(c.get(field) or "").strip())
    ascii_print(f"Unique strings to translate: {len(unique)}")
    for i, text in enumerate(sorted(unique, key=len)):
        tr.translate(text)
        if (i + 1) % 20 == 0:
            ascii_print(f"  translated {i + 1}/{len(unique)}")
            tr.save()
    tr.save()


def build_notes(c: dict[str, Any], tr: Translator) -> str:
    def t(field: str) -> str:
        val = str(c.get(field) or "").strip()
        if not val:
            return ""
        if field_needs_translation(c, field, tr):
            return tr.cache.get(val, val)
        return val

    lines = [
        "=== FICHE CONTACT (import batch) ===",
        f"Source : {c.get('source_file', '')}",
        f"Priorite CRM : {c.get('priority', '')}",
        f"Type : {c.get('type', '')}",
        f"Statut temp. : {c.get('status_temp') or '—'}",
        "",
        "--- Identite ---",
        f"Nom : {c.get('name') or '—'}",
        f"Telephone : {c.get('phone_display') or c.get('phone')}",
        f"Email : {c.get('email') or '—'}",
        f"Pays : {c.get('country') or '—'}",
        f"Langue : {c.get('language') or '—'}",
        "",
        "--- Commercial ---",
        f"Industrie : {c.get('industry') or '—'}",
        f"Courtier : {c.get('broker') or '—'}",
        f"Depot : {c.get('deposit') or '—'}",
        f"Etape FTD : {c.get('ftd_stage') or '—'}",
        f"Urgence : {c.get('urgency') or '—'}",
        f"Budget : {c.get('budget') or '—'}",
        "",
        "--- Recovery ---",
        f"Montant perdu : {c.get('amount_lost') or '—'}",
        f"Type arnaque : {c.get('scam_type') or '—'}",
        f"Statut dossier : {c.get('case_status') or '—'}",
        f"Banque : {c.get('bank') or '—'}",
        f"Paiement : {c.get('payment') or '—'}",
        "",
        "--- Synthese ---",
        f"Point de douleur : {t('pain_point') or '—'}",
        f"Objections : {t('objections') or '—'}",
        f"Resume : {t('summary') or '—'}",
        f"Prochaine etape : {t('next_step') or '—'}",
        "",
        f"Date lead : {c.get('lead_date') or '—'}",
        f"Code securite : {c.get('security_code') or '—'}",
        "",
        "data_language=fr",
    ]
    return "\n".join(lines)


def sql_escape(s: str) -> str:
    return (s or "").replace("'", "''")


def generate_sql(contacts: list[dict[str, Any]]) -> str:
    """SQL compatible schéma aapvayrhhn_crm.clients (pas la table contacts)."""
    import secrets
    import time as _time

    def _uid() -> str:
        return secrets.token_hex(8) + format(int(_time.time()), "x")

    def _s(v: Any) -> str:
        if v is None or v == "":
            return "NULL"
        return "'" + sql_escape(str(v)) + "'"

    def _digits(p: str) -> str:
        return "".join(ch for ch in (p or "") if ch.isdigit())

    lines = [
        "-- Import batch → clients (schéma aapvayrhhn_crm)",
        f"-- Genere le {datetime.now(timezone.utc).strftime('%Y-%m-%d %H:%M UTC')}",
        f"-- {len(contacts)} contacts — dedupe telephone",
        "SET NAMES utf8mb4;",
        "",
    ]
    admin_id = "663acce29da39364"
    for c in contacts:
        phone_raw = c.get("phone") or ""
        phone_norm = _digits(phone_raw)
        if not phone_norm:
            continue
        phone_disp = phone_raw if str(phone_raw).startswith("+") else f"+{phone_norm}"
        industry = c.get("industry") or c.get("type") or ""
        ftd_stage = c.get("ftd_stage") or ""
        status = "hot" if (c.get("status_temp") or "").lower() == "hot" else "nouveau"
        tags_json = json.dumps(c.get("tags") or [], ensure_ascii=False)
        cols = (
            "id, name, email, phone, status, source, product, budget, priority, notes, "
            "first_name, last_name, lead_date, industry, country, language, broker, "
            "deposit_amount, urgency, pain_point, objections, summary, next_step, "
            "security_code, ftd_stage, ftd, amount_lost, scam_type, case_status, "
            "bank_name, payment_method, file_name, tags, data_language, created_by, created_at, updated_at"
        )
        vals = ", ".join([
            _s(_uid()),
            _s(c.get("name") or f"Lead {phone_disp}"),
            _s(c.get("email") or None),
            _s(phone_disp),
            _s(status),
            _s(c.get("source_file") or "batch_import"),
            _s(c.get("type") or industry or None),
            _s(c.get("budget") or None),
            _s(c.get("priority") or "moyenne"),
            _s(c.get("notes") or None),
            _s(c.get("first_name") or None),
            _s(c.get("last_name") or None),
            _s(c.get("lead_date") or None),
            _s(industry or None),
            _s(c.get("country") or "France"),
            _s(c.get("language") or None),
            _s(c.get("broker") or None),
            _s(c.get("deposit") or None),
            _s(c.get("urgency") or None),
            _s(c.get("pain_point") or None),
            _s(c.get("objections") or None),
            _s(c.get("summary") or None),
            _s(c.get("next_step") or None),
            _s(c.get("security_code") or None),
            _s(ftd_stage or None),
            "1" if ftd_stage else "0",
            _s(c.get("amount_lost") or None),
            _s(c.get("scam_type") or None),
            _s(c.get("case_status") or None),
            _s(c.get("bank") or None),
            _s(c.get("payment") or None),
            _s(c.get("source_file") or None),
            _s(tags_json),
            _s("fr"),
            _s(admin_id),
            "NOW()",
            "NOW()",
        ])
        lines.append(
            f"INSERT INTO clients ({cols})\nSELECT {vals}\nFROM DUAL\n"
            f"WHERE NOT EXISTS (\n"
            f"  SELECT 1 FROM clients\n"
            f"  WHERE REPLACE(REPLACE(REPLACE(IFNULL(phone,''), '+', ''), ' ', ''), '-', '') = '{sql_escape(phone_norm)}'\n"
            f"  LIMIT 1\n);\n"
        )
    return "\n".join(lines)


def fetch_airtable_status() -> list[dict[str, str]]:
    results = []
    for url in AIRTABLE_URLS:
        status = "unknown"
        detail = ""
        try:
            req = urllib.request.Request(
                url,
                headers={"User-Agent": "Mozilla/5.0 (CRM batch import)"},
            )
            with urllib.request.urlopen(req, timeout=25) as resp:
                html = resp.read().decode("utf-8", errors="replace")[:8000]
            low = html.lower()
            if "sign in" in low or "login" in low or "alert" in low or "auth" in low:
                status = "inaccessible"
                detail = "Mur de connexion Airtable (login requis)"
            elif "airtable" in low:
                status = "partial"
                detail = "Page Airtable chargee mais donnees non extraites sans API"
            else:
                status = "ok"
                detail = "Reponse HTTP recue"
        except Exception as e:
            status = "error"
            detail = str(e)[:200]
        results.append({"url": url, "status": status, "detail": detail})
    return results


def write_priority_csv(path: Path, contacts: list[dict[str, Any]], priority: str) -> None:
    subset = [c for c in contacts if c.get("priority") == priority]
    if not subset:
        return
    fields = [
        "name",
        "first_name",
        "last_name",
        "phone",
        "email",
        "country",
        "type",
        "priority",
        "status_temp",
        "industry",
        "broker",
        "source_file",
        "summary",
    ]
    with path.open("w", encoding="utf-8-sig", newline="") as f:
        w = csv.DictWriter(f, fieldnames=fields, extrasaction="ignore")
        w.writeheader()
        for c in subset:
            w.writerow({k: c.get(k, "") for k in fields})


def main() -> int:
    OUT_DIR.mkdir(parents=True, exist_ok=True)
    IMPORTS_DIR.mkdir(parents=True, exist_ok=True)

    inventory: dict[str, Any] = {"files": {}, "generated_at": datetime.now(timezone.utc).isoformat()}
    all_raw: list[dict[str, Any]] = []
    per_file_counts: Counter[str] = Counter()

    for fname in CSV_FILES:
        src = TELEGRAM_DIR / fname
        if not src.is_file():
            ascii_print(f"MISSING: {fname}")
            inventory["files"][fname] = {"error": "file not found", "rows": 0}
            continue
        dest = IMPORTS_DIR / fname
        try:
            shutil.copy2(src, dest)
        except Exception as e:
            ascii_print(f"COPY WARN {fname}: {e}")

        cols, rows = read_csv_rows(src)
        inventory["files"][fname] = {"rows": len(rows), "columns": cols}
        for row in rows:
            c = row_to_contact(row, fname)
            if c:
                all_raw.append(c)
                per_file_counts[fname] += 1

    by_phone: dict[str, dict[str, Any]] = {}
    for c in all_raw:
        ph = c["phone"]
        if ph not in by_phone:
            by_phone[ph] = c
        else:
            by_phone[ph] = merge_contacts(by_phone[ph], c)

    merged = sorted(by_phone.values(), key=lambda x: x["phone"])
    tr = Translator(CACHE_PATH)
    ascii_print("Translation pass (English -> French for notes)...")
    pretranslate_contacts(merged, tr)

    classified: list[dict[str, Any]] = []
    for c in merged:
        notes = build_notes(c, tr)
        out = {k: v for k, v in c.items() if k != "phone_display"}
        out["notes"] = notes
        classified.append(out)

    matrix: dict[str, dict[str, int]] = defaultdict(lambda: defaultdict(int))
    for c in classified:
        matrix[c.get("type", "other")][c.get("priority", "moyenne")] += 1

    source_counter = Counter()
    for c in classified:
        for part in str(c.get("source_file", "")).split(";"):
            source_counter[part.strip()] += 1

    inventory_path = OUT_DIR / "inventory.json"
    classified_path = OUT_DIR / "classified_all.json"
    md_path = OUT_DIR / "by_type_priority.md"
    sql_path = OUT_DIR / "import_ready.sql"

    inventory["totals"] = {
        "raw_rows_with_phone": len(all_raw),
        "unique_phones": len(classified),
    }
    inventory_path.write_text(json.dumps(inventory, ensure_ascii=False, indent=2), encoding="utf-8")

    ascii_print("Fetching Airtable link status...")
    airtable = fetch_airtable_status()
    inventory["airtable"] = airtable
    inventory_path.write_text(json.dumps(inventory, ensure_ascii=False, indent=2), encoding="utf-8")
    classified_path.write_text(json.dumps(classified, ensure_ascii=False, indent=2), encoding="utf-8")

    md_lines = [
        "# Rapport classification — import batch CRM",
        "",
        f"Genere : {datetime.now(timezone.utc).strftime('%Y-%m-%d %H:%M UTC')}",
        "",
        "## Totaux",
        f"- Lignes brutes (telephone valide) : **{len(all_raw)}**",
        f"- Telephones uniques : **{len(classified)}**",
        "",
        "## Matrice type x priorite",
        "",
        "| Type | Basse | Moyenne | Haute | Total |",
        "|------|------:|--------:|------:|------:|",
    ]
    for t in sorted(matrix.keys()):
        row = matrix[t]
        b, m, h = row.get("basse", 0), row.get("moyenne", 0), row.get("haute", 0)
        md_lines.append(f"| {t} | {b} | {m} | {h} | {b + m + h} |")

    md_lines.extend(["", "## Priorite (global)", ""])
    pc = Counter(c["priority"] for c in classified)
    for p in PRIORITY_ORDER:
        md_lines.append(f"- **{p}** : {pc.get(p, 0)}")

    md_lines.extend(["", "## Top sources (fichiers)", ""])
    for src, cnt in source_counter.most_common(20):
        md_lines.append(f"- `{src}` : {cnt}")

    md_lines.extend(["", "## Airtable", ""])
    for a in airtable:
        md_lines.append(f"- {a['status']} — {a['detail']}")
        md_lines.append(f"  - {a['url']}")

    md_path.write_text("\n".join(md_lines) + "\n", encoding="utf-8")
    sql_path.write_text(generate_sql(classified), encoding="utf-8")

    for p in PRIORITY_ORDER:
        write_priority_csv(OUT_DIR / f"export_{p}.csv", classified, p)

    report = {
        "raw_rows": len(all_raw),
        "unique_phones": len(classified),
        "matrix": {t: dict(matrix[t]) for t in matrix},
        "per_file": dict(per_file_counts),
        "inventory_path": str(inventory_path),
        "classified_path": str(classified_path),
        "md_path": str(md_path),
        "sql_path": str(sql_path),
        "airtable": airtable,
    }
    (OUT_DIR / "run_report.json").write_text(
        json.dumps(report, ensure_ascii=False, indent=2), encoding="utf-8"
    )

    ascii_print("DONE")
    ascii_print(json.dumps(report, ensure_ascii=False))
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
