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

/**
 * 文章分类类型
 */
export type ArticleCategory = {
  id: string
  name: string
  icon: string | null
  sort: number
  status: number
  created_at: string
  updated_at: string
}

/**
 * 文章简要信息类型 (列表使用)
 */
export type ArticleItem = {
  id: string
  category_id: string
  category_name: string
  title: string
  author: string | null
  image: string | null
  description: string | null
  status: number
  views: number
  is_banner: boolean
  is_hot: boolean
  created_at: string
  updated_at: string
}

/**
 * 文章详情类型
 */
export type ArticleDetail = {
  id: string
  category_id: string
  category_name: string
  title: string
  author: string | null
  image: string | null
  description: string | null
  content: string
  status: number
  views: number
  is_banner: boolean
  is_hot: boolean
  linked_product_id: string | null
  created_at: string
  updated_at: string
}

/**
 * 获取文章分类列表
 */
export async function fetchArticleCategoryPage(
  page : number,
  pageSize : number,
  search : string | null = null
) : Promise<{ total : number, items : Array<ArticleCategory> }> {
  const res = await rpcOrNull('rpc_admin_article_category_list', {
    p_page: page,
    p_page_size: pageSize,
    p_search: search
  } as UTSJSONObject)

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

/**
 * 保存文章分类
 */
export async function saveArticleCategory(
  id : string | null,
  name : string,
  icon : string | null,
  sort : number,
  status : number
) : Promise<string | null> {
  const res = await rpcOrValue('rpc_admin_article_category_save', {
    p_id: id,
    p_name: name,
    p_icon: icon,
    p_sort: sort,
    p_status: status
  } as any)
  return res != null ? String(res) : null
}

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

/**
 * 设置文章分类状态
 */
export async function setArticleCategoryStatus(id : string, status : number) : Promise<boolean> {
  const ok = await rpcOrValue('rpc_admin_article_category_set_status', { p_id: id, p_status: status } as any)
  return ok === true
}

/**
 * 获取文章列表
 */
export async function fetchArticlePage(
  page : number,
  pageSize : number,
  categoryId : string | null = null,
  status : number | null = null,
  search : string | null = null
) : Promise<{ total : number, items : Array<ArticleItem> }> {
  const res = await rpcOrNull('rpc_admin_article_list', {
    p_page: page,
    p_page_size: pageSize,
    p_category_id: categoryId,
    p_status: status,
    p_search: search
  } as UTSJSONObject)

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

/**
 * 获取文章详情
 */
export async function fetchArticleDetail(id : string) : Promise<ArticleDetail | null> {
  const res = await rpcOrNull('rpc_admin_article_get_detail', { p_id: id } as any)
  return res as ArticleDetail | null
}

/**
 * 保存文章内容
 */
export async function saveArticle(payload : any) : Promise<string | null> {
  const res = await rpcOrValue('rpc_admin_article_save', {
    p_id: payload.id ?? null,
    p_category_id: payload.category_id,
    p_title: payload.title,
    p_author: payload.author ?? null,
    p_image: payload.image ?? null,
    p_description: payload.description ?? null,
    p_content: payload.content,
    p_status: payload.status ?? 0,
    p_is_banner: payload.is_banner ?? false,
    p_is_hot: payload.is_hot ?? false,
    p_linked_product_id: payload.linked_product_id ?? null
  } as any)
  return res != null ? String(res) : null
}

/**
 * 删除文章记录
 */
export async function deleteArticle(id : string) : Promise<boolean> {
  const ok = await rpcOrValue('rpc_admin_article_delete', { p_id: id } as any)
  return ok === true
}

/**
 * 设置文章发布状态
 */
export async function setArticleStatus(id : string, status : number) : Promise<boolean> {
  const ok = await rpcOrValue('rpc_admin_article_set_status', { p_id: id, p_status: status } as any)
  return ok === true
}
