#!/usr/bin/env python3
"""
preprocess-for-translation.py — Tách source thành các "đoạn" (section), thêm `---` separator.

Section boundary detection (đa dạng format):
  1. **Standalone bold line** (toàn dòng là **...**)
  2. ### Header
  3. ## Header (không phải ## PAGE)

Logic:
  - Gom các dòng boundary liên tiếp thành 1 group
  - Chèn `---` trước mỗi group boundary (trừ đầu file)
  - `## PAGE X` giữ nguyên làm context marker, KHÔNG tính là section boundary

USAGE:
  cd /home/tuan-nguyen/.openclaw/workspace/011-so-tay-mahavihara
  python3 translation/Gemini-3-Flash/preprocess-for-translation.py
"""

import os, sys, re, glob

SRC_DIR = "edited/gemini-flash"
OUT_DIR = "translation/Gemini-3-Flash/split"


def is_section_boundary(line: str) -> bool:
    """True if this line starts a new section."""
    s = line.strip()
    if not s:
        return False

    # 1. Standalone bold: **...** (entire line in bold)
    if re.match(r'^\*\*.+\*\*$', s):
        return True

    # 2. Markdown headers (##, ###, ####, #####) — but NOT ## PAGE X
    if re.match(r'^#{2,}\s+', s) and not re.match(r'^##\s+PAGE\s+\d+', s):
        return True

    return False


def preprocess_file(src_path: str, out_path: str) -> tuple[int, int]:
    """Preprocess one source file. Returns (num_sections, num_separators)."""
    with open(src_path) as f:
        content = f.read()

    lines = content.split('\n')

    # Step 1: Find all section boundary line indices
    boundary_indices = []
    for i, line in enumerate(lines):
        if is_section_boundary(line):
            boundary_indices.append(i)

    # Step 2: Group consecutive boundary lines
    # (e.g., consecutive Pali verses are one logical section)
    boundary_groups = []
    if boundary_indices:
        group = [boundary_indices[0]]
        for idx in boundary_indices[1:]:
            if idx == group[-1] + 1:
                # Consecutive boundary → same group
                group.append(idx)
            else:
                boundary_groups.append(group)
                group = [idx]
        boundary_groups.append(group)

    # Step 3: First line of each group = actual section start
    section_starts = {g[0] for g in boundary_groups}

    # Step 4: Build output with --- before each section start
    result_lines = []
    section_count = 0

    for i, line in enumerate(lines):
        if i in section_starts:
            if section_count > 0:
                # Add --- between sections
                result_lines.append('---')
                result_lines.append('')
            section_count += 1

        result_lines.append(line)

    result = '\n'.join(result_lines)
    sep_count = result.count('\n---\n')

    os.makedirs(os.path.dirname(out_path), exist_ok=True)
    with open(out_path, 'w') as f:
        f.write(result)

    return section_count, sep_count


def main():
    os.makedirs(OUT_DIR, exist_ok=True)
    files = sorted(glob.glob(f"{SRC_DIR}/output-*.md"))

    if not files:
        print(f"ERROR: No source files in {SRC_DIR}/")
        sys.exit(1)

    print(f"Pre-processing {len(files)} files: section-based splitting")
    print(f"  Source: {SRC_DIR}/")
    print(f"  Output: {OUT_DIR}/")
    print()

    total_sections = 0
    for f in files:
        bn = os.path.basename(f)
        out_path = os.path.join(OUT_DIR, bn)
        sections, seps = preprocess_file(f, out_path)
        total_sections += sections
        print(f"  {bn:30s}  {sections:3d} sections, {seps:2d} separators")

    print(f"\n  TOTAL: {total_sections} sections across {len(files)} files")
    print(f"  Output → {OUT_DIR}/")
    print("Done!")


if __name__ == "__main__":
    main()
