const { User, Property } = require("../../models");
const { getReqInformation, getSettings } = require("../../utils/Common");
const sequelize = require("../../utils/Connection");

const index = async (req, res) => {
  let user = req.session?.user ?? {};
  let { baseUrl, endPoint } = getReqInformation(req);
  let settings = await getSettings();
  let agents = await User.findAll({
    where: {
      userType: "agent",
    },
    attributes: ["firstName", "lastName", "image", "id"],
    include: [
      {
        model: Property,
        attributes: [
          [sequelize.fn("count", sequelize.col("userId")), "totalCount"],
        ],
      },
    ],
    group: ["id"],
    raw: true,
    nest: true,
    limit: 4,
  });
  return res.render("agent/index.ejs", {
    baseUrl,
    endPoint,
    user,
    agents,
    settings,
  });
};

module.exports = {
  index,
};
