// import 'package:cheminova/models/rd_order_item_model.dart';
//
// class SingleGetOrderModel {
//   final String id;
//    String? paymentMode;
//    String? shipTo;
//    String? billTo;
//    List<RDOrderItem>? orderItem;
//    double? subtotal;
//    double? gstTotal;
//    double? grandTotal;
//    String? status;
//    List<dynamic>? invoices;
//    AddedBy? addedBy;
//    String? pd;
//    bool? isCancelled;
//    bool? isDelivered;
//    String? deliveredDate;
//    DateTime? statusUpdatedAt;
//    String? uniqueId;
//    DateTime? createdAt;
//    DateTime? updatedAt;
//
//   SingleGetOrderModel({
//     required this.id,
//      this.paymentMode,
//      this.shipTo,
//      this.billTo,
//      this.orderItem,
//      this.subtotal,
//      this.gstTotal,
//      this.grandTotal,
//      this.status,
//      this.invoices,
//      this.addedBy,
//      this.pd,
//      this.isCancelled,
//     this.isDelivered,
//      this.deliveredDate,
//      this.statusUpdatedAt,
//     this.uniqueId,
//   this.createdAt,
//      this.updatedAt,
//   });
//
//   factory SingleGetOrderModel.fromJson(Map<String, dynamic> json) {
//     return SingleGetOrderModel(
//       id: json['_id'] ?? '',
//       paymentMode: json['paymentMode'] ?? '',
//       shipTo: json['shipTo'] ?? '',
//       billTo: json['billTo'] ?? '',
//       orderItem: (json['orderItem'] as List<dynamic>?)
//           ?.map((item) => RDOrderItem.fromJson(item))
//           .toList() ??
//           [],
//       subtotal: (json['subtotal'] as num?)?.toDouble() ?? 0.0,
//       gstTotal: (json['gstTotal'] as num?)?.toDouble() ?? 0.0,
//       grandTotal: (json['grandTotal'] as num?)?.toDouble() ?? 0.0,
//       status: json['status'] ?? '',
//       invoices: json['invoices'] ?? [],
//       addedBy: AddedBy.fromJson(json['addedBy'] ?? {}),
//       pd: json['pd'] ?? '',
//       isCancelled: json['iscancelled'] ?? false,
//       isDelivered: json['isDelivered'] ?? false,
//       deliveredDate: json['DeliveredDate'] ?? '',
//       statusUpdatedAt: DateTime.parse(json['statusUpdatedAt'] ?? DateTime.now().toString()),
//       uniqueId: json['uniqueId'] ?? '',
//       createdAt: DateTime.parse(json['createdAt'] ?? DateTime.now().toString()),
//       updatedAt: DateTime.parse(json['updatedAt'] ?? DateTime.now().toString()),
//     );
//   }
//
//   Map<String, dynamic> toJson() {
//     return {
//       '_id': id,
//       'paymentMode': paymentMode,
//       'shipTo': shipTo,
//       'billTo': billTo,
//       'orderItem': orderItem?.map((item) => item.toJson()).toList(),
//       'subtotal': subtotal,
//       'gstTotal': gstTotal,
//       'grandTotal': grandTotal,
//       'status': status,
//       'invoices': invoices,
//       'addedBy': addedBy?.toJson(),
//       'pd': pd,
//       'iscancelled': isCancelled,
//       'isDelivered': isDelivered,
//       'DeliveredDate': deliveredDate,
//       'statusUpdatedAt': statusUpdatedAt?.toIso8601String(),
//       'uniqueId': uniqueId,
//       'createdAt': createdAt?.toIso8601String(),
//       'updatedAt': updatedAt?.toIso8601String(),
//     };
//   }
//
//   @override
//   String toString() {
//     return 'SingleGetOrderModel(id: $id, paymentMode: $paymentMode, shipTo: $shipTo, billTo: $billTo, '
//         'orderItem: $orderItem, subtotal: $subtotal, gstTotal: $gstTotal, grandTotal: $grandTotal, '
//         'status: $status, invoices: $invoices, addedBy: $addedBy, pd: $pd, '
//         'isCancelled: $isCancelled, isDelivered: $isDelivered, deliveredDate: $deliveredDate, '
//         'statusUpdatedAt: $statusUpdatedAt, uniqueId: $uniqueId, createdAt: $createdAt, updatedAt: $updatedAt)';
//   }
// }
//
// class OrderItem {
//   final String productId;
//   final String sku;
//   final String name;
//   final String categoryName;
//   final String brandName;
//   final int price; // Use int for price
//   final int gst; // Use int for GST
//   final int hsnCode; // Use int for HSN Code
//   final String description;
//   final List<String> image; // Assuming images are stored as a list of strings
//   final int quantity;
//   final int remainingQuantity;
//   int? processQuantity;
//   final String id; // Use String for _id
//
//   OrderItem({
//     required this.productId,
//     required this.sku,
//     required this.name,
//     required this.categoryName,
//     required this.brandName,
//     required this.price,
//     required this.gst,
//     required this.hsnCode,
//     required this.description,
//     required this.image,
//     required this.quantity,
//     required this.remainingQuantity,
//     this.processQuantity = 1,
//     required this.id,
//   });
//
//   factory OrderItem.fromJson(Map<String, dynamic> json) {
//     return OrderItem(
//       productId: json['productId'] ?? '',
//       sku: json['SKU'] ?? '',
//       name: json['name'] ?? '',
//       categoryName: json['categoryName'] ?? '',
//       brandName: json['brandName'] ?? '',
//       price: (json['price'] as num?)?.toInt() ?? 0, // Handle as num
//       gst: (json['GST'] as num?)?.toInt() ?? 0, // Handle as num
//       hsnCode: (json['HSN_Code'] as num?)?.toInt() ?? 0, // Handle as num
//       description: json['description'] ?? '',
//       image: List<String>.from(json['image'] ?? []),
//       quantity: (json['quantity'] as num?)?.toInt() ?? 0, // Handle as num
//       remainingQuantity: (json['remainingQuantity'] as num?)?.toInt() ?? 0, // Handle as num
//       processQuantity: (json['processQuantity']as num?)?.toInt()??0,
//       id: json['_id'] ?? '',
//     );
//   }
//
//   Map<String, dynamic> toJson() {
//     return {
//       'productId': productId,
//       'SKU': sku,
//       'name': name,
//       'categoryName': categoryName,
//       'brandName': brandName,
//       'price': price,
//       'GST': gst,
//       'HSN_Code': hsnCode,
//       'description': description,
//       'image': image,
//       'quantity': quantity,
//       'remainingQuantity': remainingQuantity,
//       '_id': id,
//     };
//   }
//
//   @override
//   String toString() {
//     return 'OrderItem(productId: $productId, SKU: $sku, name: $name, categoryName: $categoryName, '
//         'brandName: $brandName, price: $price, GST: $gst, HSN_Code: $hsnCode, '
//         'description: $description, image: $image, quantity: $quantity, '
//         'remainingQuantity: $remainingQuantity, id: $id)';
//   }
// }
//
// class AddedBy {
//   final String id;
//   final String designation;
//   final String name;
//   final String email;
//   final String mobileNumber;
//   final String principalDistributor;
//   final String addedBy;
//   final String userType;
//   final String kyc;
//   String? fcmToken;
//   final DateTime createdAt;
//   final DateTime updatedAt;
//   final String mappedSC;
//   final String uniqueId;
//   final String mappedTM;
//
//   AddedBy({
//     required this.id,
//     required this.designation,
//     required this.name,
//     required this.email,
//     required this.mobileNumber,
//     required this.principalDistributor,
//     required this.addedBy,
//     required this.userType,
//     required this.kyc,
//     this.fcmToken,
//     required this.createdAt,
//     required this.updatedAt,
//     required this.mappedSC,
//     required this.uniqueId,
//     required this.mappedTM,
//   });
//
//   factory AddedBy.fromJson(Map<String, dynamic> json) {
//     return AddedBy(
//       id: json['_id'] ?? '',
//       designation: json['designation'] ?? '',
//       name: json['name'] ?? '',
//       email: json['email'] ?? '',
//       mobileNumber: json['mobile_number'] ?? '',
//       principalDistributor: json['principal_distributer'] ?? '',
//       addedBy: json['addedBy'] ?? '',
//       userType: json['userType'] ?? '',
//       kyc: json['kyc'] ?? '',
//       fcmToken: json['fcm_token']?.toString(), // Handle null safely
//       createdAt: DateTime.parse(json['createdAt'] ?? DateTime.now().toString()),
//       updatedAt: DateTime.parse(json['updatedAt'] ?? DateTime.now().toString()),
//       mappedSC: json['mappedSC'] ?? '',
//       uniqueId: json['uniqueId'] ?? '',
//       mappedTM: json['mappedTM'] ?? '',
//     );
//   }
//
//   Map<String, dynamic> toJson() {
//     return {
//       '_id': id,
//       'designation': designation,
//       'name': name,
//       'email': email,
//       'mobile_number': mobileNumber,
//       'principal_distributer': principalDistributor,
//       'addedBy': addedBy,
//       'userType': userType,
//       'kyc': kyc,
//       'fcm_token': fcmToken,
//       'createdAt': createdAt.toIso8601String(),
//       'updatedAt': updatedAt.toIso8601String(),
//       'mappedSC': mappedSC,
//       'uniqueId': uniqueId,
//       'mappedTM': mappedTM,
//     };
//   }
//
//   @override
//   String toString() {
//     return 'AddedBy(id: $id, designation: $designation, name: $name, email: $email, '
//         'mobileNumber: $mobileNumber, principalDistributor: $principalDistributor, '
//         'addedBy: $addedBy, userType: $userType, kyc: $kyc, '
//         'fcmToken: $fcmToken, createdAt: $createdAt, updatedAt: $updatedAt, '
//         'mappedSC: $mappedSC, uniqueId: $uniqueId, mappedTM: $mappedTM)';
//   }
// }
import 'dart:convert';

