"""Dexcom Clarity CSV adapter.

Real Clarity CSV pattern (simplified):
  Index,Timestamp (YYYY-MM-DDTHH:MM:SS),Event Type,Glucose Value (mg/dL),Patient Info,...
"""

import os
import pandas as pd

NAME = "Dexcom Clarity"
SENSOR_TYPE = "dexcom_g6"

SIGNATURE_HINTS = (
    ("dexcom", None),
    (None, "Glucose Value (mg/dL)"),
)


def matches(path: str) -> bool:
    fname = os.path.basename(path).lower()
    if "dexcom" in fname or "clarity" in fname:
        return True
    if not path.lower().endswith(".csv"):
        return False
    try:
        with open(path, "r", encoding="utf-8", errors="ignore") as f:
            head = f.read(2048)
        return "Glucose Value (mg/dL)" in head and "Timestamp" in head
    except Exception:
        return False


def parse(path: str) -> pd.DataFrame:
    df = pd.read_csv(path)
    # Tolerate naming variants
    ts_col = None
    glu_col = None
    pid_col = None
    for c in df.columns:
        cl = c.lower()
        if ts_col is None and "timestamp" in cl:
            ts_col = c
        if glu_col is None and "glucose" in cl and "mg/dl" in cl:
            glu_col = c
        if pid_col is None and ("patient" in cl or "subject" in cl):
            pid_col = c

    if ts_col is None or glu_col is None:
        return pd.DataFrame()

    out = pd.DataFrame()
    out["subject_id"] = df[pid_col].astype(str) if pid_col else "DEX_SUBJ_01"
    out["timestamp_KST"] = pd.to_datetime(df[ts_col], errors="coerce")
    out["glucose_mg_dl"] = pd.to_numeric(df[glu_col], errors="coerce")
    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
    out = out.dropna(subset=["timestamp_KST", "glucose_mg_dl"]).reset_index(drop=True)
    return out
