import { httpGetRequest, APIUrlGenerator } from "@/utils/baseHttpService";
import { API_ROUTES } from "@/constant/routes";
import {
  PaymentMethodsFilters,
  PaymentMethodsResponse,
  PaymentTransactionsFilters,
  PaymentTransactionsResponse,
} from "./_models";

export const getPaymentMethodsReport = async (
  filters: PaymentMethodsFilters
): Promise<PaymentMethodsResponse> => {
  const queryParams: Record<string, string | number | null> = {};

  if (filters.user_id) queryParams.user_id = filters.user_id;
  if (filters.payment_type) queryParams.payment_type = filters.payment_type;
  if (filters.status) queryParams.status = filters.status;
  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_user === "boolean") {
    queryParams.group_by_user = filters.group_by_user ? "true" : "false";
  }
  if (typeof filters.limit === "number") queryParams.limit = filters.limit;
  if (typeof filters.offset === "number") queryParams.offset = filters.offset;

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

export const getPaymentTransactionsReport = async (
  filters: PaymentTransactionsFilters
): Promise<PaymentTransactionsResponse> => {
  const queryParams: Record<string, string | number | null> = {};

  if (filters.user_id) queryParams.user_id = filters.user_id;
  if (filters.phone_number) queryParams.phone_number = filters.phone_number;
  if (filters.order_id) queryParams.order_id = filters.order_id;
  if (filters.transaction_id)
    queryParams.transaction_id = filters.transaction_id;
  if (filters.reference_number)
    queryParams.reference_number = filters.reference_number;
  if (filters.status) queryParams.status = filters.status;
  if (filters.payment_type) queryParams.payment_type = filters.payment_type;
  if (filters.min_amount) queryParams.min_amount = filters.min_amount;
  if (filters.max_amount) queryParams.max_amount = filters.max_amount;
  if (filters.from_date) queryParams.from_date = filters.from_date;
  if (filters.to_date) queryParams.to_date = filters.to_date;
  if (typeof filters.group_by_status === "boolean") {
    queryParams.group_by_status = filters.group_by_status ? "true" : "false";
  }
  if (typeof filters.group_by_type === "boolean") {
    queryParams.group_by_type = filters.group_by_type ? "true" : "false";
  }
  if (filters.sort_by) queryParams.sort_by = filters.sort_by;
  if (filters.sort_order) queryParams.sort_order = filters.sort_order;
  if (typeof filters.limit === "number") queryParams.limit = filters.limit;
  if (typeof filters.offset === "number") queryParams.offset = filters.offset;

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