import { apiRequest } from "@/lib/api/api-service";
import { ApiResponse } from "@/types/api-response";
import {
  RoleResponseDto,
  CreateRoleDto,
  UpdateRoleDto,
  RolePermissionResponseDto,
} from "./types";

/**
 * Admin Roles Service - Example Implementation
 * Matches Backend: /api/admin/roles/*
 */

const getAll = () => {
  return apiRequest<ApiResponse<RoleResponseDto[]>>({
    url: "/admin/roles",
    client: "admin",
    method: "get",
  });
};

const create = (data: CreateRoleDto) => {
  return apiRequest<ApiResponse<RoleResponseDto>>({
    url: "/admin/roles",
    client: "admin",
    method: "post",
    data,
  });
};

const update = (roleId: string, data: UpdateRoleDto) => {
  return apiRequest<ApiResponse<RoleResponseDto>>({
    url: `/admin/roles/${roleId}`,
    client: "admin",
    method: "put",
    data,
  });
};

const remove = (roleId: string) => {
  return apiRequest<ApiResponse<null>>({
    url: `/admin/roles/${roleId}`,
    client: "admin",
    method: "delete",
  });
};

const assignPermission = (roleId: string, permissionId: string) => {
  return apiRequest<ApiResponse<RolePermissionResponseDto>>({
    url: `/admin/roles/${roleId}/permissions/${permissionId}`,
    client: "admin",
    method: "post",
  });
};

const removePermission = (roleId: string, permissionId: string) => {
  return apiRequest<ApiResponse<null>>({
    url: `/admin/roles/${roleId}/permissions/${permissionId}`,
    client: "admin",
    method: "delete",
  });
};

export const adminRolesService = {
  getAll,
  create,
  update,
  remove,
  assignPermission,
  removePermission,
};
