part of '../order_dtos.dart';

@freezed
class OrderDto with _$OrderDto {
  const OrderDto._();

  const factory OrderDto({
    @JsonKey(name: 'id') String? id,
    @JsonKey(name: 'order_number') String? orderNumber,
    @JsonKey(name: 'outlet_id') String? outletId,
    @JsonKey(name: 'user_id') String? userId,
    @JsonKey(name: 'table_number') String? tableNumber,
    @JsonKey(name: 'order_type') String? orderType,
    @JsonKey(name: 'status') String? status,
    @JsonKey(name: 'subtotal') int? subtotal,
    @JsonKey(name: 'tax_amount') int? taxAmount,
    @JsonKey(name: 'discount_amount') int? discountAmount,
    @JsonKey(name: 'total_amount') int? totalAmount,
    @JsonKey(name: 'total_cost') int? totalCost,
    @JsonKey(name: 'remaining_amount') int? remainingAmount,
    @JsonKey(name: 'payment_status') String? paymentStatus,
    @JsonKey(name: 'refund_amount') int? refundAmount,
    @JsonKey(name: 'is_void') bool? isVoid,
    @JsonKey(name: 'is_refund') bool? isRefund,
    @JsonKey(name: 'notes') String? notes,
    @JsonKey(name: 'metadata') Map<String, dynamic>? metadata,
    @JsonKey(name: 'created_at') String? createdAt,
    @JsonKey(name: 'updated_at') String? updatedAt,
    @JsonKey(name: 'order_items') List<OrderItemDto>? orderItems,
    @JsonKey(name: 'payments') List<OrderPaymentDto>? payments,
    @JsonKey(name: 'total_paid') int? totalPaid,
    @JsonKey(name: 'payment_count') int? paymentCount,
    @JsonKey(name: 'split_type') String? splitType,
  }) = _OrderDto;

  factory OrderDto.fromJson(Map<String, dynamic> json) =>
      _$OrderDtoFromJson(json);

  Order toDomain() => Order(
    id: id ?? '',
    orderNumber: orderNumber ?? '',
    outletId: outletId ?? '',
    userId: userId ?? '',
    tableNumber: tableNumber ?? '',
    orderType: orderType ?? '',
    status: status ?? '',
    subtotal: subtotal ?? 0,
    taxAmount: taxAmount ?? 0,
    discountAmount: discountAmount ?? 0,
    totalAmount: totalAmount ?? 0,
    totalCost: totalCost ?? 0,
    remainingAmount: remainingAmount ?? 0,
    paymentStatus: paymentStatus ?? '',
    refundAmount: refundAmount ?? 0,
    isVoid: isVoid ?? false,
    isRefund: isRefund ?? false,
    notes: notes ?? '',
    metadata: metadata ?? {},
    createdAt: createdAt ?? '',
    updatedAt: updatedAt ?? '',
    orderItems: orderItems?.map((e) => e.toDomain()).toList() ?? [],
    payments: payments?.map((e) => e.toDomain()).toList() ?? [],
    totalPaid: totalPaid ?? 0,
    paymentCount: paymentCount ?? 0,
    splitType: splitType ?? '',
  );
}

@freezed
class OrderItemDto with _$OrderItemDto {
  const OrderItemDto._();

  const factory OrderItemDto({
    @JsonKey(name: 'id') String? id,
    @JsonKey(name: 'order_id') String? orderId,
    @JsonKey(name: 'product_id') String? productId,
    @JsonKey(name: 'product_name') String? productName,
    @JsonKey(name: 'quantity') int? quantity,
    @JsonKey(name: 'price') int? price,
    @JsonKey(name: 'subtotal') int? subtotal,
    @JsonKey(name: 'discount_amount') int? discountAmount,
    @JsonKey(name: 'total') int? total,
    @JsonKey(name: 'cost') int? cost,
    @JsonKey(name: 'metadata') Map<String, dynamic>? metadata,
    @JsonKey(name: 'created_at') String? createdAt,
    @JsonKey(name: 'updated_at') String? updatedAt,
  }) = _OrderItemDto;

