<template>
	<el-dialog v-model="visible" title="选择模型" width="1000px" :close-on-click-modal="false" @close="handleClose">
		<div class="model-selector-header">
			<div class="search-bar">
				<el-input v-model="searchParams.modelName" placeholder="搜索模型名称" clearable @clear="handleSearch">
					<template #prefix
						><el-icon><Search /></el-icon
					></template>
				</el-input>
				<el-button type="primary" @click="handleSearch">搜索</el-button>
			</div>
			<el-button type="success" @click="handleAddModel">+ 新建模型</el-button>
		</div>

		<div class="model-list" v-loading="loading">
			<el-empty v-if="!loading && modelList.length === 0" description="暂无模型数据" :image-size="100" />
			<div v-else class="model-grid">
				<div
					v-for="model in modelList"
					:key="model.id"
					class="model-card"
					:class="{ selected: selectedModel?.id === model.id, 'system-model': model.tenantId === 1 }"
					@click="handleSelectModel(model)"
				>
					<div class="model-card-header">
						<div class="model-type">{{ getModelTypeName(model.modelsType) }}</div>
						<div class="model-badges">
							<el-tag v-if="model.tenantId === 1" type="warning" size="small">系统模型</el-tag>
							<el-icon v-if="selectedModel?.id === model.id" class="check-icon" color="#67c23a"><CircleCheck /></el-icon>
						</div>
					</div>
					<div class="model-card-body">
						<h3 class="model-name">{{ model.modelName }}</h3>
						<p class="model-url">{{ model.baseUrl }}</p>
						<div class="model-status">
							<el-tag :type="model.enabled === 1 ? 'success' : 'info'" size="small">
								{{ model.enabled === 1 ? '已启用' : '已禁用' }}
							</el-tag>
						</div>
					</div>
				</div>
			</div>
		</div>

		<div v-if="pagination.total > 0" class="pagination-wrap">
			<el-pagination
				v-model:current-page="pagination.pageNum"
				v-model:page-size="pagination.pageSize"
				:total="pagination.total"
				:page-sizes="[10, 20, 50]"
				layout="total, prev, pager, next"
				small
				@current-change="handlePageChange"
			/>
		</div>

		<template #footer>
			<el-button @click="handleClose">取消</el-button>
			<el-button type="primary" @click="handleConfirm" :disabled="!selectedModel">确定</el-button>
		</template>

		<!-- 新建模型弹窗 -->
		<EditModule ref="editModuleRef" @refresh="handleRefresh" />

		<!-- 系统模型 API Key 输入弹窗 -->
		<el-dialog v-model="apiKeyDialogVisible" title="配置系统模型" width="500px" :close-on-click-modal="false" append-to-body>
			<el-alert type="info" :closable="false" style="margin-bottom: 16px">
				<template #title>
					<div style="line-height: 1.6">
						您选择的是系统模型，需要配置您自己的 API Key。<br />
						系统将为您创建一个模型副本。
					</div>
				</template>
			</el-alert>
			<el-form :model="apiKeyForm" :rules="apiKeyRules" ref="apiKeyFormRef" label-width="100px">
				<el-form-item label="模型名称" prop="modelName">
					<el-input v-model="apiKeyForm.modelName" placeholder="请输入模型名称" />
				</el-form-item>
				<el-form-item label="API Key" prop="apiKey">
					<el-input v-model="apiKeyForm.apiKey" type="password" show-password placeholder="请输入您的 API Key" />
				</el-form-item>
			</el-form>
			<template #footer>
				<el-button @click="apiKeyDialogVisible = false">取消</el-button>
				<el-button type="primary" @click="handleCreatePrivateModel" :loading="creatingModel">确定</el-button>
			</template>
		</el-dialog>
	</el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, watch } from 'vue';
import { ElMessage, type FormInstance, type FormRules } from 'element-plus';
import { Search, CircleCheck } from '@element-plus/icons-vue';
import { getModelModuleList, addModelModule } from '/@/api/digitalHuman/modelConfig/modelModule';
import { getApiErrorMessage } from '/@/utils/request';
import EditModule from '/@/views/digitalHuman/modelConfig/modelModule/component/editModule.vue';

