import {
  BE_SUBSCRIPTION_REGISTRATIONFILE_GROUPS_URL,
  BE_SUBSCRIPTION_REGISTRATION_SCHOOL_FILE_TEMPLATES_URL,
  BE_SUBSCRIPTION_REGISTRATION_SCHOOL_FILE_MASTERS_URL,
  BE_SUBSCRIPTION_REGISTRATION_PARENT_FILE_MASTERS_URL,
  BE_SUBSCRIPTION_REGISTRATION_PARENT_FILE_TEMPLATES_URL
} from '@/utils/Url';
import { fetchWithAuth, fetchWithAuthRaw } from '@/utils/fetchWithAuth';

// FETCH requests

export async function fetchRegistrationFileGroups(establishment) {
  return fetchWithAuth(
    `${BE_SUBSCRIPTION_REGISTRATIONFILE_GROUPS_URL}?establishment_id=${establishment}`
  );
}

export const fetchRegistrationFileFromGroup = (groupId) => {
  return fetchWithAuth(
    `${BE_SUBSCRIPTION_REGISTRATIONFILE_GROUPS_URL}/${groupId}/school_file_templates`
  );
};

export const fetchRegistrationSchoolFileMasters = (establishment) => {
  const url = `${BE_SUBSCRIPTION_REGISTRATION_SCHOOL_FILE_MASTERS_URL}?establishment_id=${establishment}`;
  return fetchWithAuth(url);
};

export const fetchRegistrationSchoolFileMasterById = (id) => {
  return fetchWithAuth(`${BE_SUBSCRIPTION_REGISTRATION_SCHOOL_FILE_MASTERS_URL}/${id}`);
};

export const fetchRegistrationParentFileMasters = (establishment) => {
  const url = `${BE_SUBSCRIPTION_REGISTRATION_PARENT_FILE_MASTERS_URL}?establishment_id=${establishment}`;
  return fetchWithAuth(url);
};

export const fetchRegistrationSchoolFileTemplates = (establishment) => {
  const url = `${BE_SUBSCRIPTION_REGISTRATION_SCHOOL_FILE_TEMPLATES_URL}?establishment_id=${establishment}`;
  return fetchWithAuth(url);
};

// CREATE requests

export async function createRegistrationFileGroup(groupData, csrfToken) {
  return fetchWithAuth(`${BE_SUBSCRIPTION_REGISTRATIONFILE_GROUPS_URL}`, {
    method: 'POST',
    headers: { 'X-CSRFToken': csrfToken },
    body: JSON.stringify(groupData),
  });
}

export const createRegistrationSchoolFileMaster = (data, csrfToken) => {
  // Toujours FormData, jamais JSON
  return fetchWithAuth(`${BE_SUBSCRIPTION_REGISTRATION_SCHOOL_FILE_MASTERS_URL}`, {
    method: 'POST',
    headers: { 'X-CSRFToken': csrfToken },
    body: data,
  });
};

export const createRegistrationParentFileMaster = (data, csrfToken) => {
  return fetchWithAuth(`${BE_SUBSCRIPTION_REGISTRATION_PARENT_FILE_MASTERS_URL}`, {
    method: 'POST',
    headers: { 'X-CSRFToken': csrfToken },
    body: JSON.stringify(data),
  });
};

export const createRegistrationSchoolFileTemplate = (data, csrfToken) => {
  return fetchWithAuth(`${BE_SUBSCRIPTION_REGISTRATION_SCHOOL_FILE_TEMPLATES_URL}`, {
    method: 'POST',
    headers: { 'X-CSRFToken': csrfToken },
    body: JSON.stringify(data),
  });
};

export const createRegistrationParentFileTemplate = (data, csrfToken) => {
  return fetchWithAuth(`${BE_SUBSCRIPTION_REGISTRATION_PARENT_FILE_TEMPLATES_URL}`, {
    method: 'POST',
    headers: { 'X-CSRFToken': csrfToken },
    body: JSON.stringify(data),
  });
};

