import { adminRequest } from "@/lib/admin-http";

import type { AdminRoleRow } from "@/types/api/admin-user";
import type {
  AgentAdminUserCreatePayload,
  AgentAdminUserListData,
  AgentAdminUserRoleSyncPayload,
  AgentNodeCreatePayload,
  AgentNodeListData,
  AgentNodeRow,
  AgentNodeUpdatePayload,
  AgentProfilePayload,
  AgentProfileRow,
  AgentRoleCreatePayload,
  AgentRoleListData,
  AgentTreeData,
  AgentDelegationGrantsData,
  AgentDelegationGrantSyncPayload,
} from "@/types/api/admin-agent";
import type { AdminUserPermissionRow } from "@/types/api/admin-user";

const A = `/admin`;

export async function getAgentTree(adminSiteId?: number): Promise<AgentTreeData> {
  return adminRequest.get<AgentTreeData>(`${A}/agent-nodes/tree`, {
    params: adminSiteId ? { admin_site_id: adminSiteId } : undefined,
  });
}

export async function getAgentNodes(adminSiteId?: number): Promise<AgentNodeListData> {
  return adminRequest.get<AgentNodeListData>(`${A}/agent-nodes`, {
    params: adminSiteId ? { admin_site_id: adminSiteId } : undefined,
  });
}

export async function postAgentNode(body: AgentNodeCreatePayload): Promise<AgentNodeRow> {
  return adminRequest.post<AgentNodeRow>(`${A}/agent-nodes`, body);
}

export async function putAgentNode(
  agentNodeId: number,
  body: AgentNodeUpdatePayload,
): Promise<AgentNodeRow> {
  return adminRequest.put<AgentNodeRow>(`${A}/agent-nodes/${agentNodeId}`, body);
}

export async function deleteAgentNode(agentNodeId: number): Promise<null> {
  return adminRequest.delete<null>(`${A}/agent-nodes/${agentNodeId}`);
}

export async function getAgentNodeProfile(agentNodeId: number): Promise<AgentProfileRow> {
  return adminRequest.get<AgentProfileRow>(`${A}/agent-nodes/${agentNodeId}/profile`);
}

export async function putAgentNodeProfile(
  agentNodeId: number,
  body: AgentProfilePayload,
): Promise<AgentProfileRow> {
  return adminRequest.put<AgentProfileRow>(`${A}/agent-nodes/${agentNodeId}/profile`, body);
}

export async function getAgentNodeRoles(agentNodeId: number): Promise<AgentRoleListData> {
  return adminRequest.get<AgentRoleListData>(`${A}/agent-nodes/${agentNodeId}/roles`);
}

export async function postAgentRole(
  agentNodeId: number,
  body: AgentRoleCreatePayload,
): Promise<AdminRoleRow> {
  return adminRequest.post<AdminRoleRow>(`${A}/agent-nodes/${agentNodeId}/roles`, body);
}

export async function putAgentRole(
  roleId: number,
  body: { name?: string; description?: string | null; status?: number },
): Promise<AdminRoleRow> {
  return adminRequest.put<AdminRoleRow>(`${A}/agent-roles/${roleId}`, body);
}

export async function putAgentRolePermissions(
  roleId: number,
  permissionSlugs: string[],
): Promise<AdminRoleRow> {
  return adminRequest.put<AdminRoleRow>(`${A}/agent-roles/${roleId}/permissions`, {
    permission_slugs: permissionSlugs,
  });
}

export async function deleteAgentRole(roleId: number): Promise<{ deleted: boolean; id: number }> {
  return adminRequest.delete<{ deleted: boolean; id: number }>(`${A}/agent-roles/${roleId}`);
}

export async function getAgentNodeAdminUsers(agentNodeId: number): Promise<AgentAdminUserListData> {
  return adminRequest.get<AgentAdminUserListData>(`${A}/agent-nodes/${agentNodeId}/admin-users`);
}

export async function postAgentAdminUser(
  agentNodeId: number,
  body: AgentAdminUserCreatePayload,
): Promise<AdminUserPermissionRow> {
  return adminRequest.post<AdminUserPermissionRow>(
    `${A}/agent-nodes/${agentNodeId}/admin-users`,
    body,
  );
}

export async function putAgentAdminUserRoles(
  adminUserId: number,
  body: AgentAdminUserRoleSyncPayload,
): Promise<AdminUserPermissionRow> {
  return adminRequest.put<AdminUserPermissionRow>(
    `${A}/agent-admin-users/${adminUserId}/roles`,
    body,
  );
}

export async function deleteAgentAdminUser(
  adminUserId: number,
): Promise<{ deleted: boolean; id: number }> {
  return adminRequest.delete<{ deleted: boolean; id: number }>(
    `${A}/agent-admin-users/${adminUserId}`,
  );
}

export async function getAgentDelegationGrants(
  agentNodeId: number,
): Promise<AgentDelegationGrantsData> {
  return adminRequest.get<AgentDelegationGrantsData>(
    `${A}/agent-nodes/${agentNodeId}/delegation-grants`,
  );
}

export async function putAgentDelegationGrants(
  agentNodeId: number,
  body: AgentDelegationGrantSyncPayload,
): Promise<AgentDelegationGrantsData> {
  return adminRequest.put<AgentDelegationGrantsData>(
    `${A}/agent-nodes/${agentNodeId}/delegation-grants`,
    body,
  );
}
