class PendingProduct {
  final int id;
  final String name;
  final String? canonicalName;
  final DateTime? createdAt;
  final String? categoryPath;
  final String? ownerUsername;

  const PendingProduct({
    required this.id,
    required this.name,
    this.canonicalName,
    this.createdAt,
    this.categoryPath,
    this.ownerUsername,
  });

  String get displayName =>
      canonicalName != null && canonicalName!.trim().isNotEmpty
          ? canonicalName!
          : name;

  factory PendingProduct.fromJson(Map<String, dynamic> json) {
    final categoryRef = json['categoryRef'];
    final owner = json['owner'];
    final parts = <String>[];
    if (categoryRef is Map<String, dynamic>) {
      final parent = categoryRef['parent'];
      if (parent is Map<String, dynamic>) {
        final parentName = parent['name']?.toString();
        if (parentName != null && parentName.trim().isNotEmpty) {
          parts.add(parentName.trim());
        }
      }
      final name = categoryRef['name']?.toString();
      if (name != null && name.trim().isNotEmpty) {
        parts.add(name.trim());
      }
    }

    return PendingProduct(
      id: (json['id'] as num).toInt(),
      name: (json['name'] ?? '').toString(),
      canonicalName: json['canonicalName']?.toString(),
      createdAt: json['createdAt'] == null
          ? null
          : DateTime.tryParse(json['createdAt'].toString()),
      categoryPath: parts.isEmpty ? null : parts.join(' > '),
      ownerUsername: owner is Map<String, dynamic>
          ? owner['username']?.toString()
          : null,
    );
  }
}