import { httpGetRequest, APIUrlGenerator } from "@/utils/baseHttpService";
import { API_ROUTES } from "@/constant/routes";
import {
  DiscountUsageFilters,
  DiscountUsageResponse,
  CustomerDiscountUsageFilters,
  CustomerDiscountUsageResponse,
} from "./_models";

export const getDiscountUsageReport = async (
  filters: DiscountUsageFilters
): Promise<DiscountUsageResponse> => {
  const queryParams: Record<string, string | number | boolean> = {};

  if (filters.date_range?.from) queryParams.from = filters.date_range.from;
  if (filters.date_range?.to) queryParams.to = filters.date_range.to;
  if (filters.discount_code) queryParams.discount_code = filters.discount_code;
  if (filters.discount_id !== undefined) queryParams.discount_id = filters.discount_id;
  if (filters.user_id !== undefined) queryParams.user_id = filters.user_id;
  if (filters.group_by_code !== undefined) queryParams.group_by_code = filters.group_by_code;
  if (filters.limit !== undefined) queryParams.limit = filters.limit;
  if (filters.offset !== undefined) queryParams.offset = filters.offset;

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

export const getCustomerDiscountUsageReport = async (
  filters: CustomerDiscountUsageFilters
): Promise<CustomerDiscountUsageResponse> => {
  const queryParams: Record<string, string | number> = {};

  queryParams.user_id = filters.user_id;
  if (filters.date_range?.from) queryParams.from = filters.date_range.from;
  if (filters.date_range?.to) queryParams.to = filters.date_range.to;
  if (filters.discount_code) queryParams.discount_code = filters.discount_code;
  if (filters.discount_id !== undefined) queryParams.discount_id = filters.discount_id;
  if (filters.limit !== undefined) queryParams.limit = filters.limit;
  if (filters.offset !== undefined) queryParams.offset = filters.offset;

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