interface ModelItem {
	id: string;
	tenantId?: number;
	modelName: string;
	modelsType: number;
	baseUrl: string;
	route: string;
	httpMethod: string;
	enabled: number;
	apiKey?: string;
	isPrivate?: number;
	isChatModel?: number;
	headMsg?: string;
	form?: any;
	requestMapping?: any;
	responseMapping?: any;
	maxConcurrency?: number;
	queueLimit?: number;
	timeoutSeconds?: number;
	expectedSeconds?: number;
	retryTimes?: number;
	retryQueueMaxSeconds?: number;
	autoCleanSeconds?: number;
	remark?: string;
	[key: string]: any;
}

interface Props {
	modelValue: boolean;
	defaultModel?: ModelItem | null;
}

interface Emits {
	(e: 'update:modelValue', value: boolean): void;
	(e: 'confirm', model: ModelItem): void;
}

const props = withDefaults(defineProps<Props>(), {
	modelValue: false,
	defaultModel: null,
});

const emit = defineEmits<Emits>();

const visible = ref(false);
const searchParams = reactive({ modelName: '' });
const pagination = reactive({ pageNum: 1, pageSize: 10, total: 0 });
const modelList = ref<ModelItem[]>([]);
const loading = ref(false);
const selectedModel = ref<ModelItem | null>(null);
const editModuleRef = ref();

// 系统模型 API Key 配置
const apiKeyDialogVisible = ref(false);
const apiKeyFormRef = ref<FormInstance>();
const apiKeyForm = reactive({
	modelName: '',
	apiKey: '',
});
const apiKeyRules: FormRules = {
	modelName: [{ required: true, message: '请输入模型名称', trigger: 'blur' }],
	apiKey: [{ required: true, message: '请输入 API Key', trigger: 'blur' }],
};
const creatingModel = ref(false);
const systemModelToClone = ref<ModelItem | null>(null);

watch(
	() => props.modelValue,
	(val) => {
		visible.value = val;
		if (val) {
			selectedModel.value = props.defaultModel || null;
			fetchModelList();
		}
	}
);

watch(visible, (val) => {
	if (!val) {
		emit('update:modelValue', false);
	}
});

const getModelTypeName = (type: number) => {
	const typeMap: Record<number, string> = {
		1: '推理模型',
		2: '图片模型',
		3: '音频模型',
	};
	return typeMap[type] || '未知类型';
};

const fetchModelList = async () => {
	loading.value = true;
	try {
		const params = {
			pageNum: pagination.pageNum,
			pageSize: pagination.pageSize,
			modelName: searchParams.modelName || undefined,
		};
		const res: any = await getModelModuleList(params);
		modelList.value = res.data?.list || [];
		pagination.total = res.data?.total || 0;
	} catch {
		// 接口错误由 request 全局提示后端 message
		modelList.value = [];
		pagination.total = 0;
	} finally {
		loading.value = false;
	}
};

const handleSearch = () => {
	pagination.pageNum = 1;
	fetchModelList();
};

const handlePageChange = () => {
	fetchModelList();
};

const handleSelectModel = (model: ModelItem) => {
	// 判断是否是系统模型（tenantId === 1）
	if (model.tenantId === 1) {
		// 系统模型，需要用户配置 API Key
		systemModelToClone.value = model;
		apiKeyForm.modelName = `${model.modelName} - 副本`;
		apiKeyForm.apiKey = '';
		apiKeyDialogVisible.value = true;
	} else {
		// 非系统模型，直接选中
		selectedModel.value = model;
	}
};

