"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.NotificationController = void 0;
const ResponseHelper_1 = require("../../helpers/ResponseHelper");
const NotificationUser_1 = require("../../models/NotificationUser");
const User_1 = require("../../models/User");
const Order_1 = require("../../models/Order");
const Helper_1 = require("../../helpers/Helper");
class NotificationController {
    /**
     * @api {get} /api/app/notification/list Notification List
     * @apiVersion 1.0.0
     * @apiName Notification List
     * @apiHeader {String} Authorization Pass jwt token.
     * @apiGroup Notifications
    */
    static list(req, res, next) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const startTime = new Date().getTime();
                const getNotification = yield NotificationUser_1.default.find({ to_id: req.user.id }).sort({ created_at: -1 });
                return ResponseHelper_1.default.api(res, true, req.lang["NotificationList"], getNotification, startTime);
            }
            catch (err) {
                next(err);
            }
        });
    }
    /**
     * @api {get} /api/app/notification/delete All Delete
     * @apiVersion 1.0.0
     * @apiName Delete All Notification
     * @apiHeader {String} Authorization Pass jwt token.
     * @apiGroup Notifications
    */
    /**
     * @api {get} /api/app/notification/delete/64abafe54bf77d8f8cdbba9e Single Delete
     * @apiVersion 1.0.0
     * @apiName Delete Single Notification
     * @apiHeader {String} Authorization Pass jwt token.
     * @apiGroup Notifications
    */
    static delete(req, res, next) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const startTime = new Date().getTime();
                const getNotification = yield NotificationUser_1.default.findOne({ _id: req.params.id });
                if (!getNotification && req.params.id) {
                    return ResponseHelper_1.default.api(res, false, req.lang["NotificationNotFounds"], {}, startTime);
                }
                if (getNotification) {
                    getNotification.delete();
                }
                else {
                    yield NotificationUser_1.default.find({ to_id: req.user.id }).deleteMany();
                }
                return ResponseHelper_1.default.api(res, true, req.lang["NotificationDeleteSuccess"], {}, startTime);
            }
            catch (err) {
                next(err);
            }
        });
    }
    /**
     * @api {get} /api/app/notification/read All Read
     * @apiVersion 1.0.0
     * @apiName Read All Notification
     * @apiHeader {String} Authorization Pass jwt token.
     * @apiGroup Notifications
     * @apiSuccessExample {json} Success-Response:
     * {"status":true,"message":"Notification has been viewed successfully","data":{},"exeTime":526}
    */
    /**
     * @api {get} /api/app/notification/read/64abafe54bf77d8f8cdbba9e Single Read
     * @apiVersion 1.0.0
     * @apiName Read Single Notification
     * @apiHeader {String} Authorization Pass jwt token.
     * @apiGroup Notifications
     * @apiSuccessExample {json} Success-Response:
     * {"status":true,"message":"Notification has been viewed successfully","data":{"title":"First Title","description":"First Description","is_read":true,"_id":"64abafe54bf77d8f8cdbba9e","created_at":"2023-06-22T08:54:28.798Z","from_id":"64940c44b505a33af81063d0","to_id":"64a548d55867841d5edda7fa","updated_at":"2023-07-10T08:33:18.490Z"},"exeTime":272}
    */
    static read(req, res, next) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const startTime = new Date().getTime();
                const getNotification = yield NotificationUser_1.default.findOne({ _id: req.params.id, to_id: req.user.id });
                if (!getNotification && req.params.id) {
                    return ResponseHelper_1.default.api(res, false, req.lang["NotificationNotFounds"], {}, startTime);
                }
                if (getNotification) {
                    (getNotification.is_read = true), getNotification.save();
                    return ResponseHelper_1.default.api(res, true, req.lang["NotificationReadSuccess"], getNotification, startTime);
                }
                else {
                    yield NotificationUser_1.default.find({ to_id: req.user.id }).updateMany({ is_read: true });
                    return ResponseHelper_1.default.api(res, true, req.lang["NotificationReadSuccess"], {}, startTime);
                }
            }
            catch (err) {
                next(err);
            }
        });
    }
    /**
     * @api {get} /api/app/notification/show Show
     * @apiVersion 1.0.0
     * @apiName Show Notification
     * @apiHeader {String} Authorization Pass jwt token.
     * @apiGroup Notifications
    */
    static show(req, res, next) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const startTime = new Date().getTime();
                const getNotification = yield NotificationUser_1.default.findOne({ _id: req.params.id });
                if (!getNotification) {
                    return ResponseHelper_1.default.api(res, true, req.lang["NotificationNotFounds"], {}, startTime);
                }
                return ResponseHelper_1.default.api(res, true, req.lang["NotificationGet"], getNotification, startTime);
            }
            catch (err) {
                next(err);
            }
        });
    }
    static sendNotification(req, res, next) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const startTime = new Date().getTime();
                const orderId = req.params.orderId;
                const { time } = req.body;
                const order = yield Order_1.default.findOne({ _id: orderId });
                if (!order) {
                    return ResponseHelper_1.default.api(res, true, req.lang["NotificationNotFounds"], {}, startTime);
                }
                let washer = yield User_1.default.findById(req.user.id);
                yield Helper_1.default.sendNotificationToAdmin(washer._id, Helper_1.default.adminId, 'Washer stay at customer location', `${washer.name} stay ${time} extra at customer location`);
                // if (order.order_type == 'Subscription') {
                //   washer = 
                // } else {
                // }
                return ResponseHelper_1.default.api(res, true, req.lang["NotificationGet"], order, startTime);
            }
            catch (err) {
                next(err);
            }
        });
    }
}
exports.NotificationController = NotificationController;
