import 'dart:convert';

class OrderRequestModel {
  final String? outletId;
  final String? customerId;
  final String? tableNumber;
  final String? tableId;
  final String? orderType;
  final String? notes;
  final List<OrderItemRequest>? orderItems;
  final String? customerName;

  OrderRequestModel({
    this.outletId,
    this.customerId,
    this.tableNumber,
    this.tableId,
    this.orderType,
    this.notes,
    this.orderItems,
    this.customerName,
  });

  factory OrderRequestModel.fromJson(String str) =>
      OrderRequestModel.fromMap(json.decode(str));

  String toJson() => json.encode(toMap());

  factory OrderRequestModel.fromMap(Map<String, dynamic> json) =>
      OrderRequestModel(
        outletId: json["outlet_id"],
        customerId: json["customer_id"],
        tableNumber: json["table_number"],
        tableId: json["table_id"],
        orderType: json["order_type"],
        notes: json["notes"],
        orderItems: json["order_items"] == null
            ? []
            : List<OrderItemRequest>.from(
                json["order_items"].map((x) => OrderItemRequest.fromMap(x))),
        customerName: json["customer_name"],
      );

  Map<String, dynamic> toMap() {
    Map<String, dynamic> data = {
      "outlet_id": outletId,
      "table_number": tableNumber,
      "table_id": tableId,
      "order_type": orderType,
      "notes": notes,
      "order_items": orderItems == null
          ? []
          : List<dynamic>.from(orderItems!.map((x) => x.toMap())),
      "customer_name": customerName,
    };

    if (customerId != null && customerId != "") {
      data["customer_id"] = customerId;
    }

    return data;
  }
}

class OrderItemRequest {
  final String? productId;
  final String? productVariantId;
  final int? quantity;
  final int? unitPrice;
  final String? notes;

  OrderItemRequest({
    this.productId,
    this.productVariantId,
    this.quantity,
    this.unitPrice,
    this.notes,
  });

  factory OrderItemRequest.fromJson(String str) =>
      OrderItemRequest.fromMap(json.decode(str));

  factory OrderItemRequest.fromMap(Map<String, dynamic> json) =>
      OrderItemRequest(
        productId: json["product_id"],
        productVariantId: json["product_variant_id"],
        quantity: json["quantity"],
        unitPrice: json["unit_price"]?.toDouble(),
        notes: json["notes"],
      );

  Map<String, dynamic> toMap() => {
        "product_id": productId,
        "product_variant_id": productVariantId,
        "quantity": quantity,
        "unit_price": unitPrice,
        "notes": notes,
      };
}
