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 {
  final Dio _dio = DioClient().client;
  final SecureStorage _storage = SecureStorage();
  
  var isLoading = false.obs;
  var phone = ''.obs;

  Future<void> requestOtp(String phoneNumber) async {
    try {
      if (phoneNumber.trim().isEmpty) {
        AppSnackbar.showError('خطأ', 'الرجاء إدخال رقم الهاتف أولاً');
        return;
      }
      isLoading.value = true;
      phone.value = phoneNumber;
      
      final response = await _dio.post('auth/mobile/request-otp', data: {
        'phone': phoneNumber,
      });

      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<void> 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']);
        
        AppSnackbar.showSuccess('مرحباً بك', 'تم تسجيل الدخول بنجاح');
        
        // Navigate to Biometric Setup
        Get.offAllNamed(AppRoutes.BIOMETRIC_SETUP);
      }
    } on DioException catch (e, stackTrace) {
      AppLogger.error('OTP Verify Failed', e.response?.data, stackTrace);
      AppSnackbar.showError('خطأ', e.response?.data['message'] ?? 'رمز التحقق غير صحيح');
    } finally {
      isLoading.value = false;
    }
  }
}
