import 'dart:convert';

class OrderResponseModel {
  final bool? success;
  final OrderData? data;
  final dynamic errors;

  OrderResponseModel({
    this.success,
    this.data,
    this.errors,
  });

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

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

  factory OrderResponseModel.fromMap(Map<String, dynamic> json) =>
      OrderResponseModel(
        success: json["success"],
        data: json["data"] == null ? null : OrderData.fromMap(json["data"]),
        errors: json["errors"],
      );

  Map<String, dynamic> toMap() => {
        "success": success,
        "data": data?.toMap(),
        "errors": errors,
      };
}

class OrderDetailResponseModel {
  final bool? success;
  final Order? data;
  final dynamic errors;

  OrderDetailResponseModel({
    this.success,
    this.data,
    this.errors,
  });

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

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

  factory OrderDetailResponseModel.fromMap(Map<String, dynamic> json) =>
      OrderDetailResponseModel(
        success: json["success"],
        data: json["data"] == null ? null : Order.fromMap(json["data"]),
        errors: json["errors"],
      );

  Map<String, dynamic> toMap() => {
        "success": success,
        "data": data?.toMap(),
        "errors": errors,
      };
}

class OrderData {
  final List<Order>? orders;
  final int? totalCount;
  final int? page;
  final int? limit;
  final int? totalPages;

  OrderData({
    this.orders,
    this.totalCount,
    this.page,
    this.limit,
    this.totalPages,
  });

  factory OrderData.fromMap(Map<String, dynamic> json) => OrderData(
        orders: json["orders"] == null
            ? []
            : List<Order>.from(json["orders"].map((x) => Order.fromMap(x))),
        totalCount: json["total_count"],
        page: json["page"],
        limit: json["limit"],
        totalPages: json["total_pages"],
      );

  Map<String, dynamic> toMap() => {
        "orders": orders == null
            ? []
            : List<dynamic>.from(orders!.map((x) => x.toMap())),
        "total_count": totalCount,
        "page": page,
        "limit": limit,
        "total_pages": totalPages,
      };
}

class Order {
  final String? id;
  final String? orderNumber;
  final String? outletId;
  final String? userId;
  final String? tableNumber;
  final String? orderType;
  final String? status;
  final int? subtotal;
  final int? taxAmount;
  final int? discountAmount;
  final int? totalAmount;
  final String? notes;
  final Map<String, dynamic>? metadata;
  final DateTime? createdAt;
  final DateTime? updatedAt;
  final List<OrderItem>? orderItems;
  final bool? isRefund;

  Order({
    this.id,
    this.orderNumber,
    this.outletId,
    this.userId,
    this.tableNumber,
    this.orderType,
    this.status,
    this.subtotal,
    this.taxAmount,
    this.discountAmount,
    this.totalAmount,
    this.notes,
    this.metadata,
    this.createdAt,
    this.updatedAt,
    this.orderItems,
    this.isRefund,
  });

  factory Order.fromMap(Map<String, dynamic> json) => Order(
        id: json["id"],
        orderNumber: json["order_number"],
        outletId: json["outlet_id"],
        userId: json["user_id"],
        tableNumber: json["table_number"],
        orderType: json["order_type"],
        status: json["status"],
        subtotal: json["subtotal"],
        taxAmount: json["tax_amount"],
        discountAmount: json["discount_amount"],
        totalAmount: json["total_amount"],
        notes: json["notes"],
        metadata: json["metadata"] ?? {},
        isRefund: json["is_refund"],
        createdAt: json["created_at"] == null
            ? null
            : DateTime.parse(json["created_at"]),
        updatedAt: json["updated_at"] == null
            ? null
            : DateTime.parse(json["updated_at"]),
        orderItems: json["order_items"] == null
            ? []
            : List<OrderItem>.from(
                json["order_items"].map((x) => OrderItem.fromMap(x))),
      );

  Map<String, dynamic> toMap() => {
        "id": id,
        "order_number": orderNumber,
        "outlet_id": outletId,
        "user_id": userId,
        "table_number": tableNumber,
        "order_type": orderType,
        "status": status,
        "subtotal": subtotal,
        "tax_amount": taxAmount,
        "discount_amount": discountAmount,
        "total_amount": totalAmount,
        "notes": notes,
        "metadata": metadata,
        "created_at": createdAt?.toIso8601String(),
        "updated_at": updatedAt?.toIso8601String(),
        "is_refund": isRefund,
        "order_items": orderItems == null
            ? []
            : List<dynamic>.from(orderItems!.map((x) => x.toMap())),
      };
}

class OrderItem {
  final String? id;
  final String? orderId;
  final String? productId;
  final String? productName;
  final String? productVariantId;
  final String? productVariantName;
  final int? quantity;
  final int? unitPrice;
  final int? totalPrice;
  final List<dynamic>? modifiers;
  final String? notes;
  final String? status;
  final DateTime? createdAt;
  final DateTime? updatedAt;

  OrderItem({
    this.id,
    this.orderId,
    this.productId,
    this.productName,
    this.productVariantId,
    this.productVariantName,
    this.quantity,
    this.unitPrice,
    this.totalPrice,
    this.modifiers,
    this.notes,
    this.status,
    this.createdAt,
    this.updatedAt,
  });

  factory OrderItem.fromMap(Map<String, dynamic> json) => OrderItem(
        id: json["id"],
        orderId: json["order_id"],
        productId: json["product_id"],
        productName: json["product_name"],
        productVariantId: json["product_variant_id"],
        productVariantName: json["product_variant_name"],
        quantity: json["quantity"],
        unitPrice: json["unit_price"],
        totalPrice: json["total_price"],
        modifiers: json["modifiers"] ?? [],
        notes: json["notes"],
        status: json["status"],
        createdAt: json["created_at"] == null
            ? null
            : DateTime.parse(json["created_at"]),
        updatedAt: json["updated_at"] == null
            ? null
            : DateTime.parse(json["updated_at"]),
      );

  Map<String, dynamic> toMap() => {
        "id": id,
        "order_id": orderId,
        "product_id": productId,
        "product_name": productName,
        "product_variant_id": productVariantId,
        "product_variant_name": productVariantName,
        "quantity": quantity,
        "unit_price": unitPrice,
        "total_price": totalPrice,
        "modifiers": modifiers,
        "notes": notes,
        "status": status,
        "created_at": createdAt?.toIso8601String(),
        "updated_at": updatedAt?.toIso8601String(),
      };
}
