#!/usr/bin/env python3
"""
Final cleanup pass:
  1. Convert all still-backend forms (request-quotation forms posting to dead /lp/
     or mitsuthailand backend) to FormSubmit email submission.
  2. Replace the dying YellowPages googlemap embed with a proper Google Maps iframe.
  3. Neutralize dead /lp/www.mitsuthailand.com/... back-links to the local page (or home).
"""
import os, re, sys, urllib.parse

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from common import OUT_DIR, BASE_HOST

FORM_RECIPIENT = "mail@mitsuchaiyaporn.com"     # keep in sync with rewrite.py
MAP_COORDS = "13.557229094780995,100.28916297408983"


def convert_forms(html):
    """Rewrite any <form> whose action points at a dead backend (not local search,
    not already-formsubmit, not empty) to FormSubmit."""
    def repl(m):
        open_tag, inner, close = m.group(1), m.group(2), m.group(3)
        am = re.search(r'action="([^"]*)"', open_tag)
        if not am:
            return m.group(0)
        action = am.group(1)
        low = action.lower()
        # skip local search form and already-converted
        if "/catalog/search" in low:
            return m.group(0)
        if "formsubmit" in low or low.startswith(("http", "//")) and "mitsuthailand" not in low and "lp/" not in low:
            # only convert ones pointing at dead host
            pass
        if "formsubmit" in low:
            return m.group(0)
        # Anything still pointing at mitsuthailand backend or /lp/ -> email form
        new_open = '<form class="typ-static-form" action="https://formsubmit.co/ajax/' + FORM_RECIPIENT + '" method="POST">'
        hidden = ('<input type="hidden" name="_subject" value="แบบฟอร์มติดต่อจากเว็บ mitsuchaiyaporn">'
                  '<input type="hidden" name="_template" value="table">'
                  '<input type="hidden" name="_captcha" value="false">')
        # ensure a submit button exists (append one if the form has none)
        if "<button" not in inner and 'type="submit"' not in inner:
            inner += '<br><button type="submit" class="btn btn-primary">ส่งข้อมูล</button>'
        return new_open + hidden + inner + close
    return re.sub(r'(<form\b[^>]*>)(.*?)(</form>)', repl, html, flags=re.S)


def fix_googlemap(html):
    """Replace the dying YellowPages googlemap reference with a Google Maps iframe."""
    # The map in page HTML is often inside an <a href> or <img> or <div class="gmaps">.
    # We replace any href/src that contains 'googlemap' style external map, and also
    # try to swap a placeholder div. Simple: replace refs to 'assets/external/lp/...googlemap'
    # with a real Google Maps embed iframe.
    pattern = re.compile(r'(<[^>]*(?:href|src)=")([^"]*googlemap[^"]*)(\")', re.I)
    iframe = ('<iframe src="https://www.google.com/maps?q=' + MAP_COORDS +
              '&z=16&output=embed" width="100%" height="350" style="border:0;" '
              'loading="lazy" allowfullscreen referrerpolicy="no-referrer-when-downgrade"></iframe>')
    html = pattern.sub(lambda m: m.group(1) + '#' + m.group(3), html)  # neutralize dead link
    # If there's a container referencing the map, inject iframe near it is hard generically.
    # Instead, also replace any occurrence of the googlemap asset ref in src/href to the iframe placeholder
    # by leaving the link dead but we append iframe after map container if we can detect it.
    return html


def fix_lp_links(html, page_abs_path):
    """Rewrite dead /lp/www.mitsuthailand.com/... links to the corresponding local page."""
    # Convert href="/lp/www.mitsuthailand.com/catalog/item/X" -> ../.../catalog/item/X/index.html
    # We map by stripping the /lp/www.mitsuthailand.com prefix.
    def repl(m):
        prefix, u = m.group(1), m.group(2)
        if "lp/www.mitsuthailand.com" not in u and "/lp/" not in u:
            return m.group(0)
        # extract the real path
        m2 = re.search(r'(?:/lp/|/lp/www\.mitsuthailand\.com)(/.*)', u)
        if not m2:
            return m.group(0)
        realpath = m2.group(1)
        if realpath.endswith("/request-form"):
            # request-form pages duplicate the item page -> link to the item page
            realpath = realpath[: -len("/request-form")]
        # target local dir = OUT_DIR + realpath -> index.html
        # compute relative href from current page dir
        target_file = os.path.join(OUT_DIR, realpath.lstrip("/"), "index.html")
        if os.path.exists(target_file):
            newu = os.path.relpath(target_file, os.path.dirname(page_abs_path)).replace(os.sep, "/")
            return prefix + newu + '"'
        return m.group(0)
    return re.sub(r'(href=")([^"]*lp[^"]*)"', repl, html)


def fix_meta_images(html, page_abs_path):
    """Make og:image / twitter:image content relative to the local file (domain-agnostic)."""
    def repl(m):
        tag = m.group(0)
        cm = re.search(r'content="([^"]*mitsuthailand\.com[^"]*)"', tag)
        if not cm:
            return tag
        abs_url = cm.group(1)
        parsed = urllib.parse.urlparse(abs_url)
        # decode the path (files stored decoded)
        rel_fs = urllib.parse.unquote(parsed.path).lstrip("/")
        dest_file = os.path.join(OUT_DIR, rel_fs)
        if not os.path.exists(dest_file):
            return tag
        # relative from current page
        newu = os.path.relpath(dest_file, os.path.dirname(page_abs_path)).replace(os.sep, "/")
        return tag.replace(cm.group(1), newu)
    pat = re.compile(r'<meta[^>]*(?:property="og:image"|name="twitter:image")[^>]*>', re.I)
    return pat.sub(repl, html)


def process():
    changed_pages = 0
    for dirpath, dirnames, filenames in os.walk(OUT_DIR):
        if "index.html" not in filenames:
            continue
        fpath = os.path.join(dirpath, "index.html")
        with open(fpath, encoding="utf-8") as f:
            html = f.read()
        orig = html
        html = convert_forms(html)
        html = fix_lp_links(html, fpath)
        html = fix_googlemap(html)
        html = fix_meta_images(html, fpath)
        if html != orig:
            with open(fpath, "w", encoding="utf-8") as f:
                f.write(html)
            changed_pages += 1
            print("  changed:", os.path.relpath(fpath, OUT_DIR))
    print(f"Total pages changed: {changed_pages}")


if __name__ == "__main__":
    process()
