import { apiGet, apiPost } from '@/lib/http'
import type { AdminPagination, AdminUserListItem } from '@/types/admin'

export function fetchAdminUsers(params?: Record<string, unknown>) {
  return apiGet<{ items: AdminUserListItem[]; pagination: AdminPagination }>(
    '/api/v1/admin/users',
    params,
  )
}

export function createAdminUser(payload: {
  username: string
  password: string
  role: string
  status?: string
}) {
  return apiPost<{ user: AdminUserListItem }>('/api/v1/admin/users', payload)
}

export function updateAdminUserRole(userId: number | string, payload: { role: string }) {
  return apiPost<{ user: AdminUserListItem }>(`/api/v1/admin/users/${userId}/role`, payload)
}

export function updateAdminUserStatus(userId: number | string, payload: { status: string }) {
  return apiPost<{ user: AdminUserListItem }>(`/api/v1/admin/users/${userId}/status`, payload)
}

export function updateAdminUserInventoryGroups(
  userId: number | string,
  payload: { inventoryGroupCodes: string[] },
) {
  return apiPost<{ user: AdminUserListItem }>(
    `/api/v1/admin/users/${userId}/inventory-groups`,
    payload,
  )
}

export function resetAdminUserPassword(userId: number | string, payload: { password: string }) {
  return apiPost<{ user: AdminUserListItem }>(
    `/api/v1/admin/users/${userId}/reset-password`,
    payload,
  )
}
