<script setup lang="ts">
import type { FormInstance } from 'element-plus';

import { onMounted, ref, watch } from 'vue';

import { InfoFilled } from '@element-plus/icons-vue';
import {
  ElButton,
  ElForm,
  ElFormItem,
  ElIcon,
  ElInput,
  ElMessage,
  ElOption,
  ElSelect,
  ElSwitch,
  ElTooltip,
} from 'element-plus';

import { api } from '#/api/request';
import UploadAvatar from '#/components/upload/UploadAvatar.vue';
import { $t } from '#/locales';

const props = defineProps({
  detailData: {
    type: Object,
    default: () => ({
      id: '',
      alias: '',
      deptId: '',
      icon: '',
      title: '',
      description: '',
      slug: '',
      vectorStoreEnable: false,
      vectorStoreType: '',
      vectorStoreCollection: '',
      vectorStoreConfig: '',
      vectorEmbedModelId: '',
      options: {
        canUpdateEmbeddingModel: true,
        rerankEnable: false,
      },
      rerankModelId: '',
      searchEngineEnable: false,
      englishName: '',
    }),
  },
  manageable: {
    type: Boolean,
    default: true,
  },
});

const emit = defineEmits(['reload']);

const normalizeEntity = (raw: any) => {
  const options = {
    canUpdateEmbeddingModel: true,
    ...raw?.options,
  };
  if (options.rerankEnable === undefined || options.rerankEnable === null) {
    options.rerankEnable = !!raw?.rerankModelId;
  }
  return {
    ...raw,
    options,
  };
};

const entity = ref<any>(normalizeEntity(props.detailData));

watch(
  () => props.detailData,
  (newVal) => {
    entity.value = normalizeEntity(newVal);
  },
  { immediate: true, deep: true },
);

const embeddingLlmList = ref<any>([]);
const rerankerLlmList = ref<any>([]);
const getEmbeddingLlmListData = async () => {
  try {
    const url = `/api/v1/documentCollection/modelList?modelType=embeddingModel`;
    const res = await api.get(url, {});
    if (res.errorCode === 0) {
      embeddingLlmList.value = res.data;
    }
  } catch (error) {
    ElMessage.error($t('message.apiError'));
    console.error('获取嵌入模型列表失败：', error);
  }
};

const getRerankerLlmListData = async () => {
  try {
    const res = await api.get(
      '/api/v1/documentCollection/modelList?modelType=rerankModel',
    );
    rerankerLlmList.value = res.data;
  } catch (error) {
    ElMessage.error($t('message.apiError'));
    console.error('获取重排模型列表失败：', error);
  }
};

onMounted(async () => {
  await getEmbeddingLlmListData();
  await getRerankerLlmListData();
});

const saveForm = ref<FormInstance>();
const btnLoading = ref(false);
const rules = ref({
  deptId: [
    { required: true, message: $t('message.required'), trigger: 'blur' },
  ],
  englishName: [
    { required: true, message: $t('message.required'), trigger: 'blur' },
  ],
  description: [
    { required: true, message: $t('message.required'), trigger: 'blur' },
  ],
  title: [{ required: true, message: $t('message.required'), trigger: 'blur' }],
  vectorEmbedModelId: [
    { required: true, message: $t('message.required'), trigger: 'blur' },
  ],
});

async function save() {
  if (!props.manageable) {
    ElMessage.warning($t('documentCollection.managePermissionHint'));
    return;
  }
  try {
    const valid = await saveForm.value?.validate();
    if (!valid) return;

    if (!entity.value.options) {
      entity.value.options = {};
    }
    entity.value.options.rerankEnable = !!entity.value.options.rerankEnable;

    btnLoading.value = true;
    const res = await api.post(
      '/api/v1/documentCollection/update',
      entity.value,
    );

    if (res.errorCode === 0) {
      ElMessage.success($t('message.saveOkMessage'));
      emit('reload');
    }
  } catch (error) {
    ElMessage.error($t('message.saveFail'));
    console.error('保存失败：', error);
  } finally {
    btnLoading.value = false;
  }
}
</script>

