#!/usr/bin/env python3
"""
Cleanup script for bilingual output files:
1. Ensure blank line before and after each standalone "→" arrow
2. Remove any stray <mark> and </mark> HTML tags
"""

import os
import re
import glob

translations_dir = "/home/tuan-nguyen/.openclaw/workspace/011-so-tay-mahavihara/translation/Gemini-3-Flash"
files = sorted(glob.glob(os.path.join(translations_dir, "output-*-bilingual.md")))

stats = {"files": 0, "arrow_fixes": 0, "mark_tags_removed": 0}

for fpath in files:
    with open(fpath, "r", encoding="utf-8") as f:
        original = f.read()
    
    # Count <mark>/</mark> tags
    mark_count = len(re.findall(r'</?mark>', original))
    
    # Remove <mark> and </mark> tags
    text = re.sub(r'</?mark>', '', original)
    
    # Split into lines
    lines = text.split('\n')
    
    # Process: ensure blank lines around standalone "→"
    # A standalone "→" is a line whose stripped content is exactly "→"
    new_lines = []
    i = 0
    arrow_fixes_this_file = 0
    
    while i < len(lines):
        line = lines[i]
        stripped = line.strip()
        
        if stripped == '\u2192':  # "→" on its own
            # Check if previous line is empty (or we're at start of file)
            prev_empty = (len(new_lines) == 0 or new_lines[-1].strip() == '')
            # Check if next line is empty (or we're at end of file)
            next_empty = (i + 1 >= len(lines) or lines[i + 1].strip() == '')
            
            if not prev_empty:
                new_lines.append('')
                arrow_fixes_this_file += 1
            
            # Add the → line itself
            new_lines.append(line)
            
            if not next_empty:
                new_lines.append('')
                arrow_fixes_this_file += 1
        else:
            new_lines.append(line)
        
        i += 1
    
    result = '\n'.join(new_lines)
    
    if result != original:
        with open(fpath, "w", encoding="utf-8") as f:
            f.write(result)
        stats["files"] += 1
        stats["arrow_fixes"] += arrow_fixes_this_file
        stats["mark_tags_removed"] += mark_count
        print(f"  ✓ {os.path.basename(fpath)}: {arrow_fixes_this_file} arrow fixes, {mark_count} <mark> tags removed")
    else:
        print(f"  - {os.path.basename(fpath)}: no changes needed")

print(f"\nSummary: {stats['files']} files modified, {stats['arrow_fixes']} arrow fixes applied, {stats['mark_tags_removed']} <mark> tags removed")
