import request, { type RequestOptions } from '/@/utils/request';

export interface CreationListParams {
	pageNum: number;
	pageSize: number;
	keyword?: string;
}

export interface NodeLibraryFormItem {
	field: string;
	label: string;
	type: 'input' | 'number' | 'textarea' | 'switch' | string;
	required: boolean;
	default?: string | number | boolean;
	value?: unknown;
	options?: Array<{ label: string; value: string | number }> | null;
	expand?: NodeLibraryFormItem[] | Record<string, unknown> | null;
	fieldConstraint?: {
		fileTypes?: string;
		maxFileSize?: number;
		maxFileCount?: number;
		minLength?: number;
		maxLength?: number;
		numberType?: 'integer' | 'decimal' | string;
		minValue?: number;
		maxValue?: number;
	} | null;
}

export interface NodeLibraryModelConfig {
	modelName: string;
	modelForm: NodeLibraryFormItem[];
}

export interface NodeLibraryItem {
	nodeCode: string;
	nodeName: string;
	modelType: number;
	skillOption: boolean;
	promptOption: boolean;
	isSaveFile: boolean;
	formConfig: NodeLibraryFormItem[];
	modelConfig: NodeLibraryModelConfig[];
}

export interface NodeLibraryGroup {
	group: string;
	label: string;
	items: NodeLibraryItem[];
}

export interface NodeLibraryListResponse {
	code: number;
	message: string;
	data: {
		groups: NodeLibraryGroup[];
	};
}

export interface CreationImageItem {
	name: string;
	url: string;
}

export interface CreationTitleItem {
	title: string;
	htmlFileUrl: string;
	imageUrls: CreationImageItem[] | null;
}

export interface CreationThemeItem {
	theme: string;
	titles: CreationTitleItem[];
}

export interface CreationContentTypeItem {
	contentType: string;
	themes: CreationThemeItem[];
}

export interface CreationTreeItem {
	createdDate: string;
	contentTypes: CreationContentTypeItem[];
}

// 新的执行列表数据结构
export interface ExecutionItem {
	timestamp: string;
	content: string;
	label: string;
}

export interface ExecutionFlowItem {
	flowName: string;
	Id?: number | string;
	sessionId?: string;
	items: ExecutionItem[];
}

export interface ExecutionTreeItem {
	createDate: string;
	flows: ExecutionFlowItem[];
}

export interface CreationListData {
	list: unknown[] | null;
	total: number;
	Tree: CreationTreeItem[];
	imgAddressPrefix: string;
}

export interface ExecutionListData {
	tree: ExecutionTreeItem[];
	imgAddressPrefix: string;
}

export interface CreationListResponse {
	code: number;
	message: string;
	data: CreationListData;
}

export interface ExecutionListResponse {
	code: number;
	message: string;
	data: ExecutionListData;
}

export interface CreationSubmitParams {
	mode: string;
	content_type: string;
	theme: string;
	title: string;
	description?: string;
	style: string;
	count: number;
	image_per_post: number;
	image_ratio: string;
}

export interface DownloadToFileParams {
	fileURL: string;
}

export function getCreationList(params: CreationListParams, requestOptions?: RequestOptions) {
	return request({
		url: '/black-deacon/creation/info/list',
		method: 'get',
		params,
		requestOptions,
	}) as Promise<CreationListResponse>;
}

export function getExecutionList(requestOptions?: RequestOptions) {
	return request({
		url: '/ai-agent/flow/execution/list',
		method: 'get',
		requestOptions,
	}) as Promise<ExecutionListResponse>;
}

export function getNodeLibraryList(requestOptions?: RequestOptions) {
	return request({
		url: '/ai-agent/node/library/list',
		method: 'get',
		requestOptions,
	}) as Promise<NodeLibraryListResponse>;
}

export function createCreation(data: CreationSubmitParams, requestOptions?: RequestOptions) {
	return request({
		url: '/black-deacon/creation/info/creation',
		method: 'post',
		data,
		timeout: 0,
		requestOptions,
	});
}

