import { Injectable, OnModuleInit } from "@nestjs/common";
import { WebSocketType } from "../types";
import { BaseWebSocketServer } from "../base/base-websocket-server";
import EventBus, { CrudOperation } from "@server/utils/event-bus";
import { ObjectType, SocketMsgType, MessageDto, PostDto, PostType } from "@nice/common";
@Injectable()
export class RealtimeServer extends BaseWebSocketServer implements OnModuleInit {
    onModuleInit() {
        EventBus.on("dataChanged", ({ data, type, operation }) => {
            if (type === ObjectType.MESSAGE && operation === CrudOperation.CREATED) {
                const receiverIds = (data as Partial<MessageDto>).receivers.map(receiver => receiver.id)
                this.sendToUsers(receiverIds, { type: SocketMsgType.NOTIFY, payload: { objectType: ObjectType.MESSAGE } })
            }

            if (type === ObjectType.POST) {
                const post = data as Partial<PostDto>

            }
        })

    }
    public get serverType(): WebSocketType {
        return WebSocketType.REALTIME;
    }
}
