import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { ImageService } from '@/services/image.service';
import type { CreateImageRequest, UpdateOCRRequest } from '@/types';

export function useImages(documentId?: string) {
  return useQuery({
    queryKey: ['images', documentId],
    queryFn: () => ImageService.getUserImages(documentId),
  });
}

export function usePendingImages() {
  return useQuery({
    queryKey: ['images', 'pending'],
    queryFn: () => ImageService.getPendingImages(),
  });
}

export function useImage(id: string) {
  return useQuery({
    queryKey: ['image', id],
    queryFn: () => ImageService.getById(id),
    enabled: !!id,
  });
}

export function useUploadImage() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: (data: CreateImageRequest) => ImageService.create(data),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['images'] });
    },
  });
}

export function useUploadImageFile() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: ({ file, documentId }: { file: File; documentId?: string }) =>
      ImageService.uploadFile(file, documentId),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['images'] });
    },
  });
}

export function useUpdateOCR() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: ({ id, data }: { id: string; data: UpdateOCRRequest }) =>
      ImageService.updateOCR(id, data),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['images'] });
    },
  });
}

export function useLinkToDocument() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: ({ id, documentId }: { id: string; documentId: string }) =>
      ImageService.linkToDocument(id, documentId),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['images'] });
    },
  });
}

export function useDeleteImage() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: (id: string) => ImageService.delete(id),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['images'] });
    },
  });
}

export function useReprocessImage() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: ({ id, provider }: { id: string; provider?: string }) =>
      ImageService.reprocess(id, provider),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['images'] });
    },
  });
}

export function useOCRProviders() {
  return useQuery({
    queryKey: ['ocr-providers'],
    queryFn: () => ImageService.getOCRProviders(),
    staleTime: 5 * 60 * 1000, // 5 分钟内不重新获取
  });
}

export function useConvertImageToTodo() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: ({ id, options }: { id: string; options?: { title?: string; description?: string; priority?: string } }) =>
      ImageService.convertToTodo(id, options),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['images'] });
      queryClient.invalidateQueries({ queryKey: ['todos'] });
    },
  });
}
