# app/csv_config.py
import csv
from pathlib import Path
from typing import List
from dataclasses import dataclass

# RIPRISTINATO IMPORT RELATIVO: Necessario per il Core Orchestrator
from .normalize import norm_mac

@dataclass
class GatewayConfig:
    mac: str
    name: str = ""
    label: str = ""

def load_gateway_features_csv(file_path: str, delimiter: str = ";") -> List[GatewayConfig]:
    """Carica l'anagrafica gateway per definire l'ordine delle feature del modello."""
    gws = []
    p = Path(file_path)
    if not p.exists():
        return gws
    
    try:
        with open(p, "r", encoding="utf-8") as f:
            reader = csv.DictReader(f, delimiter=delimiter)
            # Normalizzazione header
            reader.fieldnames = [fn.strip().lower() for fn in reader.fieldnames]
            for row in reader:
                raw_mac = row.get("mac", "").strip()
                if raw_mac:
                    gws.append(GatewayConfig(mac=norm_mac(raw_mac)))
    except Exception:
        pass
    return gws
