import api from './api';

/**
 * 生命事件服务
 * 处理生命事件的增删改查
 */

/**
 * 获取当前用户的所有生命事件
 * @returns {Promise<Array>} 生命事件列表
 */
export const getEventList = async () => {
  const response = await api.get('/lifeEvent/list');
  return response;
};

/**
 * 分页获取生命事件
 * @param {Object} params - 分页参数
 * @param {number} params.pageNum - 页码
 * @param {number} params.pageSize - 每页数量
 * @returns {Promise<Object>} 分页结果
 */
export const getEventPage = async ({ pageNum = 1, pageSize = 10 }) => {
  const response = await api.get('/lifeEvent/page', {
    params: { pageNum, pageSize }
  });
  return response;
};

/**
 * 根据ID获取生命事件详情
 * @param {string} id - 事件ID
 * @returns {Promise<Object>} 事件详情
 */
export const getEventById = async (id) => {
  const response = await api.get('/lifeEvent/detail', {
    params: { id }
  });
  return response;
};

/**
 * 创建生命事件
 * @param {Object} eventData - 事件数据
 * @returns {Promise<Object>} 创建的事件
 */
export const createEvent = async (eventData) => {
  const requestData = transformToBackendFormat(eventData);
  const response = await api.post('/lifeEvent/create', requestData);
  return response;
};

/**
 * 更新生命事件
 * @param {Object} eventData - 事件数据（必须包含 id）
 * @returns {Promise<Object>} 更新后的事件
 */
export const updateEvent = async (eventData) => {
  const requestData = transformToBackendFormat(eventData);
  const response = await api.put('/lifeEvent/update', requestData);
  return response;
};

/**
 * 删除生命事件
 * @param {string} id - 事件ID
 * @returns {Promise<void>}
 */
export const deleteEvent = async (id) => {
  const response = await api.delete('/lifeEvent/delete', {
    params: { id }
  });
  return response;
};

/**
 * 将前端数据格式转换为后端格式
 * @param {Object} frontendData - 前端数据
 * @returns {Object} 后端格式数据
 */
const transformToBackendFormat = (frontendData) => {
  const {
    id,
    title,
    time,
    content,
    aiFeedback,
    eventType = 'daily_log',
    emotionType,
    emotionScore,
    tags
  } = frontendData;

  return {
    id,
    title,
    eventDate: time,
    content,
    aiReply: aiFeedback,
    eventType,
    emotionType,
    emotionScore,
    tags
  };
};

/**
 * 将后端数据格式转换为前端格式
 * @param {Object} backendData - 后端数据
 * @returns {Object} 前端格式数据
 */
export const transformToFrontendFormat = (backendData) => {
  if (!backendData) return null;

  const {
    id,
    userId,
    title,
    eventDate,
    content,
    aiReply,
    eventType,
    emotionType,
    emotionScore,
    tags,
    createTime
  } = backendData;

  return {
    id,
    userId,
    title: title || '',
    time: eventDate ? eventDate.split('T')[0] : '',
    content: content || '',
    aiFeedback: aiReply || '',
    eventType: eventType || 'daily_log',
    emotionType,
    emotionScore,
    tags: tags || [],
    createTime
  };
};

/**
 * 批量转换后端数据为前端格式
 * @param {Array} backendList - 后端数据列表
 * @returns {Array} 前端格式数据列表
 */
export const transformListToFrontend = (backendList) => {
  if (!Array.isArray(backendList)) return [];
  return backendList.map(transformToFrontendFormat);
};

export default {
  getEventList,
  getEventPage,
  getEventById,
  createEvent,
  updateEvent,
  deleteEvent,
  transformToFrontendFormat,
  transformListToFrontend
};
