import { newService } from '/@/utils/request';

// 文档查询参数
export interface DocumentQueryParams {
	keyword?: string;
	datasetId?: string;
	status?: string;
	fileType?: string;
	pageNum: number;
	pageSize: number;
}

// 创建文档参数
export interface CreateDocumentParams {
	KnowledgeId: number;  // 必传
	filePath: string;     // 必传
	fileSize: number;     // 必传
	format: string;       // 必传
	title: string;        // 必传
}

// 更新文档参数
export interface UpdateDocumentParams {
	id: string;           // 必传
	KnowledgeId?: number; // 可选
	filePath?: string;    // 可选
	fileSize?: number;    // 可选
	format?: string;      // 可选
	title?: string;       // 可选
}

// 文档信息
export interface DocumentInfo {
	id?: string;
	name: string;
	datasetId: string;
	datasetName?: string;
	fileType: string; // pdf, docx, txt, md, html
	fileSize?: number;
	filePath?: string;
	charCount?: number;
	chunkCount?: number;
	status: string; // pending, processing, completed, failed
	indexStatus?: string; // not_indexed, indexing, indexed, failed
	errorMessage?: string;
	createdAt?: string;
	updatedAt?: string;
}

// 文档分段信息
export interface DocumentChunk {
	id: string;
	documentId: string;
	content: string;
	chunkIndex: number;
	charCount: number;
	tokenCount?: number;
	embedding?: number[];
	createdAt?: string;
}

// 获取文档列表
export function listDocuments(params: DocumentQueryParams) {
	return newService({
		url: '/knowledge/document/list',
		method: 'get',
		params,
	});
}

// 获取文档详情
export function getDocument(id: string) {
	return newService({
		url: '/knowledge/document/detail',
		method: 'get',
		params: { id },
	});
}

// 创建文档（JSON格式）
export function createDocument(data: CreateDocumentParams) {
	return newService({
		url: '/knowledge/document/create',
		method: 'post',
		data,
	});
}

// 更新文档
export function updateDocument(data: UpdateDocumentParams) {
	return newService({
		url: '/knowledge/document/update',
		method: 'put',
		data,
	});
}

// 上传文档
export function uploadDocument(data: FormData) {
	return newService({
		url: '/knowledge/document/upload',
		method: 'post',
		data,
		headers: {
			'Content-Type': 'multipart/form-data',
		},
	});
}

// 删除文档
export function deleteDocument(id: string) {
	return newService({
		url: '/knowledge/document/delete',
		method: 'delete',
		params: { id },
	});
}

// 批量删除文档
export function batchDeleteDocuments(ids: string[]) {
	return newService({
		url: '/knowledge/document/batchDelete',
		method: 'delete',
		data: { ids },
	});
}

// 重新处理文档
export function reprocessDocument(id: string) {
	return newService({
		url: '/knowledge/document/reprocess',
		method: 'post',
		params: { id },
	});
}

// 获取文档分段列表
export function listDocumentChunks(params: { documentId: string; pageNum: number; pageSize: number }) {
	return newService({
		url: '/knowledge/document/chunks',
		method: 'get',
		params,
	});
}

// 更新文档分段
export function updateDocumentChunk(data: { id: string; content: string }) {
	return newService({
		url: '/knowledge/document/chunk/update',
		method: 'put',
		data,
	});
}

// 删除文档分段
export function deleteDocumentChunk(id: string) {
	return newService({
		url: '/knowledge/document/chunk/delete',
		method: 'delete',
		params: { id },
	});
}

// 预览文档内容
export function previewDocument(id: string) {
	return newService({
		url: '/knowledge/document/preview',
		method: 'get',
		params: { id },
	});
}
