import 'package:flutter_bloc/flutter_bloc.dart'; import '../../data/dashboard_repository.dart'; import 'dashboard_state.dart'; // English: DashboardCubit manages the active tab and data fetching. // Arabic: يدير ملف الكيوبيت الباب النشط وجلب البيانات المخصصة له. // // English: In the web client, AlpineJS switches active tabs and calls fetch functions. // Arabic: في عميل الويب، تبدل مكتبة ألبين الباب النشط وتستدعي دوال الجلب. // // English: Here, this Cubit replicates that behavior by capturing changes and emitting updated states. // Arabic: هنا، يكرر هذا الكيوبيت هذا السلوك من خلال رصد التغييرات وإرسال حالات محدثة. class DashboardCubit extends Cubit { final DashboardRepository _repository; DashboardCubit(this._repository) : super(const DashboardState( activeTab: DashboardTab.whatsapp, isLoading: false, )); // English: Switch the active dashboard view and fetch the required API data. // Arabic: تبديل عرض لوحة التحكم النشط وجلب بيانات واجهة برمجة التطبيقات المطلوبة. Future changeTab(DashboardTab tab) async { // English: Update tab state first to give instant visual feedback in UI. // Arabic: تحديث حالة الباب أولاً لتقديم استجابة مرئية فورية في الواجهة. emit(state.copyWith(activeTab: tab, isLoading: true)); try { switch (tab) { case DashboardTab.whatsapp: final status = await _repository.getWhatsAppStatus(); final meta = await _repository.getMetaSessions(); emit(state.copyWith( whatsappStatus: status, metaSessions: meta, isLoading: false, )); break; case DashboardTab.billing: final plans = await _repository.getPlans(); emit(state.copyWith(plans: plans, isLoading: false)); break; case DashboardTab.contacts: final contacts = await _repository.getContacts(); emit(state.copyWith(contacts: contacts, isLoading: false)); break; case DashboardTab.chatbot: final rules = await _repository.getChatbotRules(); emit(state.copyWith(chatbotRules: rules, isLoading: false)); break; case DashboardTab.superAdmin: final adminStats = await _repository.getAdminStats(); emit(state.copyWith(superAdminStats: adminStats, isLoading: false)); break; default: // English: Placeholder views do not hit any backend API. // Arabic: شاشات الحجز المؤقتة لا تتصل بأي واجهة برمجة تطبيقات. emit(state.copyWith(isLoading: false)); break; } } catch (e) { final cleanMsg = e.toString().replaceAll('Exception: ', ''); emit(state.copyWith(errorMessage: cleanMsg, isLoading: false)); } } // English: Action trigger to reload current active tab data. // Arabic: مشغل الإجراء لإعادة تحميل بيانات الباب النشط الحالي. Future refreshCurrentTab() async { await changeTab(state.activeTab); } // English: Add a new contact and automatically reload the updated contacts list. // Arabic: إضافة جهة اتصال جديدة وإعادة تحميل قائمة جهات الاتصال المحدثة تلقائياً. Future addContact(String name, String phone) async { emit(state.copyWith(isLoading: true)); try { await _repository.addContact(name, phone); final contacts = await _repository.getContacts(); emit(state.copyWith(contacts: contacts, isLoading: false)); return true; } catch (e) { final cleanMsg = e.toString().replaceAll('Exception: ', ''); emit(state.copyWith(errorMessage: cleanMsg, isLoading: false)); return false; } } // English: Request a new WhatsApp QR connection session and refresh status. // Arabic: طلب جلسة اتصال واتساب جديدة برمز استجابة سريع وتحديث الحالة. Future requestWhatsAppQr() async { emit(state.copyWith(isLoading: true)); try { await _repository.requestWhatsAppQr(); final status = await _repository.getWhatsAppStatus(); emit(state.copyWith(whatsappStatus: status, isLoading: false)); } catch (e) { final cleanMsg = e.toString().replaceAll('Exception: ', ''); emit(state.copyWith(errorMessage: cleanMsg, isLoading: false)); } } // English: Disconnect active WhatsApp connection and refresh status. // Arabic: قطع اتصال الواتساب النشط وتحديث الحالة. Future disconnectWhatsApp() async { emit(state.copyWith(isLoading: true)); try { await _repository.disconnectWhatsApp(); final status = await _repository.getWhatsAppStatus(); emit(state.copyWith(whatsappStatus: status, isLoading: false)); } catch (e) { final cleanMsg = e.toString().replaceAll('Exception: ', ''); emit(state.copyWith(errorMessage: cleanMsg, isLoading: false)); } } // English: Approve a pending company subscription billing and reload admin stats. // Arabic: الموافقة على اشتراك شركة معلق وإعادة تحميل إحصائيات المدير العام. Future approveBilling(int companyId) async { emit(state.copyWith(isLoading: true)); try { await _repository.approveBilling(companyId); final stats = await _repository.getAdminStats(); emit(state.copyWith(superAdminStats: stats, isLoading: false)); } catch (e) { final cleanMsg = e.toString().replaceAll('Exception: ', ''); emit(state.copyWith(errorMessage: cleanMsg, isLoading: false)); } } // English: Connect a Facebook Page or Instagram channel and refresh sessions. Future connectMetaSession(String channelType, String pageId, String pageName, String pageAccessToken) async { emit(state.copyWith(isLoading: true)); try { await _repository.connectMetaSession(channelType, pageId, pageName, pageAccessToken); final meta = await _repository.getMetaSessions(); emit(state.copyWith(metaSessions: meta, isLoading: false)); } catch (e) { final cleanMsg = e.toString().replaceAll('Exception: ', ''); emit(state.copyWith(errorMessage: cleanMsg, isLoading: false)); } } // English: Disconnect a Meta page/profile connection and refresh sessions. Future disconnectMetaSession(int sessionId) async { emit(state.copyWith(isLoading: true)); try { await _repository.disconnectMetaSession(sessionId); final meta = await _repository.getMetaSessions(); emit(state.copyWith(metaSessions: meta, isLoading: false)); } catch (e) { final cleanMsg = e.toString().replaceAll('Exception: ', ''); emit(state.copyWith(errorMessage: cleanMsg, isLoading: false)); } } }