import 'package:dio/dio.dart'; import 'package:eatvio_app/features/notifications/models/notification_model.dart'; class NotificationsService { final Dio dio; NotificationsService(this.dio); Future?> getNotifications({required int page}) async { try { final response = await dio.get('/api/profile/notifications?page=$page'); if (response.statusCode == 200) { final data = response.data['data'] as List; final notifications = data.map((notificationJson) { try { // Улучшенная логика определения типа уведомления String? stringifiedType; // Сначала проверяем поле 'type' в корне JSON if (notificationJson.containsKey('type') && notificationJson['type'] != null) { stringifiedType = notificationJson['type'].toString(); } // Затем проверяем поле 'notification_class_name' в корне JSON else if (notificationJson.containsKey('notification_class_name') && notificationJson['notification_class_name'] != null) { stringifiedType = notificationJson['notification_class_name'].toString(); } // Если есть поле 'data', проверяем внутри него else if (notificationJson.containsKey('data') && notificationJson['data'] is Map) { final data = notificationJson['data'] as Map; if (data.containsKey('type') && data['type'] != null) { stringifiedType = data['type'].toString(); } else if (data.containsKey('notification_class_name') && data['notification_class_name'] != null) { stringifiedType = data['notification_class_name'].toString(); } } final readDate = notificationJson['read_at']; final createdAt = notificationJson['created_at']; final type = stringifiedType != null ? NotificationType.getTypeFromString(stringifiedType) : NotificationType.unknown; return switch (type) { NotificationType.planSubscriptionStartedToAuthor => PlanSubscriptionStartedToAuthorNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.planSubscriptionCanceledToAuthor => PlanSubscriptionCanceledToAuthorNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.chosenPlanStartedToClientNotification => ChosenPlanStartedToClientNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.chosenPlanTomorrowReminderToClientNotification => ChosenPlanTomorrowReminderToClientNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.diaryReminderNotification => DiaryReminderNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.consultantLeftComment => ConsultantLeftCommentNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.personalizedDietIsDefined => PersonalizedDietIsDefinedNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.chosenPlanFinishedToClient => ChosenPlanFinishedToClientNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.planSubscriptionCanceledToCustomer => PlanSubscriptionCanceledToAuthorNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.chosenPlanStartedToConsultantNotification => ChosenPlanStartedToConsultantNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.chosenPlanTomorrowReminderToConsultantNotification => ChosenPlanTomorrowReminderToConsultantNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.planSubscriptionPaidToAuthor => PlanSubscriptionPaidToAuthorNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.chosenPlanFinishedToConsultant => ChosenPlanFinishedToConsultantNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.chosenPlanLastDayFinishedForUser => ChosenPlanLastDayFinishedForUser.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), // Новые типы уведомлений NotificationType.chosenPlanConsultantLastDayStarted => ChosenPlanConsultantLastDayStarted.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.complaintToModerationForUser => ComplaintToModerationForUser.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.commentReplied => CommentReplied.fromJson(notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt), NotificationType.scoreAdded => ScoreAdded.fromJson(notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt), NotificationType.entityCommented => EntityCommented.fromJson(notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt), // Новые типы уведомлений NotificationType.planSubscriptionExpired => PlanSubscriptionExpiredNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.planSubscriptionRenewed => PlanSubscriptionRenewedNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.consultantAssigned => ConsultantAssignedNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.consultantUnassigned => ConsultantUnassignedNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.paymentFailed => PaymentFailedNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.paymentSuccessful => PaymentSuccessfulNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.goalAchieved => GoalAchievedNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.workoutReminder => WorkoutReminderNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.mealReminder => MealReminderNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), // New notification types NotificationType.dietCancelled => DietCancelledNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.dietStarted => DietStartedNotification.fromJson(notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt), NotificationType.dietExpired => DietExpiredNotification.fromJson(notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt), NotificationType.dietWillStartedIn => DietWillStartedInNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.dietCanNotBeStarted => DietCanNotBeStartedNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.personalDietCreationNeeded => PersonalDietCreationNeededNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.chosenPlanClientLastDayFinished => ChosenPlanClientLastDayFinishedNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.chosenPlanClientLastDayStarted => ChosenPlanClientLastDayStartedNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.stayOnTrackToAuthor => StayOnTrackToAuthorNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.chosenPlanStartedToClient => ChosenPlanStartedToClientNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.clientStartPlanSubscriptionToSubscriber => ClientStartPlanSubscriptionToSubscriberNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.consultantConfirmPlanSubscriptionToSubscriber => ConsultantConfirmPlanSubscriptionToSubscriberNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.clientPaidPlanSubscriptionToAuthor => ClientPaidPlanSubscriptionToAuthorNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.chosenPlanTomorrowReminderToClient => ChosenPlanTomorrowReminderToClientNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.chosenPlanTomorrowReminderToConsultant => ChosenPlanTomorrowReminderToConsultantNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.chosenPlanStartedToConsultant => ChosenPlanStartedToConsultantNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.consultantLeftDiaryComment => ConsultantLeftDiaryCommentNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.consultantConfirmPlanSubscriptionToAuthor => ConsultantConfirmPlanSubscriptionToAuthorNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.clientPaidPlanSubscriptionToSubscriber => ClientPaidPlanSubscriptionToSubscriberNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.clientStartPlanSubscriptionToAuthor => ClientStartPlanSubscriptionToAuthorNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.clientCancelPlanSubscriptionToAuthor => ClientCancelPlanSubscriptionToAuthorNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.clientCancelPlanSubscriptionToSubscriber => ClientCancelPlanSubscriptionToSubscriberNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.userDailyReportCompleted => UserDailyReportCompletedNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.stayOnTrackToCustomer => StayOnTrackToCustomerNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.phoneChanged => PhoneChangedNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.subscription => SubscriptionNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.badgeAwarded => BadgeAwardedNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.badgeRevoked => BadgeRevokedNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.consultantRequestStarted => ConsultantRequestStartedNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.clientCloseComplaintSuccess => ClientCloseComplaintSuccessNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.clientRefundSuccess => ClientRefundSuccessNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.consultantRefundSuccess => ConsultantRefundSuccessNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.clientRejectSuccess => ClientRejectSuccessNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.consultantRejectSuccess => ConsultantRejectSuccessNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.scoreSubstracted => ScoreSubstractedNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.productStatusUpdatedForAuthor => ProductStatusUpdatedForAuthorNotification.fromJson( notificationJson['data'], type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.diaryReminderForUsers => DiaryReminderForUsersNotification.fromJson( notificationJson, type, isRead: readDate != null, createdAt: createdAt, ), NotificationType.unknown => UnknownNotification(type: type, createdAt: parseDate(createdAt)), }; } catch (e) { return UnknownNotification(type: NotificationType.unknown, createdAt: DateTime.now()); } }).toList(); // Извлекаем информацию о пагинации из корня ответа final currentPage = response.data['current_page'] as int; final lastPage = response.data['last_page'] as int; final perPage = response.data['per_page'] as int; final total = response.data['total'] as int; return { 'notifications': notifications, 'pagination': {'current_page': currentPage, 'last_page': lastPage, 'per_page': perPage, 'total': total}, }; } return null; } catch (e) { rethrow; } } Future markNotificationsAsRead() async { try { await dio.post('/api/profile/mark_notifications_as_readed'); } catch (e) { rethrow; } } } DateTime parseDate(String date) => DateTime.parse(date).toLocal();