import 'package:cheminova/models/rd_order_item_model.dart';

// Main model for the order
class SingleGetOrderModel {
  String id;
  String paymentMode;
  String shipTo;
  String billTo;
  List<RDOrderItem> orderItem;
  double subtotal;
  double gstTotal;
  double grandTotal;
  String status;
  List<Invoice> invoices;
  AddedBy addedBy;
  String pd;
  bool isCancelled;
  bool isDelivered;
  String deliveredDate;
  String statusUpdatedAt;
  String uniqueId;
  String createdAt;
  String updatedAt;

  SingleGetOrderModel({
    required this.id,
    required this.paymentMode,
    required this.shipTo,
    required this.billTo,
    required this.orderItem,
    required this.subtotal,
    required this.gstTotal,
    required this.grandTotal,
    required this.status,
    required this.invoices,
    required this.addedBy,
    required this.pd,
    required this.isCancelled,
    required this.isDelivered,
    required this.deliveredDate,
    required this.statusUpdatedAt,
    required this.uniqueId,
    required this.createdAt,
    required this.updatedAt,
  });

  factory SingleGetOrderModel.fromJson(Map<String, dynamic> json) {
    return SingleGetOrderModel(
      id: json['_id']??"1",
      paymentMode: json['paymentMode']??"2343",
      shipTo: json['shipTo']??"abc",
      billTo: json['billTo']??"xyz",
      orderItem: (json['orderItem'] as List)
          .map((i) => RDOrderItem.fromJson(i))
          .toList(),
      subtotal: json['subtotal'].toDouble(),
      gstTotal: json['gstTotal'].toDouble(),
      grandTotal: json['grandTotal'].toDouble(),
      status: json['status'],
      invoices: (json['invoices'] as List)
          .map((i) => Invoice.fromJson(i))
          .toList(),
      addedBy: AddedBy.fromJson(json['addedBy']??""),
      pd: json['pd']??"",
      isCancelled: json['iscancelled']??"",
      isDelivered: json['isDelivered']??"",
      deliveredDate: json['DeliveredDate']??"",
      statusUpdatedAt: json['statusUpdatedAt']??"",
      uniqueId: json['uniqueId']??"",
      createdAt: json['createdAt']??"",
      updatedAt: json['updatedAt']??"",
    );
  }

