import request from '/@/utils/request';
import { uploadFile } from '/@/api/common/upload';

// 导出公共上传函数供其他模块使用
export { uploadFile };

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

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

// 更新文档参数
export interface UpdateDocumentParams {
	id: string;
	datasetId?: string;
	filePath?: string;
	fileSize?: number;
	format?: string;
	title?: string;
	status?: number;
}

// 文档分段查询参数
export interface DocumentChunkQueryParams {
	documentId: string;
	pageNum: number;
	pageSize: number;
}

// 文档信息
export interface DocumentInfo {
	id?: string;
	name: string;
	datasetId: string;
	fileType: string;
	fileSize?: number;
	filePath?: string;
	chunkCount?: number;
	parseStatus?: string;
	enabled?: boolean;
	createdAt?: string;
	updatedAt?: string;
}

// 文件块向量查询参数
export interface DocumentVectorQueryParams {
	documentId?: string;
	datasetId?: string;
	pageNum: number;
	pageSize: number;
}

// 文件块向量信息
export interface DocumentVectorInfo {
	id: number;
	status: number;
	vectorStatus: number;
	datasetId: number;
	documentId: number;
	content: string;
	contentHash: string;
	chunkIndex: number;
	vector: number[];
	metadata: Record<string, any>;
	createdAt: string;
	updatedAt: string;
}

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

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

// 创建文档
export function createDocument(data: CreateDocumentParams) {
	return request({
		url: '/rag/document/create',
		method: 'post',
		data,
	});
}

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

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

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

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

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

// 重新处理文档
export function reprocessDocument(id: string) {
	return request({
		url: '/rag/document/reprocessDocument',
		method: 'post',
		data: { id },
	});
}

// 获取文档分段列表
export function listDocumentChunks(params: DocumentChunkQueryParams) {
	return request({
		url: '/rag/document/listDocumentChunk',
		method: 'get',
		params,
	});
}

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

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

// 获取文件向量化处理进度
export function getDocumentProcess(id: string) {
	return request({
		url: '/rag/document/getProcess',
		method: 'get',
		params: { id },
	});
}

// 生成向量
export function generateVector(id: string, datasetId: string) {
	return request({
		url: '/rag/document/vectorization',
		method: 'post',
		data: { id, datasetId },
	});
}

// 获取文件块向量列表
export function listDocumentVectors(params: DocumentVectorQueryParams) {
	return request({
		url: '/rag/document/vector/list',
		method: 'get',
		params,
	});
}

// 更新文件块
export function updateDocumentVector(data: any) {
	return request({
		url: '/rag/document/vector/update',
		method: 'put',
		data,
	});
}

// 获取任务列表
export function listTasks() {
	return request({
		url: '/rag/task/get',
		method: 'get',
	});
}

// 重新执行任务
export function reexecuteTask(id: string) {
	return request({
		url: '/rag/task/reexecute',
		method: 'post',
		data: { id },
	});
}
