import { Sequelize } from "sequelize";
import StatusChatEnd from "../../models/StatusChatEnd";

interface Request {
    searchParam?: string;
    pageNumber?: string;
  }
  
  interface Response {
    statusChatEnd: StatusChatEnd[];
    count: number;
    hasMore: boolean;
  }
  
  const ListSchedulingService = async ({
    searchParam = "",
    pageNumber = "1"
  }: Request): Promise<Response> => {
    const whereCondition = {
      message: Sequelize.where(
        Sequelize.fn("LOWER", Sequelize.col("name")), "LIKE", `%${searchParam.toLowerCase().trim()}%`)
    };
    const limit = 20;
    const offset = limit * (+pageNumber - 1);
  
    const { count, rows: statusChatEnd } = await StatusChatEnd.findAndCountAll({
      where: whereCondition,
      attributes: ["id", "name", "farewellMessage", "isDefault"],
      limit,
      offset,
      order: [["name", "ASC"]]
    });
  
    const hasMore = count > offset + statusChatEnd.length;
  
    return {
        statusChatEnd,
        count,
        hasMore
    };
  };
  
  export default ListSchedulingService;
  