#!/usr/bin/env python3
"""Fix OCR errors + Bold Pali + Markdown for output-11-to-15.md — Bước 4 (clean)"""
import re

INFILE = "edited/gemini-flash/output-11-to-15.md"
OUTFILE = "edited/gemini-flash/output-11-to-15.md"

with open(INFILE, encoding="utf-8") as f:
    text = f.read()

# === Step 1: Fix all OCR errors ===

# Pāli word errors (checklist B/G)
text = text.replace("သင်္ချီတိ", "သင်္ကဿာတိ")
text = text.replace("ဣသိနော့", "ဣသိနော")
text = text.replace("နှိရုတ္တိ", "နိရုတ္တိ")
text = text.replace("အတ္ထသဒ္ဓါ", "အတ္ထသဒ္ဒါ")
text = text.replace("သဝတ္ထရသေ့", "သာဝတ္ထရသေ့")
text = text.replace("ပစ္စသောသုံ", "ပစ္စာသုံ")
text = text.replace("ပစ္စာသူန္တိ", "ပစ္စာသုံတိ")
text = text.replace("ပဋိအသောသုံ", "ပဋိသောသုံ")
text = text.replace("အဝေါ်စ", "အဝေါစ")
text = text.replace("မိန်  ့", "မိန့်")

# Space-in-word errors
text = text.replace("အနာ ထပိဏ်", "အနာထပိဏ်")
text = text.replace("သင်္ခ ဂတော", "သင်္ချာဂတော")
text = text.replace("မိန် ခေါ်", "မိန့်ခေါ်")
text = text.replace("အာမန္တေ သိ", "အာမန္တေသိ")
text = text.replace("ပဒပူ ရဏ", "ပဒပူရဏ")
text = text.replace("စတု သမုဋ္ဌာနိက", "စတုသမုဋ္ဌာနိက")
text = text.replace("ပရိယာ ဒါယ", "ပရိယာဒါယ")
text = text.replace("တုံ့ ပြန်", "တုံ့ပြန်")
text = text.replace("အာမန္တ နံ", "အာမန္တနံ")
text = text.replace("ဥပမာန ဝါကျ", "ဥပမာနဝါကျ")
text = text.replace("ဝိစ္ဆိဒ္ဓိ တွာ", "ဝိစ္ဆိဒ္ဓိတွာ")
text = text.replace("ဆွဲယူ မပေး", "ဆွဲယူမပေး")
text = text.replace("ဧဝံ မေ သုတံသို့", "ဧဝံ မေ သုတံ သို့")
text = text.replace("နသမနုပဿာမိ အမြင်", "န သမနုပဿာမိ=အမြင်")
text = text.replace("အကျေ ကြံ", "အကျေအကြံ")
text = text.replace("နေတော် သာဝတ္ထိ", "နေတော်မူ သာဝတ္ထိ")
text = text.replace("လေးပါးဣရိယာ ညီညာ", "လေးပါးဣရိယာပထ ညီညာ")
text = text.replace("ဣရိယာ ပုတ်", "ဣရိယာပုထ်")
text = text.replace("ကာကန္ဒီ။ပ။", "ကာကန္ဒီ။ ပ။")
text = text.replace("ဖြစ် စေအပ်", "ဖြစ်စေအပ်")
text = text.replace("ပေါင်း စပ်", "ပေါင်းစပ်")
text = text.replace("အားလုံးစုံ အကုန် ရှိရာ", "အားလုံးစုံအကုန်ရှိရာ")
text = text.replace("ဝိဂြိုဟ်နက် ပေး", "ဝိဂြိုဟ်နက်ပေး")

# Checklist #94: space before ့
text = text.replace("ရဟန်းတို ့", "ရဟန်းတို့")
text = text.replace("ဤသို ့", "ဤသို့")
text = text.replace("အဘယ်သို ့", "အဘယ်သို့")
text = text.replace("ကဲ့သို ့", "ကဲ့သို့")
text = text.replace("ပုဒ်တို ့", "ပုဒ်တို့")
text = text.replace("တို ့", "တို့")
text = text.replace("သို ့", "သို့")

# Number/format errors
text = text.replace("600 ဝိဟရတိ", "၁။ ဝိဟရတိ")
text = text.replace("{ဧကံ", "(ဧကံ")
text = text.replace("တတြခေါ်၌", "တတြ၌")
text = text.replace("သူတပါး", "သူတစ်ပါး")
text = text.replace("ဧကရူပမ္ပိ", "ဧကရူပမ္ပိ")

# Stray artifacts
text = text.replace("* N\n\n", "\n")

# Stray quotes
text = text.replace("ရူပါရုံသည်\"", "ရူပါရုံသည်")
text = text.replace("သာဝတ္ထု)", "သာဝတ္ထု\")")
text = text.replace("သာဝတ္ထိ\"ဟု", "သာဝတ္ထိ\" ဟု")
text = text.replace("ဣတ္ထိရူပံ\'ဟူ", "ဣတ္ထိရူပံ\' ဟူ")

