import { getChatModes, sendMessage } from '@/api/chat'
import { useSpeakStore } from '@/store/speak'
import { processTagContent } from '@/utils'
import { speakText } from '@/utils/speak'
import { defineStore } from 'pinia'
const chatModes = {
  training: {
    name: "寿险代理人AI陪练",
    customerAvatar: "/images/avatars/custom.png",
    userAvatar: "/images/avatars/user.png",
    token: "app-88ae2GN49aUyNO6qGg7tbTfX",
    background: "# 客户信息\n### **基础背景与性格设定** \n**姓名**：刘勇 \n**年龄**：34 岁 \n**职业**：腾讯员工 \n**家庭**：妻子为全职太太，女儿 9 岁，双方父母健在 \n**性格**：冷静、理性、务实，偏好客观数据和逻辑分析，对保险持观望态度，防备心较强 \n**近期状态**： \n- 体检显示中度脂肪肝，担心投保问题 \n- 已为全家配置百万医疗险，妻子有重疾险，但认为保障不足 （保费 50W）\n- 对理财型保险收益不认可，但对补充重疾险有潜在需求 ",
    chatBackground: "通过尚先生介绍，你和他的老同学在他的家里首次面谈。他的家中布置简洁，茶几上摆放着一套茶具。"
  },
  ai_agent: {
    name: "AI寿险代理人",
    customerAvatar: "/images/avatars/user.png",
    userAvatar: "/images/avatars/custom.png",
    token: "app-eYoE51WXCaEKXvzb9ZCj6lBn",
    background: "# AI寿险代理人\n### **角色设定** \n**身份**：AI寿险代理人 \n**特点**：专业、耐心、细致、富有同理心 \n**能力**：\n- 精通各类寿险产品知识\n- 擅长需求分析和方案定制\n- 具备优秀的沟通和谈判技巧\n- 能够处理各类客户异议\n- 熟悉保险行业最新动态",
    chatBackground: "一位AI的保险代理人，通过聊天来确认用户的需求，收集用户的基本信息"
  },
  quote_objection: {
    name: "报价中异议",
    customerAvatar: "/images/avatars/custom.png",
    userAvatar: "/images/avatars/user.png",
    token: "app-ur2Altw2LHR6niX8Q1S7Cn41",
    background: "# 客户信息\n### **基础背景与性格设定** \n**姓名**: 王大牛\n**年龄**: 40岁\n**职业**: 私营企业主\n**车牌号**: 闽C12345\n**车辆信息**：拥有一辆行驶4年的大众途观L，购买价格约25万，主要用于商务出行及家庭使用。\n**投保信息**：在其他保司购买交强险及商业险（三者险、车损险），还有1个月到期。有过一次轻微追尾事故，已通过保司处理。王先生对成本控制非常敏感，但也能意识到保险的重要性。他希望得到保障全面、价格合理的产品和优质的服务。",
    chatBackground: "现在你将扮演坐席专员，与系统扮演的客户针对报价中的各类异议开展对练，着重训练处理\"报价中异议\"的能力。按照\"保全保足\"原则给出险种推荐方案。"
  },
  post_quote_objection: {
    name: "报价后异议",
    customerAvatar: "/images/avatars/custom.png",
    userAvatar: "/images/avatars/user.png",
    token: "app-Yiccl0JoXs2QF2lkHxO6f822",
    background: "# 客户信息\n### **基础背景与性格设定** \n**姓名**: 张灵女士\n**年龄**: 32岁\n**职业**: 公司行政主管\n**车牌号**: 粤B56789\n**车辆信息**：拥有一辆行驶4年多的本田思域，1.5T，CVT燃动版，购买价格约15万，用于日常通勤及周末短途出行。\n**投保信息**：在其他保司购买交强险及商业险（三者险、车损险、不计免赔险、车上人员责任险），还有2个月到期。无事故记录，仅有两次违章停车记录。张女士注重性价比和服务质量，希望保险的保障全面、价格合理、服务贴心。",
    chatBackground: "现在你将扮演坐席专员，与系统扮演的客户针对报价后的各类异议开展对练，着重训练处理\"报价后异议\"的能力。按照\"保全保足\"原则给出险种推荐方案。"
  }
}
const tagList = ['kehu', 'pingfen', 'zongjie', 'huaxiang', 'dafen']



