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

const BASE_URL = '/sys/role'

const mockRoles: SystemRole[] = [
  { id: '1', name: '超级管理员', code: 'super_admin', sort: 1 },
  { id: '2', name: '系统管理员', code: 'admin', sort: 2 },
  { id: '3', name: '运营专员', code: 'operator', sort: 3 },
  { id: '4', name: '内容审核', code: 'auditor', sort: 4 },
  { id: '5', name: '客服', code: 'customer_service', sort: 5 },
]

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

export async function createRole(data: Partial<SystemRole>) {
  if (USE_MOCK) { await delay(); return null }
  return post<null>(`${BASE_URL}/create`, data)
}

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

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