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

/**
 * 客服账号类型
 */
export type KefuAccount = {
  id: string
  user_id: string
  nickname: string
  avatar: string | null
  status: number
  is_online: boolean
  created_at: string
  updated_at: string
  user_account: string
}

/**
 * 话术分类类型
 */
export type WordCategory = {
  id: string
  name: string
  sort: number
  created_at: string
  updated_at: string
}

/**
 * 快捷话术类型
 */
export type KefuWord = {
  id: string
  category_id: string
  title: string
  content: string
  sort: number
  created_at: string
  updated_at: string
  category_name: string
}

/**
 * 用户留言类型
 */
export type KefuFeedback = {
  id: string
  user_id: string | null
  nickname: string | null
  phone: string | null
  content: string
  status: number
  reply_content: string | null
  processed_at: string | null
  created_at: string
  updated_at: string
  user_account: string | null
}

/**
 * 自动回复类型
 */
export type AutoReply = {
  id: string
  keyword: string
  content: string
  reply_type: string
  status: number
  created_at: string
  updated_at: string
}

/**
 * 客服配置类型
 */
export type KefuConfig = {
  type: string
  feedback: string
  phone: string
  link: string
}

/**
 * 获取客服账号列表
 */
export async function fetchKefuAccountPage(
  page: number,
  pageSize: number,
  search: string | null = null,
  status: number | null = null
): Promise<{ total: number, items: Array<KefuAccount> }> {
  const res = await rpcOrNull('rpc_admin_kefu_account_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<KefuAccount> }
  return {
    total: (res as any).total as number,
    items: (res as any).items as Array<KefuAccount>
  }
}

/**
 * 保存客服账号
 */
export async function saveKefuAccount(
  id: string | null,
  userId: string | null,
  nickname: string | null,
  avatar: string | null,
  status: number
): Promise<string | null> {
  const res = await rpcOrValue('rpc_admin_kefu_account_save', {
    p_id: id,
    p_user_id: userId,
    p_nickname: nickname,
    p_avatar: avatar,
    p_status: status
  } as any)
  return res != null ? String(res) : null
}

/**
 * 删除客服账号
 */
export async function deleteKefuAccount(id: string): Promise<boolean> {
  const ok = await rpcOrValue('rpc_admin_kefu_account_delete', { p_id: id } as any)
  return ok === true
}

/**
 * 设置客服账号状态
 */
export async function setKefuAccountStatus(id: string, status: number): Promise<boolean> {
  const ok = await rpcOrValue('rpc_admin_kefu_account_set_status', { p_id: id, p_status: status } as any)
  return ok === true
}

/**
 * 获取话术分类列表
 */
export async function fetchWordCategoryList(): Promise<Array<WordCategory>> {
  return await rpcOrEmptyArray('rpc_admin_kefu_word_category_list', {} as any) as Array<WordCategory>
}

/**
 * 保存话术分类
 */
export async function saveWordCategory(id: string | null, name: string, sort: number): Promise<string | null> {
  const res = await rpcOrValue('rpc_admin_kefu_word_category_save', { p_id: id, p_name: name, p_sort: sort } as any)
  return res != null ? String(res) : null
}

/**
 * 删除话术分类
 */
export async function deleteWordCategory(id: string): Promise<boolean> {
  const ok = await rpcOrValue('rpc_admin_kefu_word_category_delete', { p_id: id } as any)
  return ok === true
}

/**
 * 获取快捷话术列表
 */
export async function fetchKefuWordList(categoryId: string | null = null, search: string | null = null): Promise<Array<KefuWord>> {
  return await rpcOrEmptyArray('rpc_admin_kefu_word_list', { p_category_id: categoryId, p_search: search } as any) as Array<KefuWord>
}

/**
 * 保存快捷话术
 */
export async function saveKefuWord(
  id: string | null,
  categoryId: string,
  title: string,
  content: string,
  sort: number
): Promise<string | null> {
  const res = await rpcOrValue('rpc_admin_kefu_word_save', {
    p_id: id,
    p_category_id: categoryId,
    p_title: title,
    p_content: content,
    p_sort: sort
  } as any)
  return res != null ? String(res) : null
}

/**
 * 删除快捷话术
 */
export async function deleteKefuWord(id: string): Promise<boolean> {
  const ok = await rpcOrValue('rpc_admin_kefu_word_delete', { p_id: id } as any)
  return ok === true
}

/**
 * 获取用户留言列表
 */
export async function fetchFeedbackPage(
  page: number,
  pageSize: number,
  search: string | null = null,
  status: number | null = null
): Promise<{ total: number, items: Array<KefuFeedback> }> {
  const res = await rpcOrNull('rpc_admin_kefu_feedback_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<KefuFeedback> }
  return {
    total: (res as any).total as number,
    items: (res as any).items as Array<KefuFeedback>
  }
}

/**
 * 处理用户留言
 */
export async function processFeedback(id: string, replyContent: string): Promise<boolean> {
  const ok = await rpcOrValue('rpc_admin_kefu_feedback_process', { p_id: id, p_reply_content: replyContent } as any)
  return ok === true
}

/**
 * 获取自动回复列表
 */
export async function fetchAutoReplyPage(
  page: number,
  pageSize: number,
  search: string | null = null
): Promise<{ total: number, items: Array<AutoReply> }> {
  const res = await rpcOrNull('rpc_admin_kefu_auto_reply_list', {
    p_page: page,
    p_page_size: pageSize,
    p_search: search
  } as UTSJSONObject)

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

/**
 * 保存自动回复配置
 */
export async function saveAutoReply(
  id: string | null,
  keyword: string,
  content: string,
  replyType: string,
  status: number
): Promise<string | null> {
  const res = await rpcOrValue('rpc_admin_kefu_auto_reply_save', {
    p_id: id,
    p_keyword: keyword,
    p_content: content,
    p_reply_type: replyType,
    p_status: status
  } as any)
  return res != null ? String(res) : null
}

/**
 * 删除自动回复
 */
export async function deleteAutoReply(id: string): Promise<boolean> {
  const ok = await rpcOrValue('rpc_admin_kefu_auto_reply_delete', { p_id: id } as any)
  return ok === true
}

/**
 * 设置自动回复状态
 */
export async function setAutoReplyStatus(id: string, status: number): Promise<boolean> {
  const ok = await rpcOrValue('rpc_admin_kefu_auto_reply_set_status', { p_id: id, p_status: status } as any)
  return ok === true
}

/**
 * 获取客服配置
 */
export async function getKefuSettings(): Promise<KefuConfig | null> {
  const res = await rpcOrValue('rpc_admin_system_config_get', { p_key: 'kefu_settings' } as any)
  return res as KefuConfig | null
}

/**
 * 保存客服配置
 */
export async function saveKefuSettings(config: KefuConfig): Promise<boolean> {
  return await rpcOrValue('rpc_admin_system_config_save', { p_key: 'kefu_settings', p_value: config } as any) === true
}
