import request from '/@/utils/request';

// 字典映射查询参数
export interface FieldMappingQueryParams {
	configName?: string;
	vendorName?: string;
	apiName?: string;
	businessDomain?: string;
	isActive?: boolean;
	pageNum?: number;
	pageSize?: number;
}

// 创建字典映射参数
export interface CreateFieldMappingParams {
	configName: string;
	vendorName: string;
	apiName: string;
	apiVersion?: string;
	sourceField: string;
	sourceFieldType?: string;
	sourceFieldDesc?: string;
	targetField: string;
	targetFieldType?: string;
	targetFieldDesc?: string;
	transformType?: string;
	transformParams?: any;
	validationRules?: any;
	defaultValue?: string;
	isRequired?: boolean;
	isActive?: boolean;
	priority?: number;
	businessDomain?: string;
	fieldGroup?: string;
	configVersion?: number;
	effectiveDate?: string;
	expiryDate?: string;
	createdBy?: string;
}

// 更新字典映射参数
export interface UpdateFieldMappingParams extends Partial<CreateFieldMappingParams> {
	id: string;
}

// 字典映射信息（响应字段）
export interface FieldMappingInfo {
	id: string;
	configName: string;
	vendorName: string;
	apiName: string;
	apiVersion?: string;
	sourceField: string;
	sourceFieldType?: string;
	sourceFieldDesc?: string;
	targetField: string;
	targetFieldType?: string;
	targetFieldDesc?: string;
	transformType?: string;
	transformTypeName?: string;
	transformParams?: any;
	validationRules?: any;
	defaultValue?: string;
	isRequired?: boolean;
	isActive?: boolean;
	priority?: number;
	businessDomain?: string;
	businessDomainName?: string;
	fieldGroup?: string;
	configVersion?: number;
	effectiveDate?: string;
	expiryDate?: string;
	createdBy?: string;
	createdTime?: string;
	updatedBy?: string;
	updatedTime?: string;
}

// 查询字典映射列表
export function listFieldMappingConfigs(params: FieldMappingQueryParams) {
	return request({
		url: '/field/mapping/config/controller/field-mapping-configs',
		method: 'get',
		params,
	});
}

// 创建字典映射
export function createFieldMappingConfig(data: CreateFieldMappingParams) {
	return request({
		url: '/field/mapping/config/controller/field-mapping-configs',
		method: 'post',
		data,
	});
}

// 根据ID查询字典映射详情
export function getFieldMappingConfig(id: string) {
	return request({
		url: `/field/mapping/config/controller/field-mapping-configs/${id}`,
		method: 'get',
	});
}

// 根据ID修改字典映射
export function updateFieldMappingConfig(id: string, data: UpdateFieldMappingParams) {
	return request({
		url: `/field/mapping/config/controller/field-mapping-configs/${id}`,
		method: 'put',
		data,
	});
}

// 根据ID修改字典映射状态
export function updateFieldMappingConfigStatus(id: string, isActive: boolean) {
	return request({
		url: `/field/mapping/config/controller/field-mapping-configs/${id}/status`,
		method: 'put',
		data: { id, isActive },
	});
}

// 根据ID删除字典映射
export function deleteFieldMappingConfig(id: string) {
	return request({
		url: `/field/mapping/config/controller/field-mapping-configs/${id}`,
		method: 'delete',
	});
}

// 根据厂商和接口查询字段映射（过滤过期）
export interface QueryFieldMappingParams {
	vendorName: string;
	apiName: string;
	apiVersion?: string;
	isActive?: boolean;
}

export function queryFieldMappingConfigs(params: QueryFieldMappingParams) {
	return request({
		url: '/field/mapping/config/controller/field-mapping-configs/query',
		method: 'get',
		params,
	});
}

// 验证字段映射配置的有效性
export interface ValidateFieldMappingParams {
	configName: string;
	vendorName: string;
	apiName: string;
	sourceField: string;
	targetField: string;
}

export function validateFieldMappingConfig(data: ValidateFieldMappingParams) {
	return request({
		url: '/field/mapping/config/controller/field-mapping-configs/validate',
		method: 'post',
		data,
	});
}
