"""
Field Configuration - Single Source of Truth

This module defines all invoice field classes used throughout the system.
The class IDs are verified against the trained YOLO model (best.pt).

IMPORTANT: Do not modify class_id values without retraining the model!
"""

from dataclasses import dataclass
from typing import Final


@dataclass(frozen=True)
class FieldDefinition:
    """Immutable field definition for invoice extraction.

    Attributes:
        class_id: YOLO class ID (0-9), must match trained model
        class_name: YOLO class name (lowercase_underscore)
        field_name: Business field name used in API responses
        csv_name: CSV column name for data import/export
        is_derived: True if field is derived from other fields (not in CSV)
    """

    class_id: int
    class_name: str
    field_name: str
    csv_name: str
    is_derived: bool = False


# Verified from model weights (runs/train/invoice_fields/weights/best.pt)
# model.names = {0: 'invoice_number', 1: 'invoice_date', ..., 8: 'customer_number', 9: 'payment_line'}
#
# DO NOT CHANGE THE ORDER - it must match the trained model!
FIELD_DEFINITIONS: Final[tuple[FieldDefinition, ...]] = (
    FieldDefinition(0, "invoice_number", "InvoiceNumber", "InvoiceNumber"),
    FieldDefinition(1, "invoice_date", "InvoiceDate", "InvoiceDate"),
    FieldDefinition(2, "invoice_due_date", "InvoiceDueDate", "InvoiceDueDate"),
    FieldDefinition(3, "ocr_number", "OCR", "OCR"),
    FieldDefinition(4, "bankgiro", "Bankgiro", "Bankgiro"),
    FieldDefinition(5, "plusgiro", "Plusgiro", "Plusgiro"),
    FieldDefinition(6, "amount", "Amount", "Amount"),
    FieldDefinition(
        7,
        "supplier_org_number",
        "supplier_organisation_number",
        "supplier_organisation_number",
    ),
    FieldDefinition(8, "customer_number", "customer_number", "customer_number"),
    FieldDefinition(
        9, "payment_line", "payment_line", "payment_line", is_derived=True
    ),
)

# Total number of field classes
NUM_CLASSES: Final[int] = len(FIELD_DEFINITIONS)
