"""Resolve group membership for a device MAC."""

from models.device import normalize_mac


def groups_for_mac(mac_hex: str, groups_model) -> list[str]:
    """Return group ids (string keys) that list this device MAC."""
    mac = normalize_mac(mac_hex)
    if not mac:
        return []
    out: list[str] = []
    for gid in groups_model.list():
        doc = groups_model.read(gid)
        if not isinstance(doc, dict):
            continue
        devs = doc.get("devices")
        if not isinstance(devs, list):
            continue
        for d in devs:
            if normalize_mac(str(d)) == mac:
                out.append(str(gid))
                break
    return out
