import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import * as api from '../service/EmailService';
import { SendEmailDto, MessageListQueryDto, SingleActionRequest, SummarizeEmailRequest, GenerateEmailRequest } from '../types/Types';

export const useSendEmail = () => {
    return useMutation({
        mutationFn: (variables: SendEmailDto) => api.sendEmail(variables),
    });
};

export const useSaveDraft = () => {
    return useMutation({
        mutationFn: (variables: SendEmailDto) => api.saveDraft(variables),
    });
};

export const useGetDraft = (query: MessageListQueryDto) => {
    return useQuery({
        queryKey: ['draft', query],
        queryFn: () => api.getDraft(query),
        staleTime: 0
    });
};

export const useGetInbox = (query: MessageListQueryDto) => {
    return useQuery({
        queryKey: ['inbox', query],
        queryFn: () => api.getInbox(query),
        staleTime: 0, // Always fetch fresh data
        refetchOnWindowFocus: true,
    });
};

export const useGetMessageDetail = (messageId: string, mailbox: string) => {
    return useQuery({
        queryKey: ['message', messageId, mailbox],
        queryFn: () => api.getMessageDetail(messageId, mailbox),
        enabled: !!messageId && !!mailbox,
        staleTime: 0
    });
};

export const useDeleteMessage = () => {
    const queryClient = useQueryClient();

    return useMutation({
        mutationFn: (messageId: string) => api.deleteMessage(messageId),
        onSuccess: () => {
            // Invalidate inbox queries to refresh the list
            queryClient.invalidateQueries({ queryKey: ['inbox'] });
        },
    });
};


export const useMoveToArchive = () => {
    return useMutation({
        mutationFn: ({ messageId, mailbox }: SingleActionRequest) => api.moveToArchive(messageId, mailbox),
    });
};

export const useMoveToTrash = () => {
    return useMutation({
        mutationFn: ({ messageId, mailbox }: SingleActionRequest) => api.moveToTrash(messageId, mailbox),
    });
};

export const useMoveToFavorite = () => {
    return useMutation({
        mutationFn: ({ messageId, mailbox }: SingleActionRequest) => api.moveToFavorite(messageId, mailbox),
    });
};

export const useMoveToJunk = () => {
    return useMutation({
        mutationFn: ({ messageId, mailbox }: SingleActionRequest) => api.moveToJunk(messageId, mailbox),
    });
};

export const useMoveToNotJunk = () => {
    return useMutation({
        mutationFn: ({ messageId, mailbox }: SingleActionRequest) => api.moveToNotJunk(messageId, mailbox),
    });
};

export const useMarkAsRead = () => {
    return useMutation({
        mutationFn: ({ messageId, mailbox }: SingleActionRequest) => api.markAsRead(messageId, mailbox),
    });
};

export const useMarkAsUnread = () => {
    return useMutation({
        mutationFn: ({ messageId, mailbox }: SingleActionRequest) => api.markAsUnread(messageId, mailbox),
    });
};

export const useUnArchiveMessage = () => {
    return useMutation({
        mutationFn: ({ messageId, mailbox }: SingleActionRequest) => api.unArchiveMessage(messageId, mailbox),
    });
};

export const useUnFavoriteMessage = () => {
    return useMutation({
        mutationFn: ({ messageId, mailbox }: SingleActionRequest) => api.unFavoriteMessage(messageId, mailbox),
    });
};

export const useRestoreMessage = () => {
    return useMutation({
        mutationFn: ({ messageId, mailbox }: SingleActionRequest) => api.restoreMessage(messageId, mailbox),
    });
};

export const useDownloadAttachment = () => {
    return useMutation({
        mutationFn: ({ messageId, attachmentId, mailbox }: { messageId: string; attachmentId: string; mailbox: string }) =>
            api.downloadAttachment(messageId, attachmentId, mailbox),
    });
};

export const useSummarizeEmail = () => {
    return useMutation({
        mutationFn: (params: SummarizeEmailRequest) => api.summarizeEmail(params),
    });
};

export const useGenerateEmail = () => {
    return useMutation({
        mutationFn: (params: GenerateEmailRequest) => api.generateEmail(params),
    });
};

export const useMarkAllRead = () => {
    return useMutation({
        mutationFn: () => api.markAllRead(),
    });
};