import { useQuery, type UseQueryResult } from '@tanstack/react-query';
import { fetchMailConversations, fetchMailThread } from '@/api/mail';
import type { Conversation, Message } from '@/types';
import { STALE_TIME } from './refreshConfig';

export function useMailConversations(deviceId?: string): UseQueryResult<Conversation[]> {
  return useQuery({
    queryKey: ['mail-conversations', deviceId],
    queryFn: () => fetchMailConversations(deviceId),
    staleTime: STALE_TIME,
  });
}

export function useMailThread(conversationId: string | null): UseQueryResult<Message[]> {
  return useQuery({
    queryKey: ['mail-thread', conversationId],
    queryFn: () => fetchMailThread(conversationId!),
    enabled: conversationId !== null,
    staleTime: STALE_TIME,
  });
}
