"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 AppError_1 = __importDefault(require("../../errors/AppError"));
const Contact_1 = __importDefault(require("../../models/Contact"));
const User_1 = __importDefault(require("../../models/User"));
const Queue_1 = __importDefault(require("../../models/Queue"));
const Message_1 = __importDefault(require("../../models/Message"));
const sequelize_1 = require("sequelize");
const sequelize_2 = require("sequelize");
const Whatsapp_1 = __importDefault(require("../../models/Whatsapp"));
//Report by user, startDate, endDate
const ShowMessageReport = (id, startDate, endDate) => __awaiter(void 0, void 0, void 0, function* () {
    let where_clause_user = {};
    let where_clause = {
        createdAt: {
            [sequelize_1.Op.gte]: startDate + ' 00:00:00.000000',
            [sequelize_1.Op.lte]: endDate + ' 23:59:59.999999'
        },
    };
    if (id == '0') {
        // where_clause_user = {   
        //   createdAt: {
        //     [Op.gte]: startDate+' 00:00:00.000000',
        //     [Op.lte]: endDate  +' 23:59:59.999999'
        //   }, 
        // }
    }
    else {
        where_clause_user = {
            userid: id,
        };
    }
    const messages = yield Message_1.default.findAll({
        where: where_clause,
        raw: true,
        attributes: ['id', 'body', 'read', 'mediaType', 'fromMe', 'mediaUrl', [sequelize_2.Sequelize.fn("DATE_FORMAT", sequelize_2.Sequelize.col("Message.createdAt"), "%d/%m/%Y %H:%i:%s"), "createdAt"]],
        include: [
            {
                model: Ticket_1.default,
                where: where_clause_user,
                required: true,
                attributes: ['id', 'status', 'statusChatEnd'],
                include: [
                    {
                        model: Contact_1.default,
                        attributes: ['name', 'number']
                    },
                    {
                        model: User_1.default,
                        attributes: ['name', 'email']
                    },
                    {
                        model: Queue_1.default,
                        attributes: ['name']
                    },
                    {
                        model: Whatsapp_1.default,
                        attributes: ['name']
                    },
                ]
            },
        ],
        order: [
            ['createdAt', 'ASC']
        ]
    });
    if (!messages) {
        throw new AppError_1.default("ERR_NO_MESSAGES_FOUND", 404);
    }
    return messages;
});
exports.default = ShowMessageReport;
