class ReceiptAlias {
  final int id;
  final String receiptName;
  final int productId;
  final String? productName;
  final String? productCanonicalName;

  const ReceiptAlias({
    required this.id,
    required this.receiptName,
    required this.productId,
    this.productName,
    this.productCanonicalName,
  });

  String get displayProductName {
    final canonical = productCanonicalName?.trim();
    if (canonical != null && canonical.isNotEmpty) return canonical;
    final fallback = productName?.trim();
    if (fallback != null && fallback.isNotEmpty) return fallback;
    return '#$productId';
  }

  factory ReceiptAlias.fromJson(Map<String, dynamic> json) {
    final product = json['product'];
    final productMap = product is Map<String, dynamic>
        ? product
        : const <String, dynamic>{};

    return ReceiptAlias(
      id: (json['id'] as num).toInt(),
      receiptName: (json['receiptName'] ?? '').toString(),
      productId: (json['productId'] as num?)?.toInt() ??
          (productMap['id'] as num?)?.toInt() ??
          0,
      productName: productMap['name']?.toString(),
      productCanonicalName: productMap['canonicalName']?.toString(),
    );
  }
}
