class User {
  const User({
    required this.id,
    this.email,
    this.phone,
    required this.role,
    this.routeId,
  });

  final String id;
  final String? email;
  final String? phone;
  final String role;
  final String? routeId;

  factory User.fromJson(Map<String, dynamic> json) {
    return User(
      id:
          _pickString(<dynamic>[json['id'], json['user_id'], json['userId']]) ??
          '',
      email: _pickString(<dynamic>[json['email'], json['mail']]),
      phone: _pickString(<dynamic>[json['phone'], json['telefono']]),
      role: _pickString(<dynamic>[json['role'], json['rol']]) ?? 'citizen',
      routeId: _pickString(<dynamic>[json['routeId'], json['route_id']]),
    );
  }

  Map<String, dynamic> toJson() {
    return <String, dynamic>{
      'id': id,
      'email': email,
      'phone': phone,
      'role': role,
      'routeId': routeId,
    };
  }

  static String? _pickString(Iterable<dynamic> candidates) {
    for (final candidate in candidates) {
      if (candidate is String && candidate.isNotEmpty) {
        return candidate;
      }
      if (candidate != null && candidate.toString().isNotEmpty) {
        return candidate.toString();
      }
    }

    return null;
  }
}
