#!/usr/bin/env python3
"""
Reformat -edited markdown files for readability.
Splits long paragraphs at sentence boundaries and section markers.
Token-efficient: rule-based script, no LLM.
"""

import re
import os
import sys

# Patterns for splitting long lines
SENTENCE_END = re.compile(r'။(?!\s|$)')  # ။ not followed by space or end
ITEM_START = re.compile(r'(?<=[။\s])(\d+[\.\)]?\s*[က-အ])')  # ၁။ text
SECTION_MARKER = re.compile(r'\[ဆောင်\]')
PALI_BREAK = re.compile(r'(?<=[။\s])(?=[က-အ]{1,3}\s*[-–])')  # word - gloss

MAX_LINE_LEN = 800  # split if longer


def reformat_text(content: str) -> str:
    """Apply formatting rules to improve readability."""
    lines = content.split('\n')
    output = []

    for line in lines:
        stripped = line.strip()

        # Preserve blank lines, headers, separators, table rows
        if not stripped or stripped.startswith('#') or stripped == '---' \
           or stripped.startswith('|') or stripped.startswith('>') \
           or stripped.startswith('<!--'):
            output.append(line)
            continue

        # Split long lines at sentence boundaries
        if len(stripped) > MAX_LINE_LEN:
            segments = re.split(r'(?<=။)', stripped)
            for seg in segments:
                if seg.strip():
                    output.append(seg.strip())
        else:
            output.append(line)

    # Second pass: add blank lines before ### headers
    result = '\n'.join(output)
    result = re.sub(r'([^\n])\n(###+\s)', r'\1\n\n\2', result)
    result = re.sub(r'([^\n])\n(\[ဆောင်\])', r'\1\n\n\2', result)

    return result


def process_directory(indir: str, outdir: str, glob: str = '*-edited.md'):
    """Process all matching files."""
    os.makedirs(outdir, exist_ok=True)
    import fnmatch
    files = sorted([f for f in os.listdir(indir) if fnmatch.fnmatch(f, glob)])

    for f in files:
        inpath = os.path.join(indir, f)
        outpath = os.path.join(outdir, f)
        with open(inpath, 'r', encoding='utf-8') as fh:
            content = fh.read()
        formatted = reformat_text(content)
        with open(outpath, 'w', encoding='utf-8') as fh:
            fh.write(formatted)
        orig_lines = content.count('\n')
        new_lines = formatted.count('\n')
        print(f"  {f}: {orig_lines} → {new_lines} lines")

    print(f"\nDone: {len(files)} files → {outdir}/")


if __name__ == '__main__':
    import argparse
    ap = argparse.ArgumentParser()
    ap.add_argument('indir', help='Input directory')
    ap.add_argument('outdir', help='Output directory')
    ap.add_argument('--glob', default='*-edited.md', help='File pattern')
    args = ap.parse_args()
    process_directory(args.indir, args.outdir, args.glob)
