#!/usr/bin/env python3
"""Debug specific cells to see what OCR detects"""

import sys
sys.path.insert(0, 'workflow')

from cells_to_csv import CellsToCSVExtractor
import cv2

# Create extractor with Google Vision
extractor = CellsToCSVExtractor(
    ocr_engine='google',
    google_credentials_path='google-credentials.json'
)

# Debug specific cells
cells_to_debug = [
    ('gogoelocroutput/cells/page003_cell07.png', 3, 7),
    ('gogoelocroutput/cells/page003_cell08.png', 3, 8),
    ('gogoelocroutput/cells/page003_cell11.png', 3, 11),
    ('gogoelocroutput/cells/page004_cell08.png', 4, 8),
]

for cell_path, page_num, cell_num in cells_to_debug:
    print(f"\n{'='*80}")
    print(f"Debugging Page {page_num}, Cell {cell_num}")
    print(f"{'='*80}")

    # Extract regions for batch
    regions, positions, cell_color, cell_dims = extractor.extract_regions_for_batch(
        cell_path, page_num, cell_num
    )

    print(f"\nTemplates found: {list(positions.keys())}")
    print(f"Regions extracted: {len(regions)}")

    if regions:
        # Process with batch OCR
        ocr_results = extractor.batch_ocr_google(regions)

        # Show results for each region
        for region_data, region_id in regions:
            field_name = region_id.split('_')[-1]
            text = ocr_results.get(region_id, "")
            print(f"  {field_name:12s}: '{text}'")

        # Parse into voter record
        voter = extractor.parse_batch_results(ocr_results, page_num, cell_num)
        print(f"\nParsed voter record:")
        for key, value in voter.items():
            print(f"  {key:15s}: {value}")
    else:
        print("No regions extracted - no templates matched!")
