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 { final stringifiedType = notificationJson['data']['notification_class_name']; final readDate = notificationJson['read_at']; final createdAt = notificationJson['created_at']; final type = NotificationType.getTypeFromString(stringifiedType); 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.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();