import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import * as api from "../service/ThemeService";
import type { CreateThemeType } from "../type/Types";

export const useGetThemes = () => {
  return useQuery({
    queryKey: ["themes"],
    queryFn: api.getThemes,
  });
};

export const useCreateTheme = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: api.createTheme,
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["themes"] });
    },
  });
};

export const useGetThemeById = (id: string) => {
  return useQuery({
    queryKey: ["theme", id],
    queryFn: () => api.getThemeById(id),
    enabled: !!id,
  });
};

export const useUpdateTheme = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: ({ id, params }: { id: string; params: CreateThemeType }) =>
      api.updateTheme(id, params),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["theme"] });
      queryClient.invalidateQueries({ queryKey: ["themes"] });
    },
  });
};

export const useDeleteTheme = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: api.deleteTheme,
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["themes"] });
    },
  });
};
