import '../../../model/sale_transaction_model.dart';
import '../../../widgets/multipal payment mathods/model/payment_transaction_model.dart';

class PurchaseTransaction {
  PurchaseTransaction({
    this.id,
    this.partyId,
    this.businessId,
    this.userId,
    this.discountAmount,
    this.discountPercent,
    this.discountType,
    this.shippingCharge,
    this.dueAmount,
    this.paidAmount,
    this.changeAmount,
    this.totalAmount,
    this.invoiceNumber,
    this.isPaid,
    this.paymentTypeId,
    this.paymentType,
    this.purchaseDate,
    this.createdAt,
    this.updatedAt,
    this.user,
    this.party,
    this.details,
    this.purchaseReturns,
    this.transactions, // New Field
    this.vatAmount,
    this.vatId,
    this.vatPercent,
    this.vat,
    this.branch,
  });

  PurchaseTransaction.fromJson(dynamic json) {
    id = num.tryParse(json['id']?.toString() ?? '');
    partyId = num.tryParse(json['party_id']?.toString() ?? '');
    businessId = num.tryParse(json['business_id']?.toString() ?? '');
    userId = num.tryParse(json['user_id']?.toString() ?? '');
    discountAmount = num.tryParse(json['discountAmount']?.toString() ?? '');
    discountPercent = num.tryParse(json['discount_percent']?.toString() ?? '');
    shippingCharge = num.tryParse(json['shipping_charge']?.toString() ?? '');
    discountType = json['discount_type'];
    dueAmount = num.tryParse(json['dueAmount']?.toString() ?? '');
    changeAmount = num.tryParse(json['change_amount']?.toString() ?? '');
    vatAmount = num.tryParse(json['vat_amount']?.toString() ?? '');
    vatPercent = num.tryParse(json['vat_percent']?.toString() ?? '');
    vatId = num.tryParse(json['vat_id']?.toString() ?? '');
    paidAmount = num.tryParse(json['paidAmount']?.toString() ?? '');
    totalAmount = num.tryParse(json['totalAmount']?.toString() ?? '');
    invoiceNumber = json['invoiceNumber'];
    isPaid = json['isPaid'];
    paymentTypeId = int.tryParse(json["payment_type_id"]?.toString() ?? '');

    vat = json['vat'] != null ? PurchaseVat.fromJson(json['vat']) : null;
    purchaseDate = json['purchaseDate'];
    createdAt = json['created_at'];
    updatedAt = json['updated_at'];
    paymentType = json['payment_type'] != null ? PaymentType.fromJson(json['payment_type']) : null;
    branch = json['branch'] != null ? Branch.fromJson(json['branch']) : null;
    user = json['user'] != null ? User.fromJson(json['user']) : null;
    party = json['party'] != null ? Party.fromJson(json['party']) : null;

    if (json['details'] != null) {
      details = [];
      json['details'].forEach((v) {
        details?.add(PurchaseDetails.fromJson(v));
      });
    }

    if (json['purchase_returns'] != null) {
      purchaseReturns = [];
      json['purchase_returns'].forEach((v) {
        purchaseReturns?.add(PurchaseReturn.fromJson(v));
      });
    }

    // New List from JSON
    if (json['transactions'] != null) {
      transactions = [];
      json['transactions'].forEach((v) {
        transactions?.add(PaymentsTransaction.fromJson(v));
      });
    }
  }

  num? id;
  num? partyId;
  num? businessId;
  num? userId;
  num? discountAmount;
  num? discountPercent;
  num? shippingCharge;
  String? discountType;
  num? dueAmount;
  num? paidAmount;
  num? changeAmount;
  num? vatAmount;
  num? vatPercent;
  num? vatId;
  num? totalAmount;
  String? invoiceNumber;
  bool? isPaid;
  int? paymentTypeId;
  PaymentType? paymentType;
  Branch? branch;
  String? purchaseDate;
  String? createdAt;
  String? updatedAt;
  User? user;
  Party? party;
  List<PurchaseDetails>? details;
  List<PurchaseReturn>? purchaseReturns;
  List<PaymentsTransaction>? transactions; // Added
  PurchaseVat? vat;
}

class PurchaseDetails {
  PurchaseDetails({
    this.id,
    this.purchaseId,
    this.productId,
    this.productPurchasePrice,
    this.quantities,
    this.productWholeSalePrice,
    this.productSalePrice,
    this.productDealerPrice,
    this.productStock,
    this.profitPercent,
    this.mfgDate,
    this.expireDate,
    this.stockId,
    this.product,
    this.stock,
  });

