import { rpcOrNull, rpcOrValue, rpcOrEmptyArray } from '@/services/analytics/rpc.uts'

/**
 * 管理员模型
 */
export type AdminUser = {
  id: string
  username: string
  real_name: string | null
  role: string
  is_active: boolean
  last_login_at: string | null
  last_login_ip: string | null
  roles: string[] | null
}

/**
 * 角色模型
 */
export type AdminRole = {
  id: string
  name: string
  code: string
  description: string | null
  is_active: boolean
  created_at: string
  updated_at: string
}

/**
 * 权限/菜单模型
 */
export type AdminPermission = {
  id: string
  parent_id: string | null
  name: string
  code: string
  type: string
  path: string | null
  icon: string | null
  sort_order: number
  is_visible: boolean
  created_at: string
  updated_at: string
}

/**
 * 获取管理员列表
 */
export async function fetchAdminPage(
  page: number,
  pageSize: number,
  search: string | null = null,
  status: number | null = null
): Promise<{ total: number, items: Array<AdminUser> }> {
  const res = await rpcOrNull('rpc_admin_get_admin_list', {
    p_page: page,
    p_page_size: pageSize,
    p_search: search,
    p_status: status
  } as UTSJSONObject)

  if (res == null) return { total: 0, items: [] as Array<AdminUser> }
  return {
    total: (res as any).total as number,
    items: (res as any).items as Array<AdminUser>
  }
}

/**
 * 获取角色列表
 */
export async function fetchRolePage(
  page: number,
  pageSize: number,
  search: string | null = null
): Promise<{ total: number, items: Array<AdminRole> }> {
  const res = await rpcOrNull('rpc_admin_get_role_list', {
    p_page: page,
    p_page_size: pageSize,
    p_search: search
  } as UTSJSONObject)

  if (res == null) return { total: 0, items: [] as Array<AdminRole> }
  return {
    total: (res as any).total as number,
    items: (res as any).items as Array<AdminRole>
  }
}

/**
 * 保存角色
 */
export async function saveRole(role: any): Promise<string | null> {
  const res = await rpcOrValue('rpc_admin_save_role', {
    p_id: role.id,
    p_name: role.name,
    p_code: role.code,
    p_description: role.description,
    p_is_active: role.is_active
  } as any)
  return res != null ? String(res) : null
}

/**
 * 删除角色
 */
export async function deleteRole(id: string): Promise<boolean> {
  const ok = await rpcOrValue('rpc_admin_delete_role', { p_id: id } as any)
  return ok === true
}

/**
 * 获取权限菜单树列表
 */
export async function fetchPermissionList(): Promise<Array<AdminPermission>> {
  return await rpcOrEmptyArray('rpc_admin_get_permission_list', {} as any) as Array<AdminPermission>
}

/**
 * 保存权限菜单
 */
export async function savePermission(permission: any): Promise<string | null> {
  const res = await rpcOrValue('rpc_admin_save_permission', {
    p_id: permission.id,
    p_parent_id: permission.parent_id,
    p_name: permission.name,
    p_code: permission.code,
    p_type: permission.type,
    p_path: permission.path,
    p_icon: permission.icon,
    p_sort_order: permission.sort_order,
    p_is_visible: permission.is_visible
  } as any)
  return res != null ? String(res) : null
}

/**
 * 删除权限菜单
 */
export async function deletePermission(id: string): Promise<boolean> {
  const ok = await rpcOrValue('rpc_admin_delete_permission', { p_id: id } as any)
  return ok === true
}
