import {
  APIUrlGenerator,
  httpGetRequest,
  httpPutRequest,
} from "@/utils/baseHttpService";
import { API_ROUTES } from "@/constant/routes";
import {
  Order,
  OrderFilters,
  PaginatedOrdersResponse,
  UpdateOrderStatusRequest,
  OrderStats,
  OrderStatus,
} from "./_models";

export const getOrders = async (filters?: OrderFilters) => {
  const queryParams: Record<string, string | number | null> = {};

  // Pagination
  if (filters?.limit) queryParams.limit = filters.limit;
  // Prefer offset; compute from page when provided
  if (filters?.offset !== undefined && filters.offset !== null) {
    queryParams.offset = filters.offset;
  } else if (filters?.page) {
    const effectiveLimit = filters?.limit || 20;
    queryParams.offset = (filters.page - 1) * effectiveLimit;
  }

  // Filter Parameters
  if (filters?.user_id) queryParams.user_id = filters.user_id;
  if (filters?.invoice_id) queryParams.invoice_id = filters.invoice_id;
  if (filters?.status) queryParams.status = filters.status;
  if (filters?.payment_status)
    queryParams.payment_status = filters.payment_status;
  if (filters?.customer_id) queryParams.customer_id = filters.customer_id;
  if (filters?.order_number) queryParams.order_number = filters.order_number;
  if (filters?.transaction_id)
    queryParams.transaction_id = filters.transaction_id;
  if (filters?.discount_code) queryParams.discount_code = filters.discount_code;

  // Amount Range Parameters (prefer new API naming)
  if (filters?.min_total) queryParams.min_total = filters.min_total;
  if (filters?.max_total) queryParams.max_total = filters.max_total;
  if (filters?.min_amount && !filters?.min_total)
    queryParams.min_total = filters.min_amount;
  if (filters?.max_amount && !filters?.max_total)
    queryParams.max_total = filters.max_amount;

  // Date Range Parameters (prefer new API naming)
  if (filters?.created_from) queryParams.created_from = filters.created_from;
  if (filters?.created_to) queryParams.created_to = filters.created_to;
  if (filters?.updated_from) queryParams.updated_from = filters.updated_from;
  if (filters?.updated_to) queryParams.updated_to = filters.updated_to;
  if (filters?.date_from && !filters?.created_from)
    queryParams.created_from = filters.date_from;
  if (filters?.date_to && !filters?.created_to)
    queryParams.created_to = filters.date_to;

  // Search Parameter (per API spec)
  if (filters?.search) queryParams.search = filters.search;

  const response = await httpGetRequest<PaginatedOrdersResponse>(
    APIUrlGenerator(API_ROUTES.GET_ORDERS, queryParams)
  );

  return response.data;
};

export const getOrder = async (id: string) => {
  const response = await httpGetRequest<Order>(
    APIUrlGenerator(API_ROUTES.GET_ORDER(id))
  );
  return response.data;
};

export const updateOrderStatus = async (
  id: string,
  payload: UpdateOrderStatusRequest
) => {
  const response = await httpPutRequest<Order>(
    APIUrlGenerator(API_ROUTES.UPDATE_ORDER_STATUS(id)),
    payload
  );
  return response.data;
};

export const getOrderStats = async (): Promise<OrderStats> => {
  try {
    const response = await httpGetRequest<OrderStats>(
      APIUrlGenerator(API_ROUTES.GET_ORDER_STATS)
    );
    return response.data;
  } catch (error) {
    console.error("Error fetching order stats:", error);
    throw error;
  }
};
