"""Reproduce the image issues: title overflow + white box on colored block."""
import sys
sys.path.insert(0, '.')
import fitz
from pathlib import Path

# Open the translated PDF from the user
src = Path('sample_files/test_corpus/test_pdf_translated (1).pdf')
doc = fitz.open(str(src))

# Page 1
p = doc[0]
print("=== Page 1 text blocks ===")
for i, b in enumerate(p.get_text("dict").get("blocks", [])):
    bbox = b.get("bbox", (0, 0, 0, 0))
    if b.get("type") != 0:  # not text
        continue
    text_lines = []
    for line in b.get("lines", []):
        for span in line.get("spans", []):
            text_lines.append(span.get("text", ""))
    text = " ".join(text_lines)[:60]
    print(f"  [{i}] bbox=({bbox[0]:.0f},{bbox[1]:.0f},{bbox[2]:.0f},{bbox[3]:.0f}) text={text!r}")

print()
print("=== Page 1 drawings ===")
for i, d in enumerate(p.get_drawings()):
    rect = d.get("rect")
    fill = d.get("fill")
    color = d.get("color")
    print(f"  [{i}] rect=({rect.x0:.0f},{rect.y0:.0f},{rect.x1:.0f},{rect.y1:.0f}) fill={fill} color={color}")

# Open the original to compare
orig = fitz.open('sample_files/test_corpus/test_pdf.pdf')
p_orig = orig[0]
print()
print("=== Original Page 1 drawings (for comparison) ===")
for i, d in enumerate(p_orig.get_drawings()):
    rect = d.get("rect")
    fill = d.get("fill")
    color = d.get("color")
    print(f"  [{i}] rect=({rect.x0:.0f},{rect.y0:.0f},{rect.x1:.0f},{rect.y1:.0f}) fill={fill} color={color}")

doc.close()
orig.close()
