"""Hints cabecera CSV pedimentos (dígitos col Clarion, enums acotados, longitudes típicas)."""
from __future__ import annotations

from typing import Any, Dict, List, Optional

from ..common.csv_hint_inject import (
    append_csv_hint,
    inject_max_char_hints_into_columns,
    merge_enum_hint_before_max,
)

# Cols 1–3: solo dígitos con longitud fija
_PEDIMENTOS_DIGITS: Dict[str, int] = {
    "AÑO": 2,
    "PATENTE": 4,
    "NUMERO": 7,
}

# Resto de campos con tope razonable (validadores/modelos variados)
_PEDIMENTOS_MAX_CHARS: Dict[str, int] = {
    "TIPO_OPERACION": 8,
    "CLAVE_PEDIMENTO": 30,
    "REGIMEN": 10,
    "FECHA_INICIO": 30,
    "FECHA_FINAL": 30,
    "FECHA_PAGO": 30,
    "ADUANA_SECCION_CRUCE": 120,
    "ACUSE_ELECTRONICO": 80,
    "INDIVIDUAL_CONSOLIDADO": 10,
    "MET_TRANSP_ENTRADA": 30,
    "MET_TRANSP_ARRIVO": 30,
    "MET_TRANSP_SALIDA": 30,
    "PEDIMENTO": 25,
    "CLIENTE_SHORT_NAME": 30,
    "TIPO_PEDIMENTO": 20,
    "OBSERVACIONES": 500,
}


def apply_pedimentos_csv_hints(columns: Optional[List[Dict[str, Any]]]) -> None:
    if not columns:
        return
    inject_max_char_hints_into_columns(columns, _PEDIMENTOS_MAX_CHARS)
    for item in columns:
        canon = item.get("canonical")
        if not canon:
            continue
        nd = _PEDIMENTOS_DIGITS.get(str(canon).strip())
        if nd is not None:
            append_csv_hint(item, {"kind": "digits", "n": nd})
        if canon == "TIPO_OPERACION":
            merge_enum_hint_before_max(item, ["I", "E"])
        elif canon == "PAGO_IMPUESTO":
            merge_enum_hint_before_max(item, ["S", "N"])
        elif canon == "ES_MIXTO":
            merge_enum_hint_before_max(item, ["SI", "NO"])
        elif canon == "INDIVIDUAL_CONSOLIDADO":
            merge_enum_hint_before_max(item, ["IND", "CON"])
