import { SendRequest } from '../components/Global/ApiRequest';

const getAllUser = async (queryParams) => {
    const response = await SendRequest({
        method: 'get',
        prefix: `user?${queryParams.toString()}`,
    });

    // Parse query params to get page and limit
    const params = Object.fromEntries(queryParams);
    const currentPage = parseInt(params.page) || 1;
    const currentLimit = parseInt(params.limit) || 10;

    // Backend returns all data, so we need to do client-side pagination
    const allData = response.data || [];
    
    // Filter out users with is_sa = true
    const filteredData = allData.filter(user => user.is_sa !== true);
    const totalData = filteredData.length;

    // Calculate start and end index for current page
    const startIndex = (currentPage - 1) * currentLimit;
    const endIndex = startIndex + currentLimit;

    // Slice data for current page
    const paginatedData = filteredData.slice(startIndex, endIndex);

    // Transform response to match TableList expected structure
    return {
        status: response.statusCode || 200,
        data: {
            data: paginatedData,
            paging: {
                page: currentPage,
                limit: currentLimit,
                total: totalData,
                page_total: Math.ceil(totalData / currentLimit)
            },
            total: totalData
        }
    };
};

const getUserById = async (id) => {
    const response = await SendRequest({
        method: 'get',
        prefix: `user/${id}`,
    });
    return response.data;
};

const createUser = async (queryParams) => {
    const response = await SendRequest({
        method: 'post',
        prefix: `user`,
        params: queryParams,
    });
    // Return full response with statusCode
    return {
        statusCode: response.statusCode || 200,
        data: response.data,
        message: response.message
    };
};

const updateUser = async (user_id, queryParams) => {
    const response = await SendRequest({
        method: 'put',
        prefix: `user/${user_id}`,
        params: queryParams,
    });
    // Return full response with statusCode
    return {
        statusCode: response.statusCode || 200,
        data: response.data,
        message: response.message
    };
};

const deleteUser = async (queryParams) => {
    const response = await SendRequest({
        method: 'delete',
        prefix: `user/${queryParams}`,
    });
    // Return full response with statusCode
    return {
        statusCode: response.statusCode || 200,
        data: response.data,
        message: response.message
    };
};

const approveUser = async (user_id) => {
    const response = await SendRequest({
        method: 'put',
        prefix: `user/${user_id}/approve`,
    });
    // Return full response with statusCode
    return {
        statusCode: response.statusCode || 200,
        data: response.data,
        message: response.message
    };
};

export { getAllUser, getUserById, createUser, updateUser, deleteUser, approveUser };