class InventoryConsumption {
  final int id;
  final int inventoryItemId;
  final double amountUsed;
  final String unit;
  final String? comment;
  final DateTime createdAt;

  const InventoryConsumption({
    required this.id,
    required this.inventoryItemId,
    required this.amountUsed,
    required this.unit,
    this.comment,
    required this.createdAt,
  });

  factory InventoryConsumption.fromJson(Map<String, dynamic> json) {
    final itemMap = json['inventoryItem'] as Map<String, dynamic>?;
    return InventoryConsumption(
      id: json['id'] as int,
      inventoryItemId: json['inventoryItemId'] as int,
      amountUsed: double.tryParse(json['amountUsed']?.toString() ?? '0') ?? 0,
      unit: itemMap?['unit'] as String? ?? '',
      comment: json['comment'] as String?,
      createdAt: DateTime.tryParse(json['createdAt']?.toString() ?? '') ?? DateTime.now(),
    );
  }
}
