from model_editor import Editor
editor = Editor()
model = editor.model
import os

# ----------------------------------------------------------

rooms = list(getattr(editor, "selected_room_2ds", []) or [])
COLS = ["VE Zone Group", "VE Zone", "Space", "Pollination ID", "Space Type" , "People", "Source Area","Pollination Area"]

def safe_get(udict, key, default=""):
    try:
        return (udict or {}).get(key, default)
    except Exception:
        return default

rows = []
for r in rooms:
    rows.append({
        "VE Zone Group":            safe_get(getattr(r, "user_data", None), "TLC_Zone Group"),
        "VE Zone":                  safe_get(getattr(r, "user_data", None), "TLC_HVAC Zone"),
        "Space Type":               safe_get(getattr(r, "user_data", None), "Space Type"),
        "People":               safe_get(getattr(r, "user_data", None), "People"),
        "Source Area":               safe_get(getattr(r, "user_data", None), "Area"),
        "Space": getattr(r, "display_name", ""),
        "Pollination ID": getattr(r, "identifier", ""),
        "Pollination Area": getattr(r, "floor_area", "")
    })
    # Troubleshooting when TLC_HVAC Zone Data is missing
    try:
        r.zone=r.user_data["TLC_HVAC Zone"]
        return r.zone
    except Exception:
        continue
    

# Exporting Data
filename = "2025-1013_pollination-takeoff.csv"
out_path = LOCAL_DIR.joinpath(filename)
df = pd.DataFrame(rows, columns=COLS)
df.to_csv(out_path, index=False)
print(f"Wrote {len(rows)} rows to CSV with pandas:\n{out_path}")

editor.update()