import { eq, countDistinct, not } from 'drizzle-orm';
import { d as database, c as chat, P as PAGINATION, r as reply } from './constants_C_mAa7qs.mjs';

const getUnreadMessages = async (page = 1) => {
  try {
    return await database.transaction(async (tx) => {
      const chats = await tx.query.chat.findMany({
        where: not(chat.isRead),
        offset: (page - 1) * PAGINATION,
        limit: page * PAGINATION,
        orderBy: chat.time
      }).prepare().execute();
      const amount = await tx.select({ total: countDistinct(chat.id) }).from(chat).where(not(chat.isRead)).prepare().execute();
      const total = amount.map((e) => e.total).reduce((a, b) => a + b);
      return {
        total,
        chats,
        page,
        total_pages: Math.ceil(total / PAGINATION)
      };
    });
  } catch (error) {
    console.error(error);
    throw new Error(JSON.stringify(error, null, 2));
  }
};
const getAllMessages = async (page = 1) => {
  try {
    return await database.transaction(async (tx) => {
      const chats = await tx.query.chat.findMany({
        offset: (page - 1) * PAGINATION,
        limit: page * PAGINATION,
        orderBy: chat.time
      }).prepare().execute();
      const amount = await tx.select({ total: countDistinct(chat.id) }).from(chat).prepare().execute();
      const total = amount.map((e) => e.total).reduce((a, b) => a + b);
      return {
        total,
        chats,
        page,
        total_pages: Math.ceil(total / PAGINATION)
      };
    });
  } catch (error) {
    console.error(error);
    throw new Error(JSON.stringify(error, null, 2));
  }
};
const getOneMessage = async (id, page = 1) => {
  try {
    return await database.transaction(async (tx) => {
      const message = await tx.query.chat.findFirst({
        where: eq(chat.id, id),
        orderBy: chat.time,
        with: {
          replies: {
            orderBy: reply.time,
            with: {
              staff: {
                columns: {
                  id: true,
                  username: true,
                  firstName: true,
                  lastName: true,
                  email: true,
                  position: true
                }
              }
            },
            limit: PAGINATION
          }
        }
      }).prepare().execute();
      const amount = message ? await tx.select({ total: countDistinct(reply.id) }).from(reply).where(eq(reply.chatId, message.id)).prepare().execute() : [];
      const total_replies = amount.map((e) => e.total).reduce((a, b) => a + b);
      await tx.update(chat).set({
        isRead: true
      }).where(eq(chat.id, id)).prepare().execute();
      return {
        chat: message,
        total_replies,
        page
      };
    });
  } catch (error) {
    console.error(error);
    throw new Error(JSON.stringify(error, null, 2));
  }
};
const replyToMessage = async ({
  chatId,
  repliedBy,
  message
}) => {
  try {
    const replyChat = await database.insert(reply).values({
      repliedBy,
      chatId,
      reply: message
    }).prepare().execute();
    await database.update(chat).set({ isReplied: true }).where(eq(chat.id, chatId)).prepare().execute();
    return replyChat;
  } catch (error) {
    console.error(error);
    throw new Error(JSON.stringify(error, null, 2));
  }
};
const newMessage = async ({
  name,
  email,
  subject,
  message
}) => {
  try {
    return await database.insert(chat).values({
      name,
      email,
      subject,
      message
    }).prepare().execute();
  } catch (error) {
    console.error(error);
    throw new Error(JSON.stringify(error, null, 2));
  }
};

export { getAllMessages as a, getUnreadMessages as b, getOneMessage as g, newMessage as n, replyToMessage as r };