  Map<String, dynamic> toJson() {
    return {
      '_id': id,
      'paymentMode': paymentMode,
      'shipTo': shipTo,
      'billTo': billTo,
      'orderItem': orderItem.map((i) => i.toJson()).toList(),
      'subtotal': subtotal,
      'gstTotal': gstTotal,
      'grandTotal': grandTotal,
      'status': status,
      'invoices': invoices.map((i) => i.toJson()).toList(),
      'addedBy': addedBy.toJson(),
      'pd': pd,
      'iscancelled': isCancelled,
      'isDelivered': isDelivered,
      'DeliveredDate': deliveredDate,
      'statusUpdatedAt': statusUpdatedAt,
      'uniqueId': uniqueId,
      'createdAt': createdAt,
      'updatedAt': updatedAt,
    };
  }
}

// Model for individual order items


// Model for invoices
class Invoice {
  Map<String, String> courierStatusTimeline;
  String id;
  String invoiceId;
  String orderId;
  List<InvoiceItem> items;
  double subtotal;
  double gstTotal;
  double invoiceAmount;
  String courierStatus;
  int v;
  String courierName;
  String courierTrackingId;

  Invoice({
    required this.courierStatusTimeline,
    required this.id,
    required this.invoiceId,
    required this.orderId,
    required this.items,
    required this.subtotal,
    required this.gstTotal,
    required this.invoiceAmount,
    required this.courierStatus,
    required this.v,
    required this.courierName,
    required this.courierTrackingId,
  });

