// 全局对话状态管理
class ConversationStore {
  constructor() {
    this.conversations = [
      {
        id: '1',
        title: '新对话',
        messages: [
          {
            role: 'assistant',
            content: '你好！我是AI助手，有什么可以帮助你的吗？',
            timestamp: new Date().toLocaleTimeString()
          }
        ],
        lastUpdate: new Date(),
        isActive: true
      }
    ]
    
    this.currentConversationId = '1'
    this.listeners = []
  }

  // 获取所有对话
  getConversations() {
    return this.conversations
  }

  // 获取当前对话
  getCurrentConversation() {
    return this.conversations.find(conv => conv.id === this.currentConversationId)
  }

  // 获取当前对话ID
  getCurrentConversationId() {
    return this.currentConversationId
  }

  // 创建新对话
  createNewConversation() {
    const newId = Date.now().toString()
    const newConversation = {
      id: newId,
      title: '新对话',
      messages: [
        {
          role: 'assistant',
          content: '你好！我是AI助手，有什么可以帮助你的吗？',
          timestamp: new Date().toLocaleTimeString()
        }
      ],
      lastUpdate: new Date(),
      isActive: true
    }
    
    // 将之前的对话设为非活跃
    this.conversations = this.conversations.map(conv => ({ ...conv, isActive: false }))
    
    // 添加新对话
    this.conversations.push(newConversation)
    this.currentConversationId = newId
    
    this.notifyListeners()
    return newId
  }

  // 切换对话
  switchConversation(conversationId) {
    this.conversations = this.conversations.map(conv => ({ 
      ...conv, 
      isActive: conv.id === conversationId 
    }))
    this.currentConversationId = conversationId
    
    this.notifyListeners()
  }

  // 删除对话
  deleteConversation(conversationId) {
    if (this.conversations.length <= 1) {
      return false
    }
    
    this.conversations = this.conversations.filter(conv => conv.id !== conversationId)
    
    // 如果删除的是当前对话，切换到第一个对话
    if (conversationId === this.currentConversationId) {
      if (this.conversations.length > 0) {
        this.currentConversationId = this.conversations[0].id
        this.conversations = this.conversations.map((conv, index) => 
          index === 0 ? { ...conv, isActive: true } : { ...conv, isActive: false }
        )
      }
    }
    
    this.notifyListeners()
    return true
  }

  // 添加消息到当前对话
  addMessage(role, content) {
    const currentConversation = this.getCurrentConversation()
    if (!currentConversation) return

    const message = {
      role,
      content,
      timestamp: new Date().toLocaleTimeString()
    }

    currentConversation.messages.push(message)
    currentConversation.lastUpdate = new Date()
    
    // 如果是用户的第一条消息，更新对话标题
    if (role === 'user' && currentConversation.title === '新对话') {
      currentConversation.title = content.slice(0, 20) + '...'
    }
    
    this.notifyListeners()
  }

  // 更新对话标题
  updateConversationTitle(conversationId, newTitle) {
    const conversation = this.conversations.find(conv => conv.id === conversationId)
    if (conversation) {
      conversation.title = newTitle
      this.notifyListeners()
    }
  }

  // 清空当前对话
  clearCurrentConversation() {
    const currentConversation = this.getCurrentConversation()
    if (currentConversation) {
      currentConversation.messages = [
        {
          role: 'assistant',
          content: '你好！我是AI助手，有什么可以帮助你的吗？',
          timestamp: new Date().toLocaleTimeString()
        }
      ]
      currentConversation.title = '新对话'
      currentConversation.lastUpdate = new Date()
      
      this.notifyListeners()
    }
  }

  // 订阅状态变化
  subscribe(listener) {
    this.listeners.push(listener)
    
    // 返回取消订阅函数
    return () => {
      const index = this.listeners.indexOf(listener)
      if (index > -1) {
        this.listeners.splice(index, 1)
      }
    }
  }

  // 通知所有监听器
  notifyListeners() {
    this.listeners.forEach(listener => {
      listener({
        conversations: this.conversations,
        currentConversationId: this.currentConversationId,
        currentConversation: this.getCurrentConversation()
      })
    })
  }
}

// 创建全局单例实例
const conversationStore = new ConversationStore()

// 导出实例和类
export default conversationStore
export { ConversationStore }
