"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.CommonController = void 0;
const ResponseHelper_1 = require("../helpers/ResponseHelper");
const CarMake_1 = require("../models/CarMake");
const CarModel_1 = require("../models/CarModel");
const Content_1 = require("../models/Content");
const Order_1 = require("../models/Order");
const Subscription_1 = require("../models/Subscription");
const Discount_1 = require("../models/Discount");
const User_1 = require("../models/User");
const moment = require("moment");
const ProductService_1 = require("../models/ProductService");
const CarAvatar_1 = require("../models/CarAvatar");
const CarColor_1 = require("../models/CarColor");
/* import { FileUpload } from "../helpers/FileUpload"; */
const AppSetting_1 = require("../models/AppSetting");
const UserCar_1 = require("../models/UserCar");
const UserSubscription_1 = require("../models/UserSubscription");
const CarType_1 = require("../models/CarType");
const Settings_1 = require("../models/Settings");
class CommonController {
    /**
     * @api {get} /api/common/app-setting App Setting
     * @apiVersion 1.0.0
     * @apiName App Setting
     * @apiGroup Masters
     */
    static appSetting(req, res, next) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const startTime = new Date().getTime();
                const getAppSetting = yield AppSetting_1.default.findOne().sort({ _id: -1 });
                return ResponseHelper_1.default.api(res, true, "App setting get successfully", getAppSetting, startTime);
            }
            catch (error) {
                next(error);
            }
        });
    }
    /**
     * @api {get} /api/common/subscription-detail/64a80789e845d0e1acc284a4 Subscription Detail
     * @apiVersion 1.0.0
     * @apiName Subscription Detail
     * @apiGroup Subscription Masters
     */
    static subscriptionDetail(req, res, next) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const startTime = new Date().getTime();
                const subscriptions = yield Subscription_1.default.findOne({
                    _id: req.params.id,
                }).populate([
                    {
                        path: "services.service_id",
                        select: "_id no_of_time name",
                    },
                    /* {
                      path: "services.service_id",
                      populate: ([
                        {
                          path: "vehicle_type",
                          select: "_id name is_active"
                        },
                      ])
                    } */
                ]);
                return ResponseHelper_1.default.ok(res, "SUCCESS", "Subscription detail get successfully", subscriptions, startTime);
            }
            catch (err) {
                next(err);
            }
        });
    }
    /**
     * @api {get} /api/common/product-service-list Product Service List
     * @apiVersion 1.0.0
     * @apiName Product Service List
     * @apiGroup Masters
     */
    static productServiceList(req, res, next) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const startTime = new Date().getTime();
                const user_car_id = req.params.user_car_id;
                const userCar = yield UserCar_1.default.findById(user_car_id);
                let getService = [];
                if (user_car_id && userCar) {
                    let carModel = yield CarModel_1.default.findById(userCar.model_id);
                    if (carModel) {
                        getService = yield ProductService_1.default.find({
                            is_active: true,
                            vehicle_type: carModel.type_id,
                        }).sort({ created_at: -1 });
                    }
                }
                else {
                    getService = yield ProductService_1.default.find({ is_active: true }).sort({
                        created_at: -1,
                    });
                }
                /* if(user_car_id && userCar){
                  getService    =   await ProductService.find({ is_active : true, vehicle_make : userCar.make_id, vehicle_model : userCar.model_id }).sort({ created_at: -1 });
                }else{
                  getService    =   await ProductService.find({ is_active : true }).sort({ created_at: -1 });
                } */
                // Assuming additionalPrice is a fixed value you want to add
                const setting = yield Settings_1.default.findOne({}).sort({ created_at: -1 });
                const additionalPrice = setting === null || setting === void 0 ? void 0 : setting.tariff_charge;
                const updatedFeaturedService = getService.map((service) => {
                    const modifyService = service.toObject();
                    modifyService.price += additionalPrice;
                    modifyService.tariff_charge = additionalPrice === null || additionalPrice === void 0 ? void 0 : additionalPrice.toFixed(2); // New key on 07-06-2024
                    return modifyService;
                });
                // End New Code Here
                const protocol = req.protocol;
                const host = req.get("host");
                updatedFeaturedService.forEach((item) => {
                    item.image = item.image
                        ? `${protocol}://${host}/public/${item.image}`
                        : "";
                });
                return ResponseHelper_1.default.api(res, true, "Product service list get successfully", updatedFeaturedService, startTime);
            }
            catch (error) {
                next(error);
            }
        });
    }
    /**
     * @api {get} /api/common/discount-code Discount List
     * @apiVersion 1.0.0
     * @apiName Discount Code
     * @apiGroup Discount Masters
     */
    static discountCode(req, res, next) {
        return __awaiter(this, void 0, void 0, function* () {
            const startTime = new Date().getTime();
            const today = moment().format("YYYY-MM-DD");
            try {
                const getDiscount = yield Discount_1.default.find({
                    is_active: true,
                    from_date: { $lte: today },
                    to_date: { $gte: today },
                });
                return ResponseHelper_1.default.api(res, true, "Discount code get successfully", getDiscount, startTime);
            }
            catch (error) {
                next(error);
            }
        });
    }
    /**
     * @api {get} /api/common/content-list/slug Content List
     * @apiVersion 1.0.0
     * @apiName Content List
     * @apiGroup Masters
     * @apiParam {String} slug Slug (Pass slug in the URL - terms-and-conditions, how-it-works, about-us, privacy-policy).
     */
    static contentList(req, res, next) {
        return __awaiter(this, void 0, void 0, function* () {
            const startTime = new Date().getTime();
            const slug = req.params.slug;
            try {
                let getContent = {};
                if (slug) {
                    getContent = yield Content_1.default.findOne({ slug: slug });
                }
                return ResponseHelper_1.default.api(res, true, "Content list get successfully", getContent, startTime);
            }
            catch (error) {
                next(error);
            }
        });
    }
    /**
     * @api {get} /api/common/washer-list Washer List
     * @apiVersion 1.0.0
     * @apiName Washer List
     * @apiGroup Masters
     */
    static washerList(req, res, next) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const startTime = new Date().getTime();
                const getWasher = yield User_1.default.find({ type: "Washer" }).sort({ name: 1 }); //is_active : true,
                const protocol = req.protocol;
                const host = req.get("host");
                getWasher.forEach((item) => {
                    item.image = item.image
                        ? `${protocol}://${host}/public/${item.image}`
                        : "";
                });
                return ResponseHelper_1.default.api(res, true, "Washer list get successfully", getWasher, startTime);
            }
            catch (err) {
                next(err);
            }
        });
    }
    static activeWasherList(req, res, next) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const startTime = new Date().getTime();
                const getWasher = yield User_1.default.find({
                    type: "Washer",
                    is_active: true,
                    is_delete: false,
                }).sort({ name: 1 });
                return ResponseHelper_1.default.api(res, true, "Active washer list get successfully", getWasher, startTime);
            }
            catch (err) {
                next(err);
            }
        });
    }
    static orderList(req, res, next) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const startTime = new Date().getTime();
                const getOrder = yield Order_1.default.find({ payment_method: "Cod" }).sort({
                    created_at: -1,
                });
                return ResponseHelper_1.default.api(res, true, "Order list get successfully", getOrder, startTime);
            }
            catch (err) {
                next(err);
            }
        });
    }
    /**
     * @api {get} /api/common/customer-list Customer List
     * @apiVersion 1.0.0
     * @apiName Customer List
     * @apiGroup Masters
     */
    static customerList(req, res, next) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const startTime = new Date().getTime();
                const getCustomer = yield User_1.default.find({ type: "Customer" }).sort({
                    name: 1,
                });
                const protocol = req.protocol;
                const host = req.get("host");
                getCustomer.forEach((item) => {
                    item.image = item.image
                        ? `${protocol}://${host}/public/${item.image}`
                        : "";
                });
                return ResponseHelper_1.default.api(res, true, "Customer list get successfully", getCustomer, startTime);
            }
            catch (err) {
                next(err);
            }
        });
    }
    /**
     * @api {get} /api/common/make-list?type_id= Car Make List
     * @apiVersion 1.0.0
     * @apiName Car Make List
     * @apiGroup Vehicle Master
     * @apiParam type_id type id
     */
    static carMakeList(req, res, next) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const startTime = new Date().getTime();
                const filter = { is_active: true };
                const getCarMake = yield CarMake_1.default.find(filter).sort({ name: 1 }); // .populate({path:'type'});
                const protocol = req.protocol;
                const host = req.get("host");
                getCarMake.forEach((item) => {
                    item.image = item.image
                        ? `${protocol}://${host}/public/${item.image}`
                        : "";
                });
                return ResponseHelper_1.default.api(res, true, "Car make list get successfully", getCarMake /* carMakesWithModels */, startTime);
            }
            catch (err) {
                next(err);
            }
        });
    }
    /**
     * @api {get} /api/common/model-list/:make_id Car Model List
     * @apiVersion 1.0.0
     * @apiName Car Model List
     * @apiGroup Vehicle Master
     */
    static carModelList(req, res, next) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const startTime = new Date().getTime();
                const getCarModel = yield CarModel_1.default.find({
                    is_active: true,
                    make_id: req.params.make_id,
                }).sort({ name: 1 });
                return ResponseHelper_1.default.api(res, true, "Car model list get successfully", getCarModel, startTime);
            }
            catch (err) {
                next(err);
            }
        });
    }
    /**
     * @api {get} /api/common/type-list Car Type List
     * @apiVersion 1.0.0
     * @apiName Car Type List
     * @apiGroup Vehicle Master
     */
    static carTypeList(req, res, next) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const startTime = new Date().getTime();
                const getCarType = yield CarType_1.default.find({
                    is_active: true,
                    is_delete: false,
                }).sort({ name: 1 });
                return ResponseHelper_1.default.api(res, true, "Car type list get successfully", getCarType, startTime);
            }
            catch (err) {
                next(err);
            }
        });
    }
    /**
     * @api {get} /api/common/color-list Car Color List
     * @apiVersion 1.0.0
     * @apiName Car Color List
     * @apiGroup Vehicle Master
     */
    static carColorList(req, res, next) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const startTime = new Date().getTime();
                const getCarColor = yield CarColor_1.default.find({ is_active: true });
                return ResponseHelper_1.default.api(res, true, "Car color list get successfully", getCarColor, startTime);
            }
            catch (err) {
                next(err);
            }
        });
    }
    /**
     * @api {get} /api/common/avatar-list Car Avatar List
     * @apiVersion 1.0.0
     * @apiName Car Avatar List
     * @apiGroup Vehicle Master
     */
    static carAvatarList(req, res, next) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const startTime = new Date().getTime();
                const getCarType = yield CarAvatar_1.default.find({ is_active: true });
                const protocol = req.protocol;
                const host = req.get("host");
                getCarType.forEach((item) => {
                    item.image = item.image
                        ? `${protocol}://${host}/public/${item.image}`
                        : "";
                });
                return ResponseHelper_1.default.api(res, true, "Car avatar list get successfully", getCarType, startTime);
            }
            catch (err) {
                next(err);
            }
        });
    }
    static subscriptions(req, res, next) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const startTime = new Date().getTime();
                const getSubscription = yield Subscription_1.default.find({ is_active: true });
                return ResponseHelper_1.default.api(res, true, "Subscription list get successfully", getSubscription, startTime);
            }
            catch (err) {
                next(err);
            }
        });
    }
    static staticPage(req, res, next) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const startTime = new Date().getTime();
                const slug = req.params.slug;
                const getContent = yield Content_1.default.findOne({ slug: slug });
                return ResponseHelper_1.default.api(res, true, "", getContent, startTime);
            }
            catch (err) {
                next(err);
            }
        });
    }
    /**
     * @api {post} /api/common/image-upload Image Upload
     * @apiVersion 1.0.0
     * @apiName Image Upload
     * @apiGroup Masters
     * @apiParam {File} image Image.
     * @apiParam {String} type Type (Ex.Profile, Type can be which image you are uploading).
     */
    static uploadImage(req, res, next) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const startTime = new Date().getTime();
                const { image } = req.body.files;
                const { type } = req.body;
                let path = type !== null && type !== void 0 ? type : "app";
                /* const upload = await FileUpload.uploadInS3(image, path); */
                return ResponseHelper_1.default.api(res, true, "Image Uploaded Successfully", {}, startTime);
            }
            catch (err) {
                next(err);
            }
        });
    }
    /***
     * ==================================================== Authentication API ====================================================
     */
    /**
     * @api {post} /api/common/subscription-list Subscription List
     * @apiVersion 1.0.0
     * @apiName Subscription List
     * @apiHeader {String} Authorization Pass jwt token.
     * @apiGroup Subscription Masters
     * @apiParam {String} car_id Car ID.
     */
    static subscriptionList(req, res, next) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const startTime = new Date().getTime();
                const user_id = req.user.id;
                const { car_id } = req.body;
                const getUserSubscription = yield UserSubscription_1.default.findOne({
                    user_id: user_id,
                    car_id: car_id,
                }).lean();
                const subscriptions = yield Subscription_1.default.find({ is_active: true })
                    .sort({ created_at: -1 })
                    .populate([
                    {
                        path: "services.service_id",
                        select: "_id no_of_time name",
                    },
                ])
                    .lean();
                const subscriptionsWithSelection = subscriptions.map((subscription) => (Object.assign(Object.assign({}, subscription), { is_selected: getUserSubscription &&
                        getUserSubscription.subscription._id.toString() ===
                            subscription._id.toString()
                        ? true
                        : false })));
                return ResponseHelper_1.default.api(res, true, "Subscription list get successfully", subscriptionsWithSelection, startTime);
            }
            catch (error) {
                next(error);
            }
        });
    }
}
exports.CommonController = CommonController;
