import { request } from '@/api/request'
import type { Document } from './document'
import type { Faq } from './faq'
import type { KbReply } from './types'

export type ResourceType = 'document' | 'faq'

/** 收藏记录。 */
export interface Favorite {
  id: number
  created_at: string
  resource_type: ResourceType
  resource_id: number
  resource_name: string
  remarks: string
  is_deleted: boolean
  resource_data?: Document | Faq
}

export interface FavoritePage {
  total: number
  page: number
  page_size: number
  data: Favorite[]
}

export interface FetchFavoriteListParams {
  page?: number
  page_size?: number
  resource_type?: ResourceType
}

export interface CollectParams {
  resource_type: ResourceType
  resource_id: number
  remarks?: string
}

export type UncollectParams = Omit<CollectParams, 'remarks'>

/** 获取收藏列表。 */
export const fetchFavoriteList = (params: FetchFavoriteListParams = {}) =>
  request.get<KbReply<FavoritePage>>('/Kb/v1/favorite/list', { params })

/** 收藏资源。 */
export const collectResource = (data: CollectParams) => request.post<KbReply<Favorite>>('/Kb/v1/favorite/collect', data)

/** 取消收藏。 */
export const uncollectResource = (data: UncollectParams) => request.post<KbReply<string>>('/Kb/v1/favorite/uncollect', data)