const handleCreatePrivateModel = async () => {
	if (!apiKeyFormRef.value || !systemModelToClone.value) return;

	try {
		await apiKeyFormRef.value.validate();

		creatingModel.value = true;

		// 基于系统模型创建新模型（继承原模型的所有配置，只替换 apiKey）
		const systemModel = systemModelToClone.value;
		const createParams = {
			modelName: apiKeyForm.modelName,
			modelsType: systemModel.modelsType,
			baseUrl: systemModel.baseUrl,
			httpMethod: systemModel.httpMethod || 'POST',
			headMsg: systemModel.headMsg || '',
			isPrivate: systemModel.isPrivate ?? 1, // 继承原模型的公有/私有属性
			enabled: systemModel.enabled ?? 1,
			isChatModel: systemModel.isChatModel || 0,
			apiKey: apiKeyForm.apiKey, // 使用用户输入的新 API Key
			form: systemModel.form || {},
			requestMapping: systemModel.requestMapping || {},
			responseMapping: systemModel.responseMapping || {},
			maxConcurrency: systemModel.maxConcurrency || 10,
			queueLimit: systemModel.queueLimit || 100,
			timeoutSeconds: systemModel.timeoutSeconds || 30,
			expectedSeconds: systemModel.expectedSeconds || 15,
			retryTimes: systemModel.retryTimes || 3,
			retryQueueMaxSeconds: systemModel.retryQueueMaxSeconds || 60,
			autoCleanSeconds: systemModel.autoCleanSeconds || 300,
			remark: systemModel.remark || '',
		};

		const res: any = await addModelModule(createParams);

		ElMessage.success('模型创建成功');

		// 关闭对话框
		apiKeyDialogVisible.value = false;

		// 刷新列表
		await fetchModelList();

		// 选中新创建的模型
		const newModelId = res.data?.id || res.data;
		if (newModelId) {
			const newModel = modelList.value.find((m) => m.id === String(newModelId));
			if (newModel) {
				selectedModel.value = newModel;
			}
		}
	} catch (error: any) {
		if (error !== 'cancel') {
			ElMessage.error(getApiErrorMessage(error, '创建模型失败'));
		}
	} finally {
		creatingModel.value = false;
	}
};

const handleAddModel = () => {
	editModuleRef.value?.openDialog('add');
};

const handleRefresh = () => {
	fetchModelList();
};

const handleConfirm = () => {
	if (selectedModel.value) {
		emit('confirm', selectedModel.value);
		handleClose();
	}
};

const handleClose = () => {
	visible.value = false;
	selectedModel.value = null;
	apiKeyDialogVisible.value = false;
	systemModelToClone.value = null;
};
</script>

<style scoped lang="scss">
.model-selector-header {
	display: flex;
	justify-content: space-between;
	align-items: center;
	margin-bottom: 20px;
	gap: 12px;
}

.search-bar {
	display: flex;
	gap: 12px;
	flex: 1;
}

.model-list {
	min-height: 300px;
	max-height: 400px;
	overflow-y: auto;
}

.model-grid {
	display: grid;
	grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
	gap: 16px;
}

.model-card {
	background: #f8fafc;
	border-radius: 8px;
	padding: 16px;
	cursor: pointer;
	transition: all 0.3s ease;
	border: 2px solid transparent;
}

.model-card:hover {
	box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
	transform: translateY(-2px);
}

.model-card.selected {
	border-color: #67c23a;
	background: #f0f9ff;
}

.model-card.system-model {
	border-color: #fbbf24;
	background: #fffbeb;
}

.model-card.system-model:hover {
	border-color: #f59e0b;
}

.model-card-header {
	display: flex;
	justify-content: space-between;
	align-items: center;
	margin-bottom: 12px;
}

.model-type {
	display: inline-block;
	padding: 2px 8px;
	background: #eff6ff;
	color: #3b82f6;
	border-radius: 4px;
	font-size: 12px;
	font-weight: 600;
}

.model-badges {
	display: flex;
	align-items: center;
	gap: 8px;
}

.check-icon {
	font-size: 20px;
}

.model-card-body {
	flex: 1;
}

.model-name {
	font-size: 16px;
	font-weight: 600;
	color: #1f2937;
	margin: 0 0 8px 0;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

.model-url {
	font-size: 13px;
	color: #64748b;
	line-height: 1.5;
	margin: 0 0 8px 0;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

.model-status {
	display: flex;
	align-items: center;
	gap: 8px;
}

.pagination-wrap {
	display: flex;
	justify-content: center;
	margin-top: 20px;
}
</style>
