/**
 * 宠物管理相关API接口模块
 */

import BaseRequest from '../utils/request-helper.js'
import { LOADING_TEXTS } from '../config/constants.js'

// ==================== API方法 ====================

// ==================== 宠物基础信息管理API ====================

/**
 * 获取用户宠物列表
 * @param {Object} [params={}] 查询参数
 * @param {Object} [config={}] 自定义请求配置
 * @returns {Promise<Object>} 返回宠物列表和分页信息
 */
export const getPetsList = (params = {}, config = {}) => {
	return BaseRequest.get('/pets', params, 'AUTHENTICATED_QUERY', config)
}

/**
 * 获取宠物详细信息
 * @param {string|number} petId 宠物ID
 * @param {Object} [config={}] 自定义请求配置
 * @returns {Promise} 返回宠物详细信息
 */
export const getPetDetail = (petId, config = {}) => {
	const params = {}
	if (config.includeRecords) params.includeRecords = true
	if (config.includeHealth) params.includeHealth = true

	return BaseRequest.get(`/pets/${petId}`, params, 'AUTHENTICATED_QUERY_WITH_LOADING', config)
}

/**
 * 添加新宠物
 * @param {Object} petData 宠物信息数据
 * @param {Object} [config={}] 自定义请求配置
 * @returns {Promise} 返回新添加的宠物信息
 */
export const addPet = (petData, config = {}) => {
	return BaseRequest.post('/pets', petData, 'AUTHENTICATED_UPDATE', LOADING_TEXTS.ADD_PET, config)
}

/**
 * 更新宠物信息
 * @param {string|number} petId 宠物ID
 * @param {Object} petData 要更新的宠物数据
 * @param {Object} [config={}] 自定义请求配置
 * @returns {Promise} 返回更新后的宠物信息
 */
export const updatePet = (petId, petData, config = {}) => {
	return BaseRequest.put(`/pets/${petId}`, petData, 'AUTHENTICATED_UPDATE', LOADING_TEXTS.UPDATE_PET, config)
}

/**
 * 删除宠物
 * @param {string|number} petId 宠物ID
 * @param {Object} [config={}] 自定义请求配置
 * @returns {Promise<Object>} 返回删除结果
 */
export const deletePet = (petId, config = {}) => {
	const data = config.force ? { force: true } : {}
	return BaseRequest.delete(`/pets/${petId}`, data, 'AUTHENTICATED_DELETE', LOADING_TEXTS.DELETE_PET, config)
}

// ==================== 宠物记录管理API ====================

/**
 * 获取宠物记录列表
 * @param {string|number} petId 宠物ID
 * @param {Object} [params={}] 查询参数
 * @param {Object} [config={}] 自定义请求配置
 * @returns {Promise<Object>} 返回记录列表和分页信息
 */
export const getPetRecords = (petId, params = {}, config = {}) => {
	return BaseRequest.get(`/pets/${petId}/records`, params, 'AUTHENTICATED_QUERY', config)
}

/**
 * 添加宠物记录
 * @description 为指定宠物添加一条新记录
 * @param {string|number} petId 宠物ID
 * @param {Object} recordData 记录数据
 * @param {string} recordData.type 记录类型：'feeding' | 'health' | 'exercise' | 'grooming' | 'other'
 * @param {string} recordData.title 记录标题
 * @param {string} recordData.content 记录内容
 * @param {string} [recordData.recordTime] 记录时间，不传则使用当前时间
 * @param {string[]} [recordData.images] 相关图片URL数组
 * @param {Object} [recordData.metadata] 额外的元数据（如体重、体温等）
 * @param {Object} [config={}] 自定义请求配置
 * @returns {Promise<PetRecord>} 返回新添加的记录信息
 * @example
 * // 添加喂食记录
 * const feedingRecord = await addPetRecord(123, {
 *   type: 'feeding',
 *   title: '晚餐',
 *   content: '吃了狗粮200g',
 *   recordTime: '2024-01-15 18:00:00'
 * })
 *
 * // 添加健康记录
 * const healthRecord = await addPetRecord(123, {
 *   type: 'health',
 *   title: '体重测量',
 *   content: '今日体重测量结果',
 *   images: ['https://example.com/weight.jpg'],
 *   metadata: {
 *     weight: 25.5,
 *     temperature: 38.5
 *   }
 * })
 */
export const addPetRecord = (petId, recordData, config = {}) => {
	return BaseRequest.post(`/pets/${petId}/records`, recordData, 'AUTHENTICATED_UPDATE', LOADING_TEXTS.ADD_RECORD, config)
}

/**
 * 更新宠物记录
 * @description 更新指定的宠物记录信息
 * @param {string|number} petId 宠物ID
 * @param {string|number} recordId 记录ID
 * @param {Object} recordData 要更新的记录数据
 * @param {string} [recordData.title] 记录标题
 * @param {string} [recordData.content] 记录内容
 * @param {string} [recordData.recordTime] 记录时间
 * @param {string[]} [recordData.images] 相关图片URL数组
 * @param {Object} [recordData.metadata] 额外的元数据
 * @param {Object} [config={}] 自定义请求配置
 * @returns {Promise<PetRecord>} 返回更新后的记录信息
 * @example
 * // 更新记录内容
 * const updatedRecord = await updatePetRecord(123, 456, {
 *   content: '更新后的记录内容',
 *   metadata: { weight: 26.0 }
 * })
 */
export const updatePetRecord = (petId, recordId, recordData, config = {}) => {
	return BaseRequest.put(`/pets/${petId}/records/${recordId}`, recordData, 'AUTHENTICATED_UPDATE', LOADING_TEXTS.UPDATE_RECORD, config)
}

/**
 * 删除宠物记录
 * @description 删除指定的宠物记录
 * @param {string|number} petId 宠物ID
 * @param {string|number} recordId 记录ID
 * @param {Object} [config={}] 自定义请求配置
 * @returns {Promise<Object>} 返回删除结果
 * @example
 * // 删除记录
 * await deletePetRecord(123, 456)
 */
export const deletePetRecord = (petId, recordId, config = {}) => {
	return BaseRequest.delete(`/pets/${petId}/records/${recordId}`, {}, 'AUTHENTICATED_DELETE', LOADING_TEXTS.DELETE_RECORD, config)
}

// ==================== 宠物健康档案API ====================

/**
 * 获取宠物健康数据
 * @param {string|number} petId 宠物ID
 * @param {Object} params 查询参数
 * @param {Object} config 自定义请求配置
 * @returns {Promise} 返回健康数据
 */
export const getPetHealthData = (petId, params = {}, config = {}) => {
	return BaseRequest.get(`/pets/${petId}/health`, params, 'AUTHENTICATED_QUERY_WITH_LOADING', config)
}

/**
 * 获取宠物成长时间线
 * @param {string|number} petId 宠物ID
 * @param {Object} params 查询参数
 * @param {Object} config 自定义请求配置
 * @returns {Promise} 返回时间线事件数组
 */
export const getPetTimeline = (petId, params = {}, config = {}) => {
	return BaseRequest.get(`/pets/${petId}/timeline`, params, 'AUTHENTICATED_QUERY_WITH_LOADING', config)
}