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

export async function fetchOrderPage(
  page: number,
  pageSize: number,
  orderStatus: number | null = null,
  search: string | null = null,
  startTime: string | null = null,
  endTime: string | null = null
): Promise<{ total: number; items: Array<any> }> {
  const res = await rpcOrNull('rpc_admin_order_list', {
    p_page: page,
    p_page_size: pageSize,
    p_order_status: orderStatus,
    p_search: search,
    p_start_time: startTime,
    p_end_time: endTime
  } as any)

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

  const anyTotal = (res as any).total
  const anyItems = (res as any).items

  const total = typeof anyTotal === 'number' ? anyTotal : parseInt(String(anyTotal ?? '0'))
  const items = Array.isArray(anyItems) ? (anyItems as Array<any>) : ([] as Array<any>)

  return { total, items }
}

export async function fetchRefundOrderPage(
  page: number,
  pageSize: number,
  status: number | null = null,
  search: string | null = null
): Promise<{ total: number; items: Array<any> }> {
  const res = await rpcOrNull('rpc_admin_refund_order_list', {
    p_page: page,
    p_page_size: pageSize,
    p_refund_status: status,
    p_search: search
  } as any)

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

  const anyTotal = (res as any).total
  const anyItems = (res as any).items

  const total = typeof anyTotal === 'number' ? anyTotal : parseInt(String(anyTotal ?? '0'))
  const items = Array.isArray(anyItems) ? (anyItems as Array<any>) : ([] as Array<any>)

  return { total, items }
}

export async function fetchWriteOffRecordPage(
  page: number,
  pageSize: number,
  search: string | null = null,
  startTime: string | null = null,
  endTime: string | null = null
): Promise<{ total: number; items: Array<any> }> {
  const res = await rpcOrNull('rpc_admin_write_off_record_list', {
    p_page: page,
    p_page_size: pageSize,
    p_search: search,
    p_start_time: startTime,
    p_end_time: endTime
  } as any)

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

  const anyTotal = (res as any).total
  const anyItems = (res as any).items

  const total = typeof anyTotal === 'number' ? anyTotal : parseInt(String(anyTotal ?? '0'))
  const items = Array.isArray(anyItems) ? (anyItems as Array<any>) : ([] as Array<any>)

  return { total, items }
}

export async function fetchCashierOrderPage(
  page: number,
  pageSize: number,
  orderNo: string | null = null,
  username: string | null = null,
  startTime: string | null = null,
  endTime: string | null = null
): Promise<{ total: number; items: Array<any> }> {
  const res = await rpcOrNull('rpc_admin_cashier_order_list', {
    p_page: page,
    p_page_size: pageSize,
    p_search_order_no: orderNo,
    p_search_username: username,
    p_start_time: startTime,
    p_end_time: endTime
  } as any)

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

  const anyTotal = (res as any).total
  const anyItems = (res as any).items

  const total = typeof anyTotal === 'number' ? anyTotal : parseInt(String(anyTotal ?? '0'))
  const items = Array.isArray(anyItems) ? (anyItems as Array<any>) : ([] as Array<any>)

  return { total, items }
}

export async function fetchOrderStats(startTime: string, endTime: string): Promise<any | null> {
  const res = await rpcOrNull('rpc_admin_order_stats', {
    p_start_time: startTime,
    p_end_time: endTime
  } as any)
  return res as any
}

export async function fetchOrderTrend(startTime: string, endTime: string, groupBy: string = 'day'): Promise<Array<any>> {
  return (await rpcOrEmptyArray('rpc_admin_order_trend', {
    p_start_time: startTime,
    p_end_time: endTime,
    p_group_by: groupBy
  } as any)) as any
}

export async function fetchOrderSourceStats(startTime: string, endTime: string): Promise<Array<any>> {
  return (await rpcOrEmptyArray('rpc_admin_order_source_stats', {
    p_start_time: startTime,
    p_end_time: endTime
  } as any)) as any
}

export async function fetchOrderTypeStats(startTime: string, endTime: string): Promise<Array<any>> {
  return (await rpcOrEmptyArray('rpc_admin_order_type_stats', {
    p_start_time: startTime,
    p_end_time: endTime
  } as any)) as any
}

export async function getOrderSettings(): Promise<UTSJSONObject | null> {
  const res = await rpcOrValue('rpc_admin_system_config_get', {
    p_key: 'order_settings'
  } as any)
  return res as any
}

export async function saveOrderSettings(config: UTSJSONObject): Promise<boolean> {
  const res = await rpcOrValue('rpc_admin_system_config_save', {
    p_key: 'order_settings',
    p_value: config
  } as any)
  return res === true
}
