import axios from "@/config/axios";
import type {
  CreateCategoryType,
  CreateProductType,
  GetCategoriesResponseType,
  GetProductDetailsResponseType,
  GetProductsParams,
  GetProductsResponseType,
  GetIconsResponseType,
} from "../types/Types";
import type { FilterValues } from "@/components/Filters";

export const createProduct = async (params: CreateProductType) => {
  const { data } = await axios.post(`/admin/products`, params);
  return data;
};

export const getProducts = async (
  params?: GetProductsParams,
  filters?: FilterValues,
): Promise<GetProductsResponseType> => {
  const { data } = await axios.get<GetProductsResponseType>(`/admin/products`, {
    params: {
      ...params,
      ...filters,
    },
  });
  return data;
};

export const getProductDetails = async (
  id: string,
): Promise<GetProductDetailsResponseType> => {
  const { data } = await axios.get<GetProductDetailsResponseType>(
    `/admin/products/${id}`,
  );
  return data;
};

export const updateProduct = async (id: string, params: CreateProductType) => {
  const { data } = await axios.patch(`/admin/products/${id}`, params);
  return data;
};

export const getCategories = async (): Promise<GetCategoriesResponseType> => {
  const { data } = await axios.get<GetCategoriesResponseType>(
    `/admin/categories`,
  );
  return data;
};

export const deleteProduct = async (id: string) => {
  const { data } = await axios.delete(`/admin/products/${id}`);
  return data;
};

export const createCategory = async (params: CreateCategoryType) => {
  const { data } = await axios.post(`/admin/categories`, params);
  return data;
};

export const deleteCategory = async (id: string) => {
  const { data } = await axios.delete(`/admin/categories/${id}`);
  return data;
};

export const updateCategory = async (
  id: string,
  params: CreateCategoryType,
) => {
  const { data } = await axios.patch(`/admin/categories/${id}`, params);
  return data;
};

export const getIcons = async (): Promise<GetIconsResponseType> => {
  const { data } = await axios.get<GetIconsResponseType>(`/admin/groups/icons`);
  return data;
};
