"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.CarController = void 0;
const ResponseHelper_1 = require("../../helpers/ResponseHelper");
const Car_1 = require("../../models/Car");
class CarController {
    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 };
                }
                if (req.query.search && req.query.search.trim()) {
                    filteredQuery.$or = [
                        {
                            "model.name": {
                                $regex: new RegExp(req.query.search),
                                $options: "i",
                            },
                        },
                        {
                            "make.name": {
                                $regex: new RegExp(req.query.search),
                                $options: "i",
                            },
                        },
                        {
                            "type.name": {
                                $regex: new RegExp(req.query.search),
                                $options: "i",
                            },
                        },
                    ];
                }
                let query = [
                    {
                        $lookup: {
                            from: "carmakes",
                            localField: "make_id",
                            foreignField: "_id",
                            as: "make"
                        },
                    },
                    {
                        $unwind: {
                            path: "$make",
                            preserveNullAndEmptyArrays: true
                        },
                    },
                    {
                        $lookup: {
                            from: "carmodels",
                            localField: "model_id",
                            foreignField: "_id",
                            as: "model"
                        },
                    },
                    {
                        $unwind: {
                            path: "$model",
                            preserveNullAndEmptyArrays: true
                        },
                    },
                    {
                        $lookup: {
                            from: "cartypes",
                            localField: "type_id",
                            foreignField: "_id",
                            as: "type"
                        },
                    },
                    {
                        $unwind: {
                            path: "$type",
                            preserveNullAndEmptyArrays: true
                        },
                    },
                    {
                        $match: filteredQuery,
                    },
                    {
                        $sort: {
                            created_at: -1,
                        },
                    },
                ];
                var myAggregate = Car_1.default.aggregate(query);
                const list = yield Car_1.default.aggregatePaginate(myAggregate, options);
                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 { make_id, model_id, type_id } = req.body;
                const getCar = yield Car_1.default.findOne({ _id: req.params.id });
                if (!getCar) {
                    const data = { make_id: make_id, model_id: model_id, type_id: type_id, };
                    const user = yield new Car_1.default(data).save();
                    return ResponseHelper_1.default.created(res, "SUCCESS", "Car has been added successfully", user);
                }
                getCar.make_id = make_id ? make_id : getCar.make_id,
                    getCar.model_id = model_id ? model_id : getCar.model_id,
                    getCar.type_id = type_id ? type_id : getCar.type_id,
                    getCar.save();
                return ResponseHelper_1.default.ok(res, "SUCCESS", "Car has been update successfully", getCar, 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 getCar = yield Car_1.default.findOne({ _id: req.params.id });
                if (!getCar) {
                    return ResponseHelper_1.default.notFound(res, "NOTFOUND", "Car not found", getCar, startTime);
                }
                (getCar.is_active = !getCar.is_active), getCar.save();
                return ResponseHelper_1.default.ok(res, "SUCCESS", "Status changed successfully", getCar, startTime);
            }
            catch (err) {
                next(err);
            }
        });
    }
    static view(req, res, next) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const startTime = new Date().getTime();
                const getCar = yield Car_1.default.findOne({ _id: req.params.id }).populate([
                    {
                        path: "type_id"
                    },
                    {
                        path: "make_id"
                    },
                    {
                        path: "model_id"
                    }
                ]);
                return ResponseHelper_1.default.ok(res, "SUCCESS", "Data get successfully", getCar, startTime);
            }
            catch (err) {
                next(err);
            }
        });
    }
}
exports.CarController = CarController;
