import 'dart:convert'; import '../../../core/services/api_service.dart'; import '../../../core/services/secure_storage_service.dart'; import 'models/chatbot_rule_model.dart'; import 'models/contact_model.dart'; import 'models/plan_model.dart'; import 'models/super_admin_stats_model.dart'; import 'models/whatsapp_status_model.dart'; class DashboardRepository { final ApiService _apiService = ApiService(); final SecureStorageService _secureStorage = SecureStorageService(); // English: Helper to retrieve token from secure storage. Throws exception if token is missing. // Arabic: دالة مساعدة لاسترداد الرمز من التخزين الآمن. تطلق استثناء إذا كان الرمز مفقودًا. Future _getToken() async { final token = await _secureStorage.readToken(); if (token == null || token.isEmpty) { throw Exception('أنت غير مصرح لك بالوصول، يرجى إعادة تسجيل الدخول'); } return token; } // English: Load the active WhatsApp connection session status. // Arabic: تحميل حالة اتصال جلسة الواتساب النشطة. Future getWhatsAppStatus() async { final token = await _getToken(); final response = await _apiService.getWhatsAppStatus(token); if (response.statusCode == 200) { final decoded = jsonDecode(response.body) as Map; final statusData = decoded['data'] as Map?; if (statusData == null) return null; return WhatsAppStatusModel.fromJson(statusData); } else { throw Exception('فشل في جلب حالة الواتساب'); } } // English: Load available subscription plans. // Arabic: تحميل باقات الاشتراك المتاحة. Future> getPlans() async { final token = await _getToken(); final response = await _apiService.getPlans(token); if (response.statusCode == 200) { final decoded = jsonDecode(response.body) as Map; final list = decoded['data'] as List? ?? []; return list.map((item) => PlanModel.fromJson(item as Map)).toList(); } else { throw Exception('فشل في جلب الباقات المتاحة'); } } // English: Load CRM contacts directory. // Arabic: تحميل دليل جهات اتصال إدارة العملاء. Future> getContacts() async { final token = await _getToken(); final response = await _apiService.getContacts(token); if (response.statusCode == 200) { final decoded = jsonDecode(response.body) as Map; final list = decoded['data'] as List? ?? []; return list.map((item) => ContactModel.fromJson(item as Map)).toList(); } else { throw Exception('فشل في جلب جهات الاتصال'); } } // English: Create a new contact inside remote CRM directory. // Arabic: إنشاء جهة اتصال جديدة داخل دليل إدارة العملاء البعيد. Future addContact(String name, String phone) async { final token = await _getToken(); final response = await _apiService.addContact(token, name, phone); if (response.statusCode == 201 || response.statusCode == 200) { return; } else if (response.statusCode == 409) { throw Exception('رقم الهاتف هذا مسجل بالفعل في جهات الاتصال الخاصة بك'); } else { final decoded = jsonDecode(response.body) as Map; final error = decoded['message'] as String? ?? 'فشل في إضافة جهة الاتصال'; throw Exception(error); } } // English: Load chatbot auto-reply rules. // Arabic: تحميل قواعد الرد الآلي لروبوت الدردشة. Future> getChatbotRules() async { final token = await _getToken(); final response = await _apiService.getChatbotRules(token); if (response.statusCode == 200) { final decoded = jsonDecode(response.body) as Map; final list = decoded['data'] as List? ?? []; return list.map((item) => ChatbotRuleModel.fromJson(item as Map)).toList(); } else { throw Exception('فشل في جلب قواعد الرد الآلي'); } } // English: Load platform statistics (Super Admin only). // Arabic: تحميل إحصائيات المنصة (للمشرف العام فقط). Future getAdminStats() async { final token = await _getToken(); final response = await _apiService.getAdminStats(token); if (response.statusCode == 200) { final decoded = jsonDecode(response.body) as Map; final statsData = decoded['data'] as Map?; if (statsData == null) { throw Exception('البيانات غير صالحة'); } return SuperAdminStatsModel.fromJson(decoded['data'] as Map); } else { final decoded = jsonDecode(response.body) as Map; final error = decoded['error'] as String? ?? 'فشل في جلب إحصائيات المشرف العام'; throw Exception(error); } } // English: Request a new WhatsApp QR connection. // Arabic: طلب اتصال واتساب جديد برمز استجابة سريع. Future requestWhatsAppQr() async { final token = await _getToken(); final response = await _apiService.requestWhatsAppQr(token); if (response.statusCode != 200) { final decoded = jsonDecode(response.body) as Map; final error = decoded['message'] as String? ?? 'فشل في طلب رمز الاستجابة السريعة'; throw Exception(error); } } // English: Disconnect the active WhatsApp connection. // Arabic: قطع اتصال الواتساب النشط. Future disconnectWhatsApp() async { final token = await _getToken(); final response = await _apiService.disconnectWhatsApp(token); if (response.statusCode != 200) { final decoded = jsonDecode(response.body) as Map; final error = decoded['message'] as String? ?? 'فشل في قطع الاتصال'; throw Exception(error); } } // English: Approve a pending company subscription. // Arabic: الموافقة على اشتراك شركة معلق. Future approveBilling(int companyId) async { final token = await _getToken(); final response = await _apiService.approveBilling(token, companyId); if (response.statusCode != 200) { final decoded = jsonDecode(response.body) as Map; final error = decoded['error'] as String? ?? 'فشل في الموافقة على الاشتراك'; throw Exception(error); } } }