// 文件模块相关接口
import { moduleRequest } from '@/utils/request'
import Mock from 'mockjs'

const request = moduleRequest(`/api/webapp`)

// Mock 数据配置
Mock.setup({
	timeout: '200-600'
})

// 生成课程选项数据
const generateCourseOptions = () => {
	return Mock.mock({
		code: 200,
		'data|8-12': [
			{
				'id|+1': 1,
				courseName:
					'@pick(["JavaScript基础教程", "Python数据分析", "React前端开发", "机器学习入门", "Vue3实战开发", "Node.js后端开发", "数据结构与算法", "计算机网络基础"])',
				courseId: 'course@integer(1, 100)'
			}
		]
	})
}

// 生成图表数据
const generateChartData = (options) => {
	const params = new URLSearchParams(options.url.split('?')[1])
	const courseId = params.get('courseId') || ''
	const timeRange = params.get('timeRange') || '30'

	// 根据课程和时间范围调整数据规模
	const timeMultiplier = { 7: 0.3, 30: 1, 90: 2.5, 365: 8 }[timeRange] || 1
	const courseMultiplier = courseId === '' ? 3.5 : 1

	const chapterCount = Math.floor(Math.random() * 4) + 5 // 5-8章
	const chapters = []
	const watchTimes = []
	const completedUsers = []
	const completionRates = []
	const notes = []
	const discussions = []
	const replies = []

	// 生成章节相关数据
	for (let i = 1; i <= chapterCount; i++) {
		chapters.push(`第${i}章`)

		// 观看时长：越后面的章节观看时长可能越短
		const baseWatchTime = 35 - i * 2 + Math.random() * 10
		watchTimes.push(Math.round(baseWatchTime * 10) / 10)

		// 完成用户数：递减趋势
		const baseUsers = Math.floor((1000 - i * 80) * timeMultiplier * courseMultiplier)
		completedUsers.push(baseUsers + Math.floor(Math.random() * 100))

		// 完成率：递减趋势
		const rate = Math.max(95 - i * 5 - Math.random() * 8, 60)
		completionRates.push(Math.round(rate * 10) / 10)

		// 互动数据
		const baseInteraction = Math.floor(baseUsers * 0.6)
		notes.push(Math.floor(baseInteraction * (0.6 + Math.random() * 0.4)))
		discussions.push(Math.floor(baseInteraction * (0.2 + Math.random() * 0.2)))
		replies.push(Math.floor(discussions[i - 1] * (2 + Math.random() * 2)))
	}

	// 学习进度分布 - 根据总体数据规模调整
	const totalUsers = Math.floor(800 * timeMultiplier * courseMultiplier)
	const progressDistribution = [
		{ value: Math.floor(totalUsers * 0.15), name: '0-25%' },
		{ value: Math.floor(totalUsers * 0.2), name: '26-50%' },
		{ value: Math.floor(totalUsers * 0.25), name: '51-75%' },
		{ value: Math.floor(totalUsers * 0.4), name: '76-100%' }
	]

	return {
		code: 200,
		data: {
			progressDistribution,
			timeTrend: {
				chapters,
				watchTime: watchTimes,
				completedUsers
			},
			chapterCompletion: {
				chapters,
				completionRates
			},
			interactionData: {
				chapters,
				notes,
				discussions,
				replies
			}
		}
	}
}
// Mock 接口定义
Mock.mock(/\/api\/webapp\/video-analysis\/course-options/, 'get', generateCourseOptions)
Mock.mock(/\/api\/webapp\/video-analysis\/charts/, 'get', generateChartData)

// 导出的API函数
export const videoAnalysisApi = {
	// 获取课程选项
	getCourseOptions() {
		return request('/video-analysis/course-options', '', 'get')
	},
	// 视频分析-观看人数统计
	watchUserCountProgress(params = {}) {
		return request('/disk/videoanalysis/watchUserCountProgress', params, 'get')
	},
	// 视频分析-讲义下载次数
	teachMaterialsDownloadCount(params = {}) {
		return request('/disk/videoanalysis/teachMaterialsDownloadCount', params, 'get')
	},
	// 视频分析-跳出时间分析
	jumpTimeAnalyse(params = {}) {
		return request('/disk/videoanalysis/jumpTimeAnalyse', params, 'get')
	},
	// 视频分析-互动统计分析
	interactionDataAnalyse(params = {}) {
		return request('/disk/videoanalysis/interactionDataAnalyse', params, 'get')
	},

	// 获取学员详细数据
	studyBehaviorDetailData(params = {}) {
		return request('/disk/videoanalysis/studyBehaviorDetailData', params, 'get')
	},

	// 获取章节数据
	videoDetailDataAnalysis(params = {}) {
		return request('/disk/videoanalysis/videoDetailDataAnalysis', params, 'get')
	},

	// 获取图表数据
	getChartData(params = {}) {
		return request('/video-analysis/charts', params, 'get')
	}
}
