"""Test if merged blocks (TOC) fit in expanded_v."""
import sys
sys.path.insert(0, '.')
import importlib.util
import fitz

spec = importlib.util.spec_from_file_location('pdf_mod', 'translators/pdf_translator.py')
pdf_mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(pdf_mod)

doc = fitz.open('sample_files/test_corpus/test_pdf.pdf')
page = doc[0]

# Extract and merge
raw_blocks = pdf_mod.PDFTranslator()._extract_text_blocks(page)
merged = pdf_mod.PDFTranslator()._merge_adjacent_blocks(raw_blocks, page.rect)

# Find the TOC block (block 3 in our trace)
toc = merged[3]
print(f"TOC bbox: {toc['bbox']}")
print(f"TOC font_size: {toc['font_size']}")
print(f"TOC text length: {len(toc['text'])}")
print(f"TOC text preview: {toc['text'][:80]!r}")

# Redact
toc_rect = fitz.Rect(toc['bbox'])
page.add_redact_annot(toc_rect, fill=(1, 1, 1))
page.apply_redactions(images=fitz.PDF_REDACT_IMAGE_NONE)

# Test insert with expanded_v
margin = 18
page_rect = page.rect
expanded_h = fitz.Rect(
    max(toc_rect.x0, page_rect.x0 + margin),
    toc_rect.y0,
    page_rect.x1 - margin,
    toc_rect.y1,
)
max_expand_y = min(
    page_rect.y1 - margin - toc_rect.y1,
    toc_rect.height * 3.0,
)
expanded_v = fitz.Rect(
    expanded_h.x0, expanded_h.y0, expanded_h.x1,
    expanded_h.y1 + max_expand_y,
)
print(f"expanded_v: {expanded_v.width}x{expanded_v.height}")

# Translate the TOC text
french_toc = """1. Introduction
.......... 1
2. Installation et configuration
.......... 2
3. Moteur de traduction
.......... 3
4. Préservation des formats
.......... 4
5. Assurance qualité
.......... 5
6. Performances et mise à l'échelle
.......... 6
7. Référence API
.......... 7
8. Dépannage
.......... 8"""

# Test on original
rc1 = page.insert_textbox(
    toc_rect, french_toc,
    fontsize=12,
    fontfile='C:/Windows/Fonts/arial.ttf',
    align=fitz.TEXT_ALIGN_LEFT,
    overlay=True,
)
print(f"Original rect: rc={rc1}")

# Test on expanded_v
rc2 = page.insert_textbox(
    expanded_v, french_toc,
    fontsize=12,
    fontfile='C:/Windows/Fonts/arial.ttf',
    align=fitz.TEXT_ALIGN_LEFT,
    overlay=True,
)
print(f"expanded_v: rc={rc2}")

doc.close()
