"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MessageService = void 0;
const database_js_1 = require("../config/database.js");
class MessageService {
    static async sendMessage(userId, data) {
        try {
            const message = await database_js_1.prisma.message.create({
                data: {
                    content: data.content,
                    image: data.image,
                    userId,
                    roomId: data.roomId,
                },
                include: {
                    user: {
                        select: {
                            id: true,
                            username: true,
                            avatar: true,
                        },
                    },
                    reactions: {
                        include: {
                            user: {
                                select: {
                                    id: true,
                                    username: true,
                                },
                            },
                        },
                    },
                },
            });
            return {
                success: true,
                data: message,
            };
        }
        catch (error) {
            console.error("Send message error:", error);
            return {
                success: false,
                error: "Failed to send message",
            };
        }
    }
    static async reactToMessage(userId, data) {
        try {
            const existingReaction = await database_js_1.prisma.messageReaction.findUnique({
                where: {
                    userId_messageId_type: {
                        userId,
                        messageId: data.messageId,
                        type: data.type,
                    },
                },
            });
            if (existingReaction) {
                await database_js_1.prisma.messageReaction.delete({
                    where: {
                        id: existingReaction.id,
                    },
                });
            }
            else {
                await database_js_1.prisma.messageReaction.create({
                    data: {
                        userId,
                        messageId: data.messageId,
                        type: data.type,
                    },
                });
            }
            const message = await database_js_1.prisma.message.findUnique({
                where: {
                    id: data.messageId,
                },
                include: {
                    reactions: {
                        include: {
                            user: {
                                select: {
                                    id: true,
                                    username: true,
                                },
                            },
                        },
                    },
                },
            });
            return {
                success: true,
                data: {
                    messageId: data.messageId,
                    reactions: message?.reactions ?? [],
                },
            };
        }
        catch (error) {
            console.error("React to message error:", error);
            return {
                success: false,
                error: "Failed to react to message",
            };
        }
    }
    static async markMessagesAsSeen(userId, messageIds) {
        try {
            const seenEntries = messageIds.map((messageId) => ({
                userId,
                messageId,
            }));
            await database_js_1.prisma.messageSeen.createMany({
                data: seenEntries,
                skipDuplicates: true,
            });
            const messages = await database_js_1.prisma.message.findMany({
                where: {
                    id: { in: messageIds },
                },
                include: {
                    seenBy: {
                        include: {
                            user: {
                                select: {
                                    id: true,
                                    username: true,
                                },
                            },
                        },
                    },
                },
            });
            const seenData = messages.map((message) => ({
                messageId: message.id,
                seenBy: message.seenBy.map((seen) => seen.userId),
            }));
            return {
                success: true,
                data: seenData,
            };
        }
        catch (error) {
            console.error("Mark messages as seen error:", error);
            return {
                success: false,
                error: "Failed to mark messages as seen",
            };
        }
    }
    static async deleteMessage(userId, messageId, roomId) {
        try {
            const message = await database_js_1.prisma.message.findFirst({
                where: {
                    id: messageId,
                    userId: userId,
                    roomId: roomId,
                },
            });
            if (!message) {
                return {
                    success: false,
                    error: "Message not found or you don't have permission to delete it",
                    status: 403,
                };
            }
            await database_js_1.prisma.message.delete({
                where: {
                    id: messageId,
                },
            });
            return {
                success: true,
                data: {
                    messageId,
                    roomId,
                },
            };
        }
        catch (error) {
            console.error("Delete message error:", error);
            return {
                success: false,
                error: "Failed to delete message",
                status: 500,
            };
        }
    }
}
exports.MessageService = MessageService;
