import { post } from '@/lib/request'
import { USE_MOCK, delay, paginate } from '@/mock'
import { mockUsers } from '@/mock/system/users'
import type { SystemUser } from '../system'
import type { PageResult } from '@/lib/request'

const BASE_URL = '/sys/user'

export async function getUserList(params: { current: number; pageSize: number; account?: string; name?: string }) {
  if (USE_MOCK) {
    await delay()
    let list = [...mockUsers]
    if (params.account) list = list.filter(u => u.account.includes(params.account!))
    if (params.name) list = list.filter(u => u.name.includes(params.name!))
    return paginate(list, params.current, params.pageSize)
  }
  return post<PageResult<SystemUser>>(`${BASE_URL}/list`, params)
}

export async function createUser(data: Partial<SystemUser> & { password?: string }) {
  if (USE_MOCK) { await delay(); return null }
  return post<null>(`${BASE_URL}/create`, data)
}

export async function updateUser(data: Partial<SystemUser> & { id: string }) {
  if (USE_MOCK) { await delay(); return null }
  return post<null>(`${BASE_URL}/update`, data)
}

export async function deleteUser(ids: string[]) {
  if (USE_MOCK) { await delay(); return null }
  return post<null>(`${BASE_URL}/delete`, { ids })
}

export async function resetPassword(data: { id: string; password: string }) {
  if (USE_MOCK) { await delay(); return null }
  return post<null>(`${BASE_URL}/resetPassword`, data)
}
