import axiosInstance from '../../../../utils/axiosInstance';

const fetchData= async (search, sort, page, limit) => {
    try {
        const response = await axiosInstance.get(`/class/admin?search=${search}&sort=${sort}&page=${page}&limit=${limit}`);
        return response.data;
    } catch (error) {
        console.error('Error fetching class:', error);
        throw error;
    }
};

const fetchDataFreeStudent = async () => {
    try {
        const response = await axiosInstance.get(`/class/student/unassigned`);
        return response.data;

    } catch (error) {
        console.error('Error fetching class:', error);
        throw error;
    }
};

const getClassById = async (id) => {
    try {
        const response = await axiosInstance.get(`/class/${id}`);
        return response.data;
    } catch (error) {
        console.error(`Error fetching class with ID ${id}:`, error);
        throw error;
    }
};

const getClassStudent = async (id) => {
    try {
        const response = await axiosInstance.get(`/class/student/${id}`);
        return response.data;
    } catch (error) {
        console.error(`Error fetching class with ID ${id}:`, error);
        throw error;
    }
}

const createData = async (classData) => {
    try {
        const response = await axiosInstance.post(`/class`, classData);
        return response.data;
    } catch (error) {
        console.error('Error adding class:', error);
        throw error;
    }
};

const updateData = async (id, classData) => {
    try {
        const response = await axiosInstance.put(`/class/update/${id}`, classData);
        return response.data;
    } catch (error) {
        console.error(`Error updating class with ID ${id}:`, error);
        throw error;
    }
};

const deleteData = async (id) => {
    try {
        const response = await axiosInstance.delete(`/class/delete/${id}`);
        return response.data;
    } catch (error) {
        console.error(`Error deleting class with ID ${id}:`, error);
        throw error;
    }
};

const studentToClass = async (asignData) => {
    try {
        const response = await axiosInstance.post(`/class/student/update`, asignData);
        return response.data;
    } catch (error) {
        console.error(`Error asign student to class :`, error);
        throw error;
    }
};

export default{
    fetchData,
    fetchDataFreeStudent,
    getClassById,
    getClassStudent,
    createData,
    updateData,
    deleteData,
    studentToClass
};
