#!/usr/bin/env python3
"""Étude 1 — le moteur Vigi-Sky retrouve-t-il les explications du GEIPAN ? (voir PROTOCOLE.md, SHA-256 64225f58…)
Usage : python3 run.py <pylib openpyxl+sgp4> <dossier données GEIPAN> <dossier moteur (sky_events.py + modules)> > resultats.json
Le moteur est celui en production au 15/11/2026, importé tel quel. Seule la branche satellites est neutralisée
(identify_sighting(..., moving=False) ne l'appelle pas ; load_targets et _load_sats sont remplacés par des listes vides
pour éviter tout accès réseau), conformément au protocole (phase 1 : astres naturels)."""
import json, math, os, random, re, sys, unicodedata, collections
from concurrent.futures import ProcessPoolExecutor
from datetime import datetime, timedelta, timezone
from zoneinfo import ZoneInfo

PYLIB, DATA, ENGINE = sys.argv[1], sys.argv[2], sys.argv[3]
sys.path.insert(0, PYLIB); sys.path.insert(0, ENGINE)
PARIS = ZoneInfo("Europe/Paris")
N_SHUFFLE = 200
SEED = 20261115


def norm(s):
    s = unicodedata.normalize("NFD", str(s).lower())
    return "".join(c for c in s if unicodedata.category(c) != "Mn").strip()


# ----------------------------------------------------------------------------- groupes (protocole)
PLANETS = {"venus": "Vénus", "jupiter": "Jupiter", "mars": "Mars", "saturne": "Saturne", "mercure": "Mercure"}
STARS = {"sirius": "Sirius", "capella": "Capella", "arcturus": "Arcturus", "antares": "Antarès", "vega": "Véga", "rigel": "Rigel",
         "procyon": "Procyon", "betelgeuse": "Bételgeuse", "altair": "Altaïr", "aldebaran": "Aldébaran", "spica": "Spica",
         "pollux": "Pollux", "deneb": "Deneb", "regulus": "Régulus"}


def group_of(label):
    n = norm(label)
    base = re.sub(r"\s*\(etoile\)$", "", n)
    if base in PLANETS:
        return "astre", PLANETS[base]
    if base in ("lune", "lune rousse"):
        return "astre", "la Lune"
    if base in STARS:
        return "astre", STARS[base]
    if base in ("etoile", "etoile brillante"):
        return "astre", "étoile"
    if (n.startswith("lanterne") or "avion" in n or n.startswith("ballon") or n.startswith("montgolfi") or n == "aeronef"
            or n.startswith("escadrille") or n.startswith("helicoptere") or n.startswith("drone") or n.startswith("laser")
            or n == "animation lumineuse" or n == "oiseau" or n.startswith("trainee de condensation") or n in ("sky rose", "sky-tracer", "spots")):
        return "negatif", label
    if any(k in n for k in ("iss", "satellite", "starlink", "iridium", "rentree", "bolide", "meteor", "lanceur", "fusee", "debris spatia", "navette")):
        return "hors_champ", label
    return "autre", label


def family_of(label, classification):
    """Familles pour la chronologie (agrégats publiables)."""
    n = norm(label)
    if classification in ("C", "D"):
        return "Non identifiés (C, D)"
    g, _ = group_of(label)
    if g == "astre" or n in ("astronomique", "planete", "conjonction"):
        return "Astres (planètes, Lune, étoiles)"
    if n.startswith("lanterne") or "lanterne" in n:
        return "Lanternes thaïes"
    if any(k in n for k in ("iss", "satellite", "starlink", "iridium")):
        return "Satellites et ISS"
    if any(k in n for k in ("rentree", "bolide", "meteor", "lanceur", "fusee", "debris spatia", "etoile filante")):
        return "Bolides et rentrées"
    if "avion" in n or n in ("aeronef", "helicoptere") or n.startswith("escadrille") or n.startswith("drone"):
        return "Avions, hélicoptères, drones"
    if n.startswith("ballon") or n.startswith("montgolfi"):
        return "Ballons"
    return "Autres explications"


# ----------------------------------------------------------------------------- lecture
def read_xlsx(path):
    import openpyxl
    rows = list(openpyxl.load_workbook(path, read_only=True).worksheets[0].iter_rows(values_only=True))
    return [dict(zip(rows[0], r)) for r in rows[1:]]


