// import 'dart:convert';

// // Model for Space
// class UserSpaceModel {
//   final String uuid;
//   final DateTime createdAt;
//   final DateTime updatedAt;

//   UserSpaceModel({
//     required this.uuid,
//     required this.createdAt,
//     required this.updatedAt,
//   });

//   factory UserSpaceModel.fromJson(Map<String, dynamic> json) {
//     return UserSpaceModel(
//       uuid: json['uuid'],
//       createdAt: DateTime.parse(json['createdAt']),
//       updatedAt: DateTime.parse(json['updatedAt']),
//     );
//   }

//   Map<String, dynamic> toJson() {
//     return {
//       'uuid': uuid,
//       'createdAt': createdAt.toIso8601String(),
//       'updatedAt': updatedAt.toIso8601String(),
//     };
//   }
// }

// // Model for User
// class EditUserModel {
//   final String uuid;
//   final DateTime createdAt;
//   final dynamic email;
//   final dynamic? jobTitle;
//   final dynamic status;
//   final String firstName;
//   final String lastName;
//   final String? phoneNumber;
//   final bool isEnabled;
//   final dynamic invitedBy;
//   final dynamic roleType;
//   final List<UserSpaceModel> spaces;
//   final String createdDate;
//   final String createdTime;

//   EditUserModel({
//     required this.uuid,
//     required this.createdAt,
//     required this.email,
//     this.jobTitle,
//     required this.status,
//     required this.firstName,
//     required this.lastName,
//     this.phoneNumber,
//     required this.isEnabled,
//     required this.invitedBy,
//     required this.roleType,
//     required this.spaces,
//     required this.createdDate,
//     required this.createdTime,
//   });

//   factory EditUserModel.fromJson(Map<String, dynamic> json) {
//     var spacesList = (json['spaces'] as List)
//         .map((spaceJson) => UserSpaceModel.fromJson(spaceJson))
//         .toList();

//     return EditUserModel(
//       uuid: json['uuid'],
//       createdAt: DateTime.parse(json['createdAt']),
//       email: json['email'],
//       jobTitle: json['jobTitle'],
//       status: json['status'],
//       firstName: json['firstName'],
//       lastName: json['lastName'],
//       phoneNumber: json['phoneNumber'],
//       isEnabled: json['isEnabled'],
//       invitedBy: json['invitedBy'],
//       roleType: json['roleType'],
//       spaces: spacesList,
//       createdDate: json['createdDate'],
//       createdTime: json['createdTime'],
//     );
//   }

//   Map<String, dynamic> toJson() {
//     return {
//       'uuid': uuid,
//       'createdAt': createdAt.toIso8601String(),
//       'email': email,
//       'jobTitle': jobTitle,
//       'status': status,
//       'firstName': firstName,
//       'lastName': lastName,
//       'phoneNumber': phoneNumber,
//       'isEnabled': isEnabled,
//       'invitedBy': invitedBy,
//       'roleType': roleType,
//       'spaces': spaces.map((space) => space.toJson()).toList(),
//       'createdDate': createdDate,
//       'createdTime': createdTime,
//     };
//   }
// }

class UserProjectResponse {
  final int statusCode;
  final String message;
  final EditUserModel data;
  final bool success;

  UserProjectResponse({
    required this.statusCode,
    required this.message,
    required this.data,
    required this.success,
  });

  /// Create a [UserProjectResponse] from JSON data
  factory UserProjectResponse.fromJson(Map<String, dynamic> json) {
    return UserProjectResponse(
      statusCode: json['statusCode'] as int,
      message: json['message'] as String,
      data: EditUserModel.fromJson(json['data'] as Map<String, dynamic>),
      success: json['success'] as bool,
    );
  }

  /// Convert the [UserProjectResponse] to JSON
  Map<String, dynamic> toJson() {
    return {
      'statusCode': statusCode,
      'message': message,
      'data': data.toJson(),
      'success': success,
    };
  }
}

