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

const getSoalNumber = (title) => {
    const match = title.match(/\d+$/); 
    return match ? parseInt(match[0], 10) : 0;
};

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

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

const getExerciseByLevelId = async (id) => {
    try {
        const response = await axiosInstance.get(`/exercise/admin/level/${id}`);

        const sortedData = response.data.payload.EXERCISES.sort((a, b) => {
            return getSoalNumber(a.TITLE) - getSoalNumber(b.TITLE);
        });

        return {NAME_LEVEL: response.data.payload.NAME_LEVEL ,NAME_SECTION: response.data.payload.NAME_SECTION ,NAME_TOPIC: response.data.payload.NAME_TOPIC , EXERCISES: sortedData};
    } catch (error) {
        console.error(`Error fetching level with ID ${id}:`, error);
        throw error;
    }
};

const createData = async (questionData) => {
    try {
        const response = await axiosInstance.post(`/exercises`, questionData);

        const question = response.data.payload[0];
        let editedResponse = "";
        if (question.QUESTION_TYPE === "MPQ") {
            editedResponse = {...question, matchingPairs: question.questionDetails};
        }else if (question.QUESTION_TYPE === "MCQ") {
            editedResponse = {...question, multipleChoices:  [question.questionDetails]};
        }else if (question.QUESTION_TYPE === "TFQ") {
            editedResponse = {...question, trueFalse:  [question.questionDetails]};
        }

        return {payload: editedResponse, message: response.data.message, status: response.data.statusCode,};
    } catch (error) {
        console.error('Error adding level:', error);
        throw error;
    }
};

const updateData = async (id, questionData) => {
    try {
        const response = await axiosInstance.put(`/exercise/${id}`, questionData);

        const question = response.data.payload;
        let editedResponse = "";
        if (question.QUESTION_TYPE === "MPQ") {
            editedResponse = {...question, matchingPairs: question.matchingPairs};
        }else if (question.QUESTION_TYPE === "MCQ") {
            editedResponse = {...question, multipleChoices:  [question.multipleChoices]};
        }else if (question.QUESTION_TYPE === "TFQ") {
            editedResponse = {...question, trueFalse:  [question.trueFalse]};
        }

        return {payload: editedResponse, message: response.data.message, status: response.data.statusCode,};
        // return response.data;
    } catch (error) {
        console.error(`Error updating level with ID ${id}:`, error);
        throw error;
    }
};

const sortingData = async (questionData) => {
    try {
        const response = await axiosInstance.put(`/exercise/title`, questionData);
        return response.data;
    } catch (error) {
        console.error(`Error updating level with ID ${id}:`, error);
        throw error;
    }
};

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

const deleteMedia = async (id, fileType) => {
    try {
        const response = await axiosInstance.post(`/exercise/file/${id}`, {fileType});
        return response.data;
    } catch (error) {
        console.error(`Error deleting question with ID ${id}:`, error);
        throw error;
    }
};

export default{
    fetchData,
    getExerciseByLevelId,
    getLevelById,
    createData,
    updateData,
    sortingData,
    deleteData,
    deleteMedia
};
