import axios from "../../../config/axios";
import { getMailboxId } from "../../../config/func";
import {
  SendEmailDto,
  MessageListQueryDto,
  InboxResponse,
  MessageDetailResponse,
  MessageDetail,
  SummarizeEmailRequest,
  GenerateEmailRequest,
} from "../types/Types";
import { MailboxEnum } from "../enum/Enum";

export const sendEmail = async (params: SendEmailDto) => {
  const { data } = await axios.post(`/email/send`, params);
  return data;
};

export const generateEmail = async (params: GenerateEmailRequest) => {
  const { data } = await axios.post(`/ai-assistant/write-email`, params);
  return data;
};

export const saveDraft = async (params: SendEmailDto) => {
  const { data } = await axios.post(`/email/messages/drafts`, params);
  return data;
};

export const getDraft = async (
  query: MessageListQueryDto
): Promise<InboxResponse> => {
  const { data } = await axios.get(`/email/messages/drafts`, { params: query });
  return data;
};

export const getInbox = async (
  query: MessageListQueryDto
): Promise<InboxResponse> => {
  const isSearch =
    !!query.search ||
    !!query.datestart ||
    query.dateend ||
    query.unseen ||
    query.from;
  const url = isSearch ? "search" : "messages/inbox";

  // اضافه کردن mailbox id از localStorage
  const mailboxId = getMailboxId("INBOX");
  const queryWithMailbox = {
    ...query,
    mailbox: mailboxId,
  };

  const { data } = await axios.get(`/email/${url}`, {
    params: queryWithMailbox,
  });
  return data;
};

export const getMessageDetail = async (
  messageId: string,
  mailbox: string
): Promise<MessageDetail> => {
  const { data }: { data: MessageDetailResponse } = await axios.get(
    `/email/messages/${messageId}?mailbox=${mailbox}`
  );
  return data.data.message;
};

export const deleteMessage = async (messageId: string) => {
  const { data } = await axios.delete(`/email/messages/${messageId}`);
  return data;
};

export const moveToArchive = async (messageId: string, mailbox: string) => {
  const { data } = await axios.patch(
    `/email/messages/${messageId}/archive?mailbox=${mailbox}`
  );
  return data;
};

export const moveToTrash = async (messageId: string, mailbox: string) => {
  const { data } = await axios.patch(
    `/email/messages/${messageId}/trash?mailbox=${mailbox}`
  );
  return data;
};

export const moveToFavorite = async (messageId: string, mailbox: string) => {
  const { data } = await axios.patch(
    `/email/messages/${messageId}/favorite?mailbox=${mailbox}`
  );
  return data;
};

export const moveToJunk = async (messageId: string, mailbox: string) => {
  const { data } = await axios.patch(
    `/email/messages/${messageId}/junk?mailbox=${mailbox}`
  );
  return data;
};

export const moveToNotJunk = async (messageId: string, mailbox: string) => {
  const { data } = await axios.patch(
    `/email/messages/${messageId}/not-junk?mailbox=${mailbox}`
  );
  return data;
};

export const restoreMessage = async (messageId: string, mailbox: string) => {
  const { data } = await axios.patch(
    `/email/messages/${messageId}/restore?mailbox=${mailbox}`
  );
  return data;
};

export const unFavoriteMessage = async (messageId: string, mailbox: string) => {
  const { data } = await axios.patch(
    `/email/messages/${messageId}/unfavorite?mailbox=${mailbox}`
  );
  return data;
};

export const unArchiveMessage = async (messageId: string, mailbox: string) => {
  const { data } = await axios.patch(
    `/email/messages/${messageId}/unarchive?mailbox=${mailbox}`
  );
  return data;
};

export const markAsRead = async (messageId: string, mailbox: string) => {
  const { data } = await axios.patch(
    `/email/messages/${messageId}/seen?mailbox=${mailbox}`
  );
  return data;
};

export const markAsUnread = async (messageId: string, mailbox: string) => {
  const { data } = await axios.patch(
    `/email/messages/${messageId}/unseen?mailbox=${mailbox}`
  );
  return data;
};

export const downloadAttachment = async (
  messageId: string,
  attachmentId: string,
  mailbox: string
): Promise<Blob> => {
  const response = await axios.get(
    `/email/messages/${messageId}/attachment?attachmentId=${attachmentId}&mailbox=${mailbox}`,
    {
      responseType: "blob",
    }
  );
  return response.data;
};

export const summarizeEmail = async (params: SummarizeEmailRequest) => {
  const { data } = await axios.post(`/ai-assistant/summarize-email`, params);
  return data;
};

export const markAllRead = async () => {
  const mailboxId = getMailboxId(MailboxEnum.INBOX);
  const { data } = await axios.patch(
    `/email/messages/mark-all-read?mailbox=${mailboxId}`
  );
  return data;
};
