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

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

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

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

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

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