"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());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CarAvatarController = void 0;
const ResponseHelper_1 = require("../../helpers/ResponseHelper");
const CarAvatar_1 = require("../../models/CarAvatar");
const Helper_1 = require("../../helpers/Helper");
const ImageService_1 = require("../../services/ImageService");
class CarAvatarController {
    static list(req, res, next) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const startTime = new Date().getTime();
                let sort = [["createdAt", -1]];
                if (req.query.sort) {
                    sort = Object.keys(req.query.sort).map((key) => [
                        key,
                        req.query.sort[key],
                    ]);
                }
                const options = {
                    page: req.query.page || 1,
                    limit: req.query.limit || 10,
                    collation: {
                        locale: "en",
                    },
                };
                let filteredQuery = {};
                if (req.query.status) {
                    var arrayValues = req.query.status.split(",");
                    var booleanValues = arrayValues.map(function (value) {
                        return value.toLowerCase() === "true";
                    });
                    filteredQuery.is_active = { $in: booleanValues };
                }
                let query = [
                    {
                        $match: filteredQuery,
                    },
                    {
                        $sort: {
                            created_at: -1,
                        },
                    },
                ];
                var myAggregate = CarAvatar_1.default.aggregate(query);
                let list = yield CarAvatar_1.default.aggregatePaginate(myAggregate, options);
                list = Helper_1.default.generateListWithAssetUrl(req, list);
                return ResponseHelper_1.default.ok(res, "SUCCESS", "List", { list: list }, startTime);
            }
            catch (err) {
                next(err);
            }
        });
    }
    static addEdit(req, res, next) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const startTime = new Date().getTime();
                const getAvatar = yield CarAvatar_1.default.findOne({ _id: req.params.id });
                if (req.file) {
                    if (!getAvatar) {
                        let avatar;
                        const data = { image: "" };
                        avatar = yield new CarAvatar_1.default(data).save();
                        const imageFullPath = yield ImageService_1.default.uploadImageLocally(req, res, next, `cars/avatars/${avatar.id}`);
                        avatar.image = imageFullPath !== null && imageFullPath !== void 0 ? imageFullPath : "";
                        yield avatar.save();
                        avatar.image = avatar.image
                            ? Helper_1.default.generateAssetUrl(req, avatar.image)
                            : "";
                        return ResponseHelper_1.default.created(res, "SUCCESS", "Avatar has been added successfully", avatar);
                    }
                    yield ImageService_1.default.deleteImageLocally(`cars/avatars/${getAvatar.id}`, getAvatar.image);
                    getAvatar.image = req.file
                        ? yield ImageService_1.default.uploadImageLocally(req, res, next, `cars/avatars/${getAvatar.id}`)
                        : getAvatar.image;
                    yield getAvatar.save();
                    getAvatar.image = Helper_1.default.generateAssetUrl(req, getAvatar.image);
                    console.log(getAvatar.image);
                    return ResponseHelper_1.default.ok(res, "SUCCESS", "Avatar has been update successfully", getAvatar, startTime);
                }
                else {
                    return ResponseHelper_1.default.badRequest(res, "BAD_REQUEST", "No avatar image provided for upload", getAvatar, startTime);
                }
            }
            catch (err) {
                next(err);
            }
        });
    }
    static statusChange(req, res, next) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const startTime = new Date().getTime();
                const getAvatar = yield CarAvatar_1.default.findOne({ _id: req.params.id });
                if (!getAvatar) {
                    return ResponseHelper_1.default.notFound(res, "NOTFOUND", "Car avatar not found", getAvatar, startTime);
                }
                (getAvatar.is_active = !getAvatar.is_active), getAvatar.save();
                return ResponseHelper_1.default.ok(res, "SUCCESS", "Status changed successfully", getAvatar, startTime);
            }
            catch (err) {
                next(err);
            }
        });
    }
}
exports.CarAvatarController = CarAvatarController;
