/** 评分列表假数据，接口就绪后替换 */

export const EMPTY_REVIEW_TEXT = '该用户很懒，什么都没有留下'

export const CONTENT_COLLAPSE_LEN = 72

export const MOCK_RATING_LIST = [
	{
		id: 'r1',
		serviceName: '斯诺克技术指导-个性化陪练',
		score: 3.2,
		content:
			'教练很专业，讲解耐心，练了一下午进步明显。场地环境也不错，就是周末人有点多，建议提前预约时间段。',
		avatar: '',
		nickName: '用户***8',
		createTime: '2025-06-03 12:25:18',
	},
	{
		id: 'r2',
		serviceName: '中式八球入门教学',
		score: 5.0,
		content: '非常满意，从零基础到能稳定进球，老师节奏把控很好，强烈推荐！',
		avatar: '',
		nickName: '台球爱好者',
		createTime: '2025-06-01 18:40:00',
	},
	{
		id: 'r3',
		serviceName: '九球战术陪练',
		score: 4.5,
		content: '',
		avatar: '',
		nickName: '阿***明',
		createTime: '2025-05-28 09:15:33',
	},
	{
		id: 'r4',
		serviceName: '斯诺克技术指导-个性化陪练',
		score: 2.0,
		content: '一般般吧，时间有点赶，希望下次能多留一点练习时间。',
		avatar: '',
		nickName: '路人甲',
		createTime: '2025-05-20 14:02:11',
	},
	{
		id: 'r5',
		serviceName: '中式八球进阶课程',
		score: 4.8,
		content:
			'技术细节讲得很到位，尤其是走位和防守思路，对我帮助很大。已经预约了下一节课，期待继续提升。',
		avatar: '',
		nickName: '老球友***2',
		createTime: '2025-05-01 12:25:00',
	},
]

export function parseTime(str) {
	if (!str) return 0
	return new Date(String(str).replace(/-/g, '/')).getTime()
}

export function formatReviewTime(timeStr) {
	if (!timeStr) return ''
	const d = new Date(String(timeStr).replace(/-/g, '/'))
	const m = d.getMonth() + 1
	const day = d.getDate()
	const hh = String(d.getHours()).padStart(2, '0')
	const mm = String(d.getMinutes()).padStart(2, '0')
	return `${m}月${day}日 ${hh}:${mm}`
}

export function formatScore(score) {
	const num = Number(score)
	if (!Number.isFinite(num)) return '0.0'
	return num.toFixed(1)
}

export function normalizeRatingItem(raw, index) {
	const content = (raw.cContent || raw.content || raw.cText || raw.text || '').trim()
	const score = Number(raw.nStar ?? raw.score ?? 0)
	return {
		id: raw.id || raw.cId || `api-${index}`,
		serviceName:
			raw.cServiceName ||
			raw.serviceName ||
			raw.cOrderTitle ||
			raw.cProjectName ||
			'服务项目',
		score: Number.isFinite(score) ? score : 0,
		content,
		displayContent: content || EMPTY_REVIEW_TEXT,
		hasContent: !!content,
		avatar: raw.cAvatar || raw.avatar || raw.cHeadImg || '',
		nickName: raw.cNickName || raw.cName || raw.userName || '用户',
		createTime: raw.dtCreateTime || raw.dTime || raw.cTime || raw.completionTime || '',
		needExpand: content.length > CONTENT_COLLAPSE_LEN,
	}
}

export function sortByTimeDesc(list) {
	return [...list].sort((a, b) => parseTime(b.createTime) - parseTime(a.createTime))
}
