"""
Match The God Journey transcripts to their official episodes using the yearly
RSS feeds, so the orphaned "audio date-code" transcripts (e.g. a first line of
http://.../audio/2020/201113.mp3) get a real, linkable episode identity.

Each feed <item> gives us three things we can match on:
  - <enclosure url>  the MP3 link  -> matches an audio-date-code transcript
  - <guid>/<link>    the post URL  -> the page we actually want to cite/link
  - <title>          the official title, already in "Title (#NNN)" form

What --apply does: for each orphaned audio transcript we can match, it rewrites
ONLY the first line (the source label) from the raw MP3 URL to the episode's
post URL. That makes the episode linkable and gives it a real title (rendered by
rag.display_name). It does NOT touch the transcript body, and it does NOT touch
transcripts that already carry a post URL.

Default run is a DRY RUN: it writes episode-retitle-proposal.csv for review and
changes nothing. To apply after review:  python3 retitle_episodes.py --apply
"""

import os, re, csv, sys, glob, html
import xml.etree.ElementTree as ET

BASE = os.path.dirname(os.path.abspath(__file__))
FEED_DIR = os.path.join(BASE, "The God Journey yearly feeds")
TRANSCRIPTS = os.path.join(BASE, "content", "transcripts")
PROPOSAL = os.path.join(BASE, "episode-retitle-proposal.csv")


def _child(item, tag):
    for ch in item:
        if ch.tag.split("}")[-1] == tag:
            return ch
    return None


def _audio_key(url):
    """Stable key for an episode's audio: everything from 'audio/' onward,
    lowercased. Ignores the blubrry prefix and domain differences."""
    m = re.search(r"audio/.*?\.mp3", url, re.I)
    return m.group(0).lower() if m else None


def _norm_url(url):
    u = url.strip().lower()
    u = re.sub(r"^https?://", "", u)
    u = re.sub(r"^www\.", "", u)
    u = u.split("#")[0].split("?")[0].rstrip("/")
    u = u.replace("/wordpress/", "/")
    return u


def _slug(url):
    return _norm_url(url).rsplit("/", 1)[-1]


def _date_from_audio(key):
    """Pull a YYMMDD (or YYYY/MMDD) date out of an audio key, as YYYY-MM-DD."""
    m = re.search(r"audio/(\d{4})/(\d{2})?(\d{2})(\d{2})[a-z]?\.mp3", key)
    if m:
        yr, yy, mm, dd = m.groups()
        return f"{yr}-{mm}-{dd}"
    return ""


def build_maps():
    """Return maps from the feeds:
      by_audio:  audio_key -> (title, post_url)
      by_url:    normalized post URL -> (title, post_url)
      by_slug:   final slug -> (title, post_url)
      pub_by_audio: audio_key -> pubDate text
    """
    by_audio, by_url, by_slug, pub_by_audio = {}, {}, {}, {}
    for fp in glob.glob(os.path.join(FEED_DIR, "*")):
        try:
            items = ET.parse(fp).getroot().findall(".//item")
        except Exception:
            continue
        for it in items:
            te = _child(it, "title")
            title = html.unescape((te.text or "").strip()) if te is not None else ""
            if not title:
                continue
            # Collect guid + link. Some feeds put a "?p=123" guid AND the real
            # permalink in <link>; prefer the permalink for citing, but index
            # BOTH real-path URLs so a transcript matches whichever it stored.
            guid = link = ""
            g = _child(it, "guid")
            if g is not None and g.text and g.text.strip().startswith("http"):
                guid = g.text.strip()
            l = _child(it, "link")
            if l is not None and l.text and l.text.strip().startswith("http"):
                link = l.text.strip()
            post = link or guid  # canonical permalink to cite/link
            for u in (link, guid):
                if u and "/" in _norm_url(u):  # skip pathless "?p=" guids
                    by_url.setdefault(_norm_url(u), (title, post))
                    by_slug.setdefault(_slug(u), (title, post))
            # audio enclosure
            enc = _child(it, "enclosure")
            if enc is not None:
                k = _audio_key(enc.attrib.get("url", ""))
                if k:
                    by_audio.setdefault(k, (title, post))
                    pe = _child(it, "pubDate")
                    if pe is not None and pe.text:
                        pub_by_audio.setdefault(k, pe.text.strip())
    return by_audio, by_url, by_slug, pub_by_audio


def _first_line(path):
    with open(path, encoding="utf-8", errors="ignore") as f:
        return f.readline().rstrip("\n")


