import axios from "axios";
import axiosInstance from "./instance";
import { redirect } from "next/navigation";


export const getHomeData = async () => {
  try {
    const { data } = await axiosInstance.get("/home");
    return { data, error: false };
  } catch (error: any) {
    redirect("/error");
    return { data: [], error: true };
    // throw new CustomError("Failed to fetch Home Data", error?.response.status, "APIError");
  }
};

export const getTradersData = async () => {
  try {
    const { data } = await axiosInstance.get("/traders");
    return data || [];
  } catch (error: any) {
    redirect("/error");
    return {}
    // throw new CustomError("Failed to fetch Traders Data", error?.response.status, "APIError");
  }
};

export const getSingleTrader = async (name: string) => {
  try {
    const { data } = await axiosInstance.get(`/trader/${name}`);
    return data || [];
  } catch (error: any) {
    redirect("/error");
    return {}
    // throw new CustomError("Failed to fetch trader data", error?.response.status, "APIError");
  }
};

export const getProductById = async (id: string) => {
  try {
    const { data } = await axiosInstance.get(`/show-products/${id}`);
    return data?.data || [];
  } catch (error:any) {
    redirect("/error");
    return {}
    // throw new CustomError("Failed to fetch Product data", error?.response.status, "APIError");
  }
};

export const getPageData = async (type?: string) => {
  try {
    const { data } = await axiosInstance.get(`/page${type ? `/${type}` : ""}`);
    return data?.data || [];
  } catch (error: any) {
    redirect("/error");
    return {}
    // throw new CustomError("Failed to fetch About data", error?.response.status, "APIError");
  }
};

export const getFaqsData = async () => {
  try {
    const { data } = await axiosInstance.get("/faqs");
    return data?.data || [];
  } catch (error: any) {
    redirect("/error");
    return []
    // throw new CustomError("Failed to fetch Policy data", error?.response.status, "APIError");
  }
};

export const getWalletData = async () => {
  try {
    const { data } = await axiosInstance.get("/wallet");
    return data?.data || [];
  } catch (error: any) {
    redirect("/error");
    return {}
    // throw new CustomError("Failed to fetch Wallet data", error?.response.status, "APIError");
  }
};


export const getOrderDetailsData = async (id: string) => {
  try {
    const { data } = await axiosInstance.get(`/orders/${id}`);
    return data?.data || {};
  } catch (error: any) {
    redirect("/error");
    return {}
    // throw new CustomError("Failed to fetch  Order Details", error?.response.status, "APIError");
  }
};

export const getReturnDetailsData = async (id: string) => {
  try {
    const { data } = await axiosInstance.get(`/orders/returns/${id}`);
    return data?.data || {};
  } catch (error: any) {
    redirect("/error");
    return {}
    // throw new CustomError("Failed to fetch  Order Details", error?.response.status, "APIError");
  }
};

export const getChat = async () => {
  try {
    const { data } = await axiosInstance.get(`/chat`);
    return data || {};
  } catch (error: any) {
    redirect("/error");
    return {}
    // throw new CustomError("Failed to fetch  Order Details", error?.response.status, "APIError");
  }
};


export const getOutStocksData = async () => {
  try {
    const { data } = await axiosInstance.get(`/products/out-of-stock`);
    return data || {};
  } catch (error: any) {
    redirect("/error");
    return {}
    // throw new CustomError("Failed to fetch  Order Details", error?.response.status, "APIError");
  }
};

export const getSettingsData = async () => {
  try {
    const { data } = await axios.get(`settings`,{
      baseURL:process.env.NEXT_PUBLIC_GENERAL_URL
    });
    return data?.data.reduce((acc: any, item: any) => {
      acc[item.key] = item.value;
      return acc;
    }, {} as Record<string, string>);
  } catch (error: any) {
    redirect("/error");
    return {};
    // throw new CustomError("Failed to fetch brands data", error?.response.status, "APIError");
  }
};

export const getCategories = async () => {
  try {
    const { data } = await axiosInstance.get(`categories`, { 
      baseURL:process.env.NEXT_PUBLIC_GENERAL_URL,
      params: { level: "1" }
    });
    return data?.data || [];
  } catch (error: any) {
    redirect("/error");
    return[]
    // throw new CustomError("Failed to fetch Category data", 500, "APIError");
  }
};
