"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getEvents = getEvents;
exports.getEvent = getEvent;
exports.eventLike = eventLike;
exports.eventComment = eventComment;
const prisma_1 = __importDefault(require("../../utils/prisma"));
const minio_1 = require("../../utils/minio");
// ------------------------------------------------------------------------------------
function getEvents(req, reply) {
    return __awaiter(this, void 0, void 0, function* () {
        const events = yield prisma_1.default.event.findMany({
            orderBy: {
                date: "desc",
            },
            where: { active: true },
            select: {
                id: true,
                title: true,
                date: true,
                images: true,
            },
        });
        const newEvents = yield Promise.all(events.map((event) => __awaiter(this, void 0, void 0, function* () {
            const presignedImages = yield Promise.all(event.images.map((image) => __awaiter(this, void 0, void 0, function* () { return yield (0, minio_1.getPresignedUrl)(image); })));
            return {
                id: event.id,
                title: event.title,
                date: event.date,
                presignedImages,
            };
        })));
        return reply.code(200).send(newEvents);
    });
}
// ------------------------------------------------------------------------------------
function getEvent(req, reply) {
    return __awaiter(this, void 0, void 0, function* () {
        const id = req.params.id;
        const event = yield prisma_1.default.event.findUnique({
            where: {
                id: id,
                active: true,
            },
            include: {
                comments: {
                    orderBy: { createdAt: "asc" },
                    include: {
                        user: {
                            select: {
                                id: true,
                                firstName: true,
                                lastName: true,
                            },
                        },
                    },
                },
                likes: true,
            },
        });
        if (!event) {
            return reply.code(404).send({
                message: "Cet événement n'existe pas",
            });
        }
        const presignedImages = yield Promise.all(event.images.map((image) => __awaiter(this, void 0, void 0, function* () { return yield (0, minio_1.getPresignedUrl)(image); })));
        const finalEvent = Object.assign(Object.assign({}, event), { presignedImages: presignedImages });
        return reply.code(200).send(finalEvent);
    });
}
// ------------------------------------------------------------------------------------
function eventLike(req, reply) {
    return __awaiter(this, void 0, void 0, function* () {
        const userId = req.user.id;
        const id = req.params.id;
        const event = yield prisma_1.default.event.findUnique({
            where: {
                id: id,
            },
        });
        if (!event) {
            return reply.code(404).send({
                message: "Cet événement n'existe pas",
            });
        }
        const oldEventLike = yield prisma_1.default.eventLike.findFirst({
            where: {
                eventId: event.id,
                userId: userId,
            },
        });
        if (oldEventLike) {
            yield prisma_1.default.eventLike.delete({
                where: {
                    id: oldEventLike.id,
                },
            });
            return reply.code(200).send(null);
        }
        const eventLike = yield prisma_1.default.eventLike.create({
            data: {
                eventId: event.id,
                userId: userId,
            },
        });
        return reply.code(200).send(eventLike);
    });
}
// ------------------------------------------------------------------------------------
function eventComment(req, reply) {
    return __awaiter(this, void 0, void 0, function* () {
        const userId = req.user.id;
        const id = req.params.id;
        const data = req.body;
        const event = yield prisma_1.default.event.findUnique({
            where: {
                id: id,
            },
        });
        if (!event) {
            return reply.code(404).send({
                message: "Cet événement n'existe pas",
            });
        }
        const eventComment = yield prisma_1.default.eventComment.create({
            data: {
                eventId: event.id,
                userId: userId,
                content: data.content,
            },
            include: {
                user: {
                    select: {
                        id: true,
                        firstName: true,
                        lastName: true,
                    },
                },
            },
        });
        return reply.code(200).send(eventComment);
    });
}
// ------------------------------------------------------------------------------------
