"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 });
const Ticket_1 = __importDefault(require("../../models/Ticket"));
const Contact_1 = __importDefault(require("../../models/Contact"));
const SchedulingNotify_1 = __importDefault(require("../../models/SchedulingNotify"));
const sequelize_1 = require("sequelize");
const AppError_1 = __importDefault(require("../../errors/AppError"));
const StatusChatEnd_1 = __importDefault(require("../../models/StatusChatEnd"));
const ListSchedulingNotifyContactService = (contactNumber = "", startDate = "", endDate = "") => __awaiter(void 0, void 0, void 0, function* () {
    let where_clause = {};
    let where_clause_notify = {};
    let nameNumber = {
        [sequelize_1.Op.or]: [
            {
                name: sequelize_1.Sequelize.where(sequelize_1.Sequelize.fn("LOWER", sequelize_1.Sequelize.col("name")), "LIKE", `%${contactNumber.toLowerCase().trim()}%`)
            },
            { number: { [sequelize_1.Op.like]: `%${contactNumber.toLowerCase().trim()}%` } }
        ]
    };
    if (contactNumber.trim().length > 0 &&
        startDate.trim().length > 0 &&
        endDate.trim().length > 0) {
        where_clause = nameNumber;
        where_clause_notify = {
            schedulingTime: {
                [sequelize_1.Op.gte]: startDate + " 00:00:00.000000",
                [sequelize_1.Op.lte]: endDate + " 23:59:59.999999"
            }
        };
    }
    else if (contactNumber.trim().length == 0 &&
        startDate.trim().length > 0 &&
        endDate.trim().length > 0) {
        where_clause = {};
        where_clause_notify = {
            schedulingDate: {
                [sequelize_1.Op.gte]: startDate + " 00:00:00.000000",
                [sequelize_1.Op.lte]: endDate + " 23:59:59.999999"
            }
        };
    }
    else if (contactNumber.trim().length == 0 &&
        startDate.trim().length > 0 &&
        endDate.trim().length == 0) {
        where_clause = {};
        where_clause_notify = {
            schedulingDate: {
                [sequelize_1.Op.gte]: startDate + " 00:00:00.000000",
                [sequelize_1.Op.lte]: startDate + " 23:59:59.999999"
            }
        };
    }
    else if (contactNumber.trim().length == 0 &&
        startDate.trim().length == 0 &&
        endDate.trim().length > 0) {
        where_clause = {};
        where_clause_notify = {
            schedulingDate: {
                [sequelize_1.Op.gte]: endDate + " 00:00:00.000000",
                [sequelize_1.Op.lte]: endDate + " 23:59:59.999999"
            }
        };
    }
    else if (contactNumber.trim().length > 0 &&
        startDate.trim().length > 0 &&
        endDate.trim().length == 0) {
        where_clause = nameNumber;
        where_clause_notify = {
            schedulingDate: {
                [sequelize_1.Op.gte]: startDate + " 00:00:00.000000",
                [sequelize_1.Op.lte]: startDate + " 23:59:59.999999"
            }
        };
    }
    else if (contactNumber.trim().length > 0 &&
        startDate.trim().length == 0 &&
        endDate.trim().length > 0) {
        where_clause = nameNumber;
        where_clause_notify = {
            schedulingDate: {
                [sequelize_1.Op.gte]: endDate + " 00:00:00.000000",
                [sequelize_1.Op.lte]: endDate + " 23:59:59.999999"
            }
        };
    }
    else if (contactNumber.trim().length > 0) {
        where_clause = nameNumber;
    }
    const ticket = yield SchedulingNotify_1.default.findAll({
        raw: true,
        where: where_clause_notify,
        attributes: [
            "id",
            "ticketId",
            "statusChatEndId",
            [
                sequelize_1.Sequelize.fn("DATE_FORMAT", sequelize_1.Sequelize.col("schedulingDate"), "%d/%m/%Y %H:%i:%s"),
                "schedulingDate"
            ],
            [
                sequelize_1.Sequelize.fn("DATE_FORMAT", sequelize_1.Sequelize.col("schedulingTime"), "%d/%m/%Y %H:%i:%s"),
                "schedulingTime"
            ],
            "message"
        ],
        include: [
            {
                model: Ticket_1.default,
                required: true,
                attributes: [],
                include: [
                    {
                        model: Contact_1.default,
                        where: where_clause,
                        attributes: ["name", "number", "profilePicUrl"]
                    }
                ]
            },
            {
                model: StatusChatEnd_1.default,
                required: true,
            }
        ],
        order: [["id", "DESC"]]
    });
    if (!ticket) {
        throw new AppError_1.default("ERR_NO_TICKET_FOUND", 404);
    }
    return ticket;
});
exports.default = ListSchedulingNotifyContactService;
