import { FeedbackFormValues } from "@/shared/schemas/feedback";
import feedbackService from "@/shared/services/feedback";
import { useCallback, useState } from "react";
import { toast } from "sonner";

type UseFeedBackFormOptions = {
  onClose: () => void;
};

export function useFeedBackForm(opts: UseFeedBackFormOptions) {
  const [isSubmitting, setIsSubmitting] = useState(false);

  const handleSubmitFeedback = useCallback(
    
    async (data: FeedbackFormValues) => {
      console.log('submit', data);
      try {
        setIsSubmitting(true);
        await feedbackService.sendFeedback(data);
        toast.success("Feedback Anda berhasil dikirim");
        opts.onClose();
      } catch (error) {
        toast.error("Gagal mengirim feedback");
        console.log("gagallll", error);
      } finally {
        setIsSubmitting(false);
      }
    },
    [opts]
  );

  const resetForm = useCallback(() => {
    opts.onClose();
  }, [opts]);

  return {
    isSubmitting,
    handleSubmitFeedback,
    resetForm,
  };
}
