export class DeviceRegistry {
  constructor() {
    this.devices = new Map();
  }

  async register(payload) {
    const required = ['tenantId', 'branchId', 'serialNo', 'model'];
    for (const field of required) {
      if (!payload[field]) {
        throw new Error(`Missing required field: ${field}`);
      }
    }

    const device = {
      tenantId: payload.tenantId,
      branchId: payload.branchId,
      serialNo: payload.serialNo,
      model: payload.model,
      ipAddress: payload.ipAddress,
      port: payload.port || 4370,
      status: 'online',
      capabilities: payload.capabilities || ['fingerprint', 'rfid', 'pin'],
      lastSeenAt: new Date().toISOString(),
    };

    this.devices.set(device.serialNo, device);
    return device;
  }

  async find(serialNo) {
    const device = this.devices.get(serialNo);
    if (!device) {
      throw new Error(`Device not registered: ${serialNo}`);
    }

    return device;
  }
}
