"""Test if redaction affects insert_textbox behavior."""
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]

# Get the title block info
raw_blocks = pdf_mod.PDFTranslator()._extract_text_blocks(page)
title = raw_blocks[0]
print(f"Title bbox: {title['bbox']}")
print(f"Title text: {title['text']!r}")

# Test 1: insert_textbox on fresh page
french = "Spécification technique : Office Translator v3.0"
rect = fitz.Rect(title['bbox'])
rc1 = page.insert_textbox(
    rect, french,
    fontsize=22,
    fontfile='C:/Windows/Fonts/arial.ttf',
    color=(0.1, 0.2, 0.5),
    align=fitz.TEXT_ALIGN_LEFT,
    overlay=True,
)
print(f"Fresh page: rc={rc1} (negative = overflow, positive = fit)")

# Test 2: Apply redaction, then insert_textbox
page.add_redact_annot(rect, fill=(1, 1, 1))
page.apply_redactions(images=fitz.PDF_REDACT_IMAGE_NONE)

rc2 = page.insert_textbox(
    rect, french,
    fontsize=22,
    fontfile='C:/Windows/Fonts/arial.ttf',
    color=(0.1, 0.2, 0.5),
    align=fitz.TEXT_ALIGN_LEFT,
    overlay=True,
)
print(f"After redaction: rc={rc2}")

# Test 3: With expanded_v
expanded_v = fitz.Rect(rect.x0, rect.y0, rect.x1, rect.y1 + rect.height * 3)
rc3 = page.insert_textbox(
    expanded_v, french,
    fontsize=22,
    fontfile='C:/Windows/Fonts/arial.ttf',
    color=(0.1, 0.2, 0.5),
    align=fitz.TEXT_ALIGN_LEFT,
    overlay=True,
)
print(f"After redaction, expanded_v ({expanded_v.width}x{expanded_v.height}): rc={rc3}")

doc.close()
