#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Importateur : transforme n'importe quel site HTML statique en site géré par le back-office.

Usage :
    python3 importer.py /chemin/vers/le/site-html /chemin/vers/le/nouveau-dossier

Ce que ça fait :
1. Copie le moteur (build.py, admin.py, blocks_tools.py, translate.py, lanceurs) dans le nouveau dossier.
2. Détecte l'habillage commun du site (head, <header>, <footer>, scripts de fin de page) → dossier layout/
   (le site garde exactement son design ; ces fichiers restent éditables dans le back-office › Menu & pied de page).
3. Convertit chaque page HTML en pages/… avec front-matter (title, description) et liens réécrits en URLs propres
   (« contact.html » → « /contact/ », redirections 301 ajoutées pour les anciennes adresses).
4. Copie tous les fichiers statiques (css, js, images, polices…) dans assets/ en réécrivant les références.
5. Extrait tous les textes/images/boutons en blocs éditables (blocks/…).
6. Affiche un rapport. Ensuite : double-cliquez « Lancer le back-office » dans le nouveau dossier.

Limites honnêtes : sites statiques uniquement (pas de PHP/CMS) ; les pages très exotiques restent éditables
en HTML dans le back-office ; vérifiez le rapport et l'aperçu page par page après import.
"""
import os, re, sys, json, shutil, html

ENGINE_FILES = ["build.py", "admin.py", "blocks_tools.py", "translate.py", "importer.py", "passenger_wsgi.py", "README-MOTEUR.md", "DEPLOIEMENT-BO-EN-LIGNE.md", "NOMENCLATURE-BEECOM.md",
                "Lancer le back-office (Mac).command", "Lancer le back-office (Windows).bat",
                "Voir le site (Mac).command", "Voir le site (Windows).bat"]
ASSET_EXT = (".css", ".js", ".mjs", ".png", ".jpg", ".jpeg", ".webp", ".gif", ".svg", ".ico",
             ".woff", ".woff2", ".ttf", ".otf", ".eot", ".mp4", ".webm", ".mp3", ".pdf", ".json", ".xml", ".txt", ".map", ".avif", ".webmanifest")
SKIP_DIRS = {".git", "node_modules", ".idea", ".vscode", "__pycache__", "dist"}
SKIP_FILES = {"robots.txt", "sitemap.xml", ".htaccess", "CNAME", "_redirects"}
# Fichiers de la racine qui doivent RESTER à la racine du site publié (référencés en /favicon.ico, /site.webmanifest…)
ROOT_KEEP = {"favicon.ico", "site.webmanifest", "browserconfig.xml", "apple-touch-icon.png", "icon-192.png", "icon-512.png"}

def read(p): return open(p, encoding="utf-8", errors="replace").read()

def first_tag(htm, tag):
    m = re.search(rf"<{tag}\b[\s\S]*?</{tag}>", htm, re.I)
    return m.group(0) if m else ""

def get_head_bits(head):
    """Feuilles de style, polices, scripts et styles inline du <head> d'origine (hors title/meta/canonical/og)."""
    keep = []
    for m in re.finditer(r"<link\b[^>]*>|<style\b[\s\S]*?</style>|<script\b[\s\S]*?</script>|<script\b[^>]*/>|<meta\b[^>]*name=\"theme-color\"[^>]*>", head, re.I):
        t = m.group(0)
        if re.search(r'rel="(canonical|alternate)"', t, re.I): continue
        if re.search(r'name="(description|viewport|robots)"', t, re.I): continue
        if re.search(r'application/ld\+json', t, re.I): continue                      # régénéré par le moteur
        if re.search(r'googletagmanager|matomo', t, re.I): continue                   # régénéré via Réglages
        keep.append(t)
    return "\n".join(keep)

def meta_of(htm, fallback_title):
    title = re.search(r"<title[^>]*>([\s\S]*?)</title>", htm, re.I)
    desc = re.search(r'<meta[^>]+name="description"[^>]+content="([^"]*)"', htm, re.I) or \
           re.search(r'<meta[^>]+content="([^"]*)"[^>]+name="description"', htm, re.I)
    return (html.unescape(title.group(1)).strip() if title else fallback_title,
            html.unescape(desc.group(1)).strip() if desc else "")

