class ShoppingListItem {
  final int id;
  final String name;
  final int? productId;
  final int? categoryId;
  final double? quantity;
  final String? unit;
  final String status;

  const ShoppingListItem({
    required this.id,
    required this.name,
    required this.productId,
    required this.categoryId,
    required this.quantity,
    required this.unit,
    required this.status,
  });

  factory ShoppingListItem.fromJson(Map<String, dynamic> json) {
    return ShoppingListItem(
      id: (json['id'] as num).toInt(),
      name: (json['name'] ?? '').toString(),
      productId: (json['productId'] as num?)?.toInt(),
      categoryId: (json['categoryId'] as num?)?.toInt(),
      quantity: (json['quantity'] as num?)?.toDouble(),
      unit: json['unit'] as String?,
      status: (json['status'] ?? 'open').toString(),
    );
  }

  String get quantityLabel {
    if (quantity == null) return '';
    final text = quantity == quantity!.roundToDouble()
        ? quantity!.toStringAsFixed(0)
        : quantity!.toStringAsFixed(2).replaceAll('.', ',');
    if (unit == null || unit!.trim().isEmpty) return text;
    return '$text ${unit!.trim()}';
  }
}
