"""Device adapters for InsuTrialKit-Kor.

Each adapter exposes:
  - SIGNATURE_HINTS: tuple of (filename_pattern, header_substring) tuples
  - parse(path) -> pandas.DataFrame in long-form standard schema

Standard schema columns:
  subject_id, timestamp_KST, glucose_mg_dl, sensor_type,
  pump_basal_uph, pump_bolus_u, meal_carb_g, exercise_flag
"""

from . import dexcom, libre, tandem, medtronic, stub_devices

ADAPTERS = [
    dexcom,
    libre,
    tandem,
    medtronic,
    stub_devices.omnipod,
    stub_devices.camaps,
    stub_devices.minimed780g,
    stub_devices.guardian4,
]


def dispatch(path: str):
    """Try each adapter in order; return (adapter_module, df) or (None, None)."""
    for adapter in ADAPTERS:
        try:
            if adapter.matches(path):
                df = adapter.parse(path)
                if df is not None and len(df) > 0:
                    return adapter, df
        except Exception:
            continue
    return None, None
