import { rpcOrNull, rpcOrValue } from '@/services/analytics/rpc.uts'

/**
 * 配送员模型
 */
export type DeliveryStaff = {
  id: string
  uid: string | null
  station_id: string | null
  station_name: string | null
  staff_no: string | null
  nickname: string
  avatar: string | null
  phone: string
  status: number
  is_active: boolean
  online_status: string
  certificate_status: string
  certificate_expire_at: string | null
  service_area: string | null
  skills: Array<string> | null
  created_at: string
  updated_at: string
}

/**
 * 提货点模型
 */
export type DeliveryStation = {
  id: string
  name: string
  phone: string
  address: string
  image: string | null
  lng: number | null
  lat: number | null
  status: number
  sort_order: number
  business_hours: UTSJSONObject | null
  created_at: string
  updated_at: string
}

/**
 * 获取配送员列表
 */
export async function fetchDeliveryStaffPage(
  page: number,
  pageSize: number,
  search: string | null = null,
  status: number | null = null
): Promise<{ total: number, items: Array<DeliveryStaff> }> {
  const res = await rpcOrNull('rpc_admin_get_delivery_staff_list', {
    p_page: page,
    p_page_size: pageSize,
    p_search: search,
    p_status: status
  } as UTSJSONObject)

  if (res == null) return { total: 0, items: [] as Array<DeliveryStaff> }
  return {
    total: (res as any).total as number,
    items: (res as any).items as Array<DeliveryStaff>
  }
}

/**
 * 保存配送员
 */
export async function saveDeliveryStaff(staff: any): Promise<string | null> {
  const rawSkills = typeof staff.skillsText === 'string' ? staff.skillsText : ''
  const parts = rawSkills.split(',')
  const skills = [] as Array<string>
  for (let i = 0; i < parts.length; i++) {
    const item = parts[i].trim()
    if (item != '') {
      skills.push(item)
    }
  }
  const res = await rpcOrValue('rpc_admin_save_delivery_staff', {
    p_id: staff.id,
    p_uid: staff.uid,
    p_station_id: staff.station_id,
    p_staff_no: staff.staff_no,
    p_nickname: staff.nickname,
    p_avatar: staff.avatar,
    p_phone: staff.phone,
    p_status: staff.status,
    p_online_status: staff.online_status,
    p_certificate_status: staff.certificate_status,
    p_certificate_expire_at: staff.certificate_expire_at,
    p_service_area: staff.service_area,
    p_skills: skills
  } as any)
  return res != null ? String(res) : null
}

/**
 * 删除配送员
 */
export async function deleteDeliveryStaff(id: string): Promise<boolean> {
  const ok = await rpcOrValue('rpc_admin_delete_delivery_staff', { p_id: id } as any)
  return ok === true
}

/**
 * 获取提货点列表
 */
export async function fetchDeliveryStationPage(
  page: number,
  pageSize: number,
  search: string | null = null,
  status: number | null = null
): Promise<{ total: number, items: Array<DeliveryStation> }> {
  const res = await rpcOrNull('rpc_admin_get_delivery_station_list', {
    p_page: page,
    p_page_size: pageSize,
    p_search: search,
    p_status: status
  } as UTSJSONObject)

  if (res == null) return { total: 0, items: [] as Array<DeliveryStation> }
  return {
    total: (res as any).total as number,
    items: (res as any).items as Array<DeliveryStation>
  }
}

/**
 * 保存提货点
 */
export async function saveDeliveryStation(station: any): Promise<string | null> {
  const res = await rpcOrValue('rpc_admin_save_delivery_station', {
    p_id: station.id,
    p_name: station.name,
    p_phone: station.phone,
    p_address: station.address,
    p_image: station.image,
    p_lng: station.lng,
    p_lat: station.lat,
    p_status: station.status,
    p_sort_order: station.sort_order,
    p_business_hours: station.business_hours
  } as any)
  return res != null ? String(res) : null
}

/**
 * 删除提货点
 */
export async function deleteDeliveryStation(id: string): Promise<boolean> {
  const ok = await rpcOrValue('rpc_admin_delete_delivery_station', { p_id: id } as any)
  return ok === true
}
