import { Sequelize } from "sequelize";

const dbConfig = require("../../config/database");
const sequelize = new Sequelize(dbConfig);

const { QueryTypes } = require("sequelize");

interface Request {
  profile?: string;
  userId?: string | number;
}

const QueuesByUser = async ({ profile, userId }: Request): Promise<any[]> => {
  let queueByUsersInfo: any = [];

  if (userId && profile) {
    // CONSULTANDO FILAS PELO ID DO USUARIO
    queueByUsersInfo = await sequelize.query(
      `select UserQueues.userId, UserQueues.queueId, Users.name, 
            Queues.name, Queues.color from UserQueues inner join Users inner join Queues on
            UserQueues.queueId = Queues.id and  UserQueues.userId = Users.id and Users.id = '${userId}' and Users.profile = '${profile}' order by userId, queueId;`,
      { type: QueryTypes.SELECT }
    );
  } else if (profile) {
    // CONSULTANDO FILAS PELO USUARIO
    queueByUsersInfo = await sequelize.query(
      `select UserQueues.userId, UserQueues.queueId, Users.name, 
            Queues.name, Queues.color from UserQueues inner join Users inner join Queues on
            UserQueues.queueId = Queues.id and  UserQueues.userId = Users.id and Users.profile = '${profile}' order by userId, queueId;`,
      { type: QueryTypes.SELECT }
    );
  } else if (userId) {
    queueByUsersInfo = await sequelize.query(
      `select UserQueues.userId, UserQueues.queueId, Users.name, 
            Queues.name, Queues.color from UserQueues inner join Users inner join Queues on
            UserQueues.queueId = Queues.id and  UserQueues.userId = Users.id and Users.id = '${userId}' order by userId, queueId;`,
      { type: QueryTypes.SELECT }
    );
  }

  // if (userId) {
  //   // CONSULTANDO FILAS PELO ID DO USUARIO
  //   queueByUsersInfo = await sequelize.query(
  //     `select UserQueues.userId, UserQueues.queueId, Users.name,
  //         Queues.name, Queues.color from UserQueues inner join Users inner join Queues on
  //         UserQueues.queueId = Queues.id and  UserQueues.userId = Users.id and Users.id = '${userId}' and Users.profile = '${profile}' order by userId, queueId;`,
  //     { type: QueryTypes.SELECT }
  //   );
  // } else {
  //   // CONSULTANDO FILAS PELO USUARIO
  //   queueByUsersInfo = await sequelize.query(
  //     `select UserQueues.userId, UserQueues.queueId, Users.name,
  //         Queues.name, Queues.color from UserQueues inner join Users inner join Queues on
  //         UserQueues.queueId = Queues.id and  UserQueues.userId = Users.id  and Users.profile = '${profile}' order by userId, queueId;`,
  //     { type: QueryTypes.SELECT }
  //   );
  // }

  return queueByUsersInfo;
};

export default QueuesByUser;
