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

export const getShipmentsByMethodReport = async (
  filters: ShipmentsByMethodFilters
): Promise<ShipmentsByMethodResponse> => {
  const queryParams: Record<string, string | number | null> = {};

  if (filters.shipping_method_code)
    queryParams.shipping_method_code = filters.shipping_method_code;
  if (filters.shipping_method_id)
    queryParams.shipping_method_id = filters.shipping_method_id;
  if (filters.user_id) queryParams.user_id = filters.user_id;
  if (filters.customer_name) queryParams.customer_name = filters.customer_name;
  if (filters.status) queryParams.status = filters.status;
  if (filters.payment_status) queryParams.payment_status = filters.payment_status;
  if (filters.min_shipping_cost)
    queryParams.min_shipping_cost = filters.min_shipping_cost;
  if (filters.max_shipping_cost)
    queryParams.max_shipping_cost = filters.max_shipping_cost;
  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 (typeof filters.group_by_method === "boolean") {
    queryParams.group_by_method = filters.group_by_method ? "true" : "false";
  }
  if (typeof filters.limit === "number") queryParams.limit = filters.limit;
  if (typeof filters.offset === "number") queryParams.offset = filters.offset;

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

