import { api, apiURL } from '@Services/Api';

const reportService = {
    /**
     * Fetches the top selling product.
     * @param {string|null} fromDate - Start date in YYYY-MM-DD format.
     * @param {string|null} toDate - End date in YYYY-MM-DD format.
     * @returns {Promise<Object>}
     */
    async getTopSellingProduct(fromDate = null, toDate = null) {
        return new Promise((resolve, reject) => {
            const params = {};
            if (fromDate) params.from_date = fromDate;
            if (toDate) params.to_date = toDate;

            api.get(apiURL('reports/top-selling-product'), {
                params,
                onSuccess: (response) => {
                    resolve(response);
                },
                onError: (error) => {
                    console.error('Error al cargar el producto más vendido:', error);
                    reject(error);
                }
            });
        });
    },

    /**
     * Fetches products without movement.
     * @param {boolean} includeStockValue - Flag to include the inventory value in the response.
     * @returns {Promise<Object>}
     */
    async getProductsWithoutMovement(fromDate = null, toDate = null, includeStockValue = true) {
        return new Promise((resolve, reject) => {
            const params = {
                include_stock_value: includeStockValue ? 1 : 0
            };

            if(fromDate) params.from_date = fromDate;
            if(toDate) params.to_date = toDate;

            api.get(apiURL('reports/products-without-movement'), {
                params,
                onSuccess: (response) => {
                    resolve(response);
                },
                onError: (error) => {
                    console.error('Error al cargar los productos sin movimiento:', error);
                    reject(error);
                }
            });
        });
    }
};

export default reportService;