import 'auth_response_model.dart'; import 'fingerprint_verify_response_model.dart'; /// Presentation-layer model derived from [AuthResponseModel]. /// /// All raw API fields are mapped and sanitised here so the UI layer /// never has to inspect raw JSON. class AuthResultUiModel { final bool isSuccess; final String statusMessage; // ── Person Information ───────────────────────────────────────────────────── final String firstName; final String lastName; final String fatherName; final String grandfatherName; final String occupation; final String gender; final String sponsor; final String idNumber; final String birthDate; final String nationality; // ── Error ────────────────────────────────────────────────────────────────── /// null when there is no error. final String? errorType; /// null when there is no error. final String? errorMessage; const AuthResultUiModel({ required this.isSuccess, required this.statusMessage, required this.firstName, required this.lastName, required this.fatherName, required this.grandfatherName, required this.occupation, required this.gender, required this.sponsor, required this.idNumber, required this.birthDate, required this.nationality, this.errorType, this.errorMessage, }); /// Convert a raw [AuthResponseModel] (legacy/mock API response) into a UI model. factory AuthResultUiModel.fromResponse(AuthResponseModel response) { final info = response.personInfo; final err = response.error; return AuthResultUiModel( isSuccess: response.success, statusMessage: response.statusMessage, firstName: info?.firstName ?? '', lastName: info?.lastName ?? '', fatherName: info?.fatherName ?? '', grandfatherName: info?.grandfatherName ?? '', occupation: info?.occupation ?? '', gender: info?.gender ?? '', sponsor: info?.sponsor ?? '', idNumber: info?.idNumber ?? '', birthDate: info?.birthDate ?? '', nationality: info?.nationality ?? '', errorType: (err?.type.isNotEmpty ?? false) ? err!.type : null, errorMessage: (err?.message.isNotEmpty ?? false) ? err!.message : null, ); } /// Convert a [FingerprintVerifyResponseModel] (real API response for both /// the National-ID and Passport authentication endpoints) into a UI model. factory AuthResultUiModel.fromFingerprintVerifyResponse( FingerprintVerifyResponseModel response) { final data = response.data; final info = data?.personInfo; // Derive gender label from sex code String genderLabel = ''; if (info?.sexCode == '1' || info?.sexCode.toLowerCase() == 'm') { genderLabel = 'Male'; } else if (info?.sexCode == '2' || info?.sexCode.toLowerCase() == 'f') { genderLabel = 'Female'; } else if (info?.sexCode.isNotEmpty ?? false) { genderLabel = info!.sexCode; } // Use English occupation if available, otherwise Arabic final occupationLabel = (info?.occupationDescEn.isNotEmpty ?? false) ? info!.occupationDescEn : (info?.occupationDescAr ?? ''); // Combine first name + family name for "last name" field in the UI final lastName = (info?.familyName.isNotEmpty ?? false) ? info!.familyName : ''; // Build error message from data fields when isError is true String? errorType; String? errorMessage; if (data?.isError == true) { errorType = data?.errorType.isNotEmpty == true ? data!.errorType : null; errorMessage = data?.errorMessage.isNotEmpty == true ? data!.errorMessage : null; } return AuthResultUiModel( isSuccess: response.success && (data?.matchFlag ?? false), statusMessage: response.message.isNotEmpty ? response.message : (data?.matchFlag == true ? 'Authentication successful!' : 'Authentication failed'), firstName: info?.firstName ?? '', lastName: lastName, fatherName: info?.fatherName ?? '', grandfatherName: info?.grandFatherName ?? '', occupation: occupationLabel, gender: genderLabel, sponsor: info?.sponsorName ?? '', idNumber: info?.id ?? '', birthDate: info?.birthDateG ?? '', nationality: info?.nationalityCode ?? '', errorType: errorType, errorMessage: errorMessage, ); } /// Convenience getter — first and last name joined on two visual lines. String get fullNameDisplay => [firstName, lastName].where((s) => s.isNotEmpty).join('\n'); bool get hasPersonInfo => firstName.isNotEmpty || lastName.isNotEmpty || fatherName.isNotEmpty || grandfatherName.isNotEmpty || occupation.isNotEmpty || gender.isNotEmpty || idNumber.isNotEmpty || birthDate.isNotEmpty || nationality.isNotEmpty; bool get hasError => errorType != null || errorMessage != null; }