import ptBR from 'date-fns/locale/pt-BR';
import { splitDateTime } from "../helpers/SplitDateTime";
import format from 'date-fns/format';
import ShowUserServiceReport from '../services/UserServices/ShowUserServiceReport';
import { getIO } from "../libs/socket";
import CountTicketsByUserQueue from "../services/UserServices/CountTicketsByUserQueue";
import ShowQueuesByUser from '../services/UserServices/ShowQueuesByUser';

// import CountTicketsByUserQueue from "../services/UserServices/CountTicketsByUserQueue";


const TicketEmiterSumOpenClosedByUser = async (userId: string, startDate: string, endDate: string) => {

    const openByUser: any[] = await ShowUserServiceReport({ startDate: startDate, endDate: endDate, ticketStatus: 'open', userId: userId.toString() });
    const closedByUser: any[] = await ShowUserServiceReport({ startDate: endDate, endDate: endDate, ticketStatus: 'closed', userId: userId.toString() });


    const openByUserOnQueue: any[] = await CountTicketsByUserQueue({ startDate: startDate, endDate: endDate, status: 'open', clientChatStart: true, userId: userId })
    const openByUserOutQueue: any[] = await CountTicketsByUserQueue({ startDate: startDate, endDate: endDate, status: 'open', clientChatStart: false, userId: userId })

    const closedByUserOnQueue: any[] = await CountTicketsByUserQueue({ startDate: startDate, endDate: endDate, status: 'closed', clientChatStart: true, userId: userId })
    const closedUserOutQueue: any[] = await CountTicketsByUserQueue({ startDate: startDate, endDate: endDate, status: 'closed', clientChatStart: false, userId: userId })



    const queuesByUser = await ShowQueuesByUser({ profile: 'user', userId: userId })

    let openCloseOnQueue = openByUserOnQueue.concat(closedByUserOnQueue)
    let openCloseOutQueue = openByUserOutQueue.concat(closedUserOutQueue)
 

      // OPEN, CLOSED TICKETS STARTED BY CLIENTS
    for (let i = 0; i < queuesByUser.length; i++) {

        queuesByUser[i].countOpen = 0
        queuesByUser[i].countClosed = 0

        for (let x = 0; x < openCloseOnQueue.length; x++) {
            if ((queuesByUser[i].userId == openCloseOnQueue[x].userId) &&
                (queuesByUser[i].queueId == openCloseOnQueue[x].queueId && openCloseOnQueue[x].status == 'open')) {
                queuesByUser[i].countOpen = openCloseOnQueue[x].totAttendance
            }
            else if ((queuesByUser[i].userId == openCloseOnQueue[x].userId) &&
                (queuesByUser[i].queueId == openCloseOnQueue[x].queueId && openCloseOnQueue[x].status == 'closed')) {
                queuesByUser[i].countClosed = openCloseOnQueue[x].totAttendance
            }

        }
    } 
 

    // OPEN, CLOSED TICKETS STARTED BY USERS
    let openClosedOutQueue = {}
    let open = openCloseOutQueue.filter((e) =>  e.status == 'open')
    let closed = openCloseOutQueue.filter((e) => e.status == 'closed')

    openClosedOutQueue = {
        ...openClosedOutQueue,
        userId: userId,
        countOpen: open && open.length > 0 ? open[0].totAttendance : 0,
        countClosed: closed && closed.length > 0 ? closed[0].totAttendance : 0
    }  


    const io = getIO();
    io.emit("onlineStatus", {
        action: "update",
        userOnlineTime: {
            sumOpen: openByUser.length > 0 ? openByUser[0] : { userId: userId, count: '' },
            sumClosed: closedByUser.length > 0 ? closedByUser[0] : { userId: userId, count: '' },

            openClosedOutQueue: openClosedOutQueue,
            openClosedInQueue: queuesByUser


        }
    }); 
 

};

export default TicketEmiterSumOpenClosedByUser