import 'package:auth/src/features/sign_up/domain/entities/address_entity.dart';
import 'package:auth/src/features/sign_up/domain/entities/sign_up_request_entity.dart';
import 'package:auth/src/features/sign_up/presentation/mappers/address_view_state_mapper.dart';
import 'package:auth/src/features/sign_up/presentation/state/sign_up_view_state.dart';

extension SignUpViewStateMapper on SignUpViewState {
  bool get canSubmit =>
      documentNumber.trim().isNotEmpty &&
      documentType.trim().isNotEmpty &&
      relationship.trim().isNotEmpty &&
      firstName.trim().isNotEmpty &&
      lastName.trim().isNotEmpty &&
      email.trim().isNotEmpty &&
      phone.trim().isNotEmpty &&
      password.isNotEmpty &&
      bornAt != null &&
      userId.trim().isNotEmpty &&
      placeOfBirth.trim().isNotEmpty &&
      birthCountry.trim().isNotEmpty &&
      address.isValid;

  SignUpRequestEntity toRequestEntity() {
    final birth = bornAt;
    if (birth == null) {
      throw Exception('bornAt is required');
    }

    return SignUpRequestEntity(
      documentNumber: documentNumber.trim(),
      documentType: documentType.trim(),
      relationship: relationship.trim(),
      firstName: firstName.trim(),
      lastName: lastName.trim(),
      email: email.trim(),
      phone: phone.trim(),
      language: language.trim(),
      password: password,
      bornAt: birth.millisecondsSinceEpoch,
      userId: userId.trim(),
      placeOfBirth: placeOfBirth.trim(),
      birthCountry: birthCountry.trim(),
      addresses: <AddressEntity>[address.toEntity()],
      taxResidences: <AddressEntity>[address.toEntity()],
    );
  }
}
