<template>
  <ChatBox
    :character-config="characterConfig"
    :ai-config="aiConfig"
  />
</template>

<script setup>
import { ref, onMounted } from 'vue';
import ChatBox from '@/components/ChatBox.vue';
import { getCharacterById } from '@/utils/aiCharacters.js';

// 角色配置
const characterConfig = ref({});

// AI 配置
const aiConfig = ref({
  modelId: 10,
  templateId: 9999
});

// 初始化：解析 URL 参数
onMounted(() => {

  const pages = getCurrentPages();
  const currentPage = pages[pages.length - 1];
  const options = currentPage.options || {};

  // 场景1: 蔚AI（characterId === 'wei-ai' 或无参数）
  if (options.characterId === 'wei-ai' || !options.characterId) {
    characterConfig.value = {
      id: 'wei-ai',
      roleId: null,
      name: options.characterName || '蔚AI',
      avatar: options.characterAvatar || '/file/background/2025/11/11/97683faa11074a4bbe7619c246754c1a.jpg',
      greeting: options.introMessage ? decodeURIComponent(options.introMessage) : '你好！我是你的虚拟女友！',
      roleDesc: ''
    };

    // 蔚AI 使用固定的 AI 配置
    aiConfig.value = {
      modelId: 10,
      templateId: 9999,
      ttsId: null,
      sttId: null,
      temperature: 0.7,
      topP: 0.9
    };
  }
  // 场景2: AI角色（有 roleId 参数）
  else if (options.roleId) {
    characterConfig.value = {
      id: options.characterId || '',
      roleId: options.roleId,
      name: decodeURIComponent(options.roleName || 'AI角色'),
      avatar: decodeURIComponent(options.avatar || '/static/logo.png'),
      greeting: decodeURIComponent(options.greeting || '你好！很高兴认识你！'),
      roleDesc: decodeURIComponent(options.roleDesc || '')
    };
    debugger
    // AI角色使用自定义配置
    aiConfig.value = {
      modelId: options.modelId ? parseInt(options.modelId) : 10,
      templateId: options.templateId ? parseInt(options.templateId) : (options.roleId ? parseInt(options.roleId) : 6),
      ttsId: options.ttsId || null,
      sttId: options.sttId || null,
      temperature: options.temperature ? parseFloat(options.temperature) : 0.7,
      topP: options.topP ? parseFloat(options.topP) : 0.9
    };
  }
  // 场景3: 默认角色（从 aiCharacters.js 获取）
  else {
    const characterId = options.characterId || 5;
    const character = getCharacterById(characterId);

    if (character) {
      characterConfig.value = {
        id: character.id,
        roleId: null,
        name: character.name,
        avatar: character.avatar,
        greeting: character.greeting,
        roleDesc: character.description || ''
      };

      // 默认角色使用固定的 AI 配置
      aiConfig.value = {
        modelId: 10,
        templateId: 9999,
        ttsId: null,
        sttId: null,
        temperature: 0.7,
        topP: 0.9
      };
    } else {
      // 兜底：如果找不到角色，使用蔚AI
      characterConfig.value = {
        id: 'wei-ai',
        roleId: null,
        name: '蔚AI',
        avatar: '/static/avatar/icon_hushi.jpg',
        greeting: '你好！我是蔚AI，很高兴为您服务！',
        roleDesc: ''
      };

      aiConfig.value = {
        modelId: 10,
        templateId: 9999,
        ttsId: null,
        sttId: null,
        temperature: 0.7,
        topP: 0.9
      };
    }
  }

  console.log('chat-box 页面初始化完成:', {
    characterConfig: characterConfig.value,
    aiConfig: aiConfig.value
  });
});
</script>

<style scoped>
/* 无需额外样式，所有样式在 ChatBox 组件中 */
</style>
