import { request } from '@/api/request'
import type { KbReply } from './types'

export { fetchCategoryTree as fetchGeneralCategoryTree } from './category'
export type { CategoryTreeNode } from './category'
export type { KbReply } from './types'

/** FAQ 审核状态。 */
export type FaqStatus = 'draft' | 'published' | 'reviewed' | 'rejected'
/** FAQ 优先级。 */
export type FaqPriority = 'low' | 'medium' | 'high'
/** FAQ 列表可见范围。 */
export type FaqScope = 'my' | 'all'

/** FAQ 资源。 */
export interface Faq {
  id: number
  created_at: string
  updated_at: string
  faq_no: string
  question: string
  answer: string
  status: FaqStatus
  priority: FaqPriority
  category_id: number
  sub_category: string
  problem_type: string
  solution: string
  process_steps: string
  prerequisites: string
  author_id: number
  author_name: string
  reviewer_id: number
  reviewer_name: string
  reviewed_at: string | null
  published_at: string | null
  view_count: number
  use_count: number
  helpful_count: number
  useless_count: number
  tags: string
  related_faqs: string
  related_docs: string
  related_links: string
  detection_point_ids: string
  attachments: string
  keywords: string
  applicable_scope: string
  remarks: string
  is_favorited: boolean
}

/** FAQ 分页结果。 */
export interface FaqPage {
  total: number
  page: number
  page_size: number
  data: Faq[]
}

/** FAQ 列表查询参数。 */
export interface FaqListParams {
  page?: number
  page_size?: number
  scope?: FaqScope
  keyword?: string
  category_id?: number
  status?: FaqStatus
  priority?: FaqPriority
  problem_type?: string
}

/** FAQ 创建和编辑字段。 */
export interface FaqFormData {
  question: string
  answer: string
  priority: FaqPriority
  category_id: number
  sub_category: string
  problem_type: string
  solution: string
  process_steps: string
  prerequisites: string
  keywords: string
  tags: string
  detection_point_ids: string
  applicable_scope: string
  remarks: string
}

/** FAQ 编辑请求。 */
export interface UpdateFaqData extends FaqFormData {
  id: number
}

/** FAQ 状态筛选项。 */
export const faqStatusOptions: Array<{ label: string; value: FaqStatus }> = [
  { label: '草稿', value: 'draft' },
  { label: '待审核', value: 'published' },
  { label: '已审核', value: 'reviewed' },
  { label: '已拒绝', value: 'rejected' },
]

/** FAQ 优先级筛选项。 */
export const faqPriorityOptions: Array<{ label: string; value: FaqPriority }> = [
  { label: '低', value: 'low' },
  { label: '中', value: 'medium' },
  { label: '高', value: 'high' },
]

/** 常用问题类型；接口仍允许自由字符串。 */
export const faqProblemTypeOptions = [
  { label: '故障', value: '故障' },
  { label: '咨询', value: '咨询' },
  { label: '请求', value: '请求' },
  { label: '其他', value: '其他' },
]

const faqStatusMeta: Record<FaqStatus, { text: string; color: string }> = {
  draft: { text: '草稿', color: 'gray' },
  published: { text: '待审核', color: 'orange' },
  reviewed: { text: '已审核', color: 'green' },
  rejected: { text: '已拒绝', color: 'red' },
}

const faqPriorityMeta: Record<FaqPriority, { text: string; color: string }> = {
  low: { text: '低', color: 'gray' },
  medium: { text: '中', color: 'blue' },
  high: { text: '高', color: 'orange' },
}

/** 获取 FAQ 状态文案。 */
export const getFaqStatusText = (status: FaqStatus | string) => faqStatusMeta[status as FaqStatus]?.text || status || '-'

/** 获取 FAQ 状态标签颜色。 */
export const getFaqStatusColor = (status: FaqStatus | string) => faqStatusMeta[status as FaqStatus]?.color || 'gray'

/** 获取 FAQ 优先级文案。 */
export const getFaqPriorityText = (priority: FaqPriority | string) => faqPriorityMeta[priority as FaqPriority]?.text || priority || '-'

/** 获取 FAQ 优先级标签颜色。 */
export const getFaqPriorityColor = (priority: FaqPriority | string) => faqPriorityMeta[priority as FaqPriority]?.color || 'gray'

/** 获取 FAQ 列表。 */
export const fetchFaqList = (params: FaqListParams) =>
  request.get<KbReply<FaqPage>>('/Kb/v1/faq/list', {
    params,
  })

/** 获取 FAQ 详情。 */
export const fetchFaqDetail = (id: number) => request.get<KbReply<Faq>>(`/Kb/v1/faq/${id}`)

/** 创建 FAQ 草稿。 */
export const createFaq = (data: FaqFormData) => request.post<KbReply<Faq>>('/Kb/v1/faq/create', data)

/** 更新 FAQ。 */
export const updateFaq = (data: UpdateFaqData) => request.post<KbReply<Faq>>('/Kb/v1/faq/update', data)

/** 提交 FAQ 审核。 */
export const publishFaq = (id: number) => request.post<KbReply<string>>('/Kb/v1/faq/publish', { id })

/** 删除 FAQ 并移入回收站。 */
export const deleteFaq = (id: number) => request.delete<KbReply<string>>(`/Kb/v1/faq/${id}`)

/** 收藏 FAQ。 */
export const favoriteFaq = (id: number) =>
  request.post<KbReply<unknown>>('/Kb/v1/favorite/collect', {
    resource_type: 'faq',
    resource_id: id,
  })

/** 取消收藏 FAQ。 */
export const unfavoriteFaq = (id: number) =>
  request.post<KbReply<string>>('/Kb/v1/favorite/uncollect', {
    resource_type: 'faq',
    resource_id: id,
  })
