part of 'personal_info_bloc.dart';

@immutable
class PersonalInfoState {
  const PersonalInfoState({
    this.firstName = '',
    this.lastName = '',
    this.email = '',
    this.phoneNumber = '',
    this.middleName,
    this.profileImageUrl,
    this.profileImagePath,
    this.isInEditMode = true,
    this.isUpdateReceived = false,
    this.status = StateStatus.idle,
    this.validationErrors = const <ProfileRequiredField, String>{},
    this.isEmailVerified = false,
    this.shouldRouteToVerification = false,
  });

  final String firstName;
  final String lastName;
  final String email;
  final String phoneNumber;
  final String? middleName;
  final String? profileImageUrl;
  final String? profileImagePath;
  final bool isInEditMode;
  final bool isUpdateReceived;
  final StateStatus status;
  final Map<ProfileRequiredField, String> validationErrors;
  final bool isEmailVerified;
  final bool shouldRouteToVerification;

  PersonalInfoState copyWith({
    String? firstName,
    String? lastName,
    String? email,
    String? phoneNumber,
    String? middleName,
    String? profileImageUrl,
    String? profileImagePath,
    bool? isInEditMode,
    bool? isUpdateReceived,
    StateStatus? status,
    Map<ProfileRequiredField, String>? validationErrors,
    bool? isEmailVerified,
    bool? shouldRouteToVerification,
  }) {
    return PersonalInfoState(
      firstName: firstName ?? this.firstName,
      lastName: lastName ?? this.lastName,
      email: email ?? this.email,
      phoneNumber: phoneNumber ?? this.phoneNumber,
      middleName: middleName ?? this.middleName,
      profileImagePath: profileImagePath ?? this.profileImagePath,
      profileImageUrl: profileImageUrl ?? this.profileImageUrl,
      isInEditMode: isInEditMode ?? this.isInEditMode,
      isUpdateReceived: isUpdateReceived ?? this.isUpdateReceived,
      status: status ?? this.status,
      validationErrors: validationErrors ?? this.validationErrors,
      isEmailVerified: isEmailVerified ?? this.isEmailVerified,
      shouldRouteToVerification:
          shouldRouteToVerification ?? this.shouldRouteToVerification,
    );
  }

  Map<ProfileRequiredField, String> invalidate() {
    final emailError = EmailValidator.validate(email, isRequired: true);

    return {
      if (firstName.isEmpty) ProfileRequiredField.firstName: 'required_to_fill'.tr(),
      if (lastName.isEmpty) ProfileRequiredField.lastName: 'required_to_fill'.tr(),
      if (emailError != null) ProfileRequiredField.email: emailError,
      if (profileImagePath == null && profileImageUrl == null)
        ProfileRequiredField.avatar: 'required'.tr(),
    };
  }
}

enum ProfileRequiredField { firstName, lastName, email, avatar }
