import {
  BaseResult,
  InvoiceResult,
  JobResponse,
  ProcessTypes,
} from '../models';
import { API } from './api';

export async function getKieJob<TResult = BaseResult>(requestId: string) {
  const response = await API.get<JobResponse<TResult>>(`/ctel/result/${requestId}`);
  // const response = await API.get<JobResponse<TResult>>(`/ctel/result/SAP3ad4bb28771044eaa6e1a2d06dcc8f5e`);
  return response.data;
}

export async function createKieJob({
  file,
  processType,
}: {
  file: File;
  processType: ProcessTypes;
}) {
  const formData = new FormData();
  formData.append('processType', String(processType));
  formData.append('file', file);

  const response = await API.post<{
    estimated_wating_time: number;
    request_id: string;
  }>('/ctel/image/process/', formData);
  return response.data.request_id;
}

export async function getKieInvoiceJob(requestId: string) {
  const result = await getKieJob<InvoiceResult>(requestId);

  return result;
}

export function createKieInvoiceJob(file: File) {
  return createKieJob({
    file,
    processType: ProcessTypes.Invoice,
  });
}

