import axios from 'axios';
import { API_URL } from '../../../../utils/Constant';
import axiosInstance from '../../../../utils/axiosInstance';

const config = {
    headers: {
      Authorization: localStorage.getItem('token')
    }
};

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

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

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

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

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

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

export default{
    fetchData,
    getSectionById,
    createData,
    updateData,
    deleteData
};