def case_key(s):
    s = re.sub(r"^\[[^\]]*\]\s*", "", str(s).strip()); s = re.sub(r"\s+T\w*\d+$", "", s)
    return re.sub(r"\s+", " ", s).upper()


def parse_dt(s):
    m = re.match(r"(\d\d)/(\d\d)/(\d{4})\s+(\d{1,2})\s*[:hH]\s*(\d\d)", str(s or ""))
    if not m:
        return None
    d, mo, y, hh, mm = map(int, m.groups())
    if hh == 0 and mm == 0:
        return None
    try:
        return datetime(y, mo, d, hh, mm, tzinfo=PARIS).astimezone(timezone.utc)
    except ValueError:
        return None


def parse_az(v):
    if isinstance(v, (int, float)):
        return float(v) % 360
    m = re.match(r"\s*(-?\d+(?:\.\d+)?)", str(v or ""))
    return float(m.group(1)) % 360 if m else None


METRO = {f"{i:02d}" for i in range(1, 96)} | {"2A", "2B"}


def build():
    cases = read_xlsx(os.path.join(DATA, "export_cas_20261110.xlsx"))
    tems = read_xlsx(os.path.join(DATA, "export_temoignages_20261110.xlsx"))
    by_key = {case_key(c["Titre du Cas"]): c for c in cases}
    rows, stats = [], collections.Counter()
    for t in tems:
        c = by_key.get(case_key(t["Témoignage"]))
        if c is None:
            stats["sans cas public"] += 1; continue
        dep = str(c["Code département"]).zfill(2) if c["Code département"] is not None else ""
        if dep not in METRO:
            stats["hors métropole"] += 1; continue
        when = parse_dt(t["Date et heure locale"])
        if when is None:
            stats["heure absente ou 00:00"] += 1; continue
        if c["Latitude"] in (None, "") or c["Longitude"] in (None, ""):
            stats["sans position"] += 1; continue
        g, target = group_of(c["Phénomène"])
        rows.append({"case": c["ID Etude de Cas"], "year": c["Année"], "dep": dep, "cls": c["Classification"], "label": str(c["Phénomène"]),
                     "group": g, "target": target, "lat": float(c["Latitude"]), "lon": float(c["Longitude"]),
                     "t": when.isoformat(), "az": parse_az(t["Direction d'observation Horizontale"])})
        stats["retenus"] += 1
    return cases, rows, stats


# ----------------------------------------------------------------------------- moteur
_SE = None


def engine():
    global _SE
    if _SE is None:
        import sky_events as se, satcheck
        se.load_targets = lambda now, refresh=True: []
        satcheck._load_sats = lambda refresh=True: []
        _SE = se
    return _SE


def run_one(r, shift_days=0):
    se = engine()
    t = datetime.fromisoformat(r["t"]) + timedelta(days=shift_days)
    res = se.identify_sighting(r["lat"], r["lon"], t, az=r["az"], moving=False)
    return [{"name": c["name"], "kind": c["kind"], "az_diff": c.get("az_diff"), "el": c.get("el"), "az": c.get("az")} for c in res["candidates"]]


def match(target, cand):
    if target == "étoile":
        return cand["kind"] == "etoile"
    return norm(cand["name"]) == norm(target)


def evaluate(rows, cands):
    """Mesures du protocole pour un jeu (réel ou décalé)."""
    out = {}
    for with_dir in (True, False):
        A = [(r, c) for r, c in zip(rows, cands) if r["group"] == "astre" and ((r["az"] is not None) == with_dir)]
        top1 = sum(1 for r, c in A if c and match(r["target"], c[0]))
        top3 = sum(1 for r, c in A if any(match(r["target"], x) for x in c[:3]))
        out["dir" if with_dir else "nodir"] = {"n": len(A), "top1": top1, "top3": top3}
    N = [(r, c) for r, c in zip(rows, cands) if r["group"] == "negatif" and r["az"] is not None]
    fp = sum(1 for r, c in N if c)       # avec direction, le moteur ne garde que les astres à moins de 35°
    out["neg_dir"] = {"n": len(N), "fausse_piste": fp}
    return out


