"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.uploadEventImage = uploadEventImage;
exports.createEvent = createEvent;
exports.editEvent = editEvent;
exports.editEventActive = editEventActive;
exports.deleteEvent = deleteEvent;
const uuid_1 = require("uuid");
const consts_1 = require("../../utils/consts");
const minio_1 = require("../../utils/minio");
const prisma_1 = __importDefault(require("../../utils/prisma"));
// ------------------------------------------------------------------------------------
function getEvents(req, reply) {
    return __awaiter(this, void 0, void 0, function* () {
        const events = yield prisma_1.default.event.findMany({
            orderBy: {
                date: "desc",
            },
            select: {
                id: true,
                title: true,
                date: true,
                active: true,
            },
        });
        return reply.code(200).send(events);
    });
}
// ------------------------------------------------------------------------------------
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,
            },
            include: {
                comments: {
                    orderBy: { createdAt: "asc" },
                    include: {
                        user: {
                            select: {
                                id: true,
                                firstName: true,
                                lastName: true,
                                role: 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 uploadEventImage(req, reply) {
    return __awaiter(this, void 0, void 0, function* () {
        const data = req.body;
        const buffer = yield data.image.toBuffer();
        const ext = data.image.filename.split(".").pop();
        const mimetype = data.image.mimetype;
        const fileName = "events/" + (0, uuid_1.v4)() + "." + ext;
        yield minio_1.minioClient.putObject(consts_1.bucketName, fileName, buffer, undefined, {
            "Content-Type": mimetype,
        });
        const presignedUrl = yield (0, minio_1.getPresignedUrl)(fileName);
        return reply
            .code(200)
            .send({ image: fileName, presignedImage: presignedUrl });
    });
}
// ------------------------------------------------------------------------------------
function createEvent(req, reply) {
    return __awaiter(this, void 0, void 0, function* () {
        const data = req.body;
        const event = yield prisma_1.default.event.create({
            data: {
                title: data.title,
                briefDescription: data.briefDescription,
                description: data.description,
                date: data.date,
                images: data.images,
            },
            select: {
                id: true,
                title: true,
                date: true,
                active: true,
            },
        });
        return reply.code(200).send(event);
    });
}
// ------------------------------------------------------------------------------------
function editEvent(req, reply) {
    return __awaiter(this, void 0, void 0, function* () {
        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 newEvent = yield prisma_1.default.event.update({
            where: { id: id },
            data: {
                title: data.title,
                briefDescription: data.briefDescription,
                description: data.description,
                date: data.date,
                images: data.images,
            },
            select: {
                id: true,
                title: true,
                date: true,
                active: true,
            },
        });
        return reply.code(200).send(newEvent);
    });
}
// ------------------------------------------------------------------------------------
function editEventActive(req, reply) {
    return __awaiter(this, void 0, void 0, function* () {
        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 newEvent = yield prisma_1.default.event.update({
            where: { id: id },
            data: {
                active: data.active,
            },
        });
        return reply.code(200).send({ active: newEvent.active });
    });
}
// ------------------------------------------------------------------------------------
function deleteEvent(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,
            },
        });
        if (!event) {
            return reply.code(404).send({
                message: "Cet événement n'existe pas",
            });
        }
        yield Promise.all(event.images.map((image) => __awaiter(this, void 0, void 0, function* () { return yield minio_1.minioClient.removeObject(consts_1.bucketName, image); })));
        yield prisma_1.default.event.delete({
            where: { id: id },
        });
        return reply.code(200).send({});
    });
}
// ------------------------------------------------------------------------------------
