"use client"

import { useMutation, useQueryClient } from "@tanstack/react-query"
import { apiClient } from "../client"

export function useReindexMessageAttachments(messageId: string | undefined) {
  const queryClient = useQueryClient()

  return useMutation({
    mutationFn: () =>
      apiClient.post<{ status: string }>(
        `/mail/messages/${messageId}/attachments/reindex`
      ),
    onSuccess: () => {
      if (!messageId) return
      queryClient.invalidateQueries({ queryKey: ["message-cid-map", messageId] })
      queryClient.invalidateQueries({ queryKey: ["message-attachments", messageId] })
      queryClient.invalidateQueries({ queryKey: ["message", messageId] })
    },
  })
}
