import { QUERY_KEYS } from "@/utils/query-key";
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { useNavigate } from "react-router-dom";
import {
  getCategories,
  getCategory,
  createCategory,
  updateCategory,
  deleteCategory,
} from "./_requests";
import {
  CreateCategoryRequest,
  UpdateCategoryRequest,
  CategoryFilters,
} from "./_models";
import toast from "react-hot-toast";

export const useCategories = (filters?: CategoryFilters) => {
  return useQuery({
    queryKey: [QUERY_KEYS.GET_CATEGORIES, filters],
    queryFn: () => getCategories(filters),
  });
};

export const useSearchCategories = (filters: CategoryFilters) => {
  return useQuery({
    queryKey: [QUERY_KEYS.SEARCH_CATEGORIES, filters],
    queryFn: () => getCategories(filters),
    enabled: Object.keys(filters).length > 0,
    staleTime: 2 * 60 * 1000, // 2 minutes for search results
  });
};

export const useCategory = (id: string, enabled: boolean = true) => {
  return useQuery({
    queryKey: [QUERY_KEYS.GET_CATEGORY, id],
    queryFn: () => getCategory(id),
    enabled: enabled && !!id,
  });
};

export const useCreateCategory = () => {
  const queryClient = useQueryClient();
  const navigate = useNavigate();

  return useMutation({
    mutationKey: [QUERY_KEYS.CREATE_CATEGORY],
    mutationFn: (data: CreateCategoryRequest) => createCategory(data),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: [QUERY_KEYS.GET_CATEGORIES] });
      toast.success("دسته‌بندی با موفقیت ایجاد شد");
      navigate("/categories");
    },
    onError: (error: any) => {
      console.error("Create category error:", error);
      toast.error(error?.message || "خطا در ایجاد دسته‌بندی");
    },
  });
};

export const useUpdateCategory = () => {
  const queryClient = useQueryClient();
  const navigate = useNavigate();

  return useMutation({
    mutationKey: [QUERY_KEYS.UPDATE_CATEGORY],
    mutationFn: (data: UpdateCategoryRequest) => updateCategory(data),
    onSuccess: (_, variables) => {
      queryClient.invalidateQueries({ queryKey: [QUERY_KEYS.GET_CATEGORIES] });
      queryClient.invalidateQueries({
        queryKey: [QUERY_KEYS.GET_CATEGORY, variables.id.toString()],
      });
      toast.success("دسته‌بندی با موفقیت ویرایش شد");
      navigate("/categories");
    },
    onError: (error: any) => {
      console.error("Update category error:", error);
      toast.error(error?.message || "خطا در ویرایش دسته‌بندی");
    },
  });
};

export const useDeleteCategory = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationKey: [QUERY_KEYS.DELETE_CATEGORY],
    mutationFn: (id: string) => deleteCategory(id),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: [QUERY_KEYS.GET_CATEGORIES] });
      toast.success("دسته‌بندی با موفقیت حذف شد");
    },
    onError: (error: any) => {
      console.error("Delete category error:", error);
      toast.error(error?.message || "خطا در حذف دسته‌بندی");
    },
  });
};
