import 'package:get/get.dart'; import 'package:dio/dio.dart'; import 'package:device_info_plus/device_info_plus.dart'; import 'dart:io'; import '../../../core/network/dio_client.dart'; import '../../../core/storage/secure_storage.dart'; import '../../../app/routes/app_pages.dart'; import '../../../core/utils/logger.dart'; import '../../../core/utils/app_snackbar.dart'; import '../../../core/services/push_notification_service.dart'; class AuthController extends GetxController { /// App-store review account that skips biometric setup. Must match /// REVIEWER_EMAIL on the server. static const String _reviewerEmail = 'reviewer@musadaq.jo'; final Dio _dio = DioClient().client; final SecureStorage _storage = SecureStorage(); var isLoading = false.obs; var phone = ''.obs; /// Turns a DioException into a message that reflects what actually failed. /// /// Without this, any transport failure (expired TLS cert, no network, DNS) /// surfaced as "wrong credentials", which sent users hunting for a password /// problem that did not exist. String _describeDioError(DioException e, {String fallback = 'بيانات الدخول غير صحيحة'}) { final data = e.response?.data; if (data is Map && data['message'] != null) { return data['message'].toString(); } switch (e.type) { case DioExceptionType.connectionTimeout: case DioExceptionType.sendTimeout: case DioExceptionType.receiveTimeout: return 'انتهت مهلة الاتصال بالخادم. تحقق من اتصالك بالإنترنت.'; case DioExceptionType.badCertificate: return 'تعذّر التحقق من شهادة أمان الخادم. يرجى التواصل مع الدعم.'; case DioExceptionType.connectionError: case DioExceptionType.unknown: final msg = e.error?.toString() ?? e.message ?? ''; if (msg.contains('CERTIFICATE') || msg.contains('HandshakeException')) { return 'تعذّر التحقق من شهادة أمان الخادم. يرجى التواصل مع الدعم.'; } return 'تعذّر الاتصال بالخادم. تحقق من اتصالك بالإنترنت.'; default: return fallback; } } Future requestOtp(String phoneNumber) async { try { if (phoneNumber.trim().isEmpty) { AppSnackbar.showError('خطأ', 'الرجاء إدخال رقم الهاتف أولاً'); return; } isLoading.value = true; // Normalize phone number String normalizedPhone = phoneNumber.replaceAll(RegExp(r'[^0-9+]'), ''); if (normalizedPhone.startsWith('+')) { normalizedPhone = normalizedPhone.substring(1); } if (normalizedPhone.startsWith('07')) { normalizedPhone = '962${normalizedPhone.substring(1)}'; } else if (normalizedPhone.startsWith('7')) { normalizedPhone = '962$normalizedPhone'; } phone.value = normalizedPhone; final response = await _dio.post('auth/mobile/request-otp', data: { 'phone': normalizedPhone, }); if (response.statusCode == 200) { AppLogger.print('OTP Request Success: ${response.data}'); AppSnackbar.showSuccess('نجاح', 'تم إرسال رمز التحقق بنجاح'); Get.toNamed(AppRoutes.OTP_VERIFY); } } on DioException catch (e, stackTrace) { String errorMessage = 'فشل الاتصال بالخادم'; if (e.response?.data != null && e.response?.data is Map) { errorMessage = e.response?.data['message'] ?? errorMessage; } AppSnackbar.showError('خطأ', errorMessage); } finally { isLoading.value = false; } } Future verifyOtp(String otp) async { try { isLoading.value = true; // Get device info final deviceInfo = DeviceInfoPlugin(); String deviceId = ''; String deviceName = ''; if (Platform.isAndroid) { final androidInfo = await deviceInfo.androidInfo; deviceId = androidInfo.id; deviceName = androidInfo.model; } else if (Platform.isIOS) { final iosInfo = await deviceInfo.iosInfo; deviceId = iosInfo.identifierForVendor ?? 'unknown_ios'; deviceName = iosInfo.name; } // Get FCM token for notifications final pushToken = await PushNotificationService.getToken(); final response = await _dio.post('auth/mobile/verify-otp', data: { 'phone': phone.value, 'otp': otp, 'device_id': deviceId, 'device_name': deviceName, 'platform': Platform.operatingSystem, 'app_version': '1.0.0', 'push_token': pushToken, }); if (response.statusCode == 200) { AppLogger.print('OTP Verify Success. Tokens received.'); final data = response.data['data']; // Save secure data await _storage.saveToken(data['access_token']); await _storage.saveDeviceSecret(data['device_secret']); // Both are required to refresh the session later: the server keeps // refresh tokens per device. await _storage.saveDeviceId(deviceId); if (data['refresh_token'] != null) { await _storage.saveRefreshToken(data['refresh_token']); } if (data['user']['email'] != null) { await _storage.saveEmail(data['user']['email']); } AppSnackbar.showSuccess('مرحباً بك', 'تم تسجيل الدخول بنجاح'); // Navigate to Biometric Setup (unless it's the reviewer) if (data['user']['email'] == _reviewerEmail) { Get.offAllNamed(AppRoutes.MAIN); } else { Get.offAllNamed(AppRoutes.BIOMETRIC_SETUP); } } } on DioException catch (e, stackTrace) { AppLogger.error('OTP Verify Failed', e.response?.data ?? e.message, stackTrace); AppSnackbar.showError( 'خطأ', _describeDioError(e, fallback: 'رمز التحقق غير صحيح')); } finally { isLoading.value = false; } } Future loginWithEmail(String email, String password) async { try { if (email.trim().isEmpty || password.trim().isEmpty) { AppSnackbar.showError( 'خطأ', 'الرجاء إدخال البريد الإلكتروني وكلمة المرور'); return; } isLoading.value = true; // Get device info final deviceInfo = DeviceInfoPlugin(); String deviceId = ''; String deviceName = ''; if (Platform.isAndroid) { final androidInfo = await deviceInfo.androidInfo; deviceId = androidInfo.id; deviceName = androidInfo.model; } else if (Platform.isIOS) { final iosInfo = await deviceInfo.iosInfo; deviceId = iosInfo.identifierForVendor ?? 'unknown_ios'; deviceName = iosInfo.name; } final response = await _dio.post('auth/login', data: { 'email': email, 'password': password, 'device_id': deviceId, 'device_name': deviceName, 'platform': Platform.operatingSystem, 'app_version': '1.0.0', // Should ideally come from PackageInfo }); if (response.statusCode == 200) { final data = response.data['data']; if (data['otp_required'] == true) { AppLogger.print('Email Login verification required via OTP.'); phone.value = data['phone'] ?? ''; AppSnackbar.showSuccess('نجاح', 'تم إرسال رمز التحقق إلى رقم هاتفك المسجل'); Get.toNamed(AppRoutes.OTP_VERIFY); return; } AppLogger.print('Email Login Success. Tokens received.'); // Save secure data await _storage.saveToken(data['access_token']); // Note: auth/login might not return device_secret, handle if missing if (data['device_secret'] != null) { await _storage.saveDeviceSecret(data['device_secret']); } await _storage.saveDeviceId(deviceId); if (data['refresh_token'] != null) { await _storage.saveRefreshToken(data['refresh_token']); } if (data['user']['email'] != null) { await _storage.saveEmail(data['user']['email']); } AppSnackbar.showSuccess('مرحباً بك', 'تم تسجيل الدخول بنجاح'); // Navigate to Dashboard for reviewer, else Biometric Setup if (email == _reviewerEmail) { Get.offAllNamed(AppRoutes.MAIN); } else { Get.offAllNamed(AppRoutes.BIOMETRIC_SETUP); } } } on DioException catch (e, stackTrace) { AppLogger.error('Email Login Failed', e.response?.data ?? e.message, stackTrace); AppSnackbar.showError('خطأ', _describeDioError(e)); } finally { isLoading.value = false; } } }