import 'brand_model.dart';
import 'category_model.dart';

class Product {
  final String id;
  final String sku;
  final String name;
 final  Category category;
 final  Brand brand;
  final int price;
  int quantity;
  final int gst;
  final int hsnCode;
  final String description;
  final String productStatus;
  final String addedBy;
  final DateTime createdAt;

  Product({
    required this.id,
    required this.sku,
    required this.name,
     required this.category,
   required this.brand,
    required this.price,
    this.quantity =1,
    required this.gst,
    required this.hsnCode,
    required this.description,
    required this.productStatus,
    required this.addedBy,
    required this.createdAt,
  });

  factory Product.fromJson(Map<String, dynamic> json) {
    return Product(
      id: json['_id'] as String,
      sku: json['SKU'] as String,
      name: json['name'] as String,
      category:Category.fromJson(json['category'] as Map<String, dynamic>),
      brand: Brand.fromJson(json['brand'] as Map<String, dynamic>),
      price: json['price'] as int,
      gst: json['GST'] as int,
      hsnCode: json['HSN_Code'] as int,
      description: json['description'] ?? '',
      productStatus: json['product_Status'] as String,
      addedBy: json['addedBy']['name'] as String,
      createdAt: DateTime.parse(json['createdAt'] as String),
    );
  }


  // Method to convert a Product to JSON
  Map<String, dynamic> toJson() {
    return {
      '_id': id,
      'SKU': sku,
      'name': name,
      'category':toJson(),
      'brand':toJson(),
      'price': price,
      'GST': gst,
      'HSN_Code': hsnCode,
      'description': description,
      'product_Status': productStatus,
      'addedBy': {
        'name': addedBy,
      },
      'createdAt': createdAt.toIso8601String(),
    };
  }

  // Override toString method for easy printing of Product details
  @override
  String toString() {
    return 'Product(id: $id, sku: $sku, name: $name, category: $category, '
        'brand: $brand, price: $price, quantity: $quantity, gst: $gst, '
        'hsnCode: $hsnCode, description: $description, productStatus: $productStatus, '
        'addedBy: $addedBy, createdAt: $createdAt)';
  }
}


// class Category {
//   final String id;
//   final String categoryName;
//
//   Category({
//     required this.id,
//     required this.categoryName,
//   });
//
//   factory Category.fromJson(Map<String, dynamic> json) {
//     return Category(
//       id: json['_id'],
//       categoryName: json['categoryName'],
//     );
//   }
// }
//
// class Brand {
//   final String id;
//   final String brandName;
//
//   Brand({
//     required this.id,
//     required this.brandName,
//   });
//
//   factory Brand.fromJson(Map<String, dynamic> json) {
//     return Brand(
//       id: json['_id'],
//       brandName: json['brandName'],
//     );
//   }
// }