import { apiRequest } from "@/shared/api/request";
import { API_ENDPOINTS, API_OPERATIONS, apiEndpointPath } from "@/shared/api/generated/endpoints";
import type { EntityId, MenuDto, MenuPayload, MenuSortItem, PermissionDto, PermissionPayload } from "@/shared/api/types";

export function getMenus(): Promise<MenuDto[]> {
  return apiRequest<MenuDto[]>(apiEndpointPath(API_OPERATIONS.navigationMenus));
}

export function listSystemMenus(): Promise<MenuDto[]> {
  return apiRequest<MenuDto[]>(apiEndpointPath(API_OPERATIONS.listSystemMenus));
}

export function createMenu(payload: MenuPayload): Promise<MenuDto> {
  const endpoint = API_ENDPOINTS.createMenu;
  return apiRequest<MenuDto, MenuPayload>(apiEndpointPath(API_OPERATIONS.createMenu), {
    body: payload,
    method: endpoint.method
  });
}

export function updateMenu(id: EntityId, payload: MenuPayload): Promise<MenuDto> {
  const endpoint = API_ENDPOINTS.updateMenu;
  return apiRequest<MenuDto, MenuPayload>(apiEndpointPath(API_OPERATIONS.updateMenu, { id }), {
    body: payload,
    method: endpoint.method
  });
}

export function deleteMenu(id: EntityId): Promise<unknown> {
  const endpoint = API_ENDPOINTS.deleteMenu;
  return apiRequest(apiEndpointPath(API_OPERATIONS.deleteMenu, { id }), { method: endpoint.method });
}

export function updateMenuVisible(id: EntityId, visible: boolean): Promise<MenuDto> {
  const endpoint = API_ENDPOINTS.updateMenuVisible;
  return apiRequest<MenuDto, { visible: boolean }>(apiEndpointPath(API_OPERATIONS.updateMenuVisible, { id }), {
    body: { visible },
    method: endpoint.method
  });
}

export function sortMenus(items: MenuSortItem[]): Promise<MenuDto[]> {
  const endpoint = API_ENDPOINTS.sortMenus;
  return apiRequest<MenuDto[], { items: MenuSortItem[] }>(apiEndpointPath(API_OPERATIONS.sortMenus), {
    body: { items },
    method: endpoint.method
  });
}

export function listPermissions(): Promise<PermissionDto[]> {
  return apiRequest<PermissionDto[]>(apiEndpointPath(API_OPERATIONS.listPermissions));
}

export function createPermission(payload: PermissionPayload): Promise<PermissionDto> {
  const endpoint = API_ENDPOINTS.createPermission;
  return apiRequest<PermissionDto, PermissionPayload>(apiEndpointPath(API_OPERATIONS.createPermission), {
    body: payload,
    method: endpoint.method
  });
}

export function updatePermission(id: EntityId, payload: PermissionPayload): Promise<PermissionDto> {
  const endpoint = API_ENDPOINTS.updatePermission;
  return apiRequest<PermissionDto, PermissionPayload>(apiEndpointPath(API_OPERATIONS.updatePermission, { id }), {
    body: payload,
    method: endpoint.method
  });
}

export function deletePermission(id: EntityId): Promise<unknown> {
  const endpoint = API_ENDPOINTS.deletePermission;
  return apiRequest(apiEndpointPath(API_OPERATIONS.deletePermission, { id }), { method: endpoint.method });
}

export function syncPermissions(): Promise<PermissionDto[]> {
  const endpoint = API_ENDPOINTS.syncPermissions;
  return apiRequest<PermissionDto[], Record<string, never>>(apiEndpointPath(API_OPERATIONS.syncPermissions), {
    body: {},
    method: endpoint.method
  });
}
