"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 sequelize_1 = require("sequelize");
const date_fns_1 = require("date-fns");
const pt_BR_1 = __importDefault(require("date-fns/locale/pt-BR"));
const SplitDateTime_1 = require("../../helpers/SplitDateTime");
const Contact_1 = __importDefault(require("../../models/Contact"));
const Queue_1 = __importDefault(require("../../models/Queue"));
const dateToday = SplitDateTime_1.splitDateTime(new Date(date_fns_1.format(new Date(), 'yyyy-MM-dd HH:mm:ss', { locale: pt_BR_1.default })));
const ListTicketsServiceCache = ({ status, date, userId, queueId }) => __awaiter(void 0, void 0, void 0, function* () {
    let where_clause = {};
    console.log(' QUEUE ID: ', queueId);
    if (date) {
        where_clause = {
            createdAt: {
                [sequelize_1.Op.gte]: date + ' 00:00:00.000000',
                [sequelize_1.Op.lte]: date + ' 23:59:59.999999'
            }
        };
    }
    else if (queueId) {
        where_clause = {
            queueId: queueId
        };
    }
    else if (userId) {
        where_clause = {
            userId: userId
        };
    }
    else {
        // where_clause = {
        //     createdAt: {
        //         [Op.gte]: dateToday.fullDate + ' 00:00:00.000000',
        //         [Op.lte]: dateToday.fullDate + ' 23:59:59.999999'
        //       }
        // }
    }
    //where_clause = { ...where_clause, status: status }
    const ticket = yield Ticket_1.default.findAll({
        where: where_clause,
        raw: true,
        // nest:true,
        include: [
            {
                model: Contact_1.default,
                as: "contact",
                attributes: ["id", "name", "number", "profilePicUrl"]
            },
            {
                model: Queue_1.default,
                as: "queue",
                attributes: ["id", "name", "color"]
            }
        ],
        order: [["updatedAt", "DESC"]]
    });
    if (!ticket) {
        throw new AppError_1.default("ERR_NO_TICKET_FOUND", 404);
    }
    return ticket;
});
exports.default = ListTicketsServiceCache;
