import { io } from "../../index";
import SystemNotificationsList from "../SystemNotifications/SystemNotification";
import { CreateSystemNotificationParams } from "../SystemNotifications/SystemNotification.Interface";
import { INotificationsAdapter, NotificationDataSystem } from "./Notifications.Interface";

export class NotificationsSystemAdapter implements INotificationsAdapter<NotificationDataSystem> {
    async send(data: NotificationDataSystem): Promise<void> {
        const sendData: CreateSystemNotificationParams = {
            userId: data.userId,
            subject: data.subject,
            conversationId: data.conversationId,
            message: data.message,
            type: data.type,
            code: data.code,
        };

        const notif = await SystemNotificationsList.createNotification(sendData);

        if (notif.userId) {
            const countUnreadNotifications = await SystemNotificationsList.finUnreadNotifications({
                userId: notif.userId,
                sessionUser: notif.userId,
            });

            io.to(`user:${data.userId}`).emit("new_system_notification", countUnreadNotifications);
        }
    }
}
