import CustomError from "@/utils/CustomError";
import toast from "react-hot-toast";
import axiosInstance from "./axiosGeneral";

export const getCities = async () => {
  try {
    const { data } = await axiosInstance.get(`cities`);
    return data || [];
  } catch (error: any) {
    toast.error(error.response?.data?.message);
    throw new CustomError("Failed to fetch Cities data", 500, "APIError");
  }
};

export const getBrands = async () => {
  try {
    const { data } = await axiosInstance.get(`brands`);
    return data?.data || [];
  } catch (error: any) {
    toast.error(
      error.response?.data?.message || "Failed to get Related Products"
    );
    throw new CustomError("Failed to fetch Category data", 500, "APIError");
  }
};

export const getFeatures = async () => {
  try {
    const { data } = await axiosInstance.get(`features`);
    return data?.data || [];
  } catch (error: any) {
    toast.error(
      error.response?.data?.message || "Failed to get Related Products"
    );
    throw new CustomError("Failed to fetch Category data", 500, "APIError");
  }
};

export const getFeatureValues = async (id: string) => {
  try {
    const { data } = await axiosInstance.get(`values/${id}`);
    return data?.data || [];
  } catch (error: any) {
    toast.error(
      error.response?.data?.message || "Failed to get Related Products"
    );
    throw new CustomError("Failed to fetch Category data", 500, "APIError");
  }
};

export const getBrandsData = async (key?: string) => {
  try {
    const { data } = await axiosInstance.get(`/brands_without_paginate${key ? `?keyword=${key}` : ""}`);
    return data || [];
  } catch (error) {
    throw new CustomError("Failed to fetch brands data", 500, "APIError");
  }
};

export const getCategories = async ({ params }: { params?: any } = {}) => {
  try {
    const { data } = await axiosInstance.get(`categories`, { params: params });
    return data?.data || [];
  } catch (error: any) {
    toast.error(
      error.response?.data?.message || "Failed to get Related Products"
    );
    throw new CustomError("Failed to fetch Category data", 500, "APIError");
  }
};