import axios from "../../../config/axios";
import {
  CreateGroupType,
  CreateRoleType,
  CreateUserType,
} from "../types/UserTypes";

export const getPermissions = async () => {
  const { data } = await axios.get(`/users/permissions`);
  return data;
};

export const createRole = async (params: CreateRoleType) => {
  const { data } = await axios.post(`/users/roles`, params);
  return data;
};

export const getRoles = async (search: string) => {
  const { data } = await axios.get(`/users/roles?q=${search}`);
  return data;
};

export const createUser = async (params: CreateUserType) => {
  const { data } = await axios.post(`/users/admins`, params);
  return data;
};

export const getUsers = async (search: string, permission?: string) => {
  let query = `?q=${search}`;
  if (permission) query += `&permission=${permission}`;
  const { data } = await axios.get(`/users/admins${query}`);
  return data;
};

export const getUserDetail = async (id: string) => {
  const { data } = await axios.get(`/users/admins/${id}`);
  return data;
};

export const updateUser = async (id: string, params: CreateUserType) => {
  const { data } = await axios.patch(`/users/admins/${id}`, params);
  return data;
};

export const deleteUser = async (id: string) => {
  const { data } = await axios.delete(`/users/admins/${id}`);
  return data;
};

export const createGroup = async (params: CreateGroupType) => {
  const { data } = await axios.post(`/users/user-group`, params);
  return data;
};

export const getGroups = async () => {
  const { data } = await axios.get(`/users/user-group`);
  return data;
};

export const getAdminPermissions = async () => {
  const { data } = await axios.get(`/users/admin-permissions`);
  return data;
};