def _shuffle_job(args):
    k, rows = args
    rnd = random.Random(SEED + k)
    cands = [run_one(r, rnd.choice([-1, 1]) * rnd.randint(30, 180)) for r in rows]
    return evaluate(rows, cands)


def main():
    cases, rows, stats = build()
    test_rows = [r for r in rows if r["group"] in ("astre", "negatif")]
    cands = [run_one(r) for r in test_rows]
    real = evaluate(test_rows, cands)
    with ProcessPoolExecutor(max_workers=os.cpu_count()) as ex:
        shuffles = list(ex.map(_shuffle_job, [(k, test_rows) for k in range(N_SHUFFLE)]))

    # séparation : écart d'azimut du meilleur candidat (avec direction), astres contre témoins négatifs
    def best_gap(r, c):
        return c[0]["az_diff"] if c else 180
    ga = [best_gap(r, c) for r, c in zip(test_rows, cands) if r["group"] == "astre" and r["az"] is not None]
    gn = [best_gap(r, c) for r, c in zip(test_rows, cands) if r["group"] == "negatif" and r["az"] is not None]
    auc = sum((1 if a < b else 0.5 if a == b else 0) for a in ga for b in gn) / (len(ga) * len(gn)) if ga and gn else None
    # matrice : astre du GEIPAN → premier candidat du moteur
    conf = collections.Counter()
    for r, c in zip(test_rows, cands):
        if r["group"] == "astre":
            conf[(r["target"], (c[0]["name"] if c and c[0]["kind"] != "etoile" else ("étoile" if c else "aucun")))] += 1
    # cas C et D avec direction : astre compatible ? (agrégé)
    cd = [r for r in rows if r["cls"] in ("C", "D") and r["az"] is not None]
    cd_c = [run_one(r) for r in cd]
    cd_kinds = collections.Counter((c[0]["name"] if c[0]["kind"] != "etoile" else "étoile brillante") if c else "aucun astre à moins de 35°" for c in cd_c)
    cd_list = [{"case": r["case"], "year": r["year"], "dep": r["dep"], "cls": r["cls"], "cand": c[0]["name"], "az_diff": c[0]["az_diff"], "el": c[0]["el"]}
               for r, c in zip(cd, cd_c) if c and c[0]["az_diff"] is not None and c[0]["az_diff"] <= 15]
    # agrégats descriptifs (tous les cas publics)
    fam_year = collections.Counter((c["Année"], family_of(c["Phénomène"], c["Classification"])) for c in cases if c["Année"])
    hours = collections.Counter((family_of(next((c["Phénomène"] for c in [{}] ), ""), None), 0) for _ in [])  # remplacé ci-dessous
    hours = collections.Counter()
    by_case = {c["ID Etude de Cas"]: c for c in cases}
    for r in rows:
        h = datetime.fromisoformat(r["t"]).astimezone(PARIS).hour
        hours[(family_of(r["label"], r["cls"]), h)] += 1
    # exemples de concordance (astre visible à l'heure dite dans la direction indiquée), pour le graphique polaire : agrégé en écarts
    polar = [{"target": r["target"], "az_witness": r["az"], "az_engine": c[0]["az"], "el": c[0]["el"], "ok": match(r["target"], c[0])}
             for r, c in zip(test_rows, cands) if r["group"] == "astre" and r["az"] is not None and c]
    json.dump({"protocol_sha256": "64225f585dd9b1241f32589fbea2aebd4c594c4279b424aa5333e4227e88c383", "run_at": datetime.now(timezone.utc).isoformat(),
               "selection": dict(stats), "groups": dict(collections.Counter(r["group"] for r in rows)),
               "targets": dict(collections.Counter(r["target"] for r in rows if r["group"] == "astre")),
               "real": real, "shuffles": shuffles, "gap_astre": ga, "gap_neg": gn, "auc": auc,
               "confusion": [[a, b, n] for (a, b), n in conf.items()],
               "cd": {"n": len(cd), "kinds": dict(cd_kinds), "list_private": cd_list},
               "family_year": [[y, f, n] for (y, f), n in fam_year.items()],
               "hours": [[f, h, n] for (f, h), n in hours.items()], "polar": polar,
               "n_cases": len(cases)}, sys.stdout, ensure_ascii=False)


if __name__ == "__main__":
    main()