  factory OrderItemDto.fromJson(Map<String, dynamic> json) =>
      _$OrderItemDtoFromJson(json);

  OrderItem toDomain() => OrderItem(
    id: id ?? '',
    orderId: orderId ?? '',
    productId: productId ?? '',
    productName: productName ?? '',
    quantity: quantity ?? 0,
    price: price ?? 0,
    subtotal: subtotal ?? 0,
    discountAmount: discountAmount ?? 0,
    total: total ?? 0,
    cost: cost ?? 0,
    metadata: metadata ?? {},
    createdAt: createdAt ?? '',
    updatedAt: updatedAt ?? '',
  );
}

@freezed
class OrderPaymentDto with _$OrderPaymentDto {
  const OrderPaymentDto._();

  const factory OrderPaymentDto({
    @JsonKey(name: 'id') String? id,
    @JsonKey(name: 'order_id') String? orderId,
    @JsonKey(name: 'payment_method_id') String? paymentMethodId,
    @JsonKey(name: 'payment_method_name') String? paymentMethodName,
    @JsonKey(name: 'payment_method_type') String? paymentMethodType,
    @JsonKey(name: 'amount') int? amount,
    @JsonKey(name: 'status') String? status,
    @JsonKey(name: 'split_number') int? splitNumber,
    @JsonKey(name: 'split_total') int? splitTotal,
    @JsonKey(name: 'split_type') String? splitType,
    @JsonKey(name: 'split_description') String? splitDescription,
    @JsonKey(name: 'refund_amount') int? refundAmount,
    @JsonKey(name: 'metadata') Map<String, dynamic>? metadata,
    @JsonKey(name: 'created_at') String? createdAt,
    @JsonKey(name: 'updated_at') String? updatedAt,
    @JsonKey(name: 'payment_order_items')
    List<PaymentOrderItemDto>? paymentOrderItems,
  }) = _OrderPaymentDto;

  factory OrderPaymentDto.fromJson(Map<String, dynamic> json) =>
      _$OrderPaymentDtoFromJson(json);

  OrderPayment toDomain() => OrderPayment(
    id: id ?? '',
    orderId: orderId ?? '',
    paymentMethodId: paymentMethodId ?? '',
    paymentMethodName: paymentMethodName ?? '',
    paymentMethodType: paymentMethodType ?? '',
    amount: amount ?? 0,
    status: status ?? '',
    splitNumber: splitNumber ?? 0,
    splitTotal: splitTotal ?? 0,
    splitType: splitType ?? '',
    splitDescription: splitDescription ?? '',
    refundAmount: refundAmount ?? 0,
    metadata: metadata ?? {},
    createdAt: createdAt ?? '',
    updatedAt: updatedAt ?? '',
    paymentOrderItems:
        paymentOrderItems?.map((e) => e.toDomain()).toList() ?? [],
  );
}

@freezed
class PaymentOrderItemDto with _$PaymentOrderItemDto {
  const PaymentOrderItemDto._();

  const factory PaymentOrderItemDto({
    @JsonKey(name: 'id') String? id,
    @JsonKey(name: 'order_payment_id') String? orderPaymentId,
    @JsonKey(name: 'order_item_id') String? orderItemId,
    @JsonKey(name: 'amount') int? amount,
    @JsonKey(name: 'refund_amount') int? refundAmount,
    @JsonKey(name: 'created_at') String? createdAt,
    @JsonKey(name: 'updated_at') String? updatedAt,
  }) = _PaymentOrderItemDto;

  factory PaymentOrderItemDto.fromJson(Map<String, dynamic> json) =>
      _$PaymentOrderItemDtoFromJson(json);

  PaymentOrderItem toDomain() => PaymentOrderItem(
    id: id ?? '',
    orderPaymentId: orderPaymentId ?? '',
    orderItemId: orderItemId ?? '',
    amount: amount ?? 0,
    refundAmount: refundAmount ?? 0,
    createdAt: createdAt ?? '',
    updatedAt: updatedAt ?? '',
  );
}
