import sqlite3
import json

TRACTATES = [
    "Berakhot", "Peah", "Demai", "Kilayim", "Sheviit", "Terumot", "Ma'aserot", "Ma'aser Sheni", 
    "Challah", "Orlah", "Bikkurim", "Shabbat", "Eruvin", "Pesachim", "Shekalim", "Yoma", 
    "Sukkah", "Beitzah", "Rosh Hashanah", "Ta'anit", "Megillah", "Mo'ed Katan", "Chagigah", 
    "Yevamot", "Ketubot", "Nedarim", "Nazir", "Sotah", "Gittin", "Kiddushin", "Bava Kamma", 
    "Bava Metzia", "Bava Batra", "Sanhedrin", "Makkot", "Shevu'ot", "Eduyot", "Avodah Zarah", 
    "Avot", "Horayot", "Zevachim", "Menachot", "Chullin", "Bekhorot", "Arakhin", "Temurah", 
    "Keritot", "Meilah", "Tamid", "Middot", "Kinnim", "Kelim", "Oholot", "Negaim", "Parah", 
    "Tahorot", "Mikvaot", "Niddah", "Makhshirin", "Zavim", "Tevul Yom", "Yadayim", "Oktzin"
]

with open('sefaria_index.json', 'r', encoding='utf-8') as f:
    data = json.load(f)

def get_base_texts(node):
    t = []
    if 'title' in node: t.append(node['title'])
    elif 'contents' in node:
        for c in node['contents']: t.extend(get_base_texts(c))
    return t

mishnah_raw = []
talmud_raw = []
for i in data:
    if i.get('category') == 'Mishnah': mishnah_raw = get_base_texts(i)
    if i.get('category') == 'Talmud':
        for c in i.get('contents', []):
            if c.get('category') == 'Bavli': talmud_raw = get_base_texts(c)

def norm(x):
    return x.replace("'", "").replace("Tohorot", "Tahorot").replace("Maaserot", "Maasrot")

missed = []
actual_mishnah = []
actual_talmud = []

for t in TRACTATES:
    nt = norm(t)
    
    # 1. Match Mishnah
    m_match = None
    # exact
    if f"Mishnah {nt}" in mishnah_raw: m_match = f"Mishnah {nt}"
    elif f"Mishnah {t}" in mishnah_raw: m_match = f"Mishnah {t}"
    else:
        # fuzzy
        opts = [m for m in mishnah_raw if m.startswith('Mishnah ') and nt in norm(m)]
        if opts: m_match = opts[0]
        else:
            opts = [m for m in mishnah_raw if m.startswith('Mishnah ') and nt[:4] in norm(m)]
            if opts: m_match = opts[0]
            
    # 2. Match Talmud
    t_match = None
    if t in talmud_raw: t_match = t
    elif nt in talmud_raw: t_match = nt
    else:
        opts = [tm for tm in talmud_raw if nt in norm(tm)]
        if opts: t_match = opts[0]
        else:
            opts = [tm for tm in talmud_raw if nt[:4] in norm(tm)]
            # Bavli usually doesn't append prefix, except maybe no match
            if opts: t_match = opts[0]

    actual_mishnah.append(m_match)
    actual_talmud.append(t_match)

print("Parsed M:", len([m for m in actual_mishnah if m]), "Parsed T:", len([t for t in actual_talmud if t]))

with open('final_mishnah.json', 'w') as f: json.dump([m for m in actual_mishnah if m], f)
with open('final_talmud.json', 'w') as f: json.dump([m for m in actual_talmud if m], f)

print(f"Missing M: {[t for i, t in enumerate(TRACTATES) if not actual_mishnah[i]]}")
print(f"Missing T: {[t for i, t in enumerate(TRACTATES) if not actual_talmud[i]]}")
