import 'added_by_model.dart';
import 'brand_model.dart';
import 'category_model.dart';

class PlacedOrderModel {
  String paymentMode;
  String shipTo;
  String billTo;
  double subtotal;
  double gstTotal;
  double grandTotal;
  List<OrderItem> orderItems;

  PlacedOrderModel({
    required this.paymentMode,
    required this.shipTo,
    required this.billTo,
    required this.subtotal,
    required this.gstTotal,
    required this.grandTotal,
    required this.orderItems,
  });

  Map<String, dynamic> toJson() => {
    'paymentMode': paymentMode,
    'shipTo': shipTo,
    'billTo': billTo,
    'subtotal': subtotal,
    'gstTotal': gstTotal,
    'grandTotal': grandTotal,
    'orderItems': orderItems.map((item) => item.toJson()).toList(),
  };

  factory PlacedOrderModel.fromJson(Map<String, dynamic> json) => PlacedOrderModel(
    paymentMode: json['paymentMode'],
    shipTo: json['shipTo'],
    billTo: json['billTo'],
    subtotal: json['subtotal'].toDouble(),
    gstTotal: json['gstTotal'].toDouble(),
    grandTotal: json['grandTotal'].toDouble(),
    orderItems: (json['orderItems'] as List).map((item) => OrderItem.fromJson(item)).toList(),
  );

  @override
  String toString() {
    return 'PlacedOrderModel{paymentMode: $paymentMode, '
        'shipTo: $shipTo, '
        'billTo: $billTo, '
        'subtotal: $subtotal, '
        'gstTotal: $gstTotal, '
        'grandTotal: $grandTotal, '
        'orderItems: ${orderItems.map((item) => item.toString()).join(', ')})';
  }
}


class OrderItem {
  String id;
  String sku;
  String name;
  double price;
  double gst;
  int hsnCode;
  String description;
  String productStatus;
  final String addedBy;
  List<String> image;
  DateTime createdAt;
  DateTime updatedAt;
  int count;
  Category category;
  Brand brand;
  int v;

  OrderItem({
    required this.id,
    required this.sku,
    required this.name,
    required this.price,
    required this.gst,
    required this.hsnCode,
    required this.description,
    required this.productStatus,
    required this.addedBy,
    required this.image,
    required this.createdAt,
    required this.updatedAt,
    required this.count,
    required this.category,
    required this.brand,
    required this.v,
  });

  // Adjusted toJson method to match the desired format
  Map<String, dynamic> toJson() => {
    '_id': id,
    'SKU': sku,
    'name': name,
    'category': category.toJson(),  // Ensure Category has a proper toJson method
    'brand': brand.toJson(),        // Ensure Brand has a proper toJson method
    'price': price,
    'GST': gst,
    'HSN_Code': hsnCode,
    'description': description,
    'product_Status': productStatus,
    'addedBy': {
      '_id': id,       // Assuming the addedBy is represented by the id
      'name': addedBy  // If addedBy is just a name, use it directly here
    },
    'image': image,
    'createdAt': createdAt.toIso8601String(),
    'updatedAt': updatedAt.toIso8601String(),
    '__v': v,
    'count': count,
  };

  factory OrderItem.fromJson(Map<String, dynamic> json) => OrderItem(
    id: json['_id'],
    sku: json['SKU'],
    name: json['name'],
    price: json['price'].toDouble(),
    gst: json['GST'].toDouble(),
    hsnCode: json['HSN_Code'],
    description: json['description'],
    productStatus: json['product_Status'],
    addedBy: json['addedBy']['name'],  // Assuming addedBy has a 'name' key
    image: List<String>.from(json['image']),
    createdAt: DateTime.parse(json['createdAt']),
    updatedAt: DateTime.parse(json['updatedAt']),
    count: json['count'],
    category: Category.fromJson(json['category']),
    brand: Brand.fromJson(json['brand']),
    v: json['__v'],
  );

  @override
  String toString() {
    return 'OrderItem(id: $id, sku: $sku, name: $name, price: $price, gst: $gst, hsnCode: $hsnCode, description: $description, productStatus: $productStatus, addedBy: $addedBy, image: $image, createdAt: $createdAt, updatedAt: $updatedAt, count: $count, category: $category, brand: $brand, v: $v)';
  }
}

