import 'dart:convert';
//
// class InvoiceResponseModel {
//   final int totalCount;
//   final int currentPage;
//   final int totalPages;
//   final List<Invoice> invoices;
//
//   InvoiceResponseModel({
//     required this.totalCount,
//     required this.currentPage,
//     required this.totalPages,
//     required this.invoices,
//   });
//
//   factory InvoiceResponseModel.fromJson(Map<String, dynamic> json) {
//     return InvoiceResponseModel(
//       totalCount: json['totalCount'] ?? 0,
//       currentPage: json['currentPage'] ?? 1,
//       totalPages: json['totalPages'] ?? 1,
//       invoices: json['invoices'] != null
//           ? List<Invoice>.from(json['invoices'].map((x) => Invoice.fromJson(x)))
//           : [], // Handle the case where 'invoices' is null
//     );
//   }
//
//   Map<String, dynamic> toJson() {
//     return {
//       'totalCount': totalCount,
//       'currentPage': currentPage,
//       'totalPages': totalPages,
//       'invoices': invoices.map((invoice) => invoice.toJson()).toList(),
//     };
//   }
//
//   @override
//   String toString() {
//     return 'InvoiceResponse(totalCount: $totalCount, currentPage: $currentPage, totalPages: $totalPages, invoices: $invoices)';
//   }
// }


class InvoiceResponseModel {
  final String id;
  final String invoiceId;
  final Order orderId;
  final List<Item> items;
  final double subtotal;
  final double gstTotal;
  final double invoiceAmount;
  final String courierStatus;
  final Map<String, String> courierStatusTimeline;

  InvoiceResponseModel({
    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.courierStatusTimeline,
  });

  factory InvoiceResponseModel.fromJson(Map<String, dynamic> json) {
    return InvoiceResponseModel(
      id: json['_id'],
      invoiceId: json['invoiceId'],
      orderId: Order.fromJson(json['orderId']),
      items: json['items'] != null
          ? List<Item>.from(json['items'].map((item) => Item.fromJson(item)))
          : [], // Provide an empty list if 'items' is null
      subtotal: json['subtotal'].toDouble(),
      gstTotal: json['gstTotal'].toDouble(),
      invoiceAmount: json['invoiceAmount'].toDouble(),
      courierStatus: json['courierStatus'],
        courierStatusTimeline: Map<String, String>.from(json['courierstatus_timeline'] ?? {}),
        //Provide an empty map if 'courierstatus_timeline' is null
    );
  }

  Map<String, dynamic> toJson() {
    return {
      '_id': id,
      'invoiceId': invoiceId,
      'orderId': orderId.toJson(),
      'items': items.map((item) => item.toJson()).toList(),
      'subtotal': subtotal,
      'gstTotal': gstTotal,
      'invoiceAmount': invoiceAmount,
      'courierStatus': courierStatus,
      'courierstatus_timeline': courierStatusTimeline,
    };
  }

  @override
  String toString() {
    return 'Invoice(id: $id, invoiceId: $invoiceId, orderId: $orderId, items: $items, subtotal: $subtotal, gstTotal: $gstTotal, invoiceAmount: $invoiceAmount, courierStatus: $courierStatus)';
  }
}

class Order {
  final String id;
  final String uniqueId;

  Order({
    required this.id,
    required this.uniqueId,
  });

  factory Order.fromJson(Map<String, dynamic> json) {
    return Order(
      id: json['_id'],
      uniqueId: json['uniqueId'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      '_id': id,
      'uniqueId': uniqueId,
    };
  }

  @override
  String toString() {
    return 'Order(id: $id, uniqueId: $uniqueId)';
  }
}

class Item {
  final String productId;
  final String sku;
  final String name;
  final String categoryName;
  final String brandName;
  final double price;
  final double gst;
  final int hsnCode;
  final int processQuantity;
  final String id;

  Item({
    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 Item.fromJson(Map<String, dynamic> json) {
    return Item(
      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,
    };
  }

  @override
  String toString() {
    return 'Item(productId: $productId, sku: $sku, name: $name, categoryName: $categoryName, brandName: $brandName, price: $price, gst: $gst, hsnCode: $hsnCode, processQuantity: $processQuantity)';
  }
}
