"""
Carga de conjuntos FK para validación de import CSV de agentes aduanales.
Clarion: catálogo de países (GPaises / m3_key).
"""
from typing import Set
import logging

from core.database import CoreSessionLocal

logger = logging.getLogger(__name__)


def load_customs_brokers_fk_sets() -> Set[str]:
    """
    Carga valid_country_m3 (códigos país m3_key) para validar Col J PAIS.
    """
    valid_country_m3: Set[str] = set()
    try:
        with CoreSessionLocal() as session:
            from api.v1.modules.public.reference_data.countries.models import Country
            for row in session.query(Country.m3_key).all():
                if row[0]:
                    valid_country_m3.add((row[0] or "").strip().upper())
    except Exception as e:
        logger.warning("Customs brokers import: could not load country m3 keys: %s", e)
    return valid_country_m3
