class ManagedUserModel {
  final String id;
  final String createdAt;
  final String updatedAt;
  final bool deleted;
  final String username;
  final String email;
  final String phone;
  final int workflowId;
  final String firstName;
  final String lastName;
  final bool enabled;
  final bool accountNonExpired;
  final bool accountNonLocked;
  final bool credentialsNonExpired;
  final List<ManagedUserAuthority> authorities;

  ManagedUserModel({
    required this.id,
    required this.createdAt,
    required this.updatedAt,
    required this.deleted,
    required this.username,
    required this.email,
    required this.phone,
    required this.workflowId,
    required this.firstName,
    required this.lastName,
    required this.enabled,
    required this.accountNonExpired,
    required this.accountNonLocked,
    required this.credentialsNonExpired,
    required this.authorities,
  });

  factory ManagedUserModel.fromJson(Map<String, dynamic> json) {
    return ManagedUserModel(
      id: json['id'] as String,
      createdAt: json['createdAt'] as String? ?? '',
      updatedAt: json['updatedAt'] as String? ?? '',
      deleted: json['deleted'] as bool? ?? false,
      username: json['username'] as String? ?? '',
      email: json['email'] as String? ?? '',
      phone: json['phone'] as String? ?? '',
      workflowId: json['workflowId'] as int? ?? 0,
      firstName: json['firstName'] as String? ?? '',
      lastName: json['lastName'] as String? ?? '',
      enabled: json['enabled'] as bool? ?? true,
      accountNonExpired: json['accountNonExpired'] as bool? ?? true,
      accountNonLocked: json['accountNonLocked'] as bool? ?? true,
      credentialsNonExpired: json['credentialsNonExpired'] as bool? ?? true,
      authorities: (json['authorities'] as List<dynamic>?)
              ?.map((e) =>
                  ManagedUserAuthority.fromJson(e as Map<String, dynamic>))
              .toList() ??
          [],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'createdAt': createdAt,
      'updatedAt': updatedAt,
      'deleted': deleted,
      'username': username,
      'email': email,
      'phone': phone,
      'workflowId': workflowId,
      'firstName': firstName,
      'lastName': lastName,
      'enabled': enabled,
      'accountNonExpired': accountNonExpired,
      'accountNonLocked': accountNonLocked,
      'credentialsNonExpired': credentialsNonExpired,
      'authorities': authorities.map((e) => e.toJson()).toList(),
    };
  }

  String get displayName {
    if (firstName.isNotEmpty && lastName.isNotEmpty) {
      return '$firstName $lastName';
    }
    if (firstName.isNotEmpty) return firstName;
    if (lastName.isNotEmpty) return lastName;
    return username;
  }

  String get initials {
    if (firstName.isNotEmpty && lastName.isNotEmpty) {
      return '${firstName[0]}${lastName[0]}'.toUpperCase();
    }
    if (firstName.isNotEmpty && firstName.length >= 2) {
      return firstName.substring(0, 2).toUpperCase();
    }
    if (username.isNotEmpty && username.length >= 2) {
      return username.substring(0, 2).toUpperCase();
    }
    return '?';
  }

  String get roleNames =>
      authorities.map((a) => a.authority.replaceFirst('ROLE_', '')).join(', ');
}

class ManagedUserAuthority {
  final String authority;

  ManagedUserAuthority({required this.authority});

  factory ManagedUserAuthority.fromJson(Map<String, dynamic> json) {
    return ManagedUserAuthority(authority: json['authority'] as String? ?? '');
  }

  Map<String, dynamic> toJson() {
    return {'authority': authority};
  }
}