/* eslint-disable @typescript-eslint/no-explicit-any */
import { UserTypes } from "@/Constant/enums";
import { updateAddressInSession } from "../lib/Helper";
import { api } from "./Api";

export async function initiateRegistration(data: any) {
  const [, response] = await api.post("/initiate_registration", data, false);
  return response;
}

export async function registerCustomer(data: any) {
  const [, response] = await api.post("/user/register", data, false);
  return response;
}

export async function loginCustomer(data: any) {
  const [, response] = await api.post("/user/login", data, false);
  return response;
}

export async function getCustomerDetails() {
  const [, response] = await api.post("/user/details", {}, true);
  return response;
}

export async function forgotPassword(data: { email: string }) {
  const [, response] = await api.post("/user/forgot_password", data, false);
  return response;
}

export async function resetPassword(data: {
  password: string;
  reset_token: string;
}) {
  const [, response] = await api.post("/user/reset_password", data, false);
  return response;
}

export async function changePassword(data: {
  password: string;
  prev_password: string;
}) {
  const [, response] = await api.post("/user/change_password", data, true);
  return response;
}

export async function updateCustomerProfile(data: any) {
  const storedUser = localStorage.getItem("ALL_DATA");
  const allData = storedUser ? JSON.parse(storedUser)?.data : null;
  const [, response] =
    allData?.userType?.id === UserTypes.NormalCustomer
      ? await api.put("/customer/update_profile", data, true)
      : allData?.userType?.id === UserTypes.InternationalCustomer 
      ? await api.put("/international/update_profile", data, true)
      : await api.put("/corporate/update_profile", data, true); // Assuming token is required
  return response;
}

export async function updateCustomerBillingAddress(data: any) {
  const [, response] = await api.put("/customer/billing-address/update", data, true)
  return response;
}

export async function updateCustomerBillingEmails(data: any) {
  const [, response] = await api.put("/customer/billing-emails/update", data, true)
  return response;
}

export async function createCustomerAddress(data: any) {
  const [, response] = await api.post("/addresses/create", data, true);
  updateAddressInSession();
  return response;
}

export async function getCustomerAddresses({
  is_primary,
  id,
  order_by,
  sort_by,
}: any) {
  const filter: any = {};
  if (is_primary) filter.is_primary = true;
  if (id) filter.id = id;
  if (order_by) filter.order_by = order_by;
  if (sort_by) filter.sort_by = sort_by;
  const [, response] = await api.get("/addresses/list", true, {}, filter);
  return response;
}

export async function updateCustomerAddress(id: number, data: any) {
  const [, response] =
    !data?.name && data?.is_primary
      ? await api.put(`/addresses/update/primary/${id}`, data, true)
      : await api.put(`/addresses/update/${id}`, data, true);
  updateAddressInSession();
  return response;
}

export async function deleteCustomerAddress(id: number) {
  const [, response] = await api.delete(`/addresses/delete/${id}`, {}, true);
  updateAddressInSession();
  return response;
}

export async function createCustomerBankDetail(data: any) {
  const [, response] = await api.post("/customer/bank_detail/create", data, true);
  updateAddressInSession();
  return response;
}

export async function getCustomerBankDetails({
  is_primary,
  id,
  order_by,
  sort_by,
}: any) {
  const filter: any = {};
  if (is_primary) filter.is_primary = true;
  if (id) filter.id = id;
  if (order_by) filter.order_by = order_by;
  if (sort_by) filter.sort_by = sort_by;
  const [, response] = await api.get("/customer/bank_detail/list", true, {}, filter);
  return response;
}

export async function updateCustomerBankDetails(id: number, data: any) {
  const [, response] =
    !data?.name && data?.is_primary
      ? await api.put(`/customer/bank_detail/make_primary/${id}`, data, true)
      : await api.put(`/customer/bank_detail/update/${id}`, data, true);
  updateAddressInSession();
  return response;
}

export async function deleteCustomerBankDetails(id: number) {
  const [, response] = await api.delete(`/customer/bank_detail/delete/${id}`, {}, true);
  updateAddressInSession();
  return response;
}

