// user-admin.jsx
import axios from 'axios';
import { SendRequest } from '../components/Global/ApiRequest';

const baseURL = import.meta.env.VITE_API_SERVER;

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

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

const updateUser = async (id, data) => {
    const response = await SendRequest({
        method: 'put',
        prefix: `admin-user/${id}`,
        params: data,
    });
    return response;
};

const deleteUser = async (id) => {
    const response = await SendRequest({
        method: 'delete',
        prefix: `admin-user/${id}`,
    });
    return response;
};

const approvalUser = async (id, queryParams) => {
    const response = await SendRequest({
        method: 'post',
        prefix: `admin-user/approve/${id}`,
        params: queryParams,
    });
    return response;
};

const uploadFile = async (formData) => {
    try {
        const token = localStorage.getItem('token')?.replace(/"/g, '') || '';
        const url = `${baseURL}/file-upload`;

        const response = await axios.post(url, formData, {
            headers: {
                Authorization: `Bearer ${token}`,
                'Accept-Language': 'en_US',
                'Content-Type': 'multipart/form-data',
            },
        });

        return {
            statusCode: response.data?.statusCode ?? 0,
            message: response.data?.message ?? '',
            data: response.data?.data ?? {},
        };
    } catch (error) {
        console.error('❌ ERROR di uploadFile:', error);
        return {
            statusCode: error?.response?.status || 500,
            message: error?.response?.data?.message || 'Upload gagal',
            data: {},
        };
    }
};








export { getAllUser, getUserDetail, updateUser, deleteUser, approvalUser, uploadFile };