import {
  BE_SCHOOL_SPECIALITIES_URL,
  BE_SCHOOL_TEACHERS_URL,
  BE_SCHOOL_SCHOOLCLASSES_URL,
  BE_SCHOOL_PLANNINGS_URL,
  BE_SCHOOL_FEES_URL,
  BE_SCHOOL_DISCOUNTS_URL,
  BE_SCHOOL_PAYMENT_PLANS_URL,
  BE_SCHOOL_PAYMENT_MODES_URL,
  BE_SCHOOL_ESTABLISHMENT_URL,
  BE_SCHOOL_ESTABLISHMENT_COMPETENCIES_URL,
} from '@/utils/Url';
import { errorHandler, requestResponseHandler } from './actionsHandlers';

export const deleteEstablishmentCompetencies = (ids, csrfToken) => {
  return fetch(BE_SCHOOL_ESTABLISHMENT_COMPETENCIES_URL, {
    method: 'DELETE',
    headers: {
      'Content-Type': 'application/json',
      'X-CSRFToken': csrfToken,
    },
    body: JSON.stringify({ ids }),
    credentials: 'include',
  })
    .then(requestResponseHandler)
    .catch(errorHandler);
};

export const createEstablishmentCompetencies = (newData, csrfToken) => {
  return fetch(BE_SCHOOL_ESTABLISHMENT_COMPETENCIES_URL, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'X-CSRFToken': csrfToken,
    },
    body: JSON.stringify(newData),
    credentials: 'include',
  })
    .then(requestResponseHandler)
    .catch(errorHandler);
};

export const fetchEstablishmentCompetencies = (establishment, cycle = 1) => {
  return fetch(
    `${BE_SCHOOL_ESTABLISHMENT_COMPETENCIES_URL}?establishment_id=${establishment}&cycle=${cycle}`
  )
    .then(requestResponseHandler)
    .catch(errorHandler);
};

export const fetchSpecialities = (establishment) => {
  return fetch(
    `${BE_SCHOOL_SPECIALITIES_URL}?establishment_id=${establishment}`
  )
    .then(requestResponseHandler)
    .catch(errorHandler);
};

export const fetchTeachers = (establishment) => {
  return fetch(`${BE_SCHOOL_TEACHERS_URL}?establishment_id=${establishment}`)
    .then(requestResponseHandler)
    .catch(errorHandler);
};

export const fetchClasses = (establishment) => {
  return fetch(
    `${BE_SCHOOL_SCHOOLCLASSES_URL}?establishment_id=${establishment}`
  )
    .then(requestResponseHandler)
    .catch(errorHandler);
};

export const fetchClasse = (id) => {
  return fetch(`${BE_SCHOOL_SCHOOLCLASSES_URL}/${id}`).then(
    requestResponseHandler
  );
};

export const fetchSchedules = () => {
  return fetch(`${BE_SCHOOL_PLANNINGS_URL}`)
    .then(requestResponseHandler)
    .catch(errorHandler);
};

export const fetchRegistrationDiscounts = (establishment) => {
  return fetch(
    `${BE_SCHOOL_DISCOUNTS_URL}?filter=registration&establishment_id=${establishment}`
  )
    .then(requestResponseHandler)
    .catch(errorHandler);
};

export const fetchTuitionDiscounts = (establishment) => {
  return fetch(
    `${BE_SCHOOL_DISCOUNTS_URL}?filter=tuition&establishment_id=${establishment}`
  )
    .then(requestResponseHandler)
    .catch(errorHandler);
};

export const fetchRegistrationFees = (establishment) => {
  return fetch(
    `${BE_SCHOOL_FEES_URL}?filter=registration&establishment_id=${establishment}`
  )
    .then(requestResponseHandler)
    .catch(errorHandler);
};

export const fetchTuitionFees = (establishment) => {
  return fetch(
    `${BE_SCHOOL_FEES_URL}?filter=tuition&establishment_id=${establishment}`
  )
    .then(requestResponseHandler)
    .catch(errorHandler);
};

export const fetchRegistrationPaymentPlans = (establishment) => {
  return fetch(
    `${BE_SCHOOL_PAYMENT_PLANS_URL}?filter=registration&establishment_id=${establishment}`
  )
    .then(requestResponseHandler)
    .catch(errorHandler);
};

export const fetchTuitionPaymentPlans = (establishment) => {
  return fetch(
    `${BE_SCHOOL_PAYMENT_PLANS_URL}?filter=tuition&establishment_id=${establishment}`
  )
    .then(requestResponseHandler)
    .catch(errorHandler);
};

export const fetchRegistrationPaymentModes = (establishment) => {
  return fetch(
    `${BE_SCHOOL_PAYMENT_MODES_URL}?filter=registration&establishment_id=${establishment}`
  )
    .then(requestResponseHandler)
    .catch(errorHandler);
};

export const fetchTuitionPaymentModes = (establishment) => {
  return fetch(
    `${BE_SCHOOL_PAYMENT_MODES_URL}?filter=tuition&establishment_id=${establishment}`
  )
    .then(requestResponseHandler)
    .catch(errorHandler);
};

export const fetchEstablishment = (establishment) => {
  return fetch(`${BE_SCHOOL_ESTABLISHMENT_URL}/${establishment}`)
    .then(requestResponseHandler)
    .catch(errorHandler);
};

export const createDatas = (url, newData, csrfToken) => {
  return fetch(url, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'X-CSRFToken': csrfToken,
    },
    body: JSON.stringify(newData),
    credentials: 'include',
  })
    .then(requestResponseHandler)
    .catch(errorHandler);
};

export const updateDatas = (url, id, updatedData, csrfToken) => {
  return fetch(`${url}/${id}`, {
    method: 'PUT',
    headers: {
      'Content-Type': 'application/json',
      'X-CSRFToken': csrfToken,
    },
    body: JSON.stringify(updatedData),
    credentials: 'include',
  })
    .then(requestResponseHandler)
    .catch(errorHandler);
};

export const removeDatas = (url, id, csrfToken) => {
  return fetch(`${url}/${id}`, {
    method: 'DELETE',
    headers: {
      'Content-Type': 'application/json',
      'X-CSRFToken': csrfToken,
    },
    credentials: 'include',
  })
    .then(requestResponseHandler)
    .catch(errorHandler);
};
