"""Stub adapters for Omnipod View, CamAPS Cloud, MiniMed 780G, Guardian 4.

Each stub provides:
  - matches(path): returns True if the filename suggests this device
  - parse(path): if file is a CSV, attempts a minimal generic parse;
                 otherwise returns empty DataFrame and prints a stub notice.

These exist for signature-dispatch demonstration only. Production use
requires implementing the real export header parser per device.
"""

import os
import pandas as pd


def _make_stub(name: str, sensor_type: str, *keywords: str):
    class _Stub:
        NAME = name
        SENSOR_TYPE = sensor_type
        SIGNATURE_HINTS = tuple((kw, None) for kw in keywords)

        @staticmethod
        def matches(path: str) -> bool:
            fname = os.path.basename(path).lower()
            return any(kw in fname for kw in keywords)

        @staticmethod
        def parse(path: str) -> pd.DataFrame:
            # Generic minimal CSV: try columns named 'timestamp' + 'glucose'
            if not path.lower().endswith(".csv"):
                return pd.DataFrame()
            try:
                df = pd.read_csv(path)
            except Exception:
                return pd.DataFrame()
            ts_col = None
            glu_col = None
            for c in df.columns:
                cl = c.lower()
                if ts_col is None and "time" in cl:
                    ts_col = c
                if glu_col is None and "glucose" in cl:
                    glu_col = c
            if ts_col is None:
                return pd.DataFrame()
            out = pd.DataFrame()
            out["subject_id"] = df.get("subject_id", pd.Series([f"{sensor_type.upper()[:3]}_SUBJ_01"] * len(df)))
            out["timestamp_KST"] = pd.to_datetime(df[ts_col], errors="coerce")
            out["glucose_mg_dl"] = pd.to_numeric(df[glu_col], errors="coerce") if glu_col else pd.NA
            out["sensor_type"] = sensor_type
            out["pump_basal_uph"] = pd.NA
            out["pump_bolus_u"] = pd.NA
            out["meal_carb_g"] = pd.NA
            out["exercise_flag"] = 0
            return out.dropna(subset=["timestamp_KST"]).reset_index(drop=True)

    _Stub.__name__ = name.replace(" ", "_")
    return _Stub


omnipod = _make_stub("Omnipod View", "omnipod", "omnipod")
camaps = _make_stub("CamAPS Cloud", "camaps", "camaps")
minimed780g = _make_stub("MiniMed 780G", "minimed_780g", "minimed", "780g")
guardian4 = _make_stub("Guardian 4", "guardian4", "guardian")
