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

export async function createTemplateJob({
  file,
  userId,
  listNameBox,
  listBBox,
  listAnchor,
  tempName,
}: {
  file: File;
  userId: string;
  listNameBox: string;
  listBBox: string;
  listAnchor: string;
  tempName: string;
}) {
  const formData = new FormData();
  formData.append('file', file);
  formData.append('user_id', String(userId));
  formData.append('template_name', tempName);
  formData.append('data_box_names', listNameBox);
  formData.append('data_boxs', listBBox);
  formData.append('anchor_boxs', listAnchor);

  const response = await API.post(`/templates/`, formData);

  return response.data;
}

export async function getTemplates(userId: string) {
  const urlSearcParams = new URLSearchParams({
    user_id: userId,
  });

  const response = await API.get<Template[]>(
    `/templates?${urlSearcParams.toString()}`,
  );

  return response.data;
}

export async function getTemplate({
  userId,
  templateId,
}: {
  userId: string;
  templateId: number;
}) {
  const urlSearcParams = new URLSearchParams({
    user_id: userId,
    template_id: String(templateId),
  });

  const response = await API.get<Template>(
    `/templates?${urlSearcParams.toString()}`,
  );

  return response.data;
}

export async function deleteTemplate({
  templateId,
}: {
  templateId: Template['id'];
}) {
  const response = await API.delete(`/templates/${templateId}`);

  return response.data;
}

export async function createTemplateOcrJob({
  file,
  processType,
  userId,
  templateId,
}: {
  file: File;
  processType: ProcessTypes;
  userId: string;
  templateId: Template['id'];
}) {
  const formData = new FormData();
  formData.append('processType', String(processType));
  formData.append('file', file);
  formData.append('userId', String(userId));
  formData.append('templateId', String(templateId));

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

  return response.data.request_id;
}

export async function getTemplateOcrJob<TResult = any>(requestId: string) {
  const response = await API.get<JobResponse<TResult>>(`/ctel/results/${requestId}`);

  return response.data;
}

export async function updateTemplate({
  fileUrl,
  templateId,
  templateName,
  userId,
  listNameBox,
  listBBox,
  listAnchor,
}: {
  fileUrl: string;
  templateId: number;
  templateName: string;
  userId: string;
  listNameBox: string;
  listBBox: string;
  listAnchor: string;
}) {
  let blob = await fetch(fileUrl).then((r) => r.blob());
  let file = new File([blob], 'image.jpg', { type: 'image/png' });

  const formData = new FormData();
  formData.append('file', file);
  formData.append('user_id', String(userId));
  formData.append('template_id', String(templateId));
  formData.append('template_name', templateName);
  formData.append('data_box_names', listNameBox);
  formData.append('data_boxs', listBBox);
  formData.append('anchor_boxs', listAnchor);

  const response = await API.post(`/templates/`, formData);

  return response.data;
}
