/**
 * 用户档案服务
 * 处理用户档案的增删改查
 * 与Web端(life-script/src/services/userProfile.js)保持一致
 */

import { get, post, put } from './request.js'

/**
 * 获取当前用户档案
 * @returns {Promise<Object|null>} 用户档案
 */
export const getCurrentProfile = async () => {
  const response = await get('/user-profile/me')
  return response
}

/**
 * 创建用户档案
 * @param {Object} profileData - 档案数据
 * @returns {Promise<Object>} 创建的档案
 */
export const createProfile = async (profileData) => {
  // 转换前端数据格式为后端格式
  const requestData = transformToBackendFormat(profileData)
  const response = await post('/user-profile/create', requestData)
  return response
}

/**
 * 更新用户档案
 * @param {Object} profileData - 档案数据（必须包含id）
 * @returns {Promise<Object>} 更新后的档案
 */
export const updateProfile = async (profileData) => {
  const requestData = transformToBackendFormat(profileData)
  const response = await put('/user-profile/update', requestData)
  return response
}

/**
 * 将前端数据格式转换为后端格式
 * @param {Object} frontendData - 前端数据
 * @returns {Object} 后端格式数据
 */
const transformToBackendFormat = (frontendData) => {
  const {
    id,
    nickname,
    gender,
    zodiac,
    profession,
    mbti,
    hobbies,
    childhood,
    joy,
    low,
    future
  } = frontendData

  return {
    id,
    nickname,
    gender,
    zodiac,
    profession,
    mbti,
    // 兴趣爱好转为JSON字符串
    hobbies: Array.isArray(hobbies) ? JSON.stringify(hobbies) : hobbies,
    // 童年经历
    childhoodDate: childhood?.date || null,
    childhoodContent: childhood?.text || null,
    // 高光时刻（对应前端的joy）
    peakDate: joy?.date || null,
    peakContent: joy?.text || null,
    // 低谷时期（对应前端的low）
    valleyDate: low?.date || null,
    valleyContent: low?.text || null,
    // 未来期许
    futureVision: future?.vision || null,
    // 理想生活状态
    idealLife: future?.ideal || null
  }
}

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

  const {
    id,
    userId,
    nickname,
    gender,
    zodiac,
    profession,
    mbti,
    hobbies,
    childhoodDate,
    childhoodContent,
    peakDate,
    peakContent,
    valleyDate,
    valleyContent,
    futureVision,
    idealLife
  } = backendData

  return {
    id,
    userId,
    nickname: nickname || '',
    gender: gender || '',
    zodiac: zodiac || '',
    profession: profession || '',
    mbti: mbti || '',
    // 兴趣爱好从JSON字符串解析
    hobbies: hobbies ? (typeof hobbies === 'string' ? JSON.parse(hobbies) : hobbies) : [],
    // 童年经历
    childhood: {
      date: childhoodDate || '',
      text: childhoodContent || ''
    },
    // 高光时刻
    joy: {
      date: peakDate || '',
      text: peakContent || ''
    },
    // 低谷时期
    low: {
      date: valleyDate || '',
      text: valleyContent || ''
    },
    // 未来期许
    future: {
      vision: futureVision || '',
      ideal: idealLife || ''
    }
  }
}

export default {
  getCurrentProfile,
  createProfile,
  updateProfile,
  transformToFrontendFormat
}
