#!/usr/bin/env python3
# Debug script to see what bubbles are produced after splitting

import sys
sys.path.insert(0, '/Users/guillemhernandezsola/code/manga-translator')

import cv2
import json
import numpy as np
import importlib.util

spec = importlib.util.spec_from_file_location("manga_translator", "/Users/guillemhernandezsola/code/manga-translator/manga-translator.py")
mt = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mt)

image_path = '004.png'
detector = mt.MacVisionDetector(source_lang='en')
raw = detector.read(image_path)
image = cv2.imread(image_path)

# Full filtering as pipeline does
filtered = []
skipped = 0
ih, iw = image.shape[:2]

for bbox, text, conf in raw:
    t = mt.normalize_text(text)
    qb = mt.quad_bbox(bbox)
    
    if conf < 0.12:
        skipped += 1
        continue
    if len(t) < 1:
        skipped += 1
        continue
    if mt.is_noise_text(t):
        skipped += 1
        continue
    if mt.is_sound_effect(t):
        skipped += 1
        continue
    if mt.is_title_text(t):
        skipped += 1
        continue
    if qb[1] < int(ih * mt.TOP_BAND_RATIO):
        if conf < 0.70 and len(t) >= 5:
            skipped += 1
            continue

    filtered.append((bbox, t, conf))

resolved_gap = mt.auto_gap(image_path)
bubbles, bubble_boxes, bubble_quads, bubble_indices = mt.group_tokens(
    filtered, image.shape, gap_px=resolved_gap, bbox_padding=3
)

print("=== AFTER GROUPING ===")
print(f"Bubbles dict keys: {sorted(bubbles.keys())}")
for bid in [7, 8]:
    if bid in bubbles:
        print(f"\nBubble {bid}:")
        print(f"  Box: {bubble_boxes[bid]}")
        print(f"  Indices ({len(bubble_indices[bid])}): {bubble_indices[bid]}")
        print(f"  Quads ({len(bubble_quads[bid])})")

# Now simulate the split logic
new_bubbles, new_bubble_boxes, new_bubble_quads, new_bubble_indices = {}, {}, {}, {}
next_bid = max(bubbles.keys()) + 1 if bubbles else 1
splits_performed = []

for bid in list(bubbles.keys()):
    box = bubble_boxes[bid]
    bubble_split = None
    
    # Try split
    split_result = mt.split_panel_box(image, box, bubble_quads=bubble_quads[bid])
    if split_result:
        box_left, box_right, split_x = split_result
        # ... split logic ...
        bubble_split = "panel_split"
    
    if bubble_split is None:
        col_split = mt.split_bubble_if_multiple_columns(bubble_indices[bid], filtered, bid=bid)
        if col_split:
            bubble_split = "column_split"
    
    if bubble_split:
        splits_performed.append(f"Bubble {bid}: {bubble_split}")
        # Don't actually split here, just mark it
    else:
        # No split
        new_bubbles[bid] = bubbles[bid]
        new_bubble_boxes[bid] = bubble_boxes[bid]
        new_bubble_quads[bid] = bubble_quads[bid]
        new_bubble_indices[bid] = bubble_indices[bid]

print("\n=== AFTER SPLIT LOGIC ===")
print(f"Splits detected: {len(splits_performed)}")
for s in splits_performed:
    print(f"  {s}")
    
print(f"\nBubbles dict keys: {sorted(new_bubbles.keys())}")
for bid in [7, 8]:
    if bid in new_bubbles:
        print(f"\nBubble {bid}:")
        print(f"  Box: {new_bubble_boxes[bid]}")
        print(f"  Indices ({len(new_bubble_indices[bid])}): {new_bubble_indices[bid][:3]}...")
