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 | null> = {};

  queryParams.view_mode = "simple";
  if (filters.date_range?.from) queryParams.from_date = filters.date_range.from;
  if (filters.date_range?.to) queryParams.to_date = 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 ? "true" : "false";
  if (filters.status) queryParams.status = filters.status;
  if (filters.type) queryParams.type = filters.type;
  if (filters.application_level) queryParams.application_level = filters.application_level;
  if (filters.min_usage_count !== undefined) queryParams.min_usage_count = filters.min_usage_count;
  if (filters.include_unused !== undefined) queryParams.include_unused = filters.include_unused ? "true" : "false";
  if (filters.sort_by) queryParams.sort_by = filters.sort_by;
  if (filters.sort_order) queryParams.sort_order = filters.sort_order;
  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_REPORTS, queryParams)
  );
  return response.data;
};

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

  if (filters.user_id !== undefined) queryParams.user_id = filters.user_id;
  if (filters.phone_number) queryParams.phone_number = filters.phone_number;
  if (filters.date_range?.from) queryParams.from_date = filters.date_range.from;
  if (filters.date_range?.to) queryParams.to_date = 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.status) queryParams.status = filters.status;
  if (filters.sort_by) queryParams.sort_by = filters.sort_by;
  if (filters.sort_order) queryParams.sort_order = filters.sort_order;
  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;
};

