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

const BASE_URL = '/sys/client'

const mockClients: SystemClient[] = [
  { id: '1', clientId: 'sundynix-admin', name: 'Web管理端', grantType: 'password', activeTimeout: 7200 },
  { id: '2', clientId: 'mini-app', name: '微信小程序', grantType: 'wechat', activeTimeout: 3600 },
  { id: '3', clientId: 'plant', name: 'Plant 花园服务', grantType: 'password', activeTimeout: 3600 },
]

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

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

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

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