def url_map_for(pages):
    """ancien lien → URL propre : contact.html → /contact/, dossier/index.html → /dossier/."""
    mp = {}
    for rel in pages:
        clean = "/" if rel == "index.html" else "/" + re.sub(r"(?:/index)?\.html$", "", rel.replace(os.sep, "/")) + "/"
        for old in {rel.replace(os.sep, "/"), "/" + rel.replace(os.sep, "/"), "./" + rel.replace(os.sep, "/")}:
            mp[old] = clean
        if rel.endswith("index.html"):
            base = rel[:-10].rstrip("/")
            for old in {base, "/" + base, base + "/", "/" + base + "/"}:
                if old: mp[old] = clean
    mp["index.html"] = "/"; mp["/index.html"] = "/"; mp["./index.html"] = "/"; mp["./"] = "/"
    return mp

def rewrite_refs(htm, urlmap, assetset):
    def rep(m):
        attr, q, val = m.group(1), m.group(2), m.group(3)
        base = val.split("#")[0].split("?")[0]
        tail = val[len(base):]
        if base in urlmap: return f'{attr}={q}{urlmap[base]}{tail}{q}'
        norm = base.lstrip("./").lstrip("/")
        if norm in assetset:
            tgt = norm if norm.startswith("assets/") else "assets/" + norm
            return f'{attr}={q}/{tgt}{tail}{q}'
        return m.group(0)
    return re.sub(r'\b(href|src|srcset|poster|action|data-src)=(["\'])([^"\']+)\2', rep, htm)