  factory Invoice.fromJson(Map<String, dynamic> json) {
    return Invoice(
      courierStatusTimeline: Map<String, String>.from(json['courierstatus_timeline']??""),
      id: json['_id']??"",
      invoiceId: json['invoiceId']??"",
      orderId: json['orderId']??"",
      items: (json['items'] as List)
          .map((i) => InvoiceItem.fromJson(i))
          .toList(),
      subtotal: json['subtotal'].toDouble(),
      gstTotal: json['gstTotal'].toDouble(),
      invoiceAmount: json['invoiceAmount'].toDouble(),
      courierStatus: json['courierStatus'],
      v: json['__v'],
      courierName: json['courier_name']??"",
      courierTrackingId: json['courier_tracking_id']??"",
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'courierstatus_timeline': courierStatusTimeline,
      '_id': id,
      'invoiceId': invoiceId,
      'orderId': orderId,
      'items': items.map((i) => i.toJson()).toList(),
      'subtotal': subtotal,
      'gstTotal': gstTotal,
      'invoiceAmount': invoiceAmount,
      'courierStatus': courierStatus,
      '__v': v,
      'courier_name': courierName,
      'courier_tracking_id': courierTrackingId,
    };
  }
}

// Model for items in an invoice
class InvoiceItem {
  String productId;
  String sku;
  String name;
  String categoryName;
  String brandName;
  double price;
  double gst;
  int hsnCode;
  int processQuantity;
  String id;

  InvoiceItem({
    required this.productId,
    required this.sku,
    required this.name,
    required this.categoryName,
    required this.brandName,
    required this.price,
    required this.gst,
    required this.hsnCode,
    required this.processQuantity,
    required this.id,
  });

