import axios from "../../../config/axios";
import { getMailboxId } from "../../../config/func";
import {
  MessageListQueryDto,
  InboxResponse,
  InboxMessage,
  DraftUpdateDto,
} from "../types/DraftTypes";

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

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

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

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

export const editDraft = async (
  messageId: string,
  draftData: DraftUpdateDto
): Promise<void> => {
  const { data } = await axios.put(
    `/email/messages/drafts/${messageId}`,
    draftData
  );
  return data;
};

export const updateDraft = async (
  messageId: string,
  draftData: DraftUpdateDto
): Promise<void> => {
  const { data } = await axios.patch(
    `/email/messages/drafts/${messageId}`,
    draftData
  );
  return data;
};

export const sendDraft = async (messageId: string): Promise<void> => {
  const { data } = await axios.post(`/email/messages/drafts/${messageId}/send`);
  return data;
};

export const getDraftDetail = async (
  messageId: string
): Promise<InboxMessage> => {
  const { data } = await axios.get(`/email/messages/${messageId}`);
  return data.data.message;
};
