class DetailPageModel {
  int id;
  int vendorId;
  int serviceType;
  String vendorname;
  String servicename;
  int category;
  int subcategory;
  String workinghours;
  String workingduration;
  String amount;
  String location;
  String description;
  String details;
  String averageReview;
  List<String>? videos;
  String createdDate;
  String vendorName;
  String? profilePic;
  String categoryName;
  String subcategoryName;
  List<String> images1;

  DetailPageModel({
    required this.id,
    required this.vendorId,
    required this.serviceType,
    required this.vendorname,
    required this.servicename,
    required this.category,
    required this.subcategory,
    required this.workinghours,
    required this.workingduration,
    required this.amount,
    required this.location,
    required this.description,
    required this.details,
    required this.averageReview,
    this.videos,
    required this.createdDate,
    required this.vendorName,
    this.profilePic,
    required this.categoryName,
    required this.subcategoryName,
    required this.images1,
  });

  factory DetailPageModel.fromJson(Map<String, dynamic> json) {
    // Helper function to safely convert to string
    String safeString(dynamic value) {
      if (value == null) return '';
      return value.toString();
    }

    // Helper function to safely convert to int
    int safeInt(dynamic value) {
      if (value == null) return 0;
      if (value is int) return value;
      if (value is String) return int.tryParse(value) ?? 0;
      return 0;
    }

    // Helper function to safely handle videos
    List<String>? safeVideos(dynamic value) {
      if (value == null) return null;
      if (value is String && value.isNotEmpty) return [value];
      if (value is List) {
        return value
            .map((e) => e?.toString() ?? '')
            .where((e) => e.isNotEmpty)
            .toList();
      }
      return null;
    }

    // Helper function to safely handle images
    List<String> safeImages(dynamic value) {
      if (value == null) return [];
      if (value is List) {
        return value
            .map((e) => e?.toString() ?? '')
            .where((e) => e.isNotEmpty)
            .toList();
      }
      if (value is String && value.isNotEmpty) return [value];
      return [];
    }

    return DetailPageModel(
      id: safeInt(json['id']),
      vendorId: safeInt(json['vendor_id']),
      serviceType: safeInt(json['service_type']),
      vendorname: safeString(json['vendorname']),
      servicename: safeString(json['servicename']),
      category: safeInt(json['category']),
      subcategory: safeInt(json['subcategory']),
      workinghours: safeString(json['workinghours']),
      workingduration: safeString(json['workingduration']),
      amount: safeString(json['amount']),
      location: safeString(json['location']),
      description: safeString(json['description']),
      details: safeString(json['details']),
      averageReview: safeString(json['average_review']),
      videos: safeVideos(json['videos']),
      createdDate: safeString(json['created_date']),
      vendorName: safeString(json['vendor_name']),
      profilePic: json['profile_pic']?.toString(),
      categoryName: safeString(json['category_name']),
      subcategoryName: safeString(json['subcategory_name']),
      images1: safeImages(json['images1']),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'vendor_id': vendorId,
      'service_type': serviceType,
      'vendorname': vendorname,
      'servicename': servicename,
      'category': category,
      'subcategory': subcategory,
      'workinghours': workinghours,
      'workingduration': workingduration,
      'amount': amount,
      'location': location,
      'description': description,
      'details': details,
      'average_review': averageReview,
      'videos': videos,
      'created_date': createdDate,
      'vendor_name': vendorName,
      'profile_pic': profilePic,
      'category_name': categoryName,
      'subcategory_name': subcategoryName,
      'images1': images1,
    };
  }

  // Helper method to get a safe display value
  String getDisplayValue(String value) {
    return value.isEmpty ? 'N/A' : value;
  }

  // Helper method to check if images are available
  bool get hasImages => images1.isNotEmpty;

  // Helper method to check if videos are available
  bool get hasVideos => videos != null && videos!.isNotEmpty;
}
