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

/** 文档状态 */
export type DocumentStatus = 'draft' | 'published' | 'reviewed' | 'rejected';

/** 文档类型 */
export type DocumentType = 'common' | 'guide' | 'solution' | 'troubleshoot' | 'process' | 'technical';

/** 文档接口类型 */
export interface Document {
  id: number;
  created_at: string;
  updated_at: string;
  doc_no: string;
  title: string;
  description: string;
  content: string;
  type: DocumentType;
  status: DocumentStatus;
  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;
  is_favorited?: boolean;
}

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

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

/** 创建文档请求参数 */
export interface CreateDocumentParams {
  title: string;
  description?: string;
  content: string;
  type?: DocumentType;
  category_id?: number;
  sub_category?: string;
  keywords?: string;
  tags?: string;
  remarks?: string;
}

/** 更新文档请求参数 */
export interface UpdateDocumentParams {
  id: number;
  title?: string;
  description?: string;
  content?: string;
  type?: DocumentType;
  category_id?: number;
  sub_category?: string;
  keywords?: string;
  tags?: string;
  remarks?: string;
}

/** 获取文档列表参数 */
export interface FetchDocumentListParams {
  page?: number;
  page_size?: number;
  keyword?: string;
  type?: DocumentType;
  status?: DocumentStatus;
  category_id?: number;
}

/** 文档类型选项 */
export const documentTypeOptions = [
  { label: '通用文档', value: 'common' },
  { label: '操作指南', value: 'guide' },
  { label: '解决方案', value: 'solution' },
  { label: '故障排查', value: 'troubleshoot' },
  { label: '流程规范', value: 'process' },
  { label: '技术文档', value: 'technical' },
];

/** 文档状态选项 */
export const documentStatusOptions = [
  { label: '草稿', value: 'draft' },
  { label: '已发布', value: 'published' },
  { label: '已审核', value: 'reviewed' },
  { label: '未通过审核', value: 'rejected' },
];

/** 获取文档状态文本 */
export const getDocumentStatusText = (status: DocumentStatus): string => {
  const statusMap: Record<DocumentStatus, string> = {
    draft: '草稿',
    published: '已发布',
    reviewed: '已审核',
    rejected: '未通过审核',
  };
  return statusMap[status] || status;
};

/** 获取文档状态颜色 */
export const getDocumentStatusColor = (status: DocumentStatus): string => {
  const colorMap: Record<DocumentStatus, string> = {
    draft: 'gray',
    published: 'blue',
    reviewed: 'green',
    rejected: 'red',
  };
  return colorMap[status] || 'gray';
};

/** 获取文档类型文本 */
export const getDocumentTypeText = (type: DocumentType): string => {
  const typeMap: Record<DocumentType, string> = {
    common: '通用文档',
    guide: '操作指南',
    solution: '解决方案',
    troubleshoot: '故障排查',
    process: '流程规范',
    technical: '技术文档',
  };
  return typeMap[type] || type;
};

/** 创建文档 */
export const createDocument = (data: CreateDocumentParams) => {
  return request.post<ApiResponse<Document>>("/Kb/v1/document/create", data);
};

/** 更新文档 */
export const updateDocument = (data: UpdateDocumentParams) => {
  return request.post<ApiResponse<Document>>("/Kb/v1/document/update", data);
};

/** 删除文档（移入回收站） */
export const deleteDocument = (id: number) => {
  return request.delete<ApiResponse<string>>(`/Kb/v1/document/${id}`);
};

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

/** 获取文档列表 */
export const fetchDocumentList = (params?: FetchDocumentListParams) => {
  return request.get<ApiResponse<PaginatedResponse<Document>>>("/Kb/v1/document/list", { params });
};

/** 发布文档 */
export const publishDocument = (id: number) => {
  return request.post<ApiResponse<string>>("/Kb/v1/document/publish", { id });
};

/** 移入回收站 */
export const moveToTrash = (resourceId: number, resourceType: string) => {
  return request.post<ApiResponse<string>>("/Kb/v1/trash/move", {
    resource_id: resourceId,
    resource_type: resourceType,
  });
};

/** 获取我的文档列表（由我创建的所有文档） */
export const fetchMyDocumentList = (params?: FetchDocumentListParams) => {
  return request.get<ApiResponse<PaginatedResponse<Document>>>("/Kb/v1/review/publish/list", { params });
};

/** 获取已审核通过的文档列表 */
export const fetchApprovedDocumentList = (params?: FetchDocumentListParams) => {
  return request.get<ApiResponse<PaginatedResponse<Document>>>("/Kb/v1/review/approved/list", { params });
};

/** 收藏文档 */
export const favoriteDocument = (id: number, remarks?: string) => {
  return request.post<ApiResponse<string>>("/Kb/v1/favorite/collect", {
    resource_type: 'document',
    resource_id: id,
    remarks,
  });
};

/** 取消收藏文档 */
export const unfavoriteDocument = (id: number) => {
  return request.post<ApiResponse<string>>("/Kb/v1/favorite/uncollect", {
    resource_type: 'document',
    resource_id: id,
  });
};


/** 下载文档 */
export const downloadDocument = (id: number) => {
  return request.get<Blob>(`/Kb/v1/document/${id}/download`, { responseType: 'blob' });
};