import 'package:initial_folder/models/rating_course_model.dart';

class DiscountCourseModel {
  DiscountCourseModel({
    this.idCourse = '',
    this.instructorId = '',
    this.title = '',
    this.price = '',
    this.instructorName = '',
    this.discountFlag,
    this.discountPrice = '',
    this.thumbnail,
    this.students,
    required this.rating,
    this.finalPrice,
    this.fotoProfile,
    this.topCourse,
    this.hargaTotalDiscount,
    this.typeCoupon,
    this.value,
  });

  String idCourse;
  String instructorId;
  String title;
  String price;
  String instructorName;
  String? discountFlag;
  String discountPrice;
  String? thumbnail;
  String? students;
  List<Rating?> rating;
  int? finalPrice;
  int? hargaTotalDiscount;
  String? fotoProfile;
  String? topCourse;
  String? typeCoupon;
  String? value;

  factory DiscountCourseModel.fromJson(Map<String, dynamic> json) {
    int parseIntWithDotRemoval(dynamic value) {
      if (value is int) return value;
      return int.tryParse(value.toString().replaceAll('.', '')) ?? 0;
    }

    return DiscountCourseModel(
      idCourse: json["id_course"].toString(),
      instructorId: json["instructor_id"].toString(),
      title: json["title"].toString(),
      price: json["price"].toString(),
      instructorName: json["instructor_name"].toString(),
      discountFlag: json["discount_flag"]?.toString(),
      discountPrice: json["discount_price"].toString(),
      thumbnail: json["thumbnail"]?.toString(),
      students: json["students"]?.toString(),
      rating: List<Rating>.from(json["rating"].map((x) => Rating.fromJson(x)))
          .toList(),
      finalPrice: parseIntWithDotRemoval(json["final_price"]),
      // finalPrice: json["final_price"] is int
      //     ? json["final_price"]
      //     : int.tryParse(json["final_price"].toString()),
      fotoProfile: json["foto_profile"]?.toString(),
      topCourse: json["top_course"]?.toString(),
      typeCoupon: json["type_coupon"]?.toString(),
      value: json["value"]?.toString(),
      hargaTotalDiscount: parseIntWithDotRemoval(json["harga_total_discount"]),
      // hargaTotalDiscount: json["harga_total_discount"] is int
      //     ? json["harga_total_discount"]
      //     : int.tryParse(json["harga_total_discount"].toString()),
    );
  }

  Map<String, dynamic> toJson() => {
        "id_course": idCourse,
        "instructor_id": instructorId,
        "title": title,
        "price": price,
        "instructor_name": instructorName,
        "discount_flag": discountFlag,
        "discount_price": discountPrice,
        "thumbnail": thumbnail,
        "students": students,
        "rating": List<dynamic>.from(rating.map((x) => x!.toJson())).toList(),
        "final_price": finalPrice,
        "foto_profile": fotoProfile,
        "top_course": topCourse,
        "harga_total_discount": hargaTotalDiscount,
        "type_coupon": typeCoupon,
        "value": value,
      };
}
