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

export type AdminUserLevel = {
  id: string
  name: string
  level_weight: number
  min_experience: number
  discount_percent: number
  is_visible: boolean
  status: number
  icon_url?: string | null
  bg_image_url?: string | null
  bg_style_json?: any
  remark?: string | null
  created_at?: string | null
  updated_at?: string | null
  deleted_at?: string | null
}

export type AdminUserLevelPageResult = {
  total: number
  items: Array<AdminUserLevel>
}

export type AdminUserLevelPageFilters = {
  search?: string
  status?: number | null
  isVisible?: boolean | null
  includeDeleted?: boolean
}

export async function fetchAdminUserLevelPage(
  page: number,
  pageSize: number,
  filters: AdminUserLevelPageFilters = {}
): Promise<AdminUserLevelPageResult> {
  const res = await rpcOrNull('rpc_admin_user_level_list', {
    p_page: page,
    p_page_size: pageSize,
    p_search: filters.search ?? null,
    p_status: filters.status ?? null,
    p_is_visible: filters.isVisible ?? null,
    p_include_deleted: filters.includeDeleted ?? false
  } as any)

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

  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: items as Array<AdminUserLevel> }
}

export type SaveAdminUserLevelInput = {
  id?: string | null
  name: string
  level_weight: number
  min_experience: number
  discount_percent: number
  is_visible: boolean
  status: number
  icon_url?: string | null
  bg_image_url?: string | null
  bg_style_json?: any
  remark?: string | null
}

export async function saveAdminUserLevel(input: SaveAdminUserLevelInput): Promise<string | null> {
  const res = await rpcOrValue('rpc_admin_user_level_save', {
    p_id: input.id ?? null,
    p_name: input.name,
    p_level_weight: input.level_weight,
    p_min_experience: input.min_experience,
    p_discount_percent: input.discount_percent,
    p_is_visible: input.is_visible,
    p_status: input.status,
    p_icon_url: input.icon_url ?? null,
    p_bg_image_url: input.bg_image_url ?? null,
    p_bg_style_json: input.bg_style_json ?? null,
    p_remark: input.remark ?? null
  } as any)

  return res != null ? String(res) : null
}

export async function deleteAdminUserLevel(id: string): Promise<boolean> {
  const ok = await rpcOrValue('rpc_admin_user_level_delete', {
    p_id: id
  } as any)
  return ok === true
}

export async function setAdminUserLevelVisible(id: string, isVisible: boolean): Promise<boolean> {
  const ok = await rpcOrValue('rpc_admin_user_level_set_visible', {
    p_id: id,
    p_is_visible: isVisible
  } as any)
  return ok === true
}

export async function setAdminUserLevelStatus(id: string, status: number): Promise<boolean> {
  const ok = await rpcOrValue('rpc_admin_user_level_set_status', {
    p_id: id,
    p_status: status
  } as any)
  return ok === true
}
