import axios from "@/config/axios";
import type {
  CreateRoleType,
  GetRolesResponseType,
  GetRoleDetailsResponseType,
  GetPermissionsResponseType,
} from "../types/Types";

export type GetRolesParams = {
  page?: number;
  limit?: number;
  search?: string;
};

export const getRoles = async (
  params?: GetRolesParams
): Promise<GetRolesResponseType> => {
  const { data } = await axios.get<GetRolesResponseType>("/admin/roles", {
    params,
  });
  return data;
};

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

export const deleteRole = async (id: string) => {
  const { data } = await axios.delete(`/admin/roles/${id}`);
  return data;
};

export const getRoleDetails = async (
  id: string
): Promise<GetRoleDetailsResponseType> => {
  const { data } = await axios.get<GetRoleDetailsResponseType>(
    `/admin/roles/${id}`
  );
  return data;
};

export const updateRole = async (id: string, params: CreateRoleType) => {
  const { data } = await axios.patch(`/admin/roles/${id}`, params);
  return data;
};

export const getPermissions = async (): Promise<GetPermissionsResponseType> => {
  const { data } = await axios.get<GetPermissionsResponseType>(
    `/admin/roles/permissions`
  );
  return data;
};
