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

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

const fetchDataSection = async () => {
    try {
        const response = await axiosInstance.get(`/section`);
        return response.data;
    } catch (error) {
        console.error('Error fetching sections:', error);
        throw error;
    }
};

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

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

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

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

export default{
    fetchData,
    fetchDataSection,
    getTopicById,
    createData,
    updateData,
    deleteData
};
