class Device {
  String? id;
  String? serialNumber;
  String? number;
  String? brand;
  String? model;
  DateTime? productionDate;
  DateTime? supplyDate;
  DateTime? installDate;
  DateTime? receivingDate;
  DateTime? operationDate;
  DateTime? warrantyDate;

  Device({
    this.id,
    this.serialNumber,
    this.number,
    this.brand,
    this.model,
    this.productionDate,
    this.supplyDate,
    this.installDate,
    this.receivingDate,
    this.operationDate,
    this.warrantyDate,
  });

  factory Device.fromJson(Map<String, dynamic> parsedJson) {
    return Device(
      id: parsedJson["nid"] ?? parsedJson["id"],
      serialNumber: parsedJson["sn"] ?? parsedJson["value"],
      number: parsedJson["asset_no"],
      brand: parsedJson["brand"] as String?,
      model: parsedJson["model"] as String?,
      productionDate: getDateFromString(parsedJson["production_date"]),
      supplyDate: getDateFromString(parsedJson["supply_date "]),
      installDate: getDateFromString(parsedJson["install_date "]),
      receivingDate: getDateFromString(parsedJson["receving_date "]),
      operationDate: getDateFromString(parsedJson["operation_date "]),
      warrantyDate: getDateFromString(parsedJson["warranty_date "]),
    );
  }

  factory Device.fromDevice(Device device) {
    return Device(
      id: device.id,
      serialNumber: device.serialNumber,
      number: device.number,
      brand: device.brand,
      model: device.model,
      productionDate: device.productionDate,
      supplyDate: device.supplyDate,
      installDate: device.installDate,
      receivingDate: device.receivingDate,
      operationDate: device.operationDate,
      warrantyDate: device.warrantyDate,
    );
  }
}

DateTime? getDateFromString(String? unixDate) {
  if (unixDate == null) {
    return null;
  }
  return DateTime.fromMillisecondsSinceEpoch(int.parse(unixDate));
}
