"use client";

import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { simulationsAPI } from "@/lib/api";
import toast from "react-hot-toast";

export function useSimulation(id: string) {
  return useQuery({
    queryKey: ["simulation", id],
    queryFn: async () => {
      const { data } = await simulationsAPI.getById(id);
      return data.data;
    },
    enabled: !!id,
  });
}

export function useSimulations(params?: {
  page?: number;
  limit?: number;
  type?: string;
}) {
  return useQuery({
    queryKey: ["simulations", params],
    queryFn: async () => {
      const { data } = await simulationsAPI.getAll(params);
      return data.data;
    },
  });
}

export function usePostInteraction() {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: async ({
      postId,
      interactionType,
    }: {
      postId: string;
      interactionType: string;
    }) => {
      const { data } = await simulationsAPI.interact(postId, interactionType);
      return data.data;
    },
    onSuccess: (data: any) => {
      if (data && data.pointsEarned > 0) {
        toast.success("+" + data.pointsEarned + " XP!", {
          icon: "🌟",
          duration: 3000,
        });
      }
      queryClient.invalidateQueries({ queryKey: ["simulation"] });
      queryClient.invalidateQueries({ queryKey: ["student-progress"] });
    },
    onError: (error: any) => {
      toast.error(error.response?.data?.message || "Gagal menyimpan interaksi");
    },
  });
}