export async function getStaff(filters?: any): Promise<any[]> {
  const queryParams: any = {};
  const rawParams = {
    page: filters?.page,
    limit: filters?.limit || "",
    search: filters?.search || "",
  };

  Object.entries(rawParams).forEach(([key, value]) => {
    if (value !== undefined && value !== null && value !== "") {
      queryParams[key] = value;
    }
  });
  const [, response] = await api.get(
    "/customer/corporate/staff/list",
    true,
    {},
    queryParams
  );
  return response?.data?.data || response?.data || [];
}

export async function getStaffDetails(id: number) {
  const [, response] = await api.get(
    `/customer/corporate/staff/details/${id}`,
    true,
    {}
  );
  return response?.data?.data || response?.data || [];
}

export async function updateStaffStatus(id: number, data: any) {
  const [, response] = await api.put(
    `/customer/corporate/staff/status/${id}`,
    data,
    true
  );
  return response;
}

export async function resetStaffPassword(id: number, data: any) {
  const [, response] = await api.put(
    `/customer/corporate/staff/reset_password`,
    data,
    true
  );
  return response;
}

export async function editStaff(id: number, data: any) {
  const [, response] = await api.put(
    `/customer/corporate/staff/update/${id}`,
    data,
    true
  );
  return response;
}

export async function getDashboard() {
  const [, response] = await api.get(`/customer/dashboard`, true, {});
  return response?.data || {};
}

export async function getUnAvailableShortCodes() {
  const [, response] = await api.get("/user/short_codes", true, {});
  return response?.data || {};
}

export async function getAllCustomers() {
  const [, response] = await api.get("/customer/customers/all", true, {});
  return response?.data || {};
}

export async function searchCustomerByPhone(phoneNumber: string) {
  const [, response] = await api.get(
    "/customer/search_by_phone",
    true,
    {},
    { phone: phoneNumber }
  );
  return response?.data || {};
}

export async function createBlock(data: object | undefined) {
  const [, response] = await api.post(
    "/customer/master/block/create",
    data,
    true
  );
  return response;
}

export async function createRoad(data: object | undefined) {
  const [, response] = await api.post(
    "/customer/master/road/create",
    data,
    true
  );
  return response;
}

export async function createBuilding(data: object | undefined) {
  const [, response] = await api.post(
    "/customer/master/building/create",
    data,
    true
  );
  return response;
}

export async function createExternalApiCredentials(data: {
  expiry_days: number;
}) {
  const [, response] = await api.post(
    "/customer/external/credentials/create",
    data,
    true
  );
  return response;
}

export async function getExternalApiCredentials() {
  const [, response] = await api.get(
    "/customer/external/credentials/details",
    true
  );
  return response;
}

export async function regenerateExternalApiCredentials(data: {
  expiry_days: number;
}) {
  const [, response] = await api.post(
    "/customer/external/credentials/regenerate",
    data,
    true
  );
  return response;
}

export async function updateExternalApiCredentials(data: {
  expiry_days: number;
}) {
  const [, response] = await api.put(
    "/customer/external/credentials/update",
    data,
    true
  );
  return response;
}

export async function deleteExternalApiCredentials() {
  const [, response] = await api.delete(
    "/customer/external/credentials/delete",
    {},
    true
  );
  return response;
}

export async function updateCustomerOrderFlowType(data: any) {
  const [, response] = await api.put(
    "/customer/update_profile/order_flow_type",
    data,
    true
  );
  return response;
}

export async function getAllOrderFlowTypes() {
  const [, response] = await api.get("/customer/order_flow_types/all", true);
  return response;
}

export async function getAllOrderFlowTypesWithSelected() {
  const [, response] = await api.get(
    "/customer/order_flow_types/with-selected",
    true
  );
  return response;
}

export async function getCustomerSettlementIntervals() {
  const [, response] = await api.get(
    "/customer/settlement_interval/details",
    true
  );
  return response;
}

export async function updateCustomerSettlementIntervals(data: {
  cod_settlement_interval: string;
  invoice_settlement_interval: string;
}) {
  const [, response] = await api.put(
    "/customer/settlement_interval/update",
    data,
    true
  );
  return response;
}
