import { request } from './request';
import { apiClient } from './axiosClient';

const ENDPOINT = '/api/product';

export const requestCreateProduct = async (data) => {
    const res = await request.post(`${ENDPOINT}/create`, data);
    return res.data;
};

export const requestGetAllProduct = async () => {
    const res = await request.get(`${ENDPOINT}/list`);
    return res.data;
};

export const requestUpdateProduct = async (id, data) => {
    const res = await request.put(`${ENDPOINT}/update/${id}`, data);
    return res.data;
};

export const requestDeleteProduct = async (id) => {
    const res = await request.delete(`${ENDPOINT}/delete/${id}`);
    return res.data;
};

export const requestGetProductByCategory = async (categoryId, filters = {}) => {
    const params = new URLSearchParams();
    if (filters.minPrice) params.append('minPrice', filters.minPrice);
    if (filters.maxPrice) params.append('maxPrice', filters.maxPrice);
    if (filters.sortBy) params.append('sortBy', filters.sortBy);
    if (filters.search) params.append('search', filters.search);
    if (filters.page) params.append('page', filters.page);
    if (filters.limit) params.append('limit', filters.limit);

    const queryString = params.toString();
    const url = `${ENDPOINT}/category/${categoryId}${queryString ? `?${queryString}` : ''}`;
    const res = await request.get(url);
    return res.data;
};

export const requestGetProductById = async (id) => {
    const res = await request.get(`${ENDPOINT}/${id}`);
    return res.data;
};