def analyse():
    """For every God Journey transcript, find its feed item and propose a new
    first line of "Official Feed Title — post URL" (the feed title is the source
    of truth; the URL slug is not — Wayne isn't always careful with slugs)."""
    by_audio, by_url, by_slug, pub_by_audio = build_maps()
    rows = []
    for path in sorted(glob.glob(os.path.join(TRANSCRIPTS, "**", "*.txt"), recursive=True)):
        label = _first_line(path).strip()
        low = label.lower()
        if "thegodjourney.com" not in low and "blubrry" not in low:
            continue  # skip vimeo / myfriendluis / individual-recording labels
        rel = os.path.relpath(path, BASE)
        ak = _audio_key(label)
        is_audio = bool(ak) and low.endswith(".mp3")

        title, post, method = "", "", "UNMATCHED"
        if is_audio:
            hit = by_audio.get(ak)
            if hit:
                title, post = hit
                method = "enclosure"
        elif low.startswith("http"):
            hit = by_url.get(_norm_url(label))
            if hit:
                title, post, method = hit[0], hit[1], "url"
            else:
                hit = by_slug.get(_slug(label))
                if hit:
                    title, post, method = hit[0], hit[1], "slug"

        # Propose "Title — URL". Link is the post URL when we have one; for a
        # matched audio orphan with no post URL, fall back to its own MP3 link.
        new = ""
        if title:
            link = post or label
            candidate = f"{title} — {link}"
            if candidate.strip() != label.strip():
                new = candidate

        date = _date_from_audio(ak) if ak else ""
        pub = pub_by_audio.get(ak, "") if ak else ""
        if date and pub:
            sane = "ok" if date[8:10] in pub else "CHECK"
        else:
            sane = "na"

        rows.append({
            "file": rel,
            "kind": "audio-orphan" if is_audio else "post-url",
            "current": label,
            "match": method,
            "new_first_line": new,
            "title": title,
            "date": date, "feed_pubDate": pub, "sanity": sane,
        })
    return rows


def write_proposal(rows):
    cols = ["file", "kind", "current", "match", "new_first_line", "title",
            "date", "feed_pubDate", "sanity"]
    with open(PROPOSAL, "w", newline="", encoding="utf-8") as f:
        w = csv.DictWriter(f, fieldnames=cols)
        w.writeheader()
        for r in rows:
            w.writerow(r)


def apply_changes(rows):
    n = 0
    for r in rows:
        if r["new_first_line"]:
            path = os.path.join(BASE, r["file"])
            with open(path, encoding="utf-8", errors="ignore") as f:
                lines = f.read().split("\n")
            lines[0] = r["new_first_line"]
            with open(path, "w", encoding="utf-8") as f:
                f.write("\n".join(lines))
            n += 1
    return n


def main():
    rows = analyse()
    write_proposal(rows)
    audio = [r for r in rows if r["kind"] == "audio-orphan"]
    post = [r for r in rows if r["kind"] == "post-url"]
    audio_matched = [r for r in audio if r["match"] != "UNMATCHED"]
    audio_unmatched = [r for r in audio if r["match"] == "UNMATCHED"]
    post_matched = [r for r in post if r["match"] != "UNMATCHED"]
    post_unmatched = [r for r in post if r["match"] == "UNMATCHED"]
    changes = [r for r in rows if r["new_first_line"]]
    check = [r for r in rows if r["sanity"] == "CHECK"]
    print(f"God Journey transcripts examined: {len(rows)}")
    print(f"  audio orphans (bare MP3 first line):     {len(audio)}")
    print(f"     -> matched via feed enclosure:        {len(audio_matched)}")
    print(f"     -> UNMATCHED (need a look):           {len(audio_unmatched)}")
    print(f"  post-URL transcripts:                    {len(post)}")
    print(f"     -> matched to a feed title:           {len(post_matched)}")
    print(f"     -> no feed match (kept as-is):        {len(post_unmatched)}")
    print(f"  proposed first-line changes total:       {len(changes)}")
    print(f"  rows flagged CHECK (date vs pubDate):    {len(check)}")
    print(f"\nProposal written: {os.path.relpath(PROPOSAL, BASE)}")
    if "--apply" in sys.argv:
        n = apply_changes(rows)
        print(f"\nAPPLIED: rewrote the first line of {n} transcript(s) to 'Title — URL'.")
        print("Re-run ingest.py to fold the corrected titles/links into the store.")
    else:
        print("\n(dry run — nothing changed. Review the CSV, then: python3 retitle_episodes.py --apply)")


if __name__ == "__main__":
    main()
