import StatusChatEnd from "../../models/StatusChatEnd";
import AppError from "../../errors/AppError";

import { Sequelize } from "sequelize";
import { splitDateTime } from "../../helpers/SplitDateTime"
import ptBR from "date-fns/locale/pt-BR";
import { format } from "date-fns"
const dbConfig = require("../../config/database");
const sequelize = new Sequelize(dbConfig);
const { QueryTypes } = require("sequelize");

const CountStatusChatEndService = async (
  startDate: string,
  endDate: string,
  queueIds?: number[]
) => { 
   let countStatusChatEnd: any

   if(queueIds && queueIds.length > 0){
      countStatusChatEnd = await sequelize.query(
          `select t.id, s.name, count(t.id) as count from Tickets t join StatusChatEnds s on 
          t.statusChatEndId = s.id and DATE(t.createdAt) BETWEEN '${startDate} 00:00:00.000000' AND '${endDate} 23:59:59.999999'
          AND t.queueId IN (${queueIds})
          group by s.id;`,
          { type: QueryTypes.SELECT }
        );
   }
   else{
      countStatusChatEnd = await sequelize.query(
    `select t.id, s.name, count(t.id) as count from Tickets t join StatusChatEnds s on 
      t.statusChatEndId = s.id and DATE(t.createdAt) BETWEEN '${startDate} 00:00:00.000000' AND '${endDate} 23:59:59.999999'
      group by s.id;`,
          { type: QueryTypes.SELECT }
        );
   }
  

  return countStatusChatEnd;
};

export default CountStatusChatEndService;
