import { useMutation, useQueryClient } from "@tanstack/react-query";
import { Survey, SurveyResponse } from "../schemas/survey";
import { deleteSurveyAction, saveSurveysAction, saveSurveyAnswer } from "../actions/surveys-actions";

// Mutation hook remains the same
export function useSurveyMutation() {
  const queryClient = useQueryClient();
  const mutation = useMutation({
    mutationFn: (data: Survey) => saveSurveysAction(data),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['surveys'] });
    },
    // onError: (error) => {
    //   console.error('Error:', error);
    // },
  });

  return mutation;
}

export function useDeleteSurvey() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: (id: number) => deleteSurveyAction(id),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['surveys'] });
    },
    onError: (error) => {
      console.error('Error:', error);
    },
  });
}

export function useSurveyAnswerMutation() {
    const queryClient = useQueryClient();
    const mutation = useMutation({
      mutationFn: (data: SurveyResponse) => saveSurveyAnswer(data),
      onSuccess: () => {
        queryClient.invalidateQueries({ queryKey: ['surveys'] });
      },
      onError: (error) => {
        console.error('Error:', error);
      },
    });
  
    return mutation;
}
