#!/usr/bin/env python3
"""
Clean leftover OCR headers/page numbers from Sadi final files (096+).

Patterns removed:
  1. Standalone Myanmar page numbers (2-4 digits) including ဂ-prefix
  2. Chapter title headers: ခန္ဓကဝတ်ကို သင်ပြခဏ်း
  3. Footer/printer marks
  4. Heading-style page markers (#/## with page numbers)
  5. Page headers with inline section titles
  6. Extra: ခေါင်းရိတ်ခဏ်း, ေါင်းရိတ်ပြီးနောက် (for earlier files)
"""

import os
import re
import shutil
from pathlib import Path

FINAL_DIR = Path("/home/tuan-nguyen/.openclaw/workspace/Chuan Muc Sadi/extracted/final")
BACKUP_DIR = FINAL_DIR / "backup"

# --- File filter: Sadi-096+ ---
def is_target_file(name: str) -> bool:
    """Return True for Sadi-096-100 and onward."""
    m = re.match(r"Sadi-(\d+)-(\d+).*\.md$", name)
    if not m:
        return False
    start = int(m.group(1))
    return start >= 96

# --- Patterns ---
MYANMAR_NUMERALS = "[၀-၉ဝ]"  # ဝ (wa letter) often OCR'd as ၀ (zero digit)

# Pattern 1: Standalone Myanmar page numbers (1-4 digits), with optional letter prefix (e.g., ဂ၁, ၈၇, ၁၀၂)
RE_STANDALONE_PAGE_NUM = re.compile(
    rf"^(?:[က-အ])?{MYANMAR_NUMERALS}{{1,4}}$"
)

# Pattern 1b: Arabic numeral "1" standalone (page 1 marker)
RE_ARABIC_1 = re.compile(r"^1$")

# Pattern 2: Chapter title header
RE_CHAPTER_HEADER = re.compile(
    r"^\s*ခန္ဓကဝတ်ကို\s*သင်ပြခဏ်း\s*$"
)

# Pattern 3: Footer / printer marks
RE_FOOTER = re.compile(
    r"^[၀-၉]+--[က-အ-၀-၉]+$"  # e.g., ၆--ပ-မ-သ-စ
)
RE_PRINTER_MARK = re.compile(r"^EJ$")

# Pattern 4a: Heading page markers like "# ၁ဝ၆ - ရုပ်ပုံ ရှင်ကျင့်ဝတ်"
RE_HEADING_PAGE_MARKER = re.compile(
    rf"^#+\s+{MYANMAR_NUMERALS}{{2,4}}\s*[-–—]\s*"
)

# Pattern 4b: Heading with just page number like "## ၁၃၁"
RE_HEADING_PAGE_NUM_ONLY = re.compile(
    rf"^#+\s+{MYANMAR_NUMERALS}{{2,4}}\s*$"
)

# Pattern 5a: "ခန္ဓကဝတ် ပါဌ်အနက်            ၂၂၃" (page header with right-aligned number)
RE_PAGE_HEADER_WITH_NUM = re.compile(
    rf"^\s*ခန္ဓကဝတ်\s+ပါ[ဌဠ]်အနက်\s+{MYANMAR_NUMERALS}{{2,4}}\s*$"
)

# Pattern 5b: "{page_num}               ရုပ်ပုံ ရှင်ကျင့်ဝတ်" (page header)
RE_PAGE_NUM_WITH_TITLE = re.compile(
    rf"^\s*{MYANMAR_NUMERALS}{{2,4}}\s{{2,}}ရုပ်ပုံ\s*ရှင်ကျင့်ဝတ်\s*$"
)

# Pattern 5c: "၂၂၂             ရုပ်ပုံ ရှင်ကျင့်ဝတ်" (number + many spaces + title)
# Same as 5b but with variable spacing

# Pattern 6a: Extra headers
RE_EXTRA_HEADERS = re.compile(
    r"^\s*(?:###\s+)?[၀-၉]+-?ခေါင်းရိတ်ခဏ်း\s*$"
)
RE_EXTRA_HEADERS2 = re.compile(
    r"^\s*ေ?ါင်းရိတ်ပြီးနောက်\s*$"
)

def is_line_to_remove(line: str) -> bool:
    """Return True if the line should be removed (leftover OCR header/footer)."""
    stripped = line.strip()
    if not stripped:
        return False

    return bool(
        RE_STANDALONE_PAGE_NUM.match(stripped) or
        RE_ARABIC_1.match(stripped) or
        RE_CHAPTER_HEADER.match(stripped) or
        RE_FOOTER.match(stripped) or
        RE_PRINTER_MARK.match(stripped) or
        RE_HEADING_PAGE_MARKER.match(stripped) or
        RE_HEADING_PAGE_NUM_ONLY.match(stripped) or
        RE_PAGE_HEADER_WITH_NUM.match(stripped) or
        RE_PAGE_NUM_WITH_TITLE.match(stripped) or
        RE_EXTRA_HEADERS.match(stripped) or
        RE_EXTRA_HEADERS2.match(stripped)
    )


def clean_file(filepath: Path) -> tuple[int, int]:
    """
    Clean a single file. Returns (lines_removed, total_lines).
    """
    with open(filepath, "r", encoding="utf-8") as f:
        lines = f.readlines()

    original_count = len(lines)
    cleaned = []
    removed = 0

    for line in lines:
        if is_line_to_remove(line):
            removed += 1
            continue
        cleaned.append(line)

    # Only write if changes were made
    if removed > 0:
        with open(filepath, "w", encoding="utf-8") as f:
            f.writelines(cleaned)

    return removed, original_count


def main():
    # Ensure backup directory exists
    BACKUP_DIR.mkdir(exist_ok=True)

    # Find target files
    files = sorted(
        [f for f in FINAL_DIR.glob("Sadi-*.md") if is_target_file(f.name)],
        key=lambda f: f.name,
    )

    if not files:
        print("No target files found (Sadi-096+).")
        return

    print(f"Found {len(files)} target files.\n")

    total_removed = 0
    total_lines = 0

    for fpath in files:
        # Backup
        backup_path = BACKUP_DIR / fpath.name
        shutil.copy2(fpath, backup_path)

        # Clean
        removed, orig = clean_file(fpath)
        total_removed += removed
        total_lines += orig

        status = f"✅ {removed} dòng bị xóa" if removed > 0 else "⬜ Sạch"
        print(f"  {fpath.name:40s} {status} ({orig} → {orig - removed} dòng)")

    print(f"\n{'='*60}")
    print(f"Tổng: {total_removed} dòng đã xóa / {total_lines} dòng ({len(files)} file)")
    print(f"Backup: {BACKUP_DIR}")
    print(f"{'='*60}")


if __name__ == "__main__":
    main()
