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,
  BE_SCHOOL_EVALUATIONS_URL,
  BE_SCHOOL_STUDENT_EVALUATIONS_URL,
  BE_SCHOOL_SCHOOL_YEARS_URL,
} from '@/utils/Url';
import { fetchWithAuth } from '@/utils/fetchWithAuth';

export const deleteEstablishmentCompetencies = (ids, csrfToken) => {
  return fetchWithAuth(BE_SCHOOL_ESTABLISHMENT_COMPETENCIES_URL, {
    method: 'DELETE',
    headers: { 'X-CSRFToken': csrfToken },
    body: JSON.stringify({ ids }),
  });
};

export const createEstablishmentCompetencies = (newData, csrfToken) => {
  return fetchWithAuth(BE_SCHOOL_ESTABLISHMENT_COMPETENCIES_URL, {
    method: 'POST',
    headers: { 'X-CSRFToken': csrfToken },
    body: JSON.stringify(newData),
  });
};

export const fetchEstablishmentCompetencies = (establishment, cycle = 1) => {
  return fetchWithAuth(
    `${BE_SCHOOL_ESTABLISHMENT_COMPETENCIES_URL}?establishment_id=${establishment}&cycle=${cycle}`
  );
};

export const fetchSpecialities = (establishment, schoolYear = null) => {
  let url = `${BE_SCHOOL_SPECIALITIES_URL}?establishment_id=${establishment}`;
  if (schoolYear) url += `&school_year=${schoolYear}`;
  return fetchWithAuth(url);
};

export const fetchTeachers = (establishment) => {
  return fetchWithAuth(`${BE_SCHOOL_TEACHERS_URL}?establishment_id=${establishment}`);
};

export const fetchClasses = (establishment, options = {}) => {
  let url = `${BE_SCHOOL_SCHOOLCLASSES_URL}?establishment_id=${establishment}`;
  if (options.schoolYear) url += `&school_year=${options.schoolYear}`;
  if (options.yearFilter) url += `&year_filter=${options.yearFilter}`;
  return fetchWithAuth(url);
};

export const fetchSchoolYears = () => {
  return fetchWithAuth(BE_SCHOOL_SCHOOL_YEARS_URL);
};

export const fetchClasse = (id) => {
  return fetchWithAuth(`${BE_SCHOOL_SCHOOLCLASSES_URL}/${id}`);
};

export const fetchSchedules = () => {
  return fetchWithAuth(`${BE_SCHOOL_PLANNINGS_URL}`);
};

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

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

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

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

export const fetchRegistrationPaymentPlans = (establishment) => {
  return fetchWithAuth(
    `${BE_SCHOOL_PAYMENT_PLANS_URL}?filter=registration&establishment_id=${establishment}`
  );
};

export const fetchTuitionPaymentPlans = (establishment) => {
  return fetchWithAuth(
    `${BE_SCHOOL_PAYMENT_PLANS_URL}?filter=tuition&establishment_id=${establishment}`
  );
};

export const fetchRegistrationPaymentModes = (establishment) => {
  return fetchWithAuth(
    `${BE_SCHOOL_PAYMENT_MODES_URL}?filter=registration&establishment_id=${establishment}`
  );
};

export const fetchTuitionPaymentModes = (establishment) => {
  return fetchWithAuth(
    `${BE_SCHOOL_PAYMENT_MODES_URL}?filter=tuition&establishment_id=${establishment}`
  );
};

export const fetchEstablishment = (establishment) => {
  return fetchWithAuth(`${BE_SCHOOL_ESTABLISHMENT_URL}/${establishment}`);
};

export const createDatas = (url, newData, csrfToken) => {
  return fetchWithAuth(url, {
    method: 'POST',
    headers: { 'X-CSRFToken': csrfToken },
    body: JSON.stringify(newData),
  });
};

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

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

// ===================== EVALUATIONS =====================

export const fetchEvaluations = (establishmentId, schoolClassId = null, period = null) => {
  let url = `${BE_SCHOOL_EVALUATIONS_URL}?establishment_id=${establishmentId}`;
  if (schoolClassId) url += `&school_class=${schoolClassId}`;
  if (period) url += `&period=${period}`;
  return fetchWithAuth(url);
};

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

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

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

// ===================== STUDENT EVALUATIONS =====================

export const fetchStudentEvaluations = (studentId = null, evaluationId = null, period = null, schoolClassId = null) => {
  let url = `${BE_SCHOOL_STUDENT_EVALUATIONS_URL}?`;
  const params = [];
  if (studentId) params.push(`student_id=${studentId}`);
  if (evaluationId) params.push(`evaluation_id=${evaluationId}`);
  if (period) params.push(`period=${period}`);
  if (schoolClassId) params.push(`school_class_id=${schoolClassId}`);
  url += params.join('&');
  return fetchWithAuth(url);
};

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

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

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