import 'dart:convert';

class UserBookingDetails {
  final int id;
  final String name;
  final String mobileNumber;
  final String email;
  final String message;
  final String serviceDate;
  final String serviceTime;
  final int serviceId;
  final int userId;
  final String address;
  final int status;
  final int type;
  final String cancelDate;
  final String createdDate;
  final String serviceName;
  final String vendorName;
  final String workingHours;
  final String amount;
  final List<String> videos;
  final String workingDuration;
  final String description;
  final String? profilePic;
  final String? averageReview;
  final int isRated;
  final String? vendorname;
  final String categoryName;
  final String subcategoryName;
  final List<String> images1;

  UserBookingDetails({
    required this.id,
    required this.name,
    required this.mobileNumber,
    required this.email,
    required this.message,
    required this.serviceDate,
    required this.serviceTime,
    required this.serviceId,
    required this.userId,
    required this.address,
    required this.status,
    required this.type,
    required this.cancelDate,
    required this.createdDate,
    required this.serviceName,
    required this.vendorName,
    required this.workingHours,
    required this.amount,
    required this.videos,
    required this.workingDuration,
    required this.description,
    this.profilePic,
    this.averageReview,
    required this.isRated,
    this.vendorname,
    required this.categoryName,
    required this.subcategoryName,
    required this.images1,
  });

  factory UserBookingDetails.fromJson(Map<String, dynamic> json) {
    // Helper function to safely convert dynamic data to List<String>
    List<String> convertToList(dynamic value) {
      if (value == null) return <String>[];
      if (value is List) {
        return List<String>.from(value.map((x) => x.toString()));
      }
      if (value is String) {
        try {
          // Try to parse if it's a JSON string
          final parsed = jsonDecode(value);
          if (parsed is List) {
            return List<String>.from(parsed.map((x) => x.toString()));
          }
          return [value];
        } catch (e) {
          return [value];
        }
      }
      return <String>[];
    }

    return UserBookingDetails(
      id: json['id'] ?? 0,
      name: json['name'] ?? '',
      mobileNumber: json['mobile_number'] ?? '',
      email: json['email'] ?? '',
      message: json['message'] ?? '',
      serviceDate: json['service_date'] ?? '',
      serviceTime: json['service_time'] ?? '',
      serviceId: json['service_id'] ?? 0,
      userId: json['user_id'] ?? 0,
      address: json['address'] ?? '',
      status: json['status'] ?? 0,
      type: json['type'] ?? 0,
      cancelDate: json['cancel_date'] ?? '',
      createdDate: json['created_date'] ?? '',
      serviceName: json['service_name'] ?? '',
      vendorName: json['vendorname'] ?? '',
      workingHours: json['workinghours'] ?? '',
      amount: json['amount'] ?? '',
      videos: convertToList(json['videos']),
      workingDuration: json['workingduration'] ?? '',
      description: json['description'] ?? '',
      profilePic: json['profile_pic'],
      averageReview: json['average_review']?.toString(),
      isRated: json['is_rated'] ?? 0,
      vendorname: json['vendor_name'],
      categoryName: json['category_name'] ?? '',
      subcategoryName: json['subcategory_name'] ?? '',
      images1: convertToList(json['images1']),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'mobile_number': mobileNumber,
      'email': email,
      'message': message,
      'service_date': serviceDate,
      'service_time': serviceTime,
      'service_id': serviceId,
      'user_id': userId,
      'address': address,
      'status': status,
      'type': type,
      'cancel_date': cancelDate,
      'created_date': createdDate,
      'service_name': serviceName,
      'vendorname': vendorName,
      'workinghours': workingHours,
      'amount': amount,
      'videos': videos,
      'workingduration': workingDuration,
      'description': description,
      'profile_pic': profilePic,
      'average_review': averageReview,
      'is_rated': isRated,
      'vendor_name': vendorname,
      'category_name': categoryName,
      'subcategory_name': subcategoryName,
      'images1': images1,
    };
  }

  @override
  String toString() {
    return 'UserBookingDetails{id: $id, name: $name, mobileNumber: $mobileNumber, email: $email, message: $message, serviceDate: $serviceDate, serviceTime: $serviceTime, serviceId: $serviceId, userId: $userId, address: $address, status: $status, type: $type, cancelDate: $cancelDate, createdDate: $createdDate, serviceName: $serviceName, vendorName: $vendorName, workingHours: $workingHours, amount: $amount, videos: $videos, workingDuration: $workingDuration, description: $description, profilePic: $profilePic, averageReview: $averageReview, isRated: $isRated, vendor_name: $vendorname, categoryName: $categoryName, subcategoryName: $subcategoryName, images1: $images1}';
  }
}
