import axios from "../../../config/axios";
import { type CategoryType, type SingleUploadResponseType, type CategoriesResponseType, type CategoryByIdResponseType, type CategoryVariantsResponseType, type CategoryVariantsType, type CreateCategoryAttributeType, type CategoryAttributesResponseType } from "../types/Types";

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

export const getCategories = async (): Promise<CategoriesResponseType> => {
  const { data } = await axios.get(`/category`);
  return data;
};

export const getCategoryById = async (id: string): Promise<CategoryByIdResponseType> => {
  const { data } = await axios.get(`/category/${id}`);
  return data;
};

export const updateCategory = async (id: string, params: Partial<CategoryType>) => {
  const { data } = await axios.patch(`/admin/category`, { catId: id, ...params });
  return data;
};

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

export const singleUpload = async (params: File): Promise<SingleUploadResponseType> => {
  const formData = new FormData();
  formData.append('file', params);
  const { data } = await axios.post(`/admin/medias/upload/single`, formData);
  return data;
};

export const getCategoryVariants = async (id: string): Promise<CategoryVariantsResponseType> => {
  const { data } = await axios.get(`/category/${id}/variants`);
  return data;
};

export const createCategoryVariant = async (params: CategoryVariantsType) => {
  const { data } = await axios.post(`/admin/category/${params.id}/variants`, params);
  return data;
};

export const updateCategoryVariant = async (params: CategoryVariantsType) => {
  const { data } = await axios.patch(`/admin/category/${params.id}/variants`, params);
  return data;
};

export const deleteCategoryVariant = async (params: { id: string; variantType: 'sizes' | 'colors' | 'meterages'; index: number }) => {
  const { data } = await axios.delete(`/admin/category/${params.id}/variants`, {
    data: {
      variantType: params.variantType,
      index: params.index
    }
  });
  return data;
};

export const createCategoryAttribute = async (id: string, params: CreateCategoryAttributeType) => {
  const { data } = await axios.post(`/admin/category/${id}/attributes`, params);
  return data;
};

export const getCategoryAttributes = async (id: string): Promise<CategoryAttributesResponseType> => {
  const { data } = await axios.get(`/category/${id}/attributes`);
  return data;
};