"""
Data models for field matching.
"""

from dataclasses import dataclass
from typing import Protocol


class TokenLike(Protocol):
    """Protocol for token objects."""
    text: str
    bbox: tuple[float, float, float, float]
    page_no: int


@dataclass
class Match:
    """Represents a matched field in the document."""
    field: str
    value: str
    bbox: tuple[float, float, float, float]  # (x0, y0, x1, y1)
    page_no: int
    score: float  # 0-1 confidence score
    matched_text: str  # Actual text that matched
    context_keywords: list[str]  # Nearby keywords that boosted confidence

    def to_yolo_format(self, image_width: float, image_height: float, class_id: int) -> str:
        """Convert to YOLO annotation format."""
        x0, y0, x1, y1 = self.bbox

        x_center = (x0 + x1) / 2 / image_width
        y_center = (y0 + y1) / 2 / image_height
        width = (x1 - x0) / image_width
        height = (y1 - y0) / image_height

        return f"{class_id} {x_center:.6f} {y_center:.6f} {width:.6f} {height:.6f}"
