// To parse this JSON data, do
//
//     final productModel = productModelFromJson(jsonString);

import 'dart:convert';

class ProductModel {
  ProductModel({
    required this.id,
    required this.name,
    required this.slug,
    required this.categoryId,
    required this.previewImage,
    required this.translations,
    required this.offer,
  });

  final int id;
  final String name;
  final String slug;
  final int categoryId;
  final PreviewImage? previewImage;
  final List<Translation> translations;
  final List<Offer> offer;

  factory ProductModel.fromJson(Map<String, dynamic> json, String currentLang) {
    final List<Translation> translations = (json['translations'] as List)
        .map((e) => Translation.fromJson(e))
        .toList();
    String language = json['name'] ?? '';

    for (Translation item in translations) {
      if (item.locale == currentLang) language = item.attributeData['name'];
    }

    return ProductModel(
      id: json["id"] ?? '',
      name: language,
      slug: json["slug"] ?? '',
      categoryId: json["category_id"] ?? '',
      previewImage: (json["preview_image"] != null)
          ? PreviewImage.fromJson(json["preview_image"])
          : null,
      translations: List<Translation>.from(
          json["translations"].map((x) => Translation.fromJson(x))),
      offer: List<Offer>.from(json["offer"].map((x) => Offer.fromJson(x))),
    );
  }

  Map<String, dynamic> toJson() => {
        "id": id,
        "name": name,
        "slug": slug,
        "category_id": categoryId,
        "preview_image": previewImage!.toJson(),
        "translations": List<dynamic>.from(translations.map((x) => x.toJson())),
      };
}

class PreviewImage {
  PreviewImage({
    required this.id,
    required this.sortOrder,
    required this.path,
  });

  final int id;
  final int sortOrder;
  final String path;

  factory PreviewImage.fromJson(Map<String, dynamic> json) => PreviewImage(
        id: json["id"] ?? '',
        sortOrder: json["sort_order"] ?? '',
        path: json["path"] ?? '',
      );

  Map<String, dynamic> toJson() => {
        "id": id,
        "sort_order": sortOrder,
        "path": path,
      };
}

class Translation {
  Translation({
    required this.locale,
    required this.modelId,
    required this.attributeData,
  });

  final String locale;
  final String modelId;
  final Map attributeData;

  factory Translation.fromJson(Map<String, dynamic> jsn) => Translation(
        locale: jsn["locale"] ?? '',
        modelId: jsn["model_id"] ?? '',
        attributeData: json.decode(jsn["attribute_data"]),
      );

  Map<String, dynamic> toJson() => {
        "locale": locale,
        "model_id": modelId,
        "attribute_data": attributeData,
      };
}

class Offer {
  Offer({
    required this.id,
    required this.name,
    required this.quantity,
    required this.description,
    required this.price,
    required this.oldPrice,
    required this.discountPriceValue,
  });

  final int id;
  final String name;
  final int quantity;
  final String description;
  final String price;
  final String oldPrice;
  final int discountPriceValue;

  factory Offer.fromJson(Map<String, dynamic> json) => Offer(
        id: json["id"] ?? '',
        name: json["name"] ?? '',
        quantity: json["quantity"] ?? '',
        discountPriceValue: json['discount_price_value'] ?? '',
        description: json["description"] ?? '',
        price: json["price"] ?? '',
        oldPrice: json["old_price"] ?? '',
      );

  Map<String, dynamic> toJson() => {
        "id": id,
        "name": name,
        "quantity": quantity,
        "description": description,
        "price": price,
        "old_price": oldPrice,
      };
}
