"""Confirm the seam fix end-to-end with REAL Camelot re-extraction.

The June ("3 Months Ended June 30, 2025") and March tables stack on page 2.
declash placed their shared seam at top-origin y=249.6pt, ABOVE June's own
"Visa Inc." total row (center 259.9pt), so the row fell into March's box.

The natural seam is the blank band between June's total row (bottom 264.3pt)
and the March title (top 274.9pt): midpoint ~269.6pt. This re-extracts June
and March with the original seam vs the whitespace seam and prints the row
each box yields, with NO change to any prod module.
"""

from __future__ import annotations

from quber.core.extractors.camelot.acquire import grid_to_markdown
from quber.core.extractors.camelot.correspondence.geometry import norm_bbox_to_table_area
from quber.core.extractors.camelot.correspondence.recovery import camelot_targeted

SRC = ".cache/s3/qubera-docs/tmus/Q1FY26-Visa-Operational-Performance-Data.pdf"
PAGE = 2
PAGE_W, PAGE_H = 612.0, 792.0

# Shared horizontal extent of the page-2 tables (from the prod som_region).
X0, X1 = 0.050689634933970336, 0.9470258725234886
JUNE_TOP = 0.039069869301535866  # June box top (unchanged)
MARCH_BOTTOM = 0.6341182824337122  # March box bottom (unchanged)

SEAM_ORIG = 249.6 / PAGE_H  # declash seam: above June's total row
SEAM_FIX = 269.6 / PAGE_H  # whitespace band between total row and March title


def tail(region, ordinal: int, n: int = 3) -> None:
    area = norm_bbox_to_table_area(region, PAGE_W, PAGE_H)
    cand = camelot_targeted(SRC, PAGE, area, ordinal)
    if cand is None:
        print("   <no grid>")
        return
    rows = grid_to_markdown(cand.cells).splitlines()
    for r in rows[-n:]:
        print("   ", r)


def head(region, ordinal: int, n: int = 4) -> None:
    area = norm_bbox_to_table_area(region, PAGE_W, PAGE_H)
    cand = camelot_targeted(SRC, PAGE, area, ordinal)
    if cand is None:
        print("   <no grid>")
        return
    rows = grid_to_markdown(cand.cells).splitlines()
    for r in rows[:n]:
        print("   ", r)


print("JUNE box bottom = ORIGINAL seam 249.6pt  (last rows):")
tail((X0, JUNE_TOP, X1, SEAM_ORIG), 1)
print("\nJUNE box bottom = FIXED seam 269.6pt  (last rows):")
tail((X0, JUNE_TOP, X1, SEAM_FIX), 1)

print("\nMARCH box top = ORIGINAL seam 249.6pt  (first rows):")
head((X0, SEAM_ORIG, X1, MARCH_BOTTOM), 2)
print("\nMARCH box top = FIXED seam 269.6pt  (first rows):")
head((X0, SEAM_FIX, X1, MARCH_BOTTOM), 2)