export const useChatStore = defineStore('chat', {
  state: () => ({
    messages: {},
    currentMode: 'training',
    conversationId: null,
    messageCache: {},
    chatModesLoading: false,
  }),

  getters: {
    chatModes: () => chatModes,
    currentToken: (state) => state.chatModes[state.currentMode]?.token,
    currentConversation: (state) => {
      return state.conversationId ? state.messages[state.conversationId] : null
    },
    currentMessages: (state) => {
      // 根据conversationId和currentMode找到对应的messageCache的messageId
      const cache = Object.values(state.messageCache).find(message =>
        message.mode === state.currentMode && message.conversationId === state.conversationId
      )

      // 获取当前会话的消息
      const currentConversation = state.conversationId ? state.messages[state.conversationId] : null
      const conversationMessages = currentConversation ? currentConversation.chatMessages : []

      return conversationMessages.filter(message =>
        message.mode === state.currentMode &&
        (message.conversationId === state.conversationId || !state.conversationId) &&
        (!cache || message.messageId !== cache.messageId)
      ).concat(cache ? [cache] : [])
    },
    // 获取所有会话列表
    conversations: (state) => {
      return Object.entries(state.messages).map(([conversationId, conversation]) => ({
        conversationId,
        mode: conversation.chatModes,
        messages: conversation.chatMessages,
        status: conversation.conversationStatus,
        summary: conversation.summary,
        // 使用第一条消息的时间作为会话创建时间
        createdAt: conversation.chatMessages[0]?.createdAt || 0
      })).sort((a, b) => b.createdAt - a.createdAt) // 按时间倒序排序
    }
  },

  actions: {
    async fetchChatModes() {
      if (this.chatModesLoading) return
      this.chatModesLoading = true
      try {
        const response = await getChatModes()
        this.chatModes = response.data
      } catch (error) {
        console.error('获取聊天模式失败:', error)
      } finally {
        this.chatModesLoading = false
      }
    },

    setCurrentMode(mode) {
      this.currentMode = mode

      // 找到当前模式的最后一条会话
      const lastConversation = this.conversations
        .filter(conv => conv.mode === mode)
        .sort((a, b) => b.createdAt - a.createdAt)[0]

      // 设置当前会话ID
      this.conversationId = lastConversation?.conversationId || null

      console.log('%c currentConversation:', 'color: #2196F3; font-weight: bold', {
        mode,
        lastConversation,
        currentConversation: this.currentConversation
      })
    },

    // 切换到指定会话
    switchConversation(conversationId) {
      if (this.messages[conversationId]) {
        this.conversationId = conversationId
        // 清除当前的消息缓存
        Object.keys(this.messageCache).forEach(messageId => {
          if (this.messageCache[messageId].conversationId === conversationId) {
            delete this.messageCache[messageId]
          }
        })
      }
    },

    async sendMessage(question) {
      if (!question.trim()) return

      // 设置会话状态为正在输入
      if (this.conversationId && this.messages[this.conversationId]) {
        this.messages[this.conversationId].conversationStatus = 'typing'
      }

      this.hasStartedResponse = false

      const spokenSet = new Set(); // 新增：记录已播报的 messageId
      try {
        const messageStream = await sendMessage({
          inputs: {},
          query: question,
          response_mode: 'streaming',
          conversation_id: this.conversationId,
          user: 'sys123'
        }, this.currentToken)

        let answerCache = ''
        for await (const message of messageStream) {
          if (!message || !message.event) {
            continue
          }
          switch (message.event) {
            case 'workflow_started':
              console.log('%c workflow_started:message:', 'color: #2196F3; font-weight: bold', message)
              // 创建新消息对象
              const currentMessage = {
                mode: this.currentMode,
                conversationId: message.conversation_id,
                messageId: message.message_id,
                createdAt: message.created_at,
                createdAtTimestamp: '',
                question: question,
                answerBuffer: '',
                showEvaluation: false,
                respondingType: '',
                kehu: '',
                pingfen: '',
                zongjie: '',
                huaxiang: '',
                dafen: '',
                kehuFinished: false,
                pingfenFinished: false,
                zongjieFinished: false,
                huaxiangFinished: false,
                dafenFinished: false,
              }
              this.conversationId = message.conversation_id
              this.messageCache[message.message_id] = currentMessage

               // 初始化会话消息结构
               if (!this.messages[currentMessage.conversationId]) {
                this.messages[currentMessage.conversationId] = {
                  chatModes: currentMessage.mode,
                  chatMessages: [],
                  conversationStatus: 'typing', // 会话状态 active 活跃，finished 已完成, typing 正在输入
                  //总结
                  summary: '',
                }
              }
              break
            case 'message':
              if (message.answer) {
                console.log('%c message:answer:', 'color: #2196F3; font-weight: bold', message.answer)
                answerCache += message.answer
                const cachedMessage = this.messageCache[message.message_id]
                if (cachedMessage) {
                  for (const tag of tagList) {
                    answerCache = processTagContent(answerCache, tag, cachedMessage)
                  }
                  // 只在kehuFinished为true且未播报过时播报
                  if (
                    cachedMessage.kehuFinished &&
                    !spokenSet.has(message.message_id)
                  ) {
                    const speakStore = useSpeakStore()
                    if (speakStore.autoTTS) {
                      speakText(cachedMessage.kehu)
                      spokenSet.add(message.message_id)
                    }
                  }
                }
              }
              break
            case 'workflow_finished':
              console.log('%c workflow_finished:message:', 'color: #2196F3; font-weight: bold', message)
              if (this.messageCache[message.message_id]) {
                const cachedMessage = this.messageCache[message.message_id]
                if (cachedMessage) {
                  cachedMessage.respondingType = ''
                  cachedMessage.answerBuffer = message.data.outputs.answer


                  this.messages[cachedMessage.conversationId].chatMessages.push(cachedMessage)

                  // 如果包含dafen标签，将会话标记为完成并将打分内容归档到summary
                  if (cachedMessage.dafen) {
                    this.messages[cachedMessage.conversationId].conversationStatus = 'finished'
                    this.messages[cachedMessage.conversationId].summary = cachedMessage.dafen
                  } else {
                    // 恢复会话状态为活跃
                    this.messages[cachedMessage.conversationId].conversationStatus = 'active'
                  }

                  console.log('%c workflow_finished:cachedMessage:', 'color: #ff0000; font-weight: bold', cachedMessage)
                }
                delete this.messageCache[message.message_id]
              }
              break
          }
        }
      } catch (error) {
        console.error('发送消息失败:', error)
        throw error
      }
    },
    toggleEvaluation(messageId) {
      console.log('%c toggleEvaluation:messageId:', 'color: #2196F3; font-weight: bold', messageId)
      const cache = Object.values(this.messageCache).find(message =>
        message.mode === this.currentMode && message.messageId === messageId
      )
      if (cache) {
        cache.showEvaluation = !cache.showEvaluation
      } else {
        // 遍历所有会话查找消息
        for (const conversationId in this.messages) {
          const conversation = this.messages[conversationId]
          const message = conversation.chatMessages.find(msg => msg.messageId === messageId)
          if (message) {
            message.showEvaluation = !message.showEvaluation
            break
          }
        }
      }
    },

    startNewChat() {
      // 如果当前有活跃会话，将其标记为完成
      // if (this.conversationId && this.messages[this.conversationId]) {
      //   this.messages[this.conversationId].conversationStatus = 'finished'
      // }

      // 只清除当前会话相关的消息缓存
      if (this.conversationId) {
        // 从messageCache中删除当前会话的消息
        Object.keys(this.messageCache).forEach(messageId => {
          if (this.messageCache[messageId].conversationId === this.conversationId) {
            delete this.messageCache[messageId]
          }
        })
      }

      // 清除当前会话ID
      this.conversationId = null
    },

    // 删除指定会话
    deleteConversation(conversationId) {
      // 如果要删除的是当前会话，先清除相关缓存
      if (this.conversationId === conversationId) {
        Object.keys(this.messageCache).forEach(messageId => {
          if (this.messageCache[messageId].conversationId === conversationId) {
            delete this.messageCache[messageId]
          }
        })
        this.conversationId = null
      }

      // 从messages中删除会话
      if (this.messages[conversationId]) {
        delete this.messages[conversationId]
      }
    }
  },

  persist: {
    key: 'chatStore',
    storage: localStorage,
    paths: ['messages', 'currentMode', 'conversationId', 'chatModes']
  }
})