<template>
  <div class="document-config-container">
    <ElForm
      label-width="150px"
      ref="saveForm"
      :model="entity"
      :disabled="!props.manageable"
      status-icon
      :rules="rules"
    >
      <div v-if="!props.manageable" class="config-readonly-tip">
        {{ $t('documentCollection.managePermissionHint') }}
      </div>
      <ElFormItem
        prop="icon"
        :label="$t('documentCollection.icon')"
        style="display: flex; align-items: center"
      >
        <UploadAvatar v-model="entity.icon" />
      </ElFormItem>
      <ElFormItem prop="title" :label="$t('documentCollection.title')">
        <ElInput
          v-model.trim="entity.title"
          :placeholder="$t('documentCollection.placeholder.title')"
        />
      </ElFormItem>
      <ElFormItem prop="alias" :label="$t('documentCollection.alias')">
        <ElInput v-model.trim="entity.alias" />
      </ElFormItem>
      <ElFormItem
        prop="englishName"
        :label="$t('documentCollection.englishName')"
      >
        <ElInput v-model.trim="entity.englishName" />
      </ElFormItem>
      <ElFormItem
        prop="description"
        :label="$t('documentCollection.description')"
      >
        <ElInput
          v-model.trim="entity.description"
          :rows="4"
          type="textarea"
          :placeholder="$t('documentCollection.placeholder.description')"
        />
      </ElFormItem>
      <ElFormItem prop="vectorEmbedModelId">
        <template #label>
          <span style="display: flex; align-items: center">
            {{ $t('documentCollection.vectorEmbedLlmId') }}
            <ElTooltip
              :content="$t('documentCollection.vectorEmbedModelTips')"
              placement="top"
              effect="light"
            >
              <ElIcon
                style="
                  margin-left: 4px;
                  font-size: 14px;
                  color: #909399;
                  cursor: pointer;
                "
              >
                <InfoFilled />
              </ElIcon>
            </ElTooltip>
          </span>
        </template>

        <ElSelect
          v-model="entity.vectorEmbedModelId"
          :disabled="!entity?.options?.canUpdateEmbeddingModel"
          :placeholder="$t('documentCollection.placeholder.embedLlm')"
        >
          <ElOption
            v-for="item in embeddingLlmList"
            :key="item.id"
            :label="item.title"
            :value="item.id || ''"
          />
        </ElSelect>
      </ElFormItem>
      <ElFormItem
        prop="dimensionOfVectorModel"
        :label="$t('documentCollection.dimensionOfVectorModel')"
      >
        <template #label>
          <span style="display: flex; align-items: center">
            {{ $t('documentCollection.dimensionOfVectorModel') }}
            <ElTooltip
              :content="$t('documentCollection.dimensionOfVectorModelTips')"
              placement="top"
              effect="light"
            >
              <ElIcon
                style="
                  margin-left: 4px;
                  font-size: 14px;
                  color: #909399;
                  cursor: pointer;
                "
              >
                <InfoFilled />
              </ElIcon>
            </ElTooltip>
          </span>
        </template>
        <ElInput
          :disabled="!entity?.options?.canUpdateEmbeddingModel"
          v-model.trim="entity.dimensionOfVectorModel"
          type="number"
        />
      </ElFormItem>
      <ElFormItem
        prop="options.rerankEnable"
        :label="$t('documentCollection.rerankEnable')"
      >
        <ElSwitch v-model="entity.options.rerankEnable" />
      </ElFormItem>
      <ElFormItem
        prop="rerankModelId"
        :label="$t('documentCollection.rerankLlmId')"
      >
        <ElSelect
          v-model="entity.rerankModelId"
          clearable
          :placeholder="$t('documentCollection.placeholder.rerankLlm')"
        >
          <ElOption
            v-for="item in rerankerLlmList"
            :key="item.id"
            :label="item.title"
            :value="item.id || ''"
          />
        </ElSelect>
      </ElFormItem>
      <ElFormItem style="margin-top: 20px; text-align: right">
        <ElButton
          type="primary"
          @click="save"
          :loading="btnLoading"
          :disabled="btnLoading || !props.manageable"
        >
          {{ $t('button.save') }}
        </ElButton>
      </ElFormItem>
    </ElForm>
  </div>
</template>

<style scoped>
.document-config-container {
  height: 100%;
  overflow: auto;
}

.config-readonly-tip {
  margin-bottom: 16px;
  font-size: 12px;
  line-height: 1.6;
  color: var(--el-text-color-secondary);
}
</style>
