import { useMutation, useQuery } from '@tanstack/react-query';
import * as api from '../service/CategoryService';
import { type CategoryType, type CreateCategoryAttributeType, type CategoryAttributesResponseType } from '../types/Types';

export const useCreateCategory = () => {
    return useMutation({
        mutationFn: (variables: CategoryType) => api.createCategory(variables),
    });
};

export const useGetCategories = () => {
    return useQuery({
        queryKey: ['categories'],
        queryFn: api.getCategories,
    });
};

export const useGetCategoryById = (id: string) => {
    return useQuery({
        queryKey: ['category', id],
        queryFn: () => api.getCategoryById(id),
        enabled: !!id,
    });
};

export const useUpdateCategory = () => {
    return useMutation({
        mutationFn: ({ id, data }: { id: string; data: Partial<CategoryType> }) =>
            api.updateCategory(id, data),
    });
};

export const useDeleteCategory = () => {
    return useMutation({
        mutationFn: (id: string) => api.deleteCategory(id),
    });
};

export const useSingleUpload = () => {
    return useMutation({
        mutationFn: (variables: File) => api.singleUpload(variables),
    });
};

export const useGetCategoryVariants = (id: string) => {
    return useQuery({
        queryKey: ['category', 'variants', id],
        queryFn: () => api.getCategoryVariants(id),
        enabled: !!id,
    });
};

export const useCreateCategoryVariant = () => {
    return useMutation({
        mutationFn: api.createCategoryVariant,
    });
};

export const useUpdateCategoryVariant = () => {
    return useMutation({
        mutationFn: api.updateCategoryVariant,
    });
};

export const useDeleteCategoryVariant = () => {
    return useMutation({
        mutationFn: api.deleteCategoryVariant,
    });
};

export const useCreateCategoryAttribute = () => {
    return useMutation({
        mutationFn: ({ id, data }: { id: string; data: CreateCategoryAttributeType }) =>
            api.createCategoryAttribute(id, data),
    });
};

export const useGetCategoryAttributes = (id: string) => {
    return useQuery<CategoryAttributesResponseType>({
        queryKey: ['category', 'attributes', id],
        queryFn: () => api.getCategoryAttributes(id),
        enabled: !!id,
    });
};