import {
  APIUrlGenerator,
  httpGetRequest,
  httpPostRequest,
  httpPutRequest,
  httpDeleteRequest,
} from "@/utils/baseHttpService";
import { API_ROUTES } from "@/constant/routes";
import {
  CreateDiscountCodeRequest,
  UpdateDiscountCodeRequest,
  DiscountCode,
  DiscountCodeFilters,
  PaginatedDiscountCodesResponse,
  DiscountReportFilters,
  DiscountReportSimpleResponse,
} from "./_models";

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

  if (filters?.page) queryParams.page = filters.page;
  if (filters?.limit) queryParams.limit = filters.limit;
  if (filters?.status) queryParams.status = filters.status;
  if (filters?.type) queryParams.type = filters.type;
  if (filters?.application_level) {
    if (Array.isArray(filters.application_level)) {
      if (filters.application_level.length > 0) {
        queryParams.application_level = filters.application_level.join(",");
      }
    } else {
      queryParams.application_level = filters.application_level;
    }
  }
  if (filters?.code) queryParams.code = filters.code;
  if (typeof filters?.active_only === "boolean")
    queryParams.active_only = filters.active_only ? "true" : "false";

  const response = await httpGetRequest<PaginatedDiscountCodesResponse>(
    APIUrlGenerator(API_ROUTES.GET_DISCOUNT_CODES, queryParams)
  );

  return response.data.discount_codes || [];
};

export const getDiscountCode = async (id: string) => {
  const response = await httpGetRequest<DiscountCode>(
    APIUrlGenerator(API_ROUTES.GET_DISCOUNT_CODE(id))
  );
  return response.data as unknown as DiscountCode;
};

export const createDiscountCode = async (
  payload: CreateDiscountCodeRequest
) => {
  const response = await httpPostRequest<DiscountCode>(
    APIUrlGenerator(API_ROUTES.CREATE_DISCOUNT_CODE),
    payload
  );
  return response.data;
};

export const updateDiscountCode = async (
  id: string,
  payload: UpdateDiscountCodeRequest
) => {
  const response = await httpPutRequest<DiscountCode>(
    APIUrlGenerator(API_ROUTES.UPDATE_DISCOUNT_CODE(id)),
    payload
  );
  return response.data;
};

export const deleteDiscountCode = async (id: string) => {
  const response = await httpDeleteRequest<{ message: string }>(
    APIUrlGenerator(API_ROUTES.DELETE_DISCOUNT_CODE(id))
  );
  return response.data;
};

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

  if (filters?.view_mode) queryParams.view_mode = filters.view_mode;
  if (filters?.discount_code) queryParams.discount_code = filters.discount_code;
  if (filters?.discount_id) queryParams.discount_id = filters.discount_id;
  if (filters?.user_id) queryParams.user_id = filters.user_id;
  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?.from_date) queryParams.from_date = filters.from_date;
  if (filters?.to_date) queryParams.to_date = filters.to_date;
  if (filters?.min_usage_count)
    queryParams.min_usage_count = filters.min_usage_count;
  if (typeof filters?.include_unused === "boolean") {
    queryParams.include_unused = filters.include_unused ? "true" : "false";
  }
  if (typeof filters?.group_by_code === "boolean") {
    queryParams.group_by_code = filters.group_by_code ? "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<DiscountReportSimpleResponse>(
    APIUrlGenerator(API_ROUTES.DISCOUNT_REPORTS, queryParams)
  );

  return response.data;
};
