// User management (ERPCore Controllers/UsersController.cs). Create orchestrates
// account creation in both AuthHex and ERPCore's local shadow table server-side.
import { apiRequest, buildQuery } from "@/lib/api-client"
import { PagedResponse } from "@/types/common"
import { CreateUserRequest, ManagedUser, UpdateUserRoleRequest, UserTypeOption } from "@/types/users"

export interface ListUsersParams {
  page?: number
  pageSize?: number
  q?: string
}

export const usersApi = {
  list(params: ListUsersParams = {}): Promise<PagedResponse<ManagedUser>> {
    return apiRequest<PagedResponse<ManagedUser>>(`/users${buildQuery(params)}`)
  },

  get(userId: number): Promise<ManagedUser> {
    return apiRequest<ManagedUser>(`/users/${userId}`)
  },

  create(request: CreateUserRequest): Promise<ManagedUser> {
    return apiRequest<ManagedUser>("/users", { method: "POST", body: request })
  },

  updateRole(userId: number, request: UpdateUserRoleRequest): Promise<ManagedUser> {
    return apiRequest<ManagedUser>(`/users/${userId}/role`, { method: "PUT", body: request })
  },

  userTypes(): Promise<UserTypeOption[]> {
    return apiRequest<UserTypeOption[]>("/users/user-types")
  },
}
