import * as api from "../service/UserService";
import { useMutation, useQuery } from "@tanstack/react-query";
import {
  CreateGroupType,
  CreateRoleType,
  CreateUserType,
} from "../types/UserTypes";

export const useGetPermissions = () => {
  return useQuery({
    queryKey: ["permissions"],
    queryFn: () => api.getPermissions(),
  });
};

export const useCreateRole = () => {
  return useMutation({
    mutationFn: (variables: CreateRoleType) => api.createRole(variables),
  });
};

export const useGetRoles = (search: string) => {
  return useQuery({
    queryKey: ["roles", search],
    queryFn: () => api.getRoles(search),
  });
};

export const useCreateUser = () => {
  return useMutation({
    mutationFn: (variables: CreateUserType) => api.createUser(variables),
  });
};

export const useGetUsers = (search: string, permission?: string) => {
  return useQuery({
    queryKey: ["users", search, permission],
    queryFn: () => api.getUsers(search, permission),
  });
};

export const useGetUserDetail = (id: string) => {
  return useQuery({
    queryKey: ["user-detail", id],
    queryFn: () => api.getUserDetail(id),
  });
};

export const useUpdateUser = () => {
  return useMutation({
    mutationFn: ({ id, params }: { id: string; params: CreateUserType }) =>
      api.updateUser(id, params),
  });
};

export const useDeleteUser = () => {
  return useMutation({
    mutationFn: (id: string) => api.deleteUser(id),
  });
};

export const useCreateGroup = () => {
  return useMutation({
    mutationFn: (variables: CreateGroupType) => api.createGroup(variables),
  });
};

export const useGetGroups = () => {
  return useQuery({
    queryKey: ["users-group"],
    queryFn: () => api.getGroups(),
  });
};

export const useGetAdminPermissions = () => {
  return useQuery({
    queryKey: ["admin-permissions"],
    queryFn: () => api.getAdminPermissions(),
  });
};
