"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 });
const sequelize_1 = require("sequelize");
const dbConfig = require("../../config/database");
const sequelize = new sequelize_1.Sequelize(dbConfig);
const { QueryTypes } = require('sequelize');
const CountTicketsByUserQueue = ({ startDate, endDate, status, clientChatStart, userId }) => __awaiter(void 0, void 0, void 0, function* () {
    let usersQueueInfo = [];
    if (clientChatStart) {
        if (userId) {
            // CONSULTANDO POR CONVERSAS INICIADAS PELO CLIENTE 
            usersQueueInfo = yield sequelize.query(`select Users.id as userId, Tickets.status, Queues.id as queueId, Queues.name as queueName, \
            (select count(id) from Tickets where userId = Users.id and Tickets.status = '${status}' and Tickets.queueId = Queues.id \
            and (date(Tickets.createdAt) BETWEEN '${startDate} 00:00:00.000000' AND '${endDate} 23:59:59.999999')) as totAttendance \
            from Tickets inner join Users inner join Queues on Tickets.queueId = Queues.id and Tickets.userId = Users.id and Tickets.status = '${status}' \
            and (date(Tickets.createdAt) BETWEEN '${startDate} 00:00:00.000000' AND '${endDate} 23:59:59.999999') and Users.id = '${userId}' group by Users.email, Queues.name;`, { type: QueryTypes.SELECT });
        }
        else {
            // CONSULTANDO POR CONVERSAS INICIADAS PELO CLIENTE 
            usersQueueInfo = yield sequelize.query(`select Users.id as userId, Tickets.status, Queues.id as queueId, Queues.name as queueName, \
             (select count(id) from Tickets where userId = Users.id and Tickets.status = '${status}' and Tickets.queueId = Queues.id \
             and (date(Tickets.createdAt) BETWEEN '${startDate} 00:00:00.000000' AND '${endDate} 23:59:59.999999')) as totAttendance \
             from Tickets inner join Users inner join Queues on Tickets.queueId = Queues.id and Tickets.userId = Users.id and Tickets.status = '${status}' \
             and (date(Tickets.createdAt) BETWEEN '${startDate} 00:00:00.000000' AND '${endDate} 23:59:59.999999') group by Users.email, Queues.name;`, { type: QueryTypes.SELECT });
        }
    }
    else {
        if (userId) {
            // CONSULTANDO POR CONVERSAS INICIADAS PELO ATENDENTE 
            usersQueueInfo = yield sequelize.query(`select Users.id as userId, Tickets.status, \
            (select count(id) from Tickets where userId = Users.id and Tickets.status = '${status}' and Tickets.queueId is null \
            and (date(Tickets.createdAt) BETWEEN '${startDate} 00:00:00.000000' AND '${endDate} 23:59:59.999999')) as totAttendance \
            from Tickets inner join Users on \
            Tickets.queueId is null and Tickets.userId = Users.id and Tickets.status = '${status}' \
            and (date(Tickets.createdAt) BETWEEN '${startDate} 00:00:00.000000' AND '${endDate} 23:59:59.999999') and Users.id = '${userId}' group by Users.email;`, { type: QueryTypes.SELECT });
        }
        else {
            // CONSULTANDO POR CONVERSAS INICIADAS PELO ATENDENTE 
            usersQueueInfo = yield sequelize.query(`select Users.id as userId, Tickets.status, \
            (select count(id) from Tickets where userId = Users.id and Tickets.status = '${status}' and Tickets.queueId is null \
            and (date(Tickets.createdAt) BETWEEN '${startDate} 00:00:00.000000' AND '${endDate} 23:59:59.999999')) as totAttendance \
            from Tickets inner join Users on \
            Tickets.queueId is null and Tickets.userId = Users.id and Tickets.status = '${status}' \
            and (date(Tickets.createdAt) BETWEEN '${startDate} 00:00:00.000000' AND '${endDate} 23:59:59.999999') group by Users.email;`, { type: QueryTypes.SELECT });
        }
    }
    return usersQueueInfo;
});
exports.default = CountTicketsByUserQueue;
