<template>
	<el-dialog v-model="visible" :title="t('pages.model.detailTitle')" width="700px">
		<el-descriptions v-if="model" :column="1" border>
			<el-descriptions-item :label="t('pages.model.fields.name')">{{
				model.name
			}}</el-descriptions-item>
			<el-descriptions-item :label="t('pages.model.fields.title')">{{
				model.title
			}}</el-descriptions-item>
			<el-descriptions-item :label="t('pages.model.fields.type')">{{
				modelTypeName
			}}</el-descriptions-item>
			<el-descriptions-item :label="t('pages.model.fields.source')">{{
				modelSourceName
			}}</el-descriptions-item>
			<el-descriptions-item :label="t('pages.model.fields.provider')">{{
				model.provider
			}}</el-descriptions-item>
			<el-descriptions-item :label="t('pages.model.fields.description')">{{
				model.description || t('pages.model.none')
			}}</el-descriptions-item>
			<el-descriptions-item
				v-if="model.source === 'remote'"
				:label="t('pages.model.fields.baseUrl')"
			>
				{{ model.parameters.base_url || t('pages.model.none') }}
			</el-descriptions-item>
			<el-descriptions-item
				v-if="model.source === 'remote'"
				:label="t('pages.model.fields.connectionTest')"
			>
				<div class="model-check-box">
					<el-button type="primary" plain :loading="checkLoading" @click="$emit('check')">
						{{ t('pages.model.testConnection') }}
					</el-button>
					<el-alert
						v-if="checkResult.message"
						:type="checkResult.success ? 'success' : 'error'"
						:title="checkResult.message"
						:closable="false"
						show-icon
					/>
				</div>
			</el-descriptions-item>
			<el-descriptions-item :label="t('pages.model.fields.createdAt')">{{
				model.creationTime
			}}</el-descriptions-item>
			<el-descriptions-item :label="t('pages.model.fields.updatedAt')">{{
				model.updateTime
			}}</el-descriptions-item>
		</el-descriptions>
		<template #footer>
			<el-button @click="visible = false">{{ t('common.close') }}</el-button>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { useI18n } from '@/composables/useI18n'
import type { ModelDetail } from '../types'

const { t } = useI18n()

const props = defineProps<{
	modelValue: boolean
	model: ModelDetail | null
	checkLoading: boolean
	checkResult: {
		success: boolean
		message: string
	}
	getModelTypeName: (type?: string) => string
}>()

const emit = defineEmits<{
	(e: 'update:modelValue', value: boolean): void
	(e: 'check'): void
}>()

const visible = computed({
	get: () => props.modelValue,
	set: (value) => emit('update:modelValue', value)
})

const modelTypeName = computed(() => props.getModelTypeName(props.model?.type))
const modelSourceName = computed(() =>
	props.model?.source === 'local'
		? t('pages.model.sources.localOllama')
		: t('pages.model.sources.remoteApi')
)

function maskApiKey(key: string) {
	if (!key) return t('pages.model.none')
	if (key.length <= 8) return '****'
	return `${key.slice(0, 4)}${'*'.repeat(key.length - 8)}${key.slice(-4)}`
}
</script>

<style scoped lang="less">
.model-check-box {
	width: 100%;
	display: flex;
	flex-direction: column;
	gap: 8px;
}
</style>
