import request from '@/utils/request'
import { getToken } from '@/utils/auth'
// 获取AI聊天的SSE连接URL
// 直接返回SSE连接的URL
export function getChatStreamUrl() {
  // 根据实际部署环境调整基础URL
  const baseUrl = process.env.VUE_APP_BASE_API || '';
  return `uniapp/dify/chat/stream`;
}

// 普通的非流式AI聊天请求
export function postChatMessage(data) {
  return request({
    url: 'uniapp/dify/chat',
    method: 'post',
    data: data
  })
}

// 发送流式AI聊天请求(返回fetch Promise)
export function postChatMessageData(data) {
  const baseUrl = process.env.VUE_APP_BASE_API || '';
  const url = `${baseUrl}/uniapp/dify/chat/stream`;
  
  return fetch(url, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Accept': 'text/event-stream',
      'Authorization': 'Bearer ' + getToken()
    },
    body: JSON.stringify(data),
    credentials: 'include', // 确保发送凭证（cookie等）
    mode: 'cors'           // 允许跨域请求
  });
}

// 终止流式请求
export function stopChatStream(data) {
  return request({
    url: '/uniapp/dify/chat/stop',
    method: 'post',
    data: data
  })
} 
// 获取下一轮建议问题列表
export function chatStreamSuggested(data) {
  return request({
    url: '/uniapp/dify/chat/suggested',
    method: 'get',
    params: data
  })
} 