"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.update = exports.updateTicketSettings = exports.ticketSettings = exports.index = void 0;
const socket_1 = require("../libs/socket");
const AppError_1 = __importDefault(require("../errors/AppError"));
const UpdateSettingService_1 = __importDefault(require("../services/SettingServices/UpdateSettingService"));
const ListSettingsService_1 = __importDefault(require("../services/SettingServices/ListSettingsService"));
const LoadSettings_1 = __importDefault(require("../helpers/LoadSettings"));
const UpdateSettingTicket_1 = __importDefault(require("../services/SettingServices/UpdateSettingTicket"));
const SettingTicket_1 = __importDefault(require("../models/SettingTicket"));
const Whatsapp_1 = __importDefault(require("../models/Whatsapp"));
const WhatsappOfficialNumberInfo_1 = __importDefault(require("../helpers/WhatsappOfficialNumberInfo"));
const index = (req, res) => __awaiter(void 0, void 0, void 0, function* () {
    // if (req.user.profile !== "master") {
    //   throw new AppError("ERR_NO_PERMISSION", 403);
    // }
    const settings = yield ListSettingsService_1.default();
    return res.status(200).json({ settings });
});
exports.index = index;
const ticketSettings = (req, res) => __awaiter(void 0, void 0, void 0, function* () {
    const { number } = req.params;
    const config = yield SettingTicket_1.default.findAll({ where: { number } });
    return res.status(200).json({ config });
});
exports.ticketSettings = ticketSettings;
const updateTicketSettings = (req, res) => __awaiter(void 0, void 0, void 0, function* () {
    const { number, saturdayBusinessTime, outBusinessHours, ticketExpiration, weekend, saturday, sunday, holiday } = req.body;
    if (!number)
        throw new AppError_1.default("No number selected", 400);
    if (outBusinessHours && Object.keys(outBusinessHours).length > 0) {
        yield UpdateSettingTicket_1.default(Object.assign(Object.assign({}, outBusinessHours), { key: "outBusinessHours", number }));
    }
    if (saturdayBusinessTime && Object.keys(saturdayBusinessTime).length > 0) {
        yield UpdateSettingTicket_1.default(Object.assign(Object.assign({}, saturdayBusinessTime), { key: "saturdayBusinessTime", number }));
    }
    if (ticketExpiration && Object.keys(ticketExpiration).length > 0) {
        yield UpdateSettingTicket_1.default(Object.assign(Object.assign({}, ticketExpiration), { key: "ticketExpiration", number }));
    }
    if (weekend && Object.keys(weekend).length > 0) {
        yield UpdateSettingTicket_1.default(Object.assign(Object.assign({}, weekend), { key: "weekend", number }));
    }
    if (saturday && Object.keys(saturday).length > 0) {
        yield UpdateSettingTicket_1.default(Object.assign(Object.assign({}, saturday), { key: "saturday", number }));
    }
    if (sunday && Object.keys(sunday).length > 0) {
        yield UpdateSettingTicket_1.default(Object.assign(Object.assign({}, sunday), { key: "sunday", number }));
    }
    if (holiday && Object.keys(holiday).length > 0) {
        yield UpdateSettingTicket_1.default(Object.assign(Object.assign({}, holiday), { key: "holiday", number }));
    }
    return res.status(200).json({
        outBusinessHours,
        ticketExpiration,
        weekend,
        saturday,
        sunday,
        holiday
    });
});
exports.updateTicketSettings = updateTicketSettings;
const update = (req, res) => __awaiter(void 0, void 0, void 0, function* () {
    if (req.user.profile !== "master") {
        throw new AppError_1.default("ERR_NO_PERMISSION", 403);
    }
    const { settingKey: key } = req.params;
    const { value, obj } = req.body;
    const setting = yield UpdateSettingService_1.default({
        key,
        value,
        obj
    });
    if (key && key == "whatsaAppCloudApi") {
        let whatsapps = yield Whatsapp_1.default.findAll();
        if (whatsapps) {
            for (let i in whatsapps) {
                const { id, wabaId, isOfficial } = whatsapps[i];
                if (isOfficial && wabaId) {
                    try {
                        const whatsapp = yield Whatsapp_1.default.findByPk(id);
                        if (whatsapp) {
                            if (value == "disabled") {
                                whatsapp.update({ status: "OPENING" });
                            }
                            else if (value == "enabled") {
                                const info = yield WhatsappOfficialNumberInfo_1.default(wabaId);
                                if (info) {
                                    whatsapp.update({
                                        classification: info.quality_rating,
                                        status: "CONNECTED"
                                    });
                                }
                            }
                        }
                    }
                    catch (error) {
                        console.log("error on try update classification number from oficial whatsapp in SettingControllers.ts: ", error);
                    }
                }
            }
        }
    }
    LoadSettings_1.default();
    const io = socket_1.getIO();
    io.emit("settings", {
        action: "update",
        setting
    });
    return res.status(200).json(setting);
});
exports.update = update;
