#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Fix DOB template - FINAL: MORE RIGHT and MORE WIDE
"""

import cv2
import numpy as np

# Load cell 1
cell_path = 'page6_cells/cell_01_row1_col1.png'
cell_img = cv2.imread(cell_path)
cell_gray = cv2.cvtColor(cell_img, cv2.COLOR_BGR2GRAY)

print("Fixing DOB template FINAL: MORE RIGHT, MORE WIDE...\n")

# Current: (140, 173, 165, 40)
# NEW: More right and much wider
dob_coords = {
    'dob_label': (155, 173, 185, 40),  # x=155 (was 140), width=185 (was 165)
}

for name, (x, y, w, h) in dob_coords.items():
    # Crop the region
    template = cell_gray[y:y+h, x:x+w]

    # Save template
    template_path = f'wider_templates/{name}.png'
    cv2.imwrite(template_path, template)
    print(f"Updated: {template_path}")
    print(f"  Position: ({x},{y})")
    print(f"  Size: {w}x{h}")

    # Save visualization
    annotated = cell_img.copy()
    cv2.rectangle(annotated, (x, y), (x+w, y+h), (255, 255, 0), 3)
    cv2.putText(annotated, "dob_label (FINAL)", (x, y-5),
               cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 0), 2)

    vis_path = f'wider_templates/{name}_visualization.png'
    cv2.imwrite(vis_path, annotated)
    print(f"  Visualization: {vis_path}")

print(f"\n✓ DOB template FINAL!")
print("  x: 140 → 155 (MORE RIGHT)")
print("  width: 165 → 185 (MORE WIDE)")
