
import { Sequelize, } from "sequelize";

const dbConfig = require("../../config/database");
const sequelize = new Sequelize(dbConfig);

const { QueryTypes } = require('sequelize');

interface Request {
    startDate: string;
    endDate: string;
    status: string;
    clientChatStart: boolean;
    userId?: string | number;
}

const CountTicketsByUserQueue = async ({ startDate, endDate, status, clientChatStart, userId }: Request): Promise<any[]> => {

    let usersQueueInfo = []

    if (clientChatStart) {

        if (userId) {
            // CONSULTANDO POR CONVERSAS INICIADAS PELO CLIENTE 
            usersQueueInfo = await 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 = await 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 = await 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 = await 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;
};

export default CountTicketsByUserQueue;




