import { request } from "@/api/request";

export type ReviewStatsResourceType = "all" | "document" | "faq";

/** 审核统计接口返回的 data 字段 */
export interface ReviewStatsPayload {
  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 DocumentResource {
  id: number;
  created_at: string;
  updated_at: string;
  doc_no: string;
  title: string;
  description: string;
  content: string;
  type: string;
  status: string;
  category_id: number;
  sub_category: string;
  author_id: number;
  author_name: string;
  reviewer_id: number;
  reviewer_name: string;
  reviewed_at: string | null;
  published_at: string | null;
  publisher_id: number;
  view_count: number;
  like_count: number;
  comment_count: number;
  download_count: number;
  version: string;
  version_notes: string;
  tags: string;
  attachments: string | null;
  related_docs: string | null;
  metadata: string | null;
  keywords: string;
  remarks: string;
}

/** FAQ资源类型 */
export interface FaqResource {
  id: number;
  created_at: string;
  updated_at: string;
  faq_no: string;
  question: string;
  answer: string;
  status: string;
  priority: string;
  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 | null;
  related_docs: string | null;
  related_links: string | null;
  attachments: string | null;
  keywords: string;
  applicable_scope: string;
  remarks: string;
}

/** 审核列表项（resource_type 为 all 时） */
export interface ReviewListItem {
  type: "document" | "faq";
  resource: DocumentResource | FaqResource;
}

/** 分页响应类型 */
export interface PaginatedResponse<T> {
  total: number;
  page: number;
  page_size: number;
  data: T[];
}

/** API响应包装类型 */
export interface ApiResponse<T = any> {
  code: number;
  message: string;
  data: T;
}

/** 获取审核列表参数 */
export interface FetchReviewListParams {
  page?: number;
  page_size?: number;
  resource_type?: ReviewStatsResourceType;
}

/** 审核通过参数 */
export interface ApproveParams {
  resource_type: "document" | "faq";
  id: number;
}

/** 审核拒绝参数 */
export interface RejectParams {
  resource_type: "document" | "faq";
  id: number;
  reason?: string;
}

/** 按当前登录用户统计需要本人审核的数量（不含本人为作者的稿件） */
export const fetchReviewStats = (params?: { resource_type?: ReviewStatsResourceType }) =>
  request.get("/Kb/v1/review/stats", params ? { params } : undefined);

/** 获取待审核列表 */
export const fetchReviewList = (params?: FetchReviewListParams) =>
  request.get<ApiResponse<PaginatedResponse<ReviewListItem | DocumentResource | FaqResource>>>("/Kb/v1/review/list", { params });

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

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

/** 获取文档详情 */
export const fetchDocumentDetail = (id: number) =>
  request.get<ApiResponse<DocumentResource>>(`/Kb/v1/document/${id}`);

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

/** 资源类型选项 */
export const resourceTypeOptions = [
  { label: '全部', value: 'all' },
  { label: '文档', value: 'document' },
  { label: 'FAQ', value: 'faq' },
];

/** 获取资源类型文本 */
export const getResourceTypeText = (type: string): string => {
  const typeMap: Record<string, string> = {
    document: '文档',
    faq: 'FAQ',
  };
  return typeMap[type] || type;
};

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