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

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

const fetchDataClass = 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 progress:', error);
        throw error;
    }
};

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

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

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

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

const postFeedback = async (id, feedback) => {
    try {
        const response = await axiosInstance.post(`/monitoring/feedback/${id}`, {feedback});
        return response.data;
    } catch (error) {
        console.error('Error post feedback:', error);
        throw error;
    }
};

const getCsvProgress = async (data) => {
    const configs = {
        headers: {
          Authorization: localStorage.getItem('token')
        },
        responseType: 'blob',
    };
    try {
        const response = await axiosInstance.post(`/monitoring/class/csv`,data ,configs);
        return response.data;
    } catch (error) {
        console.error(`Error get file:`, error);
        throw error;
    }
};

export default{
    fetchDataStudent,
    fetchDataClass,
    getStudentById,
    getTopicByClass,
    fetchDataStudentProgress,
    fetchDataClassProgress,
    getCsvProgress,
    postFeedback
};
