"""Medtronic CareLink CSV/XLSX-export adapter.

Real CareLink CSV pattern (simplified, often a multi-line header):
  Date,Time,Sensor Glucose (mg/dL),Basal Rate (U/h),Bolus Volume Delivered (U),BWZ Carb Input (g),Patient
"""

import os
import pandas as pd

NAME = "Medtronic CareLink"
SENSOR_TYPE = "medtronic_780g"

SIGNATURE_HINTS = (
    ("medtronic", None),
    ("carelink", None),
    (None, "Sensor Glucose (mg/dL)"),
)


def matches(path: str) -> bool:
    fname = os.path.basename(path).lower()
    if "medtronic" in fname or "carelink" in fname:
        return True
    if not (path.lower().endswith(".csv") or path.lower().endswith(".xlsx")):
        return False
    try:
        with open(path, "r", encoding="utf-8", errors="ignore") as f:
            head = f.read(4096)
        return "Sensor Glucose (mg/dL)" in head or "BWZ Carb Input" in head
    except Exception:
        return False


def parse(path: str) -> pd.DataFrame:
    if path.lower().endswith(".xlsx"):
        try:
            df = pd.read_excel(path)
        except Exception:
            return pd.DataFrame()
    else:
        # CareLink real exports often have preamble; try a few skiprows
        df = None
        for sk in (0, 5, 6, 7, 11):
            try:
                tmp = pd.read_csv(path, skiprows=sk)
                if any("Glucose" in str(c) or "Basal" in str(c) for c in tmp.columns):
                    df = tmp
                    break
            except Exception:
                continue
        if df is None:
            df = pd.read_csv(path)

    cols_lower = {c.lower(): c for c in df.columns}

    def pick_substr(*subs):
        for c in df.columns:
            cl = c.lower()
            for s in subs:
                if s in cl:
                    return c
        return None

    date_col = pick_substr("date")
    time_col = pick_substr("time")
    ts_col = pick_substr("timestamp", "device timestamp")
    glu_col = pick_substr("sensor glucose")
    basal_col = pick_substr("basal rate")
    bolus_col = pick_substr("bolus volume", "bolus delivered")
    carb_col = pick_substr("bwz carb input", "carb input")
    pid_col = pick_substr("patient", "subject")

    out = pd.DataFrame()
    if ts_col:
        out["timestamp_KST"] = pd.to_datetime(df[ts_col], errors="coerce")
    elif date_col and time_col:
        out["timestamp_KST"] = pd.to_datetime(
            df[date_col].astype(str) + " " + df[time_col].astype(str), errors="coerce"
        )
    else:
        return pd.DataFrame()

    out["subject_id"] = df[pid_col].astype(str) if pid_col else "MDT_SUBJ_01"
    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.to_numeric(df[basal_col], errors="coerce") if basal_col else pd.NA
    out["pump_bolus_u"] = pd.to_numeric(df[bolus_col], errors="coerce") if bolus_col else pd.NA
    out["meal_carb_g"] = pd.to_numeric(df[carb_col], errors="coerce") if carb_col else pd.NA
    out["exercise_flag"] = 0
    out = out.dropna(subset=["timestamp_KST"]).reset_index(drop=True)
    # Reorder
    out = out[["subject_id", "timestamp_KST", "glucose_mg_dl", "sensor_type",
               "pump_basal_uph", "pump_bolus_u", "meal_carb_g", "exercise_flag"]]
    return out
