import 'dart:convert';

class ProductDetailResponse {
  int? code;
  List<ProductDetails>? details;
  String? message;
  Pricedetails? pricedetails;
  bool? status;

  ProductDetailResponse({
    this.code,
    this.details,
    this.message,
    this.pricedetails,
    this.status,
  });

  factory ProductDetailResponse.fromJson(Map<String, dynamic> json) {
    return ProductDetailResponse(
      code: json['code'] is int ? json['code'] : null,
      details: json['details'] != null
          ? (json['details'] as List)
          .map((v) => ProductDetails.fromJson(v as Map<String, dynamic>))
          .toList()
          : null,
      message: json['message'] as String?,
      pricedetails: json['pricedetails'] != null
          ? Pricedetails.fromJson(json['pricedetails'] as Map<String, dynamic>)
          : null,
      status: json['status'] is bool ? json['status'] : null,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'code': code,
      'details': details?.map((v) => v.toJson()).toList(),
      'message': message,
      'pricedetails': pricedetails?.toJson(),
      'status': status,
    };
  }
}

class ProductDetails {
  int? orderdetailid;
  int? orderheaderid;
  int? tenantid;
  int? locationid;
  int? productid;
  String? productname;
  String? productdescription;
  int? supplyqty;
  int? balanceqty;
  int? orderqty;
  double? price;                    // ← double
  int? unitid;
  String? unitname;
  int? productaddonid;
  int? addontypeid;
  int? productmapid;
  int? productvariantid;
  String? productaddondescription;
  int? discountid;
  String? discountname;
  String? discountcode;
  String? discountterms;
  int? discountpercentage;
  double? discountamount;           // ← double
  double? landingamount;            // ← double
  int? taxpercentage;
  double? taxamount;                // ← double
  double? othercharges;                // ← double
  double? totaltaxamount;           // ← double
  double? productsumprice;          // ← double
  String? itemstatus;
  String? delivered;
  int? orderamount;                 // ← Note: JSON key is "Orderamount" (capital O)
  String? productimage;

  ProductDetails({
    this.orderdetailid,
    this.orderheaderid,
    this.tenantid,
    this.locationid,
    this.productid,
    this.productname,
    this.productdescription,
    this.supplyqty,
    this.balanceqty,
    this.orderqty,
    this.price,
    this.unitid,
    this.unitname,
    this.productaddonid,
    this.addontypeid,
    this.productmapid,
    this.productvariantid,
    this.productaddondescription,
    this.discountid,
    this.discountname,
    this.discountcode,
    this.discountterms,
    this.discountpercentage,
    this.discountamount,
    this.landingamount,
    this.taxpercentage,
    this.taxamount,
    this.othercharges,
    this.totaltaxamount,
    this.productsumprice,
    this.itemstatus,
    this.delivered,
    this.orderamount,
    this.productimage,
  });

  factory ProductDetails.fromJson(Map<String, dynamic> json) {
    return ProductDetails(
      orderdetailid: json['orderdetailid'] is int ? json['orderdetailid'] : null,
      orderheaderid: json['orderheaderid'] is int ? json['orderheaderid'] : null,
      tenantid: json['tenantid'] is int ? json['tenantid'] : null,
      locationid: json['locationid'] is int ? json['locationid'] : null,
      productid: json['productid'] is int ? json['productid'] : null,
      productname: json['productname'] as String?,
      productdescription: json['productdescription'] as String?,
      supplyqty: json['supplyqty'] is int ? json['supplyqty'] : null,
      balanceqty: json['balanceqty'] is int ? json['balanceqty'] : null,
      orderqty: json['orderqty'] is int ? json['orderqty'] : null,
      price: _toDouble(json['price']),
      unitid: json['unitid'] is int ? json['unitid'] : null,
      unitname: json['unitname'] as String?,
      productaddonid: json['productaddonid'] is int ? json['productaddonid'] : null,
      addontypeid: json['addontypeid'] is int ? json['addontypeid'] : null,
      productmapid: json['productmapid'] is int ? json['productmapid'] : null,
      productvariantid: json['productvariantid'] is int ? json['productvariantid'] : null,
      productaddondescription: json['productaddondescription'] as String?,
      discountid: json['discountid'] is int ? json['discountid'] : null,
      discountname: json['discountname'] as String?,
      discountcode: json['discountcode'] as String?,
      discountterms: json['discountterms'] as String?,
      discountpercentage: json['discountpercentage'] is int ? json['discountpercentage'] : null,
      discountamount: _toDouble(json['discountamount']),
      landingamount: _toDouble(json['landingamount']),
      taxpercentage: json['taxpercentage'] is int ? json['taxpercentage'] : null,
      taxamount: _toDouble(json['taxamount']),
      othercharges: _toDouble(json['othercharges']),
      totaltaxamount: _toDouble(json['totaltaxamount']),
      productsumprice: _toDouble(json['productsumprice']),
      itemstatus: json['itemstatus'] as String?,
      delivered: json['delivered'] as String?,
      orderamount: json['Orderamount'] is int ? json['Orderamount'] : null, // Capital O!
      productimage: json['productimage'] as String?,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'orderdetailid': orderdetailid,
      'orderheaderid': orderheaderid,
      'tenantid': tenantid,
      'locationid': locationid,
      'productid': productid,
      'productname': productname,
      'productdescription': productdescription,
      'supplyqty': supplyqty,
      'balanceqty': balanceqty,
      'orderqty': orderqty,
      'price': price,
      'unitid': unitid,
      'unitname': unitname,
      'productaddonid': productaddonid,
      'addontypeid': addontypeid,
      'productmapid': productmapid,
      'productvariantid': productvariantid,
      'productaddondescription': productaddondescription,
      'discountid': discountid,
      'discountname': discountname,
      'discountcode': discountcode,
      'discountterms': discountterms,
      'discountpercentage': discountpercentage,
      'discountamount': discountamount,
      'landingamount': landingamount,
      'taxpercentage': taxpercentage,
      'taxamount': taxamount,
      'othercharges': othercharges,
      'totaltaxamount': totaltaxamount,
      'productsumprice': productsumprice,
      'itemstatus': itemstatus,
      'delivered': delivered,
      'Orderamount': orderamount, // Capital O in JSON
      'productimage': productimage,
    };
  }

  // Helper to safely convert int or double → double?
  static double? _toDouble(dynamic value) {
    if (value == null) return null;
    if (value is double) return value;
    if (value is int) return value.toDouble();
    return null;
  }
}

class Pricedetails {
  double? orderamount;       // ← double
  double? totaltaxamount;    // ← double

  Pricedetails({this.orderamount, this.totaltaxamount});

  factory Pricedetails.fromJson(Map<String, dynamic> json) {
    return Pricedetails(
      orderamount: _toDouble(json['orderamount']),
      totaltaxamount: _toDouble(json['totaltaxamount']),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'orderamount': orderamount,
      'totaltaxamount': totaltaxamount,
    };
  }

  // Reuse helper
  static double? _toDouble(dynamic value) {
    if (value == null) return null;
    if (value is double) return value;
    if (value is int) return value.toDouble();
    return null;
  }
}