import axios from 'axios';

const BASE_URL = 'http://127.0.0.1:5000';
// 登录请求方法
export const login = async (formData: { username: string; password: string }) => {
  try {
    const response = await axios.post(`${BASE_URL}/api/login`, formData);
    // 检查后端返回的响应结果
    if (response.data.success) {
      console.log('登录成功');
      return response.data;
    } else {
      console.error('登录失败：', response.data.message);
      throw new Error(response.data.message || '登录失败，请检查账号和密码！');
    }
  } catch (error) {
    console.error('登录请求时发生错误：', error);
    throw error;
  }
};

// 调用 Flask 进行RAG增强回答
export const queryRAGKnowledge = async (query: string) => {
  try {
    const response = await axios.post(`${BASE_URL}/query`, { query });
    // 提取结果中的 content 字段
    const contents = response.data.results.map((result: { sentence: any; }) => result.sentence);
    return contents; // 返回内容列表

  } catch (error) {
    console.error('查询 RAG 知识库时发生错误：', error);
    throw error;
  }
};

// 保存用户的 AI 问答记录
export const saveAIChatRecord = async (record: {
  nickname: string;
  chat_time: string;
  chat_title: string;
  chat_content: string; // Markdown 格式
}) => {
  try {
    const response = await axios.post(`${BASE_URL}/save_chat_record`, record);
    if (response.data.status === 'success') {
      console.log('AI 问答记录保存成功:', response.data.message);
      return response.data;
    } else {
      console.error('AI 问答记录保存失败:', response.data.message);
      throw new Error(response.data.message || 'AI 问答记录保存失败！');
    }
  } catch (error) {
    console.error('保存 AI 问答记录时发生错误:', error);
    throw error;
  }
};

// 获取 AI 问答记录
export const getAIChatRecords = async () => {
  try {
    const response = await axios.get(`${BASE_URL}/get_chat_records`);
    if (response.data.status === 'success') {
      console.log('获取 AI 问答记录成功:', response.data.records);
      return response.data.records;
    } else {
      console.error('获取 AI 问答记录失败:', response.data.message);
      throw new Error(response.data.message || '获取 AI 问答记录失败！');
    }
  } catch (error) {
    console.error('获取 AI 问答记录时发生错误:', error);
    throw error;
  }
};

// 调用 Flask 进行图像识别
export const predict = async (data: any) => {
  try {
    const response = await axios.post(`${BASE_URL}/predict`, { data });
    return response.data;
  } catch (error) {
    console.error('图像预测时发生错误:', error);
    throw error;
  }
};

// 保存用户的陶瓷识别记录
export const saveCeramicDetectionRecord = async (record: {
  nickname: string;
  detection_time: string;
  result: string;
  confidence: string;
}) => {
  try {
    const response = await axios.post(`${BASE_URL}/save_detection`, record);
    if (response.data.status === 'success') {
      console.log('陶瓷识别记录保存成功:', response.data.message);
      return response.data;
    } else {
      console.error('陶瓷识别记录保存失败:', response.data.message);
      throw new Error(response.data.message || '陶瓷识别记录保存失败！');
    }
  } catch (error) {
    console.error('保存陶瓷识别记录时发生错误:', error);
    throw error;
  }
};

// 获取陶瓷识别记录
export const getCeramicDetectionRecords = async (username: string) => {
  try {
    const response = await axios.get(`${BASE_URL}/get_detection_records`, {
      params: { nickname: username }, // 传递用户名参数
    });
    if (response.data.status === 'success') {
      console.log('获取陶瓷识别记录成功:', response.data.records);
      return response.data.data || []; // 确保返回数组
    } else {
      console.error('获取陶瓷识别记录失败:', response.data.message);
      return []; // 返回空数组
    }
  } catch (error) {
    console.error('获取陶瓷识别记录时发生错误:', error);
    return []; // 返回空数组以防止前端崩溃
  }
};