"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.getAlbums = getAlbums;
exports.getAlbum = getAlbum;
exports.uploadAlbumImage = uploadAlbumImage;
exports.createAlbum = createAlbum;
exports.editAlbum = editAlbum;
exports.deleteAlbum = deleteAlbum;
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 getAlbums(req, reply) {
    return __awaiter(this, void 0, void 0, function* () {
        const albums = yield prisma_1.default.album.findMany({
            orderBy: {
                date: "desc",
            },
            select: {
                id: true,
                title: true,
                date: true,
            },
        });
        return reply.code(200).send(albums);
    });
}
// ------------------------------------------------------------------------------------
function getAlbum(req, reply) {
    return __awaiter(this, void 0, void 0, function* () {
        const id = req.params.id;
        const album = yield prisma_1.default.album.findUnique({
            where: {
                id: id,
            },
        });
        if (!album) {
            return reply.code(404).send({
                message: "Cet album n'existe pas",
            });
        }
        const presignedImages = yield Promise.all(album.images.map((image) => __awaiter(this, void 0, void 0, function* () { return yield (0, minio_1.getPresignedUrl)(image); })));
        const finalAlbum = Object.assign(Object.assign({}, album), { presignedImages: presignedImages });
        return reply.code(200).send(finalAlbum);
    });
}
// ------------------------------------------------------------------------------------
function uploadAlbumImage(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 = "albums/" + (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 createAlbum(req, reply) {
    return __awaiter(this, void 0, void 0, function* () {
        const data = req.body;
        const album = yield prisma_1.default.album.create({
            data: {
                title: data.title,
                date: data.date,
                images: data.images,
            },
        });
        return reply.code(200).send(album);
    });
}
// ------------------------------------------------------------------------------------
function editAlbum(req, reply) {
    return __awaiter(this, void 0, void 0, function* () {
        const id = req.params.id;
        const data = req.body;
        const album = yield prisma_1.default.album.findUnique({
            where: {
                id: id,
            },
        });
        if (!album) {
            return reply.code(404).send({
                message: "Cet album n'existe pas",
            });
        }
        const newAlbum = yield prisma_1.default.album.update({
            where: { id: id },
            data: {
                title: data.title,
                date: data.date,
                images: data.images,
            },
        });
        return reply.code(200).send(newAlbum);
    });
}
// ------------------------------------------------------------------------------------
function deleteAlbum(req, reply) {
    return __awaiter(this, void 0, void 0, function* () {
        const id = req.params.id;
        const album = yield prisma_1.default.album.findUnique({
            where: {
                id: id,
            },
        });
        if (!album) {
            return reply.code(404).send({
                message: "Cet album n'existe pas",
            });
        }
        yield Promise.all(album.images.map((image) => __awaiter(this, void 0, void 0, function* () { return yield minio_1.minioClient.removeObject(consts_1.bucketName, image); })));
        yield prisma_1.default.album.delete({
            where: { id: id },
        });
        return reply.code(200).send({});
    });
}
// ------------------------------------------------------------------------------------
