import fs from "fs";

const mappingPath = process.env.MAPPING_PATH || "/app/config/mappings.json";
let mappings: any = {};

export function loadMappings() {
  try {
    mappings = JSON.parse(fs.readFileSync(mappingPath, "utf-8"));
  } catch {
    mappings = {};
  }
}

export function resolveAssetId(deviceId: string, metric?: string) {
  const dev = mappings?.devices?.[deviceId];
  if (!dev) return undefined;
  if (metric && dev.metrics && dev.metrics[metric] && dev.metrics[metric].assetId) {
    return dev.metrics[metric].assetId;
  }
  return dev.assetId || undefined;
}
