class PantryItem {
  final int id;
  final int productId;
  final String productName;
  final String? canonicalName;
  final String? category;
  final int? categoryId;

  const PantryItem({
    required this.id,
    required this.productId,
    required this.productName,
    this.canonicalName,
    this.category,
    this.categoryId,
  });

  String get displayName {
    if (canonicalName != null && canonicalName!.trim().isNotEmpty) {
      return canonicalName!;
    }
    return productName;
  }

  factory PantryItem.fromJson(Map<String, dynamic> json) {
    final product = json['product'] as Map<String, dynamic>? ?? {};
    return PantryItem(
      id: (json['id'] as num).toInt(),
      productId: (json['productId'] as num).toInt(),
      productName: (product['name'] ?? '').toString(),
      canonicalName: product['canonicalName']?.toString(),
      category: product['category']?.toString(),
      categoryId: (product['categoryId'] as num?)?.toInt(),
    );
  }
}