"""
Debug script to test connector extraction and matching.
"""

import sys
import logging
from pathlib import Path

# Add project root to path
project_root = Path(__file__).parent.parent
sys.path.insert(0, str(project_root))

from src.extraction_v2.shape_extractor import ShapeExtractor

# Set up logging to see debug messages
logging.basicConfig(
    level=logging.DEBUG,
    format='%(name)s - %(levelname)s - %(message)s'
)

def debug_connectors(file_path: str):
    """Debug connector extraction and matching."""
    print("=" * 80)
    print(f"DEBUGGING CONNECTORS: {file_path}")
    print("=" * 80)

    extractor = ShapeExtractor()

    # Extract shapes (this will also extract and match connectors)
    shapes = extractor.extract_shapes_from_file(file_path)

    print(f"\n{'=' * 80}")
    print(f"RESULTS: Found {len(shapes)} shapes")
    print(f"{'=' * 80}")

    # Show shapes with and without connectors
    shapes_with_connectors = [s for s in shapes if s.get("connector_target_cell")]
    shapes_without_connectors = [s for s in shapes if not s.get("connector_target_cell")]

    print(f"\nShapes WITH connectors: {len(shapes_with_connectors)}")
    for shape in shapes_with_connectors:
        print(f"  - {shape.get('text', '')[:40]}")
        print(f"    From: {shape.get('from_col')}{shape.get('from_row')}")
        print(f"    Target: {shape.get('connector_target_cell')}")

    print(f"\nShapes WITHOUT connectors: {len(shapes_without_connectors)}")
    for shape in shapes_without_connectors:
        print(f"  - {shape.get('text', '')[:40]}")
        print(f"    Position: {shape.get('from_col')}{shape.get('from_row')} to {shape.get('to_col')}{shape.get('to_row')}")
        print(f"    EMU: ({shape.get('emu_x')}, {shape.get('emu_y')})")

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("Usage: python debug_connectors.py <path-to-xlsx-file>")
        sys.exit(1)

    file_path = sys.argv[1]
    debug_connectors(file_path)
