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
} from '@/utils/Url';

const requestResponseHandler = async (response) => {

  const body = await response.json();
  if (response.ok) {
    return body;
  }
   // Throw an error with the JSON body containing the form errors
  const error = new Error(body?.errorMessage || "Une erreur est survenue");
  error.details = body;
  throw error;
}


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

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

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

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

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

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

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

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

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

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

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

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

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

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)
};

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)
  };

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