import request from './request'

export interface UserItem {
  id: number
  username: string
  email?: string
  real_name?: string
  phone?: string
  dept_id?: number
  is_active: boolean
  is_superuser: boolean
  dept?: { id: number; name: string }
  roles: { id: number; name: string; code: string }[]
  created_at: string
}

export interface RoleItem {
  id: number
  name: string
  code: string
  description?: string
}

export interface DeptItem {
  id: number
  name: string
  parent_id?: number
  sort_order: number
}

export function getUsers(params?: { page?: number; page_size?: number; keyword?: string }) {
  return request.get('/users', { params })
}

export function createUser(data: {
  username: string
  password: string
  email?: string
  real_name?: string
  phone?: string
  dept_id?: number
  is_active?: boolean
  role_ids?: number[]
}) {
  return request.post('/users', data)
}

export function updateUser(id: number, data: Partial<UserItem> & { role_ids?: number[] }) {
  return request.put(`/users/${id}`, data)
}

export function deleteUser(id: number) {
  return request.delete(`/users/${id}`)
}

export function getRoles() {
  return request.get('/users/roles')
}

export function getDepts() {
  return request.get('/users/depts')
}
