import 'dart:convert';

class GetProducts {
  int? code;
  List<ProductData>? details; // Changed from 'data' to 'details'
  String? message;
  bool? status;

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

  GetProducts.fromJson(Map<String, dynamic> json) {
    code = json['code'];
    if (json['details'] != null) {
      details = <ProductData>[];
      json['details'].forEach((v) {
        details!.add(ProductData.fromJson(v));
      });
    }
    message = json['message'];
    status = json['status'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['code'] = code;
    if (details != null) {
      data['details'] = details!.map((v) => v.toJson()).toList();
    }
    data['message'] = message;
    data['status'] = status;
    return data;
  }
}

class ProductData {
  int? productid;
  int? applocationid;
  int? productlocationid;
  int? tenantid;
  int? categoryid;
  String? categoryname;
  int? subcategoryid;
  String? subcategoryname;
  String? productname;
  String? productimage;
  String? productdesc;
  String? productunit;
  String? unitvalue;
  double? productcost;
  double? taxamount;
  double? taxpercent;
  int? producttax;
  int? productstock;
  int? productcombo;
  int? variants;
  int? quantity;
  int? approve;
  String? status;

  ProductData({
    this.productid,
    this.applocationid,
    this.productlocationid,
    this.tenantid,
    this.categoryid,
    this.categoryname,
    this.subcategoryid,
    this.subcategoryname,
    this.productname,
    this.productimage,
    this.productdesc,
    this.productunit,
    this.unitvalue,
    this.productcost,
    this.taxamount,
    this.taxpercent,
    this.producttax,
    this.productstock,
    this.productcombo,
    this.variants,
    this.quantity,
    this.approve,
    this.status,
  });

  ProductData.fromJson(Map<String, dynamic> json) {
    productid = json['productid'];
    applocationid = json['applocationid'];
    productlocationid = json['productlocationid'];
    tenantid = json['tenantid'];
    categoryid = json['categoryid'];
    categoryname = json['categoryname'] ?? '';
    subcategoryid = json['subcategoryid'];
    subcategoryname = json['Subcategoryname'] ?? '';
    productname = json['productname'];
    productimage = json['productimage'];
    productdesc = json['productdesc'];
    productunit = json['productunit'];
    unitvalue = json['unitvalue'];
    productcost = json['productcost'] is int
        ? (json['productcost'] as int).toDouble()
        : json['productcost'];
    taxamount = json['taxamount']?.toDouble();
    taxpercent = json['taxpercent']?.toDouble();
    producttax = json['producttax'];
    productstock = json['productstock'];
    productcombo = json['productcombo'];
    variants = json['variants'];
    quantity = json['quantity'];
    approve = json['approve'];
    status = json['status'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['productid'] = productid;
    data['applocationid'] = applocationid;
    data['productlocationid'] = productlocationid;
    data['tenantid'] = tenantid;
    data['categoryid'] = categoryid;
    data['categoryname'] = categoryname;
    data['subcategoryid'] = subcategoryid;
    data['Subcategoryname'] = subcategoryname;
    data['productname'] = productname;
    data['productimage'] = productimage;
    data['productdesc'] = productdesc;
    data['productunit'] = productunit;
    data['unitvalue'] = unitvalue;
    data['productcost'] = productcost;
    data['taxamount'] = taxamount;
    data['taxpercent'] = taxpercent;
    data['producttax'] = producttax;
    data['productstock'] = productstock;
    data['productcombo'] = productcombo;
    data['variants'] = variants;
    data['quantity'] = quantity;
    data['approve'] = approve;
    data['status'] = status;
    return data;
  }

  // Optional: copyWith for immutable updates
  ProductData copyWith({
    int? productid,
    int? applocationid,
    int? productlocationid,
    int? tenantid,
    int? categoryid,
    String? categoryname,
    int? subcategoryid,
    String? subcategoryname,
    String? productname,
    String? productimage,
    String? productdesc,
    String? productunit,
    String? unitvalue,
    double? productcost,
    double? taxamount,
    double? taxpercent,
    int? producttax,
    int? productstock,
    int? productcombo,
    int? variants,
    int? quantity,
    int? approve,
    String? status,
  }) {
    return ProductData(
      productid: productid ?? this.productid,
      applocationid: applocationid ?? this.applocationid,
      productlocationid: productlocationid ?? this.productlocationid,
      tenantid: tenantid ?? this.tenantid,
      categoryid: categoryid ?? this.categoryid,
      categoryname: categoryname ?? this.categoryname,
      subcategoryid: subcategoryid ?? this.subcategoryid,
      subcategoryname: subcategoryname ?? this.subcategoryname,
      productname: productname ?? this.productname,
      productimage: productimage ?? this.productimage,
      productdesc: productdesc ?? this.productdesc,
      productunit: productunit ?? this.productunit,
      unitvalue: unitvalue ?? this.unitvalue,
      productcost: productcost ?? this.productcost,
      taxamount: taxamount ?? this.taxamount,
      taxpercent: taxpercent ?? this.taxpercent,
      producttax: producttax ?? this.producttax,
      productstock: productstock ?? this.productstock,
      productcombo: productcombo ?? this.productcombo,
      variants: variants ?? this.variants,
      quantity: quantity ?? this.quantity,
      approve: approve ?? this.approve,
      status: status ?? this.status,
    );
  }
}

// Keeping `Products` class for backward compatibility (optional)
class Products extends ProductData {
  Products({
    int? productid,
    int? applocationid,
    int? productlocationid,
    int? tenantid,
    int? categoryid,
    String? categoryname,
    int? subcategoryid,
    String? subcategoryname,
    String? productname,
    String? productimage,
    String? productdesc,
    String? productunit,
    String? unitvalue,
    double? productcost,
    double? taxamount,
    double? taxpercent,
    int? producttax,
    int? productstock,
    int? productcombo,
    int? variants,
    int? quantity,
    int? approve,
    String? status,
  }) : super(
    productid: productid,
    applocationid: applocationid,
    productlocationid: productlocationid,
    tenantid: tenantid,
    categoryid: categoryid,
    categoryname: categoryname,
    subcategoryid: subcategoryid,
    subcategoryname: subcategoryname,
    productname: productname,
    productimage: productimage,
    productdesc: productdesc,
    productunit: productunit,
    unitvalue: unitvalue,
    productcost: productcost,
    taxamount: taxamount,
    taxpercent: taxpercent,
    producttax: producttax,
    productstock: productstock,
    productcombo: productcombo,
    variants: variants,
    quantity: quantity,
    approve: approve,
    status: status,
  );

  factory Products.fromJson(Map<String, dynamic> json) {
    return Products(
      productid: json['productid'],
      applocationid: json['applocationid'],
      productlocationid: json['productlocationid'],
      tenantid: json['tenantid'],
      categoryid: json['categoryid'],
      categoryname: json['categoryname'] ?? '',
      subcategoryid: json['subcategoryid'],
      subcategoryname: json['Subcategoryname'] ?? '',
      productname: json['productname'],
      productimage: json['productimage'],
      productdesc: json['productdesc'],
      productunit: json['productunit'],
      unitvalue: json['unitvalue'],
      productcost: json['productcost'] is int
          ? (json['productcost'] as int).toDouble()
          : json['productcost'],
      taxamount: json['taxamount']?.toDouble(),
      taxpercent: json['taxpercent']?.toDouble(),
      producttax: json['producttax'],
      productstock: json['productstock'],
      productcombo: json['productcombo'],
      variants: json['variants'],
      quantity: json['quantity'],
      approve: json['approve'],
      status: json['status'],
    );
  }
}