  PurchaseDetails.fromJson(dynamic json) {
    id = num.tryParse(json['id']?.toString() ?? '');
    purchaseId = num.tryParse(json['purchase_id']?.toString() ?? '');
    productId = num.tryParse(json['product_id']?.toString() ?? '');
    productPurchasePrice = num.tryParse(json['productPurchasePrice']?.toString() ?? '');
    quantities = num.tryParse(json['quantities']?.toString() ?? '');
    productDealerPrice = num.tryParse(json['productDealerPrice']?.toString() ?? '');
    productSalePrice = num.tryParse(json['productSalePrice']?.toString() ?? '');
    productStock = num.tryParse(json['productStock']?.toString() ?? '');
    profitPercent = num.tryParse(json['profit_percent']?.toString() ?? '');
    mfgDate = json['mfg_date'];
    expireDate = json['expire_date'];
    stockId = num.tryParse(json['stock_id']?.toString() ?? ''); // Added
    productWholeSalePrice = num.tryParse(json['productWholeSalePrice']?.toString() ?? '');
    product = json['product'] != null ? Product.fromJson(json['product']) : null;
    stock = json['stock'] != null ? PurchaseStock.fromJson(json['stock']) : null;
  }

  num? id;
  num? purchaseId;
  num? productId;
  num? productPurchasePrice;
  num? quantities;
  num? productDealerPrice;
  num? productSalePrice;
  num? productWholeSalePrice;
  num? productStock;
  num? profitPercent;
  num? stockId; // Added
  PurchaseStock? stock;
  String? mfgDate;
  String? expireDate;
  Product? product;

  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    map['id'] = id;
    map['purchase_id'] = purchaseId;
    map['product_id'] = productId;
    map['productPurchasePrice'] = productPurchasePrice;
    map['quantities'] = quantities;
    map['stock_id'] = stockId;
    if (product != null) {
      map['product'] = product?.toJson();
    }
    return map;
  }
}

class Product {
  Product({
    this.id,
    this.productName,
    this.categoryId,
    this.category,
    this.productType,
    this.vatAmount,
    this.vatType,
    this.vat,
  });

  Product.fromJson(dynamic json) {
    id = num.tryParse(json['id']?.toString() ?? '');
    productName = json['productName'];
    productType = json['product_type'];
    categoryId = num.tryParse(json['category_id']?.toString() ?? '');
    vatAmount = num.tryParse(json['vat_amount']?.toString() ?? '');
    vatType = json['vat_type'];

    category = json['category'] != null ? Category.fromJson(json['category']) : null;
    vat = json['vat'] != null ? PurchaseProductVat.fromJson(json['vat']) : null;
  }

  num? id;
  String? productName;
  String? productType;
  String? vatType;
  num? categoryId;
  num? vatAmount;

  Category? category;
  PurchaseProductVat? vat;

  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    map['id'] = id;
    map['productName'] = productName;
    map['category_id'] = categoryId;
    if (category != null) {
      map['category'] = category?.toJson();
    }
    return map;
  }
}

class Category {
  Category({
    this.id,
    this.categoryName,
  });

  Category.fromJson(dynamic json) {
    id = num.tryParse(json['id']?.toString() ?? '');
    categoryName = json['categoryName'];
  }

  num? id;
  String? categoryName;

  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    map['id'] = id;
    map['categoryName'] = categoryName;
    return map;
  }
}

class PurchaseStock {
  PurchaseStock({
    this.id,
    this.batchNo,
    this.variantName,
    this.warehouseId,
  });

  PurchaseStock.fromJson(dynamic json) {
    id = num.tryParse(json['id']?.toString() ?? '');
    batchNo = json['batch_no'] ?? 'N/A';
    variantName = json['variant_name']; // Added
    warehouseId = num.tryParse(json['warehouse_id']?.toString() ?? ''); // Added
  }

  num? id;
  String? batchNo;
  String? variantName; // Added
  num? warehouseId; // Added
}

class Party {
  Party({
    this.id,
    this.name,
    this.email,
    this.phone,
    this.type,
    this.address,
  });

  Party.fromJson(dynamic json) {
    id = num.tryParse(json['id']?.toString() ?? '');
    name = json['name'];
    email = json['email'];
    phone = json['phone'];
    type = json['type']; // Added based on JSON
    address = json['address']; // Added based on JSON
  }

  num? id;
  String? name;
  String? email;
  String? address;
  String? phone;
  String? type;

  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    map['id'] = id;
    map['name'] = name;
    map['email'] = email;
    map['address'] = address;
    map['phone'] = phone;
    map['type'] = type;
    return map;
  }
}

