"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 sequelize_2 = require("sequelize");
const UserOnlineTime_1 = __importDefault(require("../../models/UserOnlineTime"));
//Report by user, startDate, endDate
const ShowUserServiceReport = ({ startDate, endDate, userId = "", ticketStatus = "" }) => __awaiter(void 0, void 0, void 0, function* () {
    // Para contornar a gambiarra do ShowTicketReport
    userId == '0' ? userId = '' : userId = userId;
    let where_clause = {};
    let objQuery = [];
    if (ticketStatus.length > 0) {
        if (userId.length > 0) {
            where_clause = {
                userId: userId,
                status: ticketStatus,
                createdAt: {
                    [sequelize_1.Op.gte]: startDate + ' 00:00:00.000000',
                    [sequelize_1.Op.lte]: endDate + ' 23:59:59.999999'
                },
            };
        }
        else {
            where_clause = {
                status: ticketStatus,
                createdAt: {
                    [sequelize_1.Op.gte]: startDate + ' 00:00:00.000000',
                    [sequelize_1.Op.lte]: endDate + ' 23:59:59.999999'
                },
            };
        }
        objQuery = yield Ticket_1.default.findAll({
            where: where_clause,
            raw: true,
            attributes: ['userId', [sequelize_2.Sequelize.fn("COUNT", sequelize_2.Sequelize.col("Ticket.status")), "count"]],
            group: ['userId']
        });
    }
    else {
        if (userId.length > 0) {
            where_clause = {
                userId: userId,
                createdAt: {
                    [sequelize_1.Op.gte]: startDate + ' 00:00:00.000000',
                    [sequelize_1.Op.lte]: endDate + ' 23:59:59.999999'
                },
            };
        }
        else {
            where_clause = {
                createdAt: {
                    [sequelize_1.Op.gte]: startDate + ' 00:00:00.000000',
                    [sequelize_1.Op.lte]: endDate + ' 23:59:59.999999'
                },
            };
        }
        objQuery = yield UserOnlineTime_1.default.findAll({
            where: where_clause,
            raw: true,
            // attributes: ['userId', [Sequelize.literal(`( SELECT SUM(time_to_sec(TIME(onlineTime)) ))`), 'sum']],
            attributes: ['userId', [sequelize_2.Sequelize.literal(`( SELECT SEC_TO_TIME(SUM(time_to_sec(TIME(onlineTime)) )))`), 'sum']],
            group: ['userId']
        });
    }
    if (!objQuery) {
        throw new AppError_1.default("ERR_NO_OBJ_QUERY_FOUND", 404);
    }
    return objQuery;
});
exports.default = ShowUserServiceReport;
