import { useMutation, useQueryClient } from "@tanstack/react-query";
import { Axiosinstance } from "./Axiosinstance";

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

  return useMutation({
    mutationFn: async (data) => {
      const response = await Axiosinstance.post("accounts/register/", data);
      return response.data; // مثلاً { message: "...", user: {...} }
    },
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["todos"] });
    },
  });
};

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

  return useMutation({
    mutationFn: async (data) => {
      const response = await Axiosinstance.post(
        "accounts/verify-code/",
        data
      );
      return response.data;
    },
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["todos"] });
    },
  });
};

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

  return useMutation({
    mutationFn: async (data) => {
      const response = await Axiosinstance.post(
        "accounts/request-code/",
        data
      );
      return response.data;
    },
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["todos"] });
    },
  });
};

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

  return useMutation({
    // data: { username, password }
    mutationFn: async (data) => {
      const response = await Axiosinstance.post(
        "accounts/login/",
        data
      );
      // طبق لاگت، body این شکلیه:
      // { access: "...", refresh: "..." }
      return response.data; // همون { access, refresh }
    },
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["todos"] });
    },
  });
};
