import { useMutation, useQuery } from '@tanstack/react-query';
import {
  CustomUseMutationOptions,
  CustomUseQueryOptions,
  ProcessTypes,
} from '../models';
import {
  createTemplateJob,
  createTemplateOcrJob,
  deleteTemplate,
  getTemplateOcrJob,
  getTemplates,
  updateTemplate,
} from '../requests';

export function useCreateTemplateJob(
  options?: CustomUseMutationOptions<
    Awaited<ReturnType<typeof createTemplateJob>>,
    unknown,
    Parameters<typeof createTemplateJob>[0]
  >,
) {
  return useMutation(createTemplateJob, options);
}

export function useDeleteTemplate(
  options?: CustomUseMutationOptions<
    Awaited<ReturnType<typeof deleteTemplate>>,
    unknown,
    Parameters<typeof deleteTemplate>[0]
  >,
) {
  return useMutation(deleteTemplate, options);
}

export function useTemplates(
  userId,
  options?: CustomUseQueryOptions<Awaited<ReturnType<typeof getTemplates>>>,
) {
  return useQuery({
    queryKey: ['list-templates', userId],
    queryFn: () => getTemplates(userId),
    ...options,
  });
}

export function useCreateTemplateOcrJob(
  options?: CustomUseMutationOptions<
    string,
    unknown,
    {
      file: File;
      processType: ProcessTypes;
      userId: string;
      templateId: number;
    }
  >,
) {
  return useMutation(createTemplateOcrJob, options);
}

export function useTemplateOcrJob(
  requestId,
  options?: CustomUseQueryOptions<
    Awaited<ReturnType<typeof getTemplateOcrJob>>
  >,
) {
  return useQuery({
    queryKey: ['template-ocr-job', requestId],
    queryFn: () => getTemplateOcrJob(requestId),
    ...options,
  });
}

export function useUpdateTemplate(
  options?: CustomUseMutationOptions<
    string,
    unknown,
    Parameters<typeof updateTemplate>[0]
  >,
) {
  return useMutation(updateTemplate, options);
}
