"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 sequelize_1 = require("sequelize");
const dbConfig = require("../../config/database");
const sequelize = new sequelize_1.Sequelize(dbConfig);
const { QueryTypes } = require("sequelize");
const format_1 = __importDefault(require("date-fns/format"));
const pt_BR_1 = __importDefault(require("date-fns/locale/pt-BR"));
const ListTicketTimeLife = ({ timeseconds, status, number, userId }) => __awaiter(void 0, void 0, void 0, function* () {
    let tickets = [];
    let currentDate = format_1.default(new Date(), "yyyy-MM-dd HH:mm:ss", { locale: pt_BR_1.default });
    // console.log('------------------> currentDate: ', currentDate)
    if (userId) {
        // CONSULTANDO FILAS PELO ID DO USUARIO
        tickets = yield sequelize.query(`select user.id as user_id, user.name as user_name, t.id as ticket_id from Tickets as t inner join Users as user on
        t.userId = user.id and user.name = 'botqueue' and t.status='${status}' and (TIMESTAMPDIFF(SECOND, t.updatedAt, '${currentDate}')) >= ${timeseconds};`, { type: QueryTypes.SELECT });
    }
    else if (number) {
        // CONSULTANDO TICKETS PELO WHATSAPP
        tickets = yield sequelize.query(`SELECT t.id AS ticket_id
       FROM Tickets t
       JOIN Whatsapps w ON t.whatsappId = w.id 
       AND w.number = ${number}
       WHERE t.status = 'open' 
       AND TIMESTAMPDIFF(SECOND, t.updatedAt, '${currentDate}') >= ${timeseconds};`, { type: QueryTypes.SELECT });
    }
    else {
        // CONSULTANDO FILAS PELO USUARIO
        tickets = yield sequelize.query(`select id as ticket_id from Tickets where status='${status}' and 
        (TIMESTAMPDIFF(SECOND, updatedAt, '${currentDate}')) >= ${timeseconds};`, { type: QueryTypes.SELECT });
    }
    return tickets;
});
exports.default = ListTicketTimeLife;