def run(src, dest):
    engine = os.path.dirname(os.path.abspath(__file__))
    src, dest = os.path.abspath(src), os.path.abspath(dest)
    if not os.path.isfile(os.path.join(src, "index.html")):
        print("✘ Pas de index.html dans", src); sys.exit(1)
    if os.path.exists(dest) and os.listdir(dest):
        print("✘ Le dossier de destination existe déjà et n'est pas vide :", dest); sys.exit(1)
    os.makedirs(dest, exist_ok=True)
    report = {"pages": 0, "assets": 0, "fields": 0, "warn": []}

    # 1. moteur
    for f in ENGINE_FILES:
        p = os.path.join(engine, f)
        if os.path.exists(p): shutil.copy2(p, os.path.join(dest, f))
    for d in ("pages", "assets", "blocks", "layout"): os.makedirs(os.path.join(dest, d), exist_ok=True)

    # 2. inventaire
    pages, assets = [], []
    for dp, dn, fns in os.walk(src):
        dn[:] = [d for d in dn if d not in SKIP_DIRS]
        for fn in fns:
            rel = os.path.relpath(os.path.join(dp, fn), src)
            if fn in SKIP_FILES or fn.startswith("."): continue
            if fn.lower().endswith((".html", ".htm")): pages.append(rel)
            elif fn.lower().endswith(ASSET_EXT): assets.append(rel)
    assetset = {a.replace(os.sep, "/") for a in assets}
    urlmap = url_map_for(pages)

    # 3. assets → assets/ (chemins conservés ; un dossier source déjà nommé assets/ n'est pas doublé)
    #    Exception : les fichiers « racine » (favicon.ico, site.webmanifest…) vont dans root/ et
    #    restent servis à la racine du site (build.py copie root/* tel quel dans dist/).
    rootfiles = [a for a in assets if os.sep not in a and a in ROOT_KEEP]
    assets = [a for a in assets if a not in rootfiles]
    assetset -= set(rootfiles)
    if rootfiles: os.makedirs(os.path.join(dest, "root"), exist_ok=True)
    for a in rootfiles: shutil.copy2(os.path.join(src, a), os.path.join(dest, "root", a))
    for a in assets:
        an = a.replace(os.sep, "/")
        tgt = an if an.startswith("assets/") else "assets/" + an
        d = os.path.join(dest, *tgt.split("/"))
        os.makedirs(os.path.dirname(d), exist_ok=True)
        shutil.copy2(os.path.join(src, a), d)
    report["assets"] = len(assets) + len(rootfiles)

    # 4. habillage commun : l'en-tête de référence est le plus répandu sur les pages INTÉRIEURES ;
    # si l'accueil a son propre menu, il devient layout/header-home.html (affiché sur l'accueil seul)
    idx = read(os.path.join(src, "index.html"))
    head = first_tag(idx, "head")
    home_header = first_tag(idx, "header")
    hdr_counts = {}
    for rel in pages:
        if rel == "index.html": continue
        hh_ = first_tag(read(os.path.join(src, rel)), "header")
        if hh_: hdr_counts[hh_] = hdr_counts.get(hh_, 0) + 1
    header = max(hdr_counts, key=hdr_counts.get) if hdr_counts else home_header
    home_variant = home_header if (home_header and home_header != header) else ""
    footer = first_tag(idx, "footer")
    endscripts = "\n".join(re.findall(r"<script\b[\s\S]*?</script>", first_tag(idx, "body") or idx, re.I)[-3:]) if False else ""
    bodym = re.search(r"<body\b[^>]*>([\s\S]*)</body>", idx, re.I)
    tail_scripts = []
    if bodym:
        tail = bodym.group(1)
        for m in reversed(list(re.finditer(r"<script\b[\s\S]*?</script>\s*$", tail, re.I))):
            tail_scripts.insert(0, m.group(0).strip()); tail = tail[:m.start()].rstrip()
    # bloc commun juste après le header (ex. barre mobile collante) → rattaché à l'en-tête,
    # UNIQUEMENT s'il est strictement identique sur toutes les pages (sinon il reste dans chaque page)
    after_header = ""
    if home_header and bodym:
        mafter = re.match(r'\s*(<div\b[^>]*class="[^"]*\b(?:mobile-sticky|sticky)[^"]*"[\s\S]*?</div>)', bodym.group(1).split(home_header, 1)[1] if home_header in bodym.group(1) else "")
        if mafter:
            cand = mafter.group(1)
            if all(cand in read(os.path.join(src, r)) for r in pages): after_header = cand
    open(os.path.join(dest, "layout", "head.html"), "w", encoding="utf-8").write(rewrite_refs(get_head_bits(head), urlmap, assetset))
    open(os.path.join(dest, "layout", "header.html"), "w", encoding="utf-8").write(rewrite_refs(header + ("\n" + after_header if after_header else ""), urlmap, assetset))
    if home_variant:
        open(os.path.join(dest, "layout", "header-home.html"), "w", encoding="utf-8").write(rewrite_refs(home_variant + ("\n" + after_header if after_header else ""), urlmap, assetset))
        report["warn"].append("En-tête d'accueil différent de celui des pages intérieures → conservé dans layout/header-home.html (affiché sur l'accueil seul, éditable dans Menu & habillage)")
    open(os.path.join(dest, "layout", "footer.html"), "w", encoding="utf-8").write(rewrite_refs(footer, urlmap, assetset))
    open(os.path.join(dest, "layout", "scripts.html"), "w", encoding="utf-8").write(rewrite_refs("\n".join(tail_scripts), urlmap, assetset))
    # pop-up : css/js dédiés extraits du moteur
    try:
        css = read(os.path.join(engine, "assets", "css", "style.css"))
        m = re.search(r"/\* -+ Pop-up -+ \*/([\s\S]*?)(?=/\* -+ )", css + "/* ---------- fin */")
        if m: open(os.path.join(dest, "assets", "vfg-popup.css"), "w", encoding="utf-8").write(m.group(0))
        js = read(os.path.join(engine, "assets", "js", "main.js"))
        m = re.search(r"/\* ---- Pop-up[\s\S]*?\n  /\* ---- ", js)
        if m: open(os.path.join(dest, "assets", "vfg-popup.js"), "w", encoding="utf-8").write("(function(){'use strict';\n" + m.group(0).rsplit("/* ---- ", 1)[0] + "})();")
    except Exception as e:
        report["warn"].append(f"pop-up non préparée : {e}")

    # 5. pages
    sys.path.insert(0, engine)
    import blocks_tools
    # sitemap d'origine : lastmod/priority conservés page par page (le moteur régénère le sitemap à l'identique)
    smmeta = {}
    smp = os.path.join(src, "sitemap.xml")
    if os.path.exists(smp):
        for mu in re.finditer(r"<url>([\s\S]*?)</url>", read(smp)):
            blk = mu.group(1)
            loc = re.search(r"<loc>([^<]+)</loc>", blk)
            if not loc: continue
            path = "/" + loc.group(1).split("//", 1)[-1].split("/", 1)[-1] if "/" in loc.group(1).split("//", 1)[-1] else "/"
            lastmod = re.search(r"<lastmod>([^<]*)</lastmod>", blk)
            prio = re.search(r"<priority>([^<]*)</priority>", blk)
            smmeta[path] = (lastmod.group(1) if lastmod else "", prio.group(1) if prio else "")
    redirects = []
    for rel in sorted(pages):
        htm = read(os.path.join(src, rel))
        title, desc = meta_of(htm, os.path.splitext(os.path.basename(rel))[0])
        page_lds = [re.sub(r"\s*\n\s*", " ", x.group(0)) for x in
                    re.finditer(r'<script type="application/ld\+json">[\s\S]*?</script>', first_tag(htm, "head"), re.I)]
        ogt = re.search(r'<meta property="og:type" content="([^"]+)"', htm)
        ogi = re.search(r'<meta property="og:image" content="([^"]+)"', htm)
        og_path = re.sub(r"^https?://[^/]+", "", html.unescape(ogi.group(1))) if ogi else ""
        if og_path.endswith("og-default.jpg"): og_path = ""   # image par défaut : rien à noter
        bodym = re.search(r"<body\b[^>]*>([\s\S]*)</body>", htm, re.I)
        body = bodym.group(1) if bodym else htm
        hh, ff = first_tag(body, "header"), first_tag(body, "footer")
        if hh:
            body = body.replace(hh, "", 1)
            if rel != "index.html" and hh != header:
                report["warn"].append(f"{rel} : en-tête différent de l'en-tête commun → remplacé par le menu commun (seul l'accueil peut avoir sa variante)")
        elif header: report["warn"].append(f"{rel} : pas de <header> détecté (gardé tel quel)")
        if after_header and after_header in body: body = body.replace(after_header, "", 1)
        if ff: body = body.replace(ff, "", 1)
        # scripts de fin de page identiques à l'habillage : retirés un par un
        body = body.strip()
        changed = True
        while changed:
            changed = False
            for scr in tail_scripts:
                if body.endswith(scr): body = body[: -len(scr)].rstrip(); changed = True
        body = body.strip()
        mmain = re.fullmatch(r"<main\b[^>]*>([\s\S]*)</main>", body, re.I)
        if mmain: body = mmain.group(1).strip()
        body = body.replace('action="https://api.web3forms.com/submit"', 'action="{{FORM_ACTION}}"').replace('action="/api/contact.php"', 'action="{{FORM_ACTION}}"')
        # chaque image reçoit un attribut alt (vide si absent) : le back-office crée ainsi son champ
        # « texte alternatif » éditable, remplissable en masse par le bouton IA de la page Images
        body = re.sub(r"<img\b(?![^>]*\balt=)([^>]*)>", r'<img alt=""\1>', body)
        body = re.sub(r'(name="access_key"\s+value=")[^"]*(")', r'\g<1>{{W3F}}\g<2>', body)
        body = rewrite_refs(body, urlmap, assetset).strip() + "\n"
        try:
            tok, secs = blocks_tools.extract(body)
        except Exception as e:
            tok, secs = body, []; report["warn"].append(f"{rel} : blocs non extraits ({e})")
        out_rel = rel.replace(os.sep, "/").replace(".htm", ".html") if not rel.endswith(".html") else rel.replace(os.sep, "/")
        out_rel = re.sub(r"/index\.html$", ".html", out_rel) if out_rel != "index.html" and out_rel.endswith("/index.html") else out_rel
        fp = os.path.join(dest, "pages", out_rel)
        os.makedirs(os.path.dirname(fp), exist_ok=True)
        fm = f"---\ntitle: {title}\ndescription: {desc}\n"
        if rel == "404.html": fm += "noindex: true\n"
        if ogt and ogt.group(1) == "article": fm += "type: article\n"
        if og_path: fm += f"og_image: {og_path}\n"
        clean = urlmap.get("/" + rel.replace(os.sep, "/"), "/" if rel == "index.html" else None)
        if clean in smmeta:
            lastmod, prio = smmeta[clean]
            if lastmod: fm += f"modified: {lastmod}\n"
            if prio: fm += f"priority: {prio}\n"
        if page_lds: fm += "ld_json: " + "|||".join(page_lds) + "\n"
        fm += "---\n"
        open(fp, "w", encoding="utf-8").write(fm + tok)
        if secs:
            name = re.sub(r"\.html$", "", out_rel).replace("/", "__")
            json.dump({"page": out_rel, "label": title[:60], "auto": True, "sections": secs},
                      open(os.path.join(dest, "blocks", name + ".json"), "w", encoding="utf-8"), ensure_ascii=False, indent=1)
            report["fields"] += sum(len(sc["fields"]) for sc in secs)
        # redirection 301 ancienne URL « à plat » (contact.html → /contact/) ; les …/index.html sont déjà
        # normalisés par la règle générale du .htaccess, inutile de les lister
        if rel not in ("index.html", "404.html") and not rel.replace(os.sep, "/").endswith("/index.html"):
            old = "/" + rel.replace(os.sep, "/")
            redirects.append({"from": old, "to": urlmap.get(old, "/")})
        report["pages"] += 1
    json.dump(redirects, open(os.path.join(dest, "redirects.json"), "w", encoding="utf-8"), ensure_ascii=False, indent=1)

    # popup administrable (désactivée par défaut)
    ppop = os.path.join(engine, "blocks", "_popup.json")
    if os.path.exists(ppop): shutil.copy2(ppop, os.path.join(dest, "blocks", "_popup.json"))
    # 6. config minimale
    t0, _ = meta_of(idx, "mon-site")
    segs = [x.strip() for x in re.split(r"\s*[|—–]\s*", t0) if x.strip()]
    site_name = min(segs, key=len) if segs else "mon-site"
    mcan = re.search(r'rel="canonical"\s+href="(https?://[^/"]+)', idx)
    site_url = mcan.group(1) if mcan else "https://CHANGEZ-MOI.fr"
    dom = site_url.split("//", 1)[-1]
    if dom.startswith("www."): dom = dom[4:]   # préprod/FTP sur le domaine nu (preprod.mon-site.fr, pas preprod.www.…)
    json.dump({"SITE_NAME": site_name or "mon-site", "SITE_URL": site_url, "ENV": "preprod",
               "PREPROD_URL": f"https://preprod.{dom}", "FTP_DIR_PREPROD": f"/preprod.{dom}", "FTP_DIR_PROD": f"/{dom}",
               "FORM_TO": "", "FORM_FROM": f"noreply@{dom}", "LANGS": ""},
              open(os.path.join(dest, "config.json"), "w", encoding="utf-8"), ensure_ascii=False, indent=1)

    # 7. build de vérification (le build.py du NOUVEAU dossier)
    import subprocess
    r = subprocess.run([sys.executable, "build.py"], cwd=dest, capture_output=True, text=True)
    ok = (r.stdout.strip().splitlines() or ["site généré."])[-1] if r.returncode == 0 else f"⚠ la génération a échoué :\n{(r.stderr or r.stdout)[-500:]}"

    print(f"""
✔ Import terminé : {report['pages']} page(s), {report['assets']} fichier(s) statique(s), {report['fields']} champs éditables extraits.
  Habillage conservé dans layout/ (head, header, footer, scripts) — le design ne change pas.
  Redirections 301 anciennes URLs (.html) → URLs propres : redirects.json.
  {ok}""")
    if report["warn"]:
        print("  Avertissements :")
        for w in report["warn"][:15]: print("   -", w)
    print(f"""
  Prochaines étapes dans {dest} :
  1. Double-cliquez « Lancer le back-office » (un compte administrateur temporaire est proposé au premier lancement).
  2. Réglages : URL publique du site, e-mail de réception des formulaires, FTP.
  3. Vérifiez chaque page dans l'aperçu, puis publiez.""")

if __name__ == "__main__":
    if len(sys.argv) < 3:
        print(__doc__); sys.exit(1)
    run(sys.argv[1], sys.argv[2])
