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

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 mockResponse(paginate(list, params.current, params.pageSize))
  }
  return get<{ code: number; data: { list: SystemUser[]; total: number }; msg: string }>('/system/users', params)
}

export async function createUser(data: Partial<SystemUser>) {
  if (USE_MOCK) { await delay(); return mockResponse(null, '创建成功') }
  return post<{ code: number; data: null; msg: string }>('/system/users', data)
}

export async function updateUser(id: string, data: Partial<SystemUser>) {
  if (USE_MOCK) { await delay(); return mockResponse(null, '更新成功') }
  return put<{ code: number; data: null; msg: string }>(`/system/users/${id}`, data)
}

export async function deleteUser(id: string) {
  if (USE_MOCK) { await delay(); return mockResponse(null, '删除成功') }
  return del<{ code: number; data: null; msg: string }>(`/system/users/${id}`)
}