// EDIT requests

export const editRegistrationFileGroup = (groupId, groupData, csrfToken) => {
  return fetchWithAuth(
    `${BE_SUBSCRIPTION_REGISTRATIONFILE_GROUPS_URL}/${groupId}`,
    {
      method: 'PUT',
      headers: { 'X-CSRFToken': csrfToken },
      body: JSON.stringify(groupData),
    }
  );
};

export const editRegistrationSchoolFileMaster = (fileId, data, csrfToken) => {
  return fetchWithAuth(
    `${BE_SUBSCRIPTION_REGISTRATION_SCHOOL_FILE_MASTERS_URL}/${fileId}`,
    {
      method: 'PUT',
      headers: { 'X-CSRFToken': csrfToken },
      body: data,
    }
  );
};

export const editRegistrationParentFileMaster = (id, data, csrfToken) => {
  return fetchWithAuth(
    `${BE_SUBSCRIPTION_REGISTRATION_PARENT_FILE_MASTERS_URL}/${id}`,
    {
      method: 'PUT',
      headers: { 'X-CSRFToken': csrfToken },
      body: JSON.stringify(data),
    }
  );
};

export const editRegistrationSchoolFileTemplates = (
  fileId,
  data,
  csrfToken
) => {
  return fetchWithAuth(
    `${BE_SUBSCRIPTION_REGISTRATION_SCHOOL_FILE_TEMPLATES_URL}/${fileId}`,
    {
      method: 'PUT',
      headers: { 'X-CSRFToken': csrfToken },
      body: data,
    }
  );
};

export const editRegistrationParentFileTemplates = (
  fileId,
  data,
  csrfToken
) => {
  return fetchWithAuth(
    `${BE_SUBSCRIPTION_REGISTRATION_PARENT_FILE_TEMPLATES_URL}/${fileId}`,
    {
      method: 'PUT',
      headers: { 'X-CSRFToken': csrfToken },
      body: data,
    }
  );
};

// DELETE requests

export async function deleteRegistrationFileGroup(groupId, csrfToken) {
  return fetchWithAuthRaw(
    `${BE_SUBSCRIPTION_REGISTRATIONFILE_GROUPS_URL}/${groupId}`,
    {
      method: 'DELETE',
      headers: { 'X-CSRFToken': csrfToken },
    }
  );
}

export const deleteRegistrationSchoolFileMaster = (fileId, csrfToken) => {
  return fetchWithAuthRaw(
    `${BE_SUBSCRIPTION_REGISTRATION_SCHOOL_FILE_MASTERS_URL}/${fileId}`,
    {
      method: 'DELETE',
      headers: { 'X-CSRFToken': csrfToken },
    }
  );
};

export const deleteRegistrationParentFileMaster = (id, csrfToken) => {
  return fetchWithAuthRaw(
    `${BE_SUBSCRIPTION_REGISTRATION_PARENT_FILE_MASTERS_URL}/${id}`,
    {
      method: 'DELETE',
      headers: { 'X-CSRFToken': csrfToken },
    }
  );
};

export const deleteRegistrationSchoolFileTemplates = (fileId, csrfToken) => {
  return fetchWithAuthRaw(
    `${BE_SUBSCRIPTION_REGISTRATION_SCHOOL_FILE_TEMPLATES_URL}/${fileId}`,
    {
      method: 'DELETE',
      headers: { 'X-CSRFToken': csrfToken },
    }
  );
};

export const deleteRegistrationParentFileTemplate = (id, csrfToken) => {
  return fetchWithAuthRaw(
    `${BE_SUBSCRIPTION_REGISTRATION_PARENT_FILE_TEMPLATES_URL}/${id}`,
    {
      method: 'DELETE',
      headers: { 'X-CSRFToken': csrfToken },
    }
  );
};
