class Transaction {
  final String id;
  final List<TransactionItem> items;
  final int total;
  final DateTime timestamp;
  final String paymentMethod;

  Transaction({
    required this.id,
    required this.items,
    required this.total,
    required this.timestamp,
    required this.paymentMethod,
  });

  factory Transaction.fromJson(Map<String, dynamic> json) {
    return Transaction(
      id: json['id'],
      items: (json['items'] as List)
          .map((item) => TransactionItem.fromJson(item))
          .toList(),
      total: json['total'],
      timestamp: DateTime.parse(json['timestamp']),
      paymentMethod: json['paymentMethod'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'items': items.map((item) => item.toJson()).toList(),
      'total': total,
      'timestamp': timestamp.toIso8601String(),
      'paymentMethod': paymentMethod,
    };
  }
}

class TransactionItem {
  final String itemId;
  final String name;
  final int price;
  final int quantity;

  TransactionItem({
    required this.itemId,
    required this.name,
    required this.price,
    required this.quantity,
  });

  factory TransactionItem.fromJson(Map<String, dynamic> json) {
    return TransactionItem(
      itemId: json['itemId'],
      name: json['name'],
      price: json['price'],
      quantity: json['quantity'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'itemId': itemId,
      'name': name,
      'price': price,
      'quantity': quantity,
    };
  }
}