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

export type ReviewResourceType = 'all' | 'document' | 'faq'

/** 统一审核列表项。 */
export interface ReviewListItem {
  type: 'document' | 'faq'
  resource: Document | Faq
}

/** 审核分页结果。 */
export interface ReviewPage {
  total: number
  page: number
  page_size: number
  data: ReviewListItem[]
}

/** 当前审核人的待审数量。 */
export interface ReviewStats {
  need_my_review_document: number
  need_my_review_faq: number
  need_my_review_total: number
  need_my_review_unreviewed_document: number
  need_my_review_unreviewed_faq: number
  need_my_review_unreviewed_total: number
}

/** 审核列表参数。 */
export interface FetchReviewListParams {
  page?: number
  page_size?: number
  resource_type?: ReviewResourceType
}

export interface ApproveParams {
  resource_type: 'document' | 'faq'
  id: number
}

export interface RejectParams extends ApproveParams {
  reason: string
}

/** 获取当前用户可审核的待审资源。 */
export const fetchReviewList = (params: FetchReviewListParams) => request.get<KbReply<ReviewPage>>('/Kb/v1/review/list', { params })

/** 获取当前审核人的待审统计。 */
export const fetchReviewStats = (params: { resource_type?: ReviewResourceType } = {}) =>
  request.get<KbReply<ReviewStats>>('/Kb/v1/review/stats', { params })

/** 审核通过。 */
export const approveReview = (data: ApproveParams) => request.post<KbReply<string>>('/Kb/v1/review/approve', data)

/** 审核拒绝。 */
export const rejectReview = (data: RejectParams) => request.post<KbReply<string>>('/Kb/v1/review/reject', data)

export const resourceTypeOptions: Array<{ label: string; value: ReviewResourceType }> = [
  { label: '全部', value: 'all' },
  { label: '文档', value: 'document' },
  { label: 'FAQ', value: 'faq' },
]

/** 获取审核资源类型文案。 */
export const getResourceTypeText = (type: string) => ({ document: '文档', faq: 'FAQ' })[type] || type

/** 获取审核资源类型颜色。 */
export const getResourceTypeColor = (type: string) => ({ document: 'arcoblue', faq: 'green' })[type] || 'gray'
