part of '../order_dtos.dart';

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

  const factory AddItemOrderRequestDto({
    @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,
  }) = _AddItemOrderRequestDto;

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

  AddItemOrderRequest toDomain() => AddItemOrderRequest(
    productId: productId ?? '',
    productVariantId: productvariantId ?? '',
    quantity: quantity ?? 0,
    unitPrice: unitPrice ?? 0,
    notes: notes ?? '',
  );

  factory AddItemOrderRequestDto.fromDomain(
    AddItemOrderRequest addItemOrderRequest,
  ) => AddItemOrderRequestDto(
    productId: addItemOrderRequest.productId,
    productvariantId: addItemOrderRequest.productVariantId,
    quantity: addItemOrderRequest.quantity,
    unitPrice: addItemOrderRequest.unitPrice,
    notes: addItemOrderRequest.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;
  }
}