export function downloadToFile(data: DownloadToFileParams, requestOptions?: RequestOptions) {
	return request({
		url: '/oss/file/downloadToBrowser',
		method: 'post',
		data,
		responseType: 'blob',
		requestOptions,
	});
}

export function saveWorkflow(data: { flowName: string; description: string; flowContent: any }, requestOptions?: RequestOptions) {
	return request({
		url: '/ai-agent/flow/user/create',
		method: 'post',
		data,
		requestOptions,
	});
}

export interface WorkflowItem {
	id: string;
	flowName?: string;
	flowTemplateName?: string;
	description: string;
	flowContent: any;
	nodeInputParams?: any[];
	outputParams?: Array<Record<string, string>>;
	imgAddressPrefix?: string;
	fileUrls?: string[];
	resultUrl?: string;
	sessionId?: string;
}

export interface WorkflowListResponse {
	code: number;
	message: string;
	data: {
		listFlowUserRes: {
			list: WorkflowItem[];
			total: number;
		};
		listFlowTemplateRes: {
			list: WorkflowItem[];
			total: number;
		};
		isAdmin?: boolean;
	};
}

export function getWorkflowList(requestOptions?: RequestOptions) {
	return request({
		url: '/ai-agent/flow/user/list',
		method: 'get',
		requestOptions,
	}) as Promise<WorkflowListResponse>;
}

export interface WorkflowDetailResponse {
	code: number;
	message: string;
	data: WorkflowItem;
}

export function getWorkflowDetail(id: string, requestOptions?: RequestOptions) {
	return request({
		url: '/ai-agent/flow/user/get',
		method: 'get',
		params: { id },
		requestOptions,
	}) as Promise<WorkflowDetailResponse>;
}

export function getExecutionDetail(id: string, requestOptions?: RequestOptions) {
	return request({
		url: '/ai-agent/flow/execution/get',
		method: 'get',
		params: { id },
		requestOptions,
	}) as Promise<WorkflowDetailResponse>;
}

export function updateWorkflow(data: { id: string; flowName: string; description: string; flowContent: any }, requestOptions?: RequestOptions) {
	return request({
		url: '/ai-agent/flow/user/update',
		method: 'put',
		data,
		requestOptions,
	});
}

export function deleteWorkflow(id: string, requestOptions?: RequestOptions) {
	return request({
		url: '/ai-agent/flow/user/delete',
		method: 'delete',
		params: { id },
		requestOptions,
	});
}

// 执行工作流相关类型定义
export interface FlowNodeFormField {
	default?: any;
	field?: string;
	label?: string;
	options?: { label?: string; value?: string }[];
	required?: boolean;
	type?: string;
	value?: string;
}

export interface FlowNodeInputSource {
	field?: string[];
	nodeId?: string;
	quoteOutput?: boolean;
}

export interface FlowNodeModelItem {
	modelApiKey?: string;
	modelForm?: FlowNodeFormField[];
	modelName?: string;
}

export interface FlowNode {
	config?: { [key: string]: any };
	formConfig?: FlowNodeFormField[];
	id?: string;
	inputSource?: FlowNodeInputSource[];
	modelConfig?: FlowNodeModelItem;
	name?: string;
	nodeCode?: string;
	outputResult?: FlowNodeFormField[];
	skillName?: string;
}

export interface FlowEdge {
	from?: string;
	id?: string;
	to?: string;
}

export interface FlowInfo {
	edges?: FlowEdge[];
	nodes?: FlowNode[];
	startNodeId?: string;
	version?: string;
}

export interface ExecuteFlowParams {
	desc?: string;
	fileUrl?: string[];
	flowContent?: FlowInfo;
	flowId?: string;
	flowName?: string;
	nodeInputParams?: FlowNode[];
	sessionId?: string;
	skillName?: string;
	resultUrl?: string;
}

export function executeFlow(data: ExecuteFlowParams | FormData, requestOptions?: RequestOptions) {
	return request({
		url: '/ai-agent/flow/execution/execute',
		method: 'post',
		data,
		headers: data instanceof FormData ? { 'Content-Type': 'multipart/form-data' } : undefined,
		timeout: 0,
		requestOptions,
	});
}