# Extra spaces
text = text.replace("ဆက်စပ်  ့", "ဆက်စပ်၍")

# Paragraph breaks
text = text.replace("သည်။] ဧဝံ", "သည်။]\n\nဧဝံ")

# Zero-width space fix
text = text.replace("ဌ၊ ဋီဖွင့်", "ဌ၊ ဋီဖွင့်")

# === Step 2: Bold Pāli terms before = (targeted, manual approach) ===
# Bold ONLY specific Pāli terms that appear before = in nissaya format
# Use exact text replacement to avoid over-matching

# Individual Pāli terms = Myanmar gloss
bold_mappings = [
    # Short Pāli terms
    ("ဝိဟရတိ=", "**ဝိဟရတိ**="),
    ("အနာထပိဏ္ဍိကဿ=", "**အနာထပိဏ္ဍိကဿ**="),
    ("အာရာမေ=", "**အာရာမေ**="),
    ("ဘဂဝါ=", "**ဘဂဝါ**="),
    ("ဘိက္ခု=", "**ဘိက္ခု**="),
    ("ဘိက္ခဝေါ=", "**ဘိက္ခဝေါ**="),
    ("ဣတိ=", "**ဣတိ**="),
    ("အာမန္တေသိ=", "**အာမန္တေသိ**="),
    ("တတြ=", "**တတြ**="),
    ("ဝါ=", "**ဝါ**="),
    ("ဘဒန္တေ=", "**ဘဒန္တေ**="),
    ("ဘဂဝတော=", "**ဘဂဝတော**="),
    ("အာမန္တနံ=", "**အာမန္တနံ**="),
    ("ပစ္စာသုံ=", "**ပစ္စာသုံ**="),
    ("ပစ္စေဿာသုံ=", "**ပစ္စေဿာသုံ**="),
    ("ပရိယာဒါယ=", "**ပရိယာဒါယ**="),
    ("တိဋ္ဌတိ=", "**တိဋ္ဌတိ**="),
    ("ဧဝံ=", "**ဧဝံ**="),
    ("ယံ=", "**ယံ**="),
    ("ပုရိသဿ=", "**ပုရိသဿ**="),
    ("စိတ္တံ=", "**စိတ္တံ**="),
    ("အညံ=", "**အညံ**="),
    ("အဟံ=", "**အဟံ**="),
    ("န သမနုပဿာမိ=", "**န သမနုပဿာမိ**="),
    ("အဟောသိ=", "**အဟောသိ**="),
    ("ပဌမံ=", "**ပဌမံ**="),
    ("ဇိတံ=", "**ဇိတံ**="),
    ("အဝေါစ=", "**အဝေါစ**="),
    ("ဧတဒဝေါစ=", "**ဧတဒဝေါစ**="),
    ("သုတံ=", "**သုတံ**="),
    # Pāli phrases (longer, quoted)
    ("သဗ္ဗ ဧတ္ထ အတ္ထိတိ သာဝတ္ထိ=", "**သဗ္ဗ ဧတ္ထ အတ္ထိတိ သာဝတ္ထိ**="),
    ("သဗ္ဗ+အတ္ထ=", "**သဗ္ဗ+အတ္ထ**="),
    ("နိစ္စကာလံ အနာထာနံ ပိဏ္ဍမဒါသိ,", "**နိစ္စကာလံ အနာထာနံ ပိဏ္ဍမဒါသိ**,"),
    ("အနာထာနံ ပိဏ္ဍ ဒဒါတီတိ အနာထပိဏ္ဍိကော=", "**အနာထာနံ ပိဏ္ဍ ဒဒါတီတိ အနာထပိဏ္ဍိကော**="),
    ("ဝိစ္ဆိဒ္ဓိတွာ ဟရတိ ပဝတ္တေတိ=", "**ဝိစ္ဆိဒ္ဓိတွာ ဟရတိ ပဝတ္တေတိ**="),
]

for old, new in bold_mappings:
    text = text.replace(old, new)

# === Step 3: Bold Pāli phrases in parentheses/quotes that don't have = ===
# These are Pāli quotes within the text
quote_bold = [
    ("(သဗ္ဗကို သာဝပြု၍)", "_(သဗ္ဗကို သာဝပြု၍)_"),
]
for old, new in quote_bold:
    text = text.replace(old, new)

# === Step 4: Paragraph spacing and structure ===
# Blank line before sutta numbers
text = re.sub(r'\n(၁။\s)', r'\n\n\1', text)
text = re.sub(r'\n(၂။\s)', r'\n\n\1', text)

# Blank line after ## PAGE X headers (ensure exactly one blank line)
text = re.sub(r'(## PAGE \d+)\n+', r'\1\n\n', text)

# Fix double+ blank lines
while '\n\n\n' in text:
    text = text.replace('\n\n\n', '\n\n')

with open(OUTFILE, "w", encoding="utf-8") as f:
    f.write(text)

print("Fix complete")
print("Chars: " + str(len(text)))
