#!/usr/bin/env python3
"""
pre-process.py — Xử lý cơ học văn bản Myanmar trước khi gửi cho Editor AI.
Tất cả các sửa lỗi ở đây là mechanical (regex-based), không cần AI.
Tiết kiệm 50-60% token so với việc để Editor AI tự sửa.

Usage:
  python3 pre-process.py <extracted_input.md> <merged_output.md>
"""

import sys, re, json, os

PATTERNS_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), 
                             '..', 'data', 'ocr-mechanical-patterns.json')
if not os.path.exists(PATTERNS_PATH):
    PATTERNS_PATH = '/home/tuan-nguyen/.openclaw/workspace/data/ocr-mechanical-patterns.json'

with open(PATTERNS_PATH) as f:
    MECHANICAL_PATTERNS = json.load(f)

HARDCODED = [
    ('တို ့', 'တို့'), ('သော ည်', 'သောည်'), ('သော် လည်း', 'သော်လည်း'),
    ('\u200b', ''), ('\u200c', ''), ('\u200d', ''),
    ('၁ဝ', '၁၀'), ('၂ဝ', '၂၀'), ('၃ဝ', '၃၀'),
    ('၄ဝ', '၄၀'), ('၅ဝ', '၅၀'), ('၆ဝ', '၆၀'),
    ('၇ဝ', '၇၀'), ('၈ဝ', '၈၀'), ('၉ဝ', '၉၀'),
    ('** ။', '။ **'),
]


def merge_lines(text):
    """
    Gộp dòng: nối dòng không kết thúc bằng dấu kết thúc câu.
    Lưu ý: း (visarga) KHÔNG phải dấu kết thúc câu.
    """
    lines = text.split('\n')
    result = []
    buffer = ''
    SENTENCE_END = '။၊၏၌'

    for line in lines:
        s = line.strip()
        if not s:
            if buffer: result.append(buffer); buffer = ''
            result.append(''); continue
        if s.startswith('## PAGE') or s.startswith('#'):
            if buffer: result.append(buffer); buffer = ''
            result.append(line); continue
        if s.startswith('---') or s.startswith('***'):
            if buffer: result.append(buffer); buffer = ''
            result.append(line); continue

        ends_sentence = bool(s and s[-1] in SENTENCE_END)
        if s.endswith('**') or s.endswith('*'):
            ends_sentence = True

        if buffer:
            buffer += ' ' + s
            if ends_sentence:
                result.append(buffer)
                buffer = ''
        elif not ends_sentence:
            buffer = s
        else:
            result.append(line)

    if buffer: result.append(buffer)
    return '\n'.join(result)


def _unused_add_paragraph_breaks(lines):
    """Add blank lines between logical paragraphs for readability."""
    out = []
    prev = ''
    for line in lines:
        s = line.strip()
        if not s or s.startswith('## PAGE') or s.startswith('#'):
            out.append(line); prev = s; continue

        need_break = False

        # Rule 1: After closing bracket ] → blank line
        if prev and prev.rstrip().endswith(']') and not s.startswith('['):
            need_break = True

        # Rule 2: Before Myanmar numbered sections (e.g., ၶ။)
        if re.match(r'^[၀-၉]+\s*။', s):
            need_break = True

        # Rule 3: Before bold numbered verse (e.g., **၇၆။**)
        if re.match(r'^\*\*[၀-၉]+\s*။\*\*', s):
            need_break = True

        # Rule 4: After ။ ending + next looks like new section
        if prev and prev.rstrip().endswith('။') and re.match(r'^[၀-၉(\[]', s):
            need_break = True

        if need_break and out and out[-1].strip():
            out.append('')
        out.append(line)
        prev = s
    return '\n'.join(out)


def apply_mechanical_fixes(text):
    """Apply mechanical OCR fixes from DB patterns + hardcoded."""
    fixed = 0
    result = text
    for find, replace in HARDCODED:
        if find in result:
            count = result.count(find)
            result = result.replace(find, replace)
            fixed += count
    for pat in MECHANICAL_PATTERNS:
        find, replace = pat['find'], pat['replace']
        if not find or not replace or find == replace: continue
        if find in result:
            count = result.count(find)
            result = result.replace(find, replace)
            fixed += count
    return result, fixed


def pre_process(input_path, output_path):
    with open(input_path, 'r', encoding='utf-8') as f:
        raw = f.read()
    src_chars = len(raw)
    merged = merge_lines(raw)
    cleaned, fix_count = apply_mechanical_fixes(merged)
    with open(output_path, 'w', encoding='utf-8') as f:
        f.write(cleaned)
    out_chars = len(cleaned)
    ratio = (out_chars / src_chars * 100) if src_chars > 0 else 0
    print(f'Pre-process: {src_chars} -> {out_chars} chars ({ratio:.0f}%)')
    print(f'Lines: merged, Mechanical fixes: {fix_count}')
    return src_chars, out_chars, fix_count


if __name__ == '__main__':
    if len(sys.argv) < 3:
        print(f'Usage: {sys.argv[0]} <input.md> <output.md>')
        sys.exit(1)
    pre_process(sys.argv[1], sys.argv[2])