class User {
  User({
    this.id,
    this.name,
    this.role,
  });

  User.fromJson(dynamic json) {
    id = num.tryParse(json['id']?.toString() ?? '');
    name = json['name'];
    role = json['role'];
  }

  num? id;
  String? name;
  String? role;

  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    map['id'] = id;
    map['name'] = name;
    map['role'] = role;
    return map;
  }
}

class Branch {
  Branch({
    this.id,
    this.name,
    this.phone,
    this.address,
  });

  Branch.fromJson(dynamic json) {
    id = num.tryParse(json['id']?.toString() ?? '');
    name = json['name'];
    phone = json['phone'];
    address = json['address'];
  }

  num? id;
  String? name;
  String? phone;
  String? address;
}

class PurchaseReturn {
  PurchaseReturn({
    this.id,
    this.businessId,
    this.purchaseId,
    this.invoiceNo,
    this.returnDate,
    this.createdAt,
    this.updatedAt,
    this.purchaseReturnDetails,
  });

  PurchaseReturn.fromJson(dynamic json) {
    id = num.tryParse(json['id']?.toString() ?? '');
    businessId = num.tryParse(json['business_id']?.toString() ?? '');
    purchaseId = num.tryParse(json['purchase_id']?.toString() ?? '');
    invoiceNo = json['invoice_no'];
    returnDate = json['return_date'];
    createdAt = json['created_at'];
    updatedAt = json['updated_at'];
    if (json['details'] != null) {
      purchaseReturnDetails = [];
      json['details'].forEach((v) {
        purchaseReturnDetails?.add(PurchaseReturnDetails.fromJson(v));
      });
    }
  }

  num? id;
  num? businessId;
  num? purchaseId;
  String? invoiceNo;
  String? returnDate;
  String? createdAt;
  String? updatedAt;
  List<PurchaseReturnDetails>? purchaseReturnDetails;

  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    map['id'] = id;
    map['business_id'] = businessId;
    map['purchase_id'] = purchaseId;
    map['invoice_no'] = invoiceNo;
    map['return_date'] = returnDate;
    map['created_at'] = createdAt;
    map['updated_at'] = updatedAt;
    if (purchaseReturnDetails != null) {
      map['details'] = purchaseReturnDetails?.map((v) => v.toJson()).toList();
    }
    return map;
  }
}

class PurchaseReturnDetails {
  PurchaseReturnDetails({
    this.id,
    this.businessId,
    this.purchaseReturnId,
    this.purchaseDetailId,
    this.returnAmount,
    this.returnQty,
  });

  PurchaseReturnDetails.fromJson(dynamic json) {
    id = num.tryParse(json['id']?.toString() ?? '');
    businessId = num.tryParse(json['business_id']?.toString() ?? '');
    purchaseReturnId = num.tryParse(json['purchase_return_id']?.toString() ?? '');
    purchaseDetailId = num.tryParse(json['purchase_detail_id']?.toString() ?? '');
    returnAmount = num.tryParse(json['return_amount']?.toString() ?? '');
    returnQty = num.tryParse(json['return_qty']?.toString() ?? '');
  }

  num? id;
  num? businessId;
  num? purchaseReturnId;
  num? purchaseDetailId;
  num? returnAmount;
  num? returnQty;

  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    map['id'] = id;
    map['business_id'] = businessId;
    map['purchase_return_id'] = purchaseReturnId;
    map['purchase_detail_id'] = purchaseDetailId;
    map['return_amount'] = returnAmount;
    map['return_qty'] = returnQty;
    return map;
  }
}

class PurchaseVat {
  PurchaseVat({
    this.id,
    this.name,
    this.rate,
  });

  PurchaseVat.fromJson(dynamic json) {
    id = num.tryParse(json['id']?.toString() ?? '');
    name = json['name'];
    rate = num.tryParse(json['rate']?.toString() ?? '');
  }

  num? id;
  String? name;
  num? rate;
}

class PaymentType {
  PaymentType({
    this.id,
    this.name,
  });

  PaymentType.fromJson(dynamic json) {
    id = num.tryParse(json['id']?.toString() ?? '');
    name = json['name'];
  }

  num? id;
  String? name;
}

class PurchaseProductVat {
  PurchaseProductVat({
    this.id,
    this.name,
    this.rate,
  });

  PurchaseProductVat.fromJson(dynamic json) {
    id = num.tryParse(json['id']?.toString() ?? '');
    name = json['name'];
    rate = num.tryParse(json['rate']?.toString() ?? '');
  }

  num? id;
  num? rate;
  String? name;
}
