import glob, os, re

KB = os.path.join(os.path.dirname(os.path.abspath(__file__)), "kb")

SECTIONS = [
    ("Overview", ["01_what_is_gohighlevel", "02_crm_overview", "06_hidden_costs_usage_fees"]),
    ("Workflows & Automation", ["03_automations_overview", "13_workflows_for_beginners"]),
    ("Funnels & Websites", ["04_funnel_builder_overview", "14_website_funnel_builder"]),
    ("Calendar, AI & Courses", ["05_features_calendar_ai_courses", "12_ai_agents_bots"]),
    ("Contacts, CRM & Pipelines", ["07_import_contacts", "10_opportunities_pipelines"]),
    ("Conversations", ["08_conversations_trigger_links_snippets"]),
    ("Calendars & Booking", ["09_setup_calendar_booking"]),
    ("Payments", ["11_setup_payments_invoices"]),
]

def first_title(path):
    with open(path, "r", encoding="utf-8") as f:
        for line in f:
            if line.startswith("# "):
                return line[2:].strip()
    return os.path.basename(path).replace(".md", "")

def source_url(path):
    with open(path, "r", encoding="utf-8") as f:
        for line in f:
            m = re.search(r"https://www\.youtube\.com/watch\?v=[\w-]+", line)
            if m:
                return m.group(0)
    return ""

toc_lines = ["# GoHighLevel Knowledge Base", "",
             "> **Purpose:** Step-by-step guidance and overviews for using GoHighLevel (GHL), distilled from tutorial videos.",
             "> **How to use:** Ask questions about any topic below; each section is self-contained with concrete steps.",
             ""]

body = []

for sec_name, fnames in SECTIONS:
    toc_lines.append(f"## {sec_name}")
    for fn in fnames:
        path = os.path.join(KB, fn + ".md")
        if not os.path.exists(path):
            continue
        title = first_title(path)
        toc_lines.append(f"- [{title}](#{re.sub(r'[^a-z0-9]+', '-', title.lower()).strip('-')})")
        with open(path, "r", encoding="utf-8") as f:
            content = f.read().strip()
        body.append(content)
    toc_lines.append("")

master = "\n".join(toc_lines) + "\n\n---\n\n"
master += "\n\n---\n\n".join(body) + "\n"

with open(os.path.join(KB, "MASTER_KB.md"), "w", encoding="utf-8") as f:
    f.write(master)

print(f"MASTER_KB.md written ({len(master)} chars, {len(body)} sections merged)")
