"""
Validaciones comunes de fila para import CSV de transportistas.
Paridad Clarion: VALIDACIONES_TRANSPORTISTAS, VALIDA_TODA_TRANSPORTISTAS, VALIDA_PARCIAL_TRANSPORTISTAS.
"""
from typing import Dict, Any, Optional, Set, Tuple, List

from ..common.common_validators import (
    MAX_LEN,
    check_required,
    check_max_length,
    check_pais_catalog_transportistas,
    check_estado_catalog_transportistas,
    check_estado_pais_consistency_transportistas,
)


def validate_row_transporter_required(row: Dict[str, Any], line_num: int) -> Optional[Dict[str, Any]]:
    """Col A (CLAVE TRANSPORTISTA) obligatoria."""
    return check_required(row, "CLAVE TRANSPORTISTA", MAX_LEN["transporter_key"], line_num)


def validate_row_transporter_required_full(
    row: Dict[str, Any],
    line_num: int,
) -> Optional[Dict[str, Any]]:
    """VALIDA_TODA: Col A y Col B (NOMBRE) obligatorios cuando se agrega o se reemplaza."""
    err = validate_row_transporter_required(row, line_num)
    if err:
        return err
    err = check_max_length(
        row, "NOMBRE", MAX_LEN["name"], line_num, required=True
    )
    if err:
        return err
    return None


def validaciones_transportistas(
    row: Dict[str, Any],
    line_num: int,
    valid_country_ame: Optional[Set[str]] = None,
    state_descriptions_upper: Optional[Set[str]] = None,
    state_country_set: Optional[Set[Tuple[str, str]]] = None,
) -> List[Dict[str, Any]]:
    """
    VALIDACIONES_TRANSPORTISTAS: estado, país, estado-país (Clarion).
    """
    errors: List[Dict[str, Any]] = []
    for err in [
        check_estado_catalog_transportistas(row, line_num, state_descriptions_upper),
        check_pais_catalog_transportistas(row, line_num, valid_country_ame),
        check_estado_pais_consistency_transportistas(row, line_num, state_country_set),
    ]:
        if err:
            errors.append(err)
    return errors


def valida_toda_transportistas(
    row: Dict[str, Any],
    line_num: int,
    valid_country_ame: Optional[Set[str]] = None,
    state_descriptions_upper: Optional[Set[str]] = None,
    state_country_set: Optional[Set[Tuple[str, str]]] = None,
) -> List[Dict[str, Any]]:
    """VALIDA_TODA: obligatorios A y B + VALIDACIONES_TRANSPORTISTAS (usado para agregar nuevo o reemplazar)."""
    errors: List[Dict[str, Any]] = []
    err = validate_row_transporter_required_full(row, line_num)
    if err:
        errors.append(err)
    errors.extend(
        validaciones_transportistas(
            row,
            line_num,
            valid_country_ame=valid_country_ame,
            state_descriptions_upper=state_descriptions_upper,
            state_country_set=state_country_set,
        )
    )
    return errors


def valida_parcial_transportistas(
    row: Dict[str, Any],
    line_num: int,
    valid_country_ame: Optional[Set[str]] = None,
    state_descriptions_upper: Optional[Set[str]] = None,
    state_country_set: Optional[Set[Tuple[str, str]]] = None,
) -> List[Dict[str, Any]]:
    """VALIDA_PARCIAL_TRANSPORTISTAS: solo VALIDACIONES_TRANSPORTISTAS (actualizar registro existente)."""
    return validaciones_transportistas(
        row,
        line_num,
        valid_country_ame=valid_country_ame,
        state_descriptions_upper=state_descriptions_upper,
        state_country_set=state_country_set,
    )