class EditUserModel {
  final String uuid;
  final String firstName;
  final String lastName;
  final String email;
  final String createdDate; // e.g. "1/3/2025"
  final String createdTime; // e.g. "8:41:43 AM"
  final String status; // e.g. "invited"
  final String invitedBy; // e.g. "SUPER_ADMIN"
  final String? phoneNumber; // can be empty
  final String? jobTitle; // can be empty
  final String roleType; // e.g. "ADMIN"
  final List<UserSpaceModel> spaces;

  EditUserModel({
    required this.uuid,
    required this.firstName,
    required this.lastName,
    required this.email,
    required this.createdDate,
    required this.createdTime,
    required this.status,
    required this.invitedBy,
    required this.phoneNumber,
    required this.jobTitle,
    required this.roleType,
    required this.spaces,
  });

  /// Create a [UserData] from JSON data
  factory EditUserModel.fromJson(Map<String, dynamic> json) {
    return EditUserModel(
      uuid: json['uuid'] as String,
      firstName: json['firstName'] as String,
      lastName: json['lastName'] as String,
      email: json['email'] as String,
      createdDate: json['createdDate'] as String,
      createdTime: json['createdTime'] as String,
      status: json['status'] as String,
      invitedBy: json['invitedBy'] as String,
      phoneNumber: json['phoneNumber'] ?? '',
      jobTitle: json['jobTitle'] ?? '',
      roleType: json['roleType'] as String,
      spaces: (json['spaces'] as List<dynamic>)
          .map((e) => UserSpaceModel.fromJson(e as Map<String, dynamic>))
          .toList(),
    );
  }

  /// Convert the [UserData] to JSON
  Map<String, dynamic> toJson() {
    return {
      'uuid': uuid,
      'firstName': firstName,
      'lastName': lastName,
      'email': email,
      'createdDate': createdDate,
      'createdTime': createdTime,
      'status': status,
      'invitedBy': invitedBy,
      'phoneNumber': phoneNumber,
      'jobTitle': jobTitle,
      'roleType': roleType,
      'spaces': spaces.map((e) => e.toJson()).toList(),
    };
  }
}

class UserSpaceModel {
  final String uuid;
  final String createdAt; // e.g. "2024-11-04T07:20:35.940Z"
  final String updatedAt; // e.g. "2024-11-28T18:47:29.736Z"
  final dynamic spaceTuyaUuid;
  final dynamic spaceName;
  final dynamic invitationCode;
  final bool disabled;
  final double x;
  final double y;
  final String icon;
  final String communityUuid;

  //communityUuid

  UserSpaceModel({
    required this.uuid,
    required this.createdAt,
    required this.updatedAt,
    required this.spaceTuyaUuid,
    required this.spaceName,
    required this.invitationCode,
    required this.disabled,
    required this.x,
    required this.y,
    required this.icon,
    required this.communityUuid,
  });

  /// Create a [UserSpaceModel] from JSON data
  factory UserSpaceModel.fromJson(Map<String, dynamic> json) {
    return UserSpaceModel(
        uuid: json['uuid'] as String,
        createdAt: json['createdAt'] as String,
        updatedAt: json['updatedAt'] as String,
        spaceTuyaUuid: json['spaceTuyaUuid'] as String?,
        spaceName: json['spaceName'] as String,
        invitationCode: json['invitationCode'] as String?,
        disabled: json['disabled'] as bool,
        x: (json['x'] as num).toDouble(),
        y: (json['y'] as num).toDouble(),
        icon: json['icon'] as String,
        communityUuid: json['communityUuid'] as String);
  }

  /// Convert the [UserSpaceModel] to JSON
  Map<String, dynamic> toJson() {
    return {
      'uuid': uuid,
      'createdAt': createdAt,
      'updatedAt': updatedAt,
      'spaceTuyaUuid': spaceTuyaUuid,
      'spaceName': spaceName,
      'invitationCode': invitationCode,
      'disabled': disabled,
      'x': x,
      'y': y,
      'icon': icon,
      'communityUuid': communityUuid
    };
  }
}
