// ignore_for_file: public_member_api_docs, sort_constructors_first
import 'dart:convert';

class OrderRequestModel {
  final int? addressId;
  final String? paymentMethod;
  final String? shippingService;
  final int? shippingCost;
  final String? paymentVaName;
  final int? subtotal;
  final int? discount;
  final int? totalCost;
  final List<Item>? items;

  OrderRequestModel({
    this.addressId,
    this.paymentMethod,
    this.shippingService,
    this.shippingCost,
    this.paymentVaName,
    this.subtotal,
    this.discount,
    this.totalCost,
    this.items,
  });

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

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

  factory OrderRequestModel.fromMap(Map<String, dynamic> json) =>
      OrderRequestModel(
        addressId: json["address_id"],
        discount: json["discount"],
        paymentMethod: json["payment_method"],
        shippingService: json["shipping_service"],
        shippingCost: json["shipping_cost"],
        paymentVaName: json["payment_va_name"],
        subtotal: json["subtotal"],
        totalCost: json["total_cost"],
        items: json["items"] == null
            ? []
            : List<Item>.from(json["items"]!.map((x) => Item.fromMap(x))),
      );

  Map<String, dynamic> toMap() => {
        "address_id": addressId,
        "discount": discount,
        "payment_method": paymentMethod,
        "shipping_service": shippingService,
        "shipping_cost": shippingCost,
        "payment_va_name": paymentVaName,
        "subtotal": subtotal,
        "total_cost": totalCost,
        "items": items == null
            ? []
            : List<dynamic>.from(items!.map((x) => x.toMap())),
      };

  OrderRequestModel copyWith({
    int? addressId,
    String? paymentMethod,
    String? shippingService,
    int? shippingCost,
    String? paymentVaName,
    int? subtotal,
    int? discount,
    int? totalCost,
    List<Item>? items,
  }) {
    final totalDiscount = ((discount ?? this.discount!) / 100 * subtotal!);
    print('total discount $totalDiscount');
    int total = (subtotal) - (totalDiscount).toInt();
    print('total cost $total');

    return OrderRequestModel(
      addressId: addressId ?? this.addressId,
      paymentMethod: paymentMethod ?? this.paymentMethod,
      shippingService: shippingService ?? this.shippingService,
      shippingCost: shippingCost ?? this.shippingCost,
      paymentVaName: paymentVaName ?? this.paymentVaName,
      subtotal: subtotal,
      discount: discount ?? this.discount,
      totalCost: total,
      items: items ?? this.items,
    );
  }

  @override
  String toString() {
    return 'OrderRequestModel(addressId: $addressId, paymentMethod: $paymentMethod, shippingService: $shippingService, shippingCost: $shippingCost, paymentVaName: $paymentVaName, subtotal: $subtotal, discount: $discount, totalCost: $totalCost, items: $items)';
  }
}

class Item {
  final int? productId;
  final int? quantity;

  Item({
    this.productId,
    this.quantity,
  });

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

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

  factory Item.fromMap(Map<String, dynamic> json) => Item(
        productId: json["product_id"],
        quantity: json["quantity"],
      );

  Map<String, dynamic> toMap() => {
        "product_id": productId,
        "quantity": quantity,
      };
}
