"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.getAppointments = getAppointments;
exports.getAppointment = getAppointment;
exports.uploadAppointmentImage = uploadAppointmentImage;
exports.createAppointment = createAppointment;
exports.editAppointment = editAppointment;
exports.editAppointmentActive = editAppointmentActive;
exports.deleteAppointment = deleteAppointment;
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 getAppointments(req, reply) {
    return __awaiter(this, void 0, void 0, function* () {
        const appointments = yield prisma_1.default.appointment.findMany({
            orderBy: {
                date: "desc",
            },
            select: {
                id: true,
                title: true,
                date: true,
                activationDate: true,
                active: true,
            },
        });
        return reply.code(200).send(appointments);
    });
}
// ------------------------------------------------------------------------------------
function getAppointment(req, reply) {
    return __awaiter(this, void 0, void 0, function* () {
        const id = req.params.id;
        const appointment = yield prisma_1.default.appointment.findUnique({
            where: {
                id: id,
            },
            include: {
                comments: {
                    orderBy: { createdAt: "asc" },
                    include: {
                        user: {
                            select: {
                                id: true,
                                firstName: true,
                                lastName: true,
                                role: true,
                            },
                        },
                    },
                },
                likes: true,
                registrations: {
                    orderBy: { createdAt: "asc" },
                    include: {
                        user: {
                            select: {
                                id: true,
                                firstName: true,
                                lastName: true,
                                email: true,
                                phoneNumber: true,
                                role: true,
                            },
                        },
                    },
                },
            },
        });
        if (!appointment) {
            return reply.code(404).send({
                message: "Cet événement n'existe pas",
            });
        }
        const presignedImages = yield Promise.all(appointment.images.map((image) => __awaiter(this, void 0, void 0, function* () { return yield (0, minio_1.getPresignedUrl)(image); })));
        const finalAppointment = Object.assign(Object.assign({}, appointment), { presignedImages: presignedImages });
        return reply.code(200).send(finalAppointment);
    });
}
// ------------------------------------------------------------------------------------
function uploadAppointmentImage(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 = "appointments/" + (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 createAppointment(req, reply) {
    return __awaiter(this, void 0, void 0, function* () {
        const data = req.body;
        const appointment = yield prisma_1.default.appointment.create({
            data: {
                title: data.title,
                briefDescription: data.briefDescription,
                description: data.description,
                date: data.date,
                time: data.time,
                activationDate: data.activationDate,
                address: data.address,
                images: data.images,
            },
            select: {
                id: true,
                title: true,
                date: true,
                activationDate: true,
                active: true,
            },
        });
        return reply.code(200).send(appointment);
    });
}
// ------------------------------------------------------------------------------------
function editAppointment(req, reply) {
    return __awaiter(this, void 0, void 0, function* () {
        const id = req.params.id;
        const data = req.body;
        const appointment = yield prisma_1.default.appointment.findUnique({
            where: {
                id: id,
            },
        });
        if (!appointment) {
            return reply.code(404).send({
                message: "Cet événement n'existe pas",
            });
        }
        const newAppointment = yield prisma_1.default.appointment.update({
            where: { id: id },
            data: {
                title: data.title,
                briefDescription: data.briefDescription,
                description: data.description,
                date: data.date,
                time: data.time,
                activationDate: data.activationDate,
                address: data.address,
                images: data.images,
            },
            select: {
                id: true,
                title: true,
                date: true,
                activationDate: true,
                active: true,
            },
        });
        return reply.code(200).send(newAppointment);
    });
}
// ------------------------------------------------------------------------------------
function editAppointmentActive(req, reply) {
    return __awaiter(this, void 0, void 0, function* () {
        const id = req.params.id;
        const data = req.body;
        const appointment = yield prisma_1.default.appointment.findUnique({
            where: {
                id: id,
            },
        });
        if (!appointment) {
            return reply.code(404).send({
                message: "Cet événement n'existe pas",
            });
        }
        const newAppointment = yield prisma_1.default.appointment.update({
            where: { id: id },
            data: {
                active: data.active,
            },
        });
        return reply.code(200).send({ active: newAppointment.active });
    });
}
// ------------------------------------------------------------------------------------
function deleteAppointment(req, reply) {
    return __awaiter(this, void 0, void 0, function* () {
        const id = req.params.id;
        const appointment = yield prisma_1.default.appointment.findUnique({
            where: {
                id: id,
            },
        });
        if (!appointment) {
            return reply.code(404).send({
                message: "Cet événement n'existe pas",
            });
        }
        yield Promise.all(appointment.images.map((image) => __awaiter(this, void 0, void 0, function* () { return yield minio_1.minioClient.removeObject(consts_1.bucketName, image); })));
        yield prisma_1.default.appointment.delete({
            where: { id: id },
        });
        return reply.code(200).send({});
    });
}
// ------------------------------------------------------------------------------------
