import { httpGetRequest, APIUrlGenerator } from "@/utils/baseHttpService";
import { API_ROUTES } from "@/constant/routes";
import { ProfitLossFilters, ProfitLossResponse } from "./_models";

export const getProfitLossReport = async (
  filters?: ProfitLossFilters
): Promise<ProfitLossResponse> => {
  const queryParams: Record<string, string | number> = {};

  if (filters?.from) queryParams.from = filters.from;
  if (filters?.to) queryParams.to = filters.to;
  if (filters?.product_sku) queryParams.product_sku = filters.product_sku;
  if (filters?.category_name) queryParams.category_name = filters.category_name;
  if (filters?.min_profit_loss_grams !== undefined) queryParams.min_profit_loss_grams = filters.min_profit_loss_grams;
  if (filters?.max_profit_loss_grams !== undefined) queryParams.max_profit_loss_grams = filters.max_profit_loss_grams;
  if (filters?.min_profit_loss_tomans !== undefined) queryParams.min_profit_loss_tomans = filters.min_profit_loss_tomans;
  if (filters?.max_profit_loss_tomans !== undefined) queryParams.max_profit_loss_tomans = filters.max_profit_loss_tomans;

  const response = await httpGetRequest<ProfitLossResponse>(
    APIUrlGenerator(API_ROUTES.PROFIT_LOSS_REPORT, queryParams)
  );
  return response.data;
};