  factory InvoiceItem.fromJson(Map<String, dynamic> json) {
    return InvoiceItem(
      productId: json['productId']??"",
      sku: json['SKU']??"",
      name: json['name']??"",
      categoryName: json['categoryName']??"",
      brandName: json['brandName']??"",
      price: json['price'].toDouble(),
      gst: json['GST'].toDouble(),
      hsnCode: json['HSN_Code']??"",
      processQuantity: json['processquantity']??"",
      id: json['_id']??"",
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'productId': productId,
      'SKU': sku,
      'name': name,
      'categoryName': categoryName,
      'brandName': brandName,
      'price': price,
      'GST': gst,
      'HSN_Code': hsnCode,
      'processquantity': processQuantity,
      '_id': id,
    };
  }
}

//Model for the user who added the order
class AddedBy {
  final String id;
  final String designation;
  final String name;
  final String email;
  final String mobileNumber;
  final String principalDistributor;
  final String addedBy;
  final String userType;
  final String kyc;
  String? fcmToken;
  final DateTime createdAt;
  final DateTime updatedAt;
  final String mappedSC;
  final String uniqueId;
  final String mappedTM;

  AddedBy({
    required this.id,
    required this.designation,
    required this.name,
    required this.email,
    required this.mobileNumber,
    required this.principalDistributor,
    required this.addedBy,
    required this.userType,
    required this.kyc,
    this.fcmToken,
    required this.createdAt,
    required this.updatedAt,
    required this.mappedSC,
    required this.uniqueId,
    required this.mappedTM,
  });

  factory AddedBy.fromJson(Map<String, dynamic> json) {
    return AddedBy(
      id: json['_id'] ?? '',
      designation: json['designation'] ?? '',
      name: json['name'] ?? '',
      email: json['email'] ?? '',
      mobileNumber: json['mobile_number'] ?? '',
      principalDistributor: json['principal_distributer'] ?? '',
      addedBy: json['addedBy'] ?? '',
      userType: json['userType'] ?? '',
      kyc: json['kyc'] ?? '',
      fcmToken: json['fcm_token']?.toString(),
      // Handle null safely
      createdAt: DateTime.parse(json['createdAt'] ?? DateTime.now().toString()),
      updatedAt: DateTime.parse(json['updatedAt'] ?? DateTime.now().toString()),
      mappedSC: json['mappedSC'] ?? '',
      uniqueId: json['uniqueId'] ?? '',
      mappedTM: json['mappedTM'] ?? '',
    );
  }

  Map<String, dynamic> toJson() {
    return {
      '_id': id,
      'designation': designation,
      'name': name,
      'email': email,
      'mobile_number': mobileNumber,
      'principal_distributer': principalDistributor,
      'addedBy': addedBy,
      'userType': userType,
      'kyc': kyc,
      'fcm_token': fcmToken,
      'createdAt': createdAt.toIso8601String(),
      'updatedAt': updatedAt.toIso8601String(),
      'mappedSC': mappedSC,
      'uniqueId': uniqueId,
      'mappedTM': mappedTM,
    };
  }

  @override
  String toString() {
    return 'AddedBy(id: $id, designation: $designation, name: $name, email: $email, '
        'mobileNumber: $mobileNumber, principalDistributor: $principalDistributor, '
        'addedBy: $addedBy, userType: $userType, kyc: $kyc, '
        'fcmToken: $fcmToken, createdAt: $createdAt, updatedAt: $updatedAt, '
        'mappedSC: $mappedSC, uniqueId: $uniqueId, mappedTM: $mappedTM)';
  }
}
// Function to parse the JSON string and create a SingleOrder object
SingleGetOrderModel parseSingleOrder(String jsonString) {
  final jsonData = json.decode(jsonString);
  return SingleGetOrderModel.fromJson(jsonData);
}
