import 'dart:convert';

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 OrderResponseModel {
  final bool? success;
  final OrderData? data;

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

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

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

  factory OrderResponseModel.fromMap(Map<String, dynamic> map) {
    return OrderResponseModel(
      success: map['success'] ?? false,
      data: OrderData.fromMap(map['data']),
    );
  }

  Map<String, dynamic> toMap() {
    return {
      'success': success,
      'data': data?.toMap(),
    };
  }
}

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

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

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

  Map<String, dynamic> toMap() {
    return {
      'orders': orders?.map((x) => x.toMap()).toList(),
      'payments': payments?.map((x) => x.toMap()).toList(),
      '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 int? totalCost;
  final int? remainingAmount;
  final String? paymentStatus;
  final int? refundAmount;
  final bool? isVoid;
  final bool? isRefund;
  final String? notes;
  final Map<String, dynamic>? metadata;
  final DateTime? createdAt;
  final DateTime? updatedAt;
  final List<OrderItem>? orderItems;
  final List<Payment>? payments;
  final int? totalPaid;
  final int? paymentCount;
  final String? splitType;

  Order({
    this.id,
    this.orderNumber,
    this.outletId,
    this.userId,
    this.tableNumber,
    this.orderType,
    this.status,
    this.subtotal,
    this.taxAmount,
    this.discountAmount,
    this.totalAmount,
    this.totalCost,
    this.remainingAmount,
    this.paymentStatus,
    this.refundAmount,
    this.isVoid,
    this.isRefund,
    this.notes,
    this.metadata,
    this.createdAt,
    this.updatedAt,
    this.orderItems,
    this.payments,
    this.totalPaid,
    this.paymentCount,
    this.splitType,
  });

  factory Order.fromMap(Map<String, dynamic> map) {
    return Order(
      id: map['id'],
      orderNumber: map['order_number'],
      outletId: map['outlet_id'],
      userId: map['user_id'],
      tableNumber: map['table_number'],
      orderType: map['order_type'],
      status: map['status'],
      subtotal: map['subtotal'],
      taxAmount: map['tax_amount'],
      discountAmount: map['discount_amount'],
      totalAmount: map['total_amount'],
      totalCost: map['total_cost'],
      remainingAmount: map['remaining_amount'],
      paymentStatus: map['payment_status'],
      refundAmount: map['refund_amount'],
      isVoid: map['is_void'],
      isRefund: map['is_refund'],
      notes: map['notes'],
      metadata: map['metadata'] ?? {},
      createdAt: DateTime.parse(map['created_at']),
      updatedAt: DateTime.parse(map['updated_at']),
      orderItems: map["order_items"] == null
          ? []
          : List<OrderItem>.from(
              map['order_items'].map((x) => OrderItem.fromMap(x))),
      payments: map["payments"] == null
          ? []
          : List<Payment>.from(map['payments'].map((x) => Payment.fromMap(x))),
      totalPaid: map['total_paid'],
      paymentCount: map['payment_count'],
      splitType: map['split_type'] ?? "",
    );
  }

  Map<String, dynamic> toMap() {
    return {
      '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,
      'total_cost': totalCost,
      'remaining_amount': remainingAmount,
      'payment_status': paymentStatus,
      'refund_amount': refundAmount,
      'is_void': isVoid,
      'is_refund': isRefund,
      'notes': notes,
      'metadata': metadata,
      'created_at': createdAt?.toIso8601String(),
      'updated_at': updatedAt?.toIso8601String(),
      'order_items': orderItems?.map((x) => x.toMap()).toList(),
      'payments': payments?.map((x) => x.toMap()).toList(),
      'total_paid': totalPaid,
      'payment_count': paymentCount,
      'split_type': splitType,
    };
  }
}

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

  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,
    this.printerType,
    this.paidQuantity,
  });

  factory OrderItem.fromMap(Map<String, dynamic> map) {
    return OrderItem(
      id: map['id'],
      orderId: map['order_id'],
      productId: map['product_id'],
      productName: map['product_name'],
      productVariantId: map['product_variant_id'],
      productVariantName: map['product_variant_name'],
      quantity: map['quantity'],
      unitPrice: map['unit_price'],
      totalPrice: map['total_price'],
      modifiers:
          map['modifiers'] == null ? [] : List<dynamic>.from(map['modifiers']),
      notes: map['notes'],
      status: map['status'],
      createdAt: DateTime.parse(map['created_at']),
      updatedAt: DateTime.parse(map['updated_at']),
      printerType: map['printer_type'],
      paidQuantity: map['paid_quantity'],
    );
  }

  Map<String, dynamic> toMap() {
    return {
      '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(),
      'printer_type': printerType,
      'paid_quantity': paidQuantity,
    };
  }
}

class Payment {
  final String? id;
  final String? orderId;
  final String? paymentMethodId;
  final String? paymentMethodName;
  final String? paymentMethodType;
  final int? amount;
  final String? status;
  final int? splitNumber;
  final int? splitTotal;
  final String? splitDescription;
  final int? refundAmount;
  final Map<String, dynamic>? metadata;
  final DateTime? createdAt;
  final DateTime? updatedAt;

  Payment({
    this.id,
    this.orderId,
    this.paymentMethodId,
    this.paymentMethodName,
    this.paymentMethodType,
    this.amount,
    this.status,
    this.splitNumber,
    this.splitTotal,
    this.splitDescription,
    this.refundAmount,
    this.metadata,
    this.createdAt,
    this.updatedAt,
  });

  factory Payment.fromMap(Map<String, dynamic> map) {
    return Payment(
      id: map['id'],
      orderId: map['order_id'],
      paymentMethodId: map['payment_method_id'],
      paymentMethodName: map['payment_method_name'],
      paymentMethodType: map['payment_method_type'],
      amount: map['amount'],
      status: map['status'],
      splitNumber: map['split_number'],
      splitTotal: map['split_total'],
      splitDescription: map['split_description'],
      refundAmount: map['refund_amount'],
      metadata: map['metadata'] ?? {},
      createdAt: DateTime.parse(map['created_at']),
      updatedAt: DateTime.parse(map['updated_at']),
    );
  }

  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'order_id': orderId,
      'payment_method_id': paymentMethodId,
      'payment_method_name': paymentMethodName,
      'payment_method_type': paymentMethodType,
      'amount': amount,
      'status': status,
      'split_number': splitNumber,
      'split_total': splitTotal,
      'split_description': splitDescription,
      'refund_amount': refundAmount,
      'metadata': metadata,
      'created_at': createdAt?.toIso8601String(),
      'updated_at': updatedAt?.toIso8601String(),
    };
  }
}
