"""Debug: show actual rc values for the title block in real translation."""
import sys
sys.path.insert(0, '.')
import importlib.util
import fitz
from pathlib import Path

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)

# Patch _try_insert to log rc values
original_try_insert = pdf_mod.PDFTranslator._try_insert
TITLES_TO_TRACE = {"Spécification technique : Office Translator v3.0", "Technical Specification: Office Translator v3.0"}

def debug_try_insert(self, page, rect, text, fontsize, fontname, fontfile, color, align):
    rc = page.insert_textbox(
        rect, text,
        fontsize=fontsize,
        fontname=fontname,
        fontfile=fontfile,
        color=color,
        align=align,
        overlay=True,
    )
    # Only trace the title
    is_title = any(t in text for t in ["Spécification technique", "Technical Specification"])
    if is_title:
        print(f"  [TITLE] size={fontsize:.1f} rect={rect.width:.0f}x{rect.height:.0f} -> rc={rc}", flush=True)
    return rc

pdf_mod.PDFTranslator._try_insert = debug_try_insert

PDFTranslator = pdf_mod.PDFTranslator
PDFTranslator().translate_file(
    Path('sample_files/test_corpus/test_pdf.pdf'),
    Path('sample_files/test_corpus/test_pdf_translated.pdf'),
    target_language='fr',
    source_language='en',
)
