part of '../order_dtos.dart';

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

  const factory OrderRequestDto({
    @JsonKey(name: "outlet_id") String? outletId,
    @JsonKey(name: "customer_id") String? customerId,
    @JsonKey(name: "table_number") String? tableNumber,
    @JsonKey(name: "table_id") String? tableId,
    @JsonKey(name: "order_type") String? orderType,
    @JsonKey(name: "notes") String? notes,
    @JsonKey(name: "order_items") List<OrderItemRequestDto>? orderItems,
    @JsonKey(name: "customer_name") String? customerName,
  }) = _OrderRequestDto;

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

  // Optional: mapper ke domain entity
  OrderRequest toDomain() => OrderRequest(
    outletId: outletId ?? '',
    customerId: customerId ?? '',
    tableNumber: tableNumber ?? '',
    tableId: tableId ?? '',
    orderType: orderType ?? '',
    notes: notes ?? '',
    orderItems: orderItems?.map((e) => e.toDomain()).toList() ?? const [],
    customerName: customerName ?? '',
  );

  factory OrderRequestDto.fromDomain(OrderRequest request) => OrderRequestDto(
    outletId: request.outletId,
    customerId: request.customerId,
    tableNumber: request.tableNumber,
    tableId: request.tableId,
    orderType: request.orderType,
    notes: request.notes,
    orderItems: request.orderItems
        .map((e) => OrderItemRequestDto.fromDomain(e))
        .toList(),
    customerName: request.customerName,
  );

  Map<String, dynamic> toRequest() {
    Map<String, dynamic> data = {
      "outlet_id": outletId,
      "table_number": tableNumber,
      "order_type": orderType,
      "notes": notes,
      "order_items": orderItems?.map((e) => e.toRequest()).toList(),
      "customer_name": customerName,
    };

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

    if (tableId != null && tableId != '') {
      data["table_id"] = tableId;
    }

    return data;
  }
}

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

  const factory OrderItemRequestDto({
    @JsonKey(name: "product_id") String? productId,
    @JsonKey(name: "product_variant_id") String? productVariantId,
    @JsonKey(name: "quantity") int? quantity,
    @JsonKey(name: "unit_price") int? unitPrice,
    @JsonKey(name: "notes") String? notes,
  }) = _OrderItemRequestDto;

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

  // Optional: mapper ke domain entity
  OrderItemRequest toDomain() => OrderItemRequest(
    productId: productId ?? '',
    productVariantId: productVariantId ?? '',
    quantity: quantity ?? 0,
    unitPrice: unitPrice ?? 0,
    notes: notes ?? '',
  );

  factory OrderItemRequestDto.fromDomain(OrderItemRequest request) =>
      OrderItemRequestDto(
        productId: request.productId,
        productVariantId: request.productVariantId,
        quantity: request.quantity,
        unitPrice: request.unitPrice,
        notes: request.notes,
      );

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

    if (productVariantId != null && productVariantId != '') {
      data["product_variant_id"] = productVariantId;
    }

    return data;
  }
}
