// 教学活动分析相关接口
import { moduleRequest } from '@/utils/request'
import Mock from 'mockjs'

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

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


// 讨论数据 - 这是全部课程的汇总讨论数据
const discussionData = [
	{
		id: 1,
		title: 'JavaScript闭包概念理解',
		author: '张三',
		createTime: '2024-01-15 10:30',
		replyCount: 25,
		lastReplyTime: '2024-01-16 14:20'
	},
	{
		id: 2,
		title: 'Python数据分析库选择',
		author: '李四',
		createTime: '2024-01-14 16:45',
		replyCount: 18,
		lastReplyTime: '2024-01-15 09:15'
	},
	{
		id: 3,
		title: 'React组件生命周期',
		author: '王五',
		createTime: '2024-01-13 11:20',
		replyCount: 32,
		lastReplyTime: '2024-01-14 17:30'
	},
	{
		id: 4,
		title: '机器学习算法选择',
		author: '赵六',
		createTime: '2024-01-12 14:15',
		replyCount: 16,
		lastReplyTime: '2024-01-13 10:45'
	},
	{
		id: 5,
		title: '前端性能优化技巧',
		author: '钱七',
		createTime: '2024-01-11 09:30',
		replyCount: 45,
		lastReplyTime: '2024-01-15 16:20'
	},
	{
		id: 6,
		title: 'Vue3 Composition API使用心得',
		author: '孙八',
		createTime: '2024-01-10 15:20',
		replyCount: 38,
		lastReplyTime: '2024-01-14 11:30'
	},
	{
		id: 7,
		title: 'Node.js异步编程最佳实践',
		author: '周九',
		createTime: '2024-01-09 13:45',
		replyCount: 29,
		lastReplyTime: '2024-01-13 16:15'
	},
	{
		id: 8,
		title: '数据库索引优化策略',
		author: '陈十',
		createTime: '2024-01-08 11:20',
		replyCount: 22,
		lastReplyTime: '2024-01-12 15:40'
	},
	{
		id: 9,
		title: 'Web安全防护实践',
		author: '刘十一',
		createTime: '2024-01-07 14:30',
		replyCount: 19,
		lastReplyTime: '2024-01-11 09:25'
	},
	{
		id: 10,
		title: '移动端适配解决方案',
		author: '杨十二',
		createTime: '2024-01-06 16:15',
		replyCount: 27,
		lastReplyTime: '2024-01-10 13:50'
	},
	{
		id: 11,
		title: 'TypeScript类型系统深入',
		author: '黄十三',
		createTime: '2024-01-05 10:45',
		replyCount: 34,
		lastReplyTime: '2024-01-09 17:20'
	},
	{
		id: 12,
		title: 'Docker容器化部署',
		author: '吴十四',
		createTime: '2024-01-04 13:20',
		replyCount: 21,
		lastReplyTime: '2024-01-08 11:15'
	},
	{
		id: 13,
		title: 'GraphQL API设计',
		author: '郑十五',
		createTime: '2024-01-03 15:40',
		replyCount: 15,
		lastReplyTime: '2024-01-07 14:30'
	},
	{
		id: 14,
		title: '微服务架构实践',
		author: '王十六',
		createTime: '2024-01-02 09:15',
		replyCount: 28,
		lastReplyTime: '2024-01-06 16:45'
	},
	{
		id: 15,
		title: 'Redis缓存策略',
		author: '李十七',
		createTime: '2024-01-01 11:30',
		replyCount: 23,
		lastReplyTime: '2024-01-05 12:20'
	}
]

// 生成每周统计数据
const generateWeeklyStatsData = (options) => {
	const urlParts = options.url.split('?')
	const params = urlParts.length > 1 ? new URLSearchParams(urlParts[1]) : new URLSearchParams()
	const courseId = params.get('courseId') || ''

	// 全部课程数据应该是所有课程的总和
	const baseMultiplier = courseId ? 0.12 + Math.random() * 0.28 : 1.0 // 单个课程占总数的12%-40%

	// 基础数据 - 全部课程的总数据
	const baseWeeklyStats = {
		studentWeeklyPosts: 456,
		studentWeeklyReplies: 1678,
		teacherWeeklyPosts: 123,
		teacherWeeklyReplies: 567
	}

	const calculatedStats = {
		studentWeeklyPosts: Math.round(baseWeeklyStats.studentWeeklyPosts * baseMultiplier),
		studentWeeklyReplies: Math.round(baseWeeklyStats.studentWeeklyReplies * baseMultiplier),
		teacherWeeklyPosts: Math.round(baseWeeklyStats.teacherWeeklyPosts * baseMultiplier),
		teacherWeeklyReplies: Math.round(baseWeeklyStats.teacherWeeklyReplies * baseMultiplier)
	}

	return {
		code: 200,
		message: '获取成功',
		data: {
			studentWeeklyPosts: calculatedStats.studentWeeklyPosts,
			studentWeeklyReplies: calculatedStats.studentWeeklyReplies,
			studentAvgPostsPerDay: parseFloat((calculatedStats.studentWeeklyPosts / 7).toFixed(1)),
			teacherWeeklyPosts: calculatedStats.teacherWeeklyPosts,
			teacherWeeklyReplies: calculatedStats.teacherWeeklyReplies,
			teacherAvgPostsPerDay: parseFloat((calculatedStats.teacherWeeklyPosts / 7).toFixed(1))
		}
	}
}

// 生成讨论数据
const generateDiscussionData = (options) => {
	const urlParts = options.url.split('?')
	const params = urlParts.length > 1 ? new URLSearchParams(urlParts[1]) : new URLSearchParams()
	const courseId = params.get('courseId') || ''
	const page = parseInt(params.get('page')) || 1
	const pageSize = parseInt(params.get('pageSize')) || 10

	let filteredData = discussionData
	if (courseId) {
		// 单个课程只显示部分讨论数据，模拟该课程相关的讨论
		const courseDiscussionCount = Math.floor(discussionData.length * (0.2 + Math.random() * 0.3)) // 20%-50%
		filteredData = discussionData.slice(0, Math.max(courseDiscussionCount, 2)) // 至少显示2条
	}

	const startIndex = (page - 1) * pageSize
	const endIndex = startIndex + pageSize
	const pageData = filteredData.slice(startIndex, endIndex)

	return {
		code: 200,
		message: '获取成功',
		data: {
			list: pageData,
			total: filteredData.length,
			page,
			pageSize
		}
	}
}

// Mock 接口定义
Mock.mock(/\/api\/webapp\/teaching-analysis\/weekly-stats/, 'get', generateWeeklyStatsData)
Mock.mock(/\/api\/webapp\/teaching-analysis\/discussions/, 'get', generateDiscussionData)

// 导出的API函数
export const getCourseOptions = (params = {}) => {
	return request('disk/courseinfo/allList', params, 'get')
}
// 文档观看统计
export const getTeachingStats = (params = {}) => {
	return request('teachingActivity/documentStatistic', params, 'get')
}
// 文档跳出分析
export const documentJumpStatistic = (params = {}) => {
	return request('teachingActivity/documentJumpStatistic', params, 'get')
}

export const getWeeklyStats = (params = {}) => {
	return request('teaching-analysis/weekly-stats', params, 'get')
}
// 开课每日访问人数统计
export const getDailyVisits = (params = {}) => {
	return request('teachingActivity/courseOpenStatistic', params, 'get')
}

export const getDiscussionData = (params = {}) => {
	return request('teaching-analysis/discussions', params, 'get')
}

export const getDocumentStats = (params = {}) => {
	return request('teachingActivity/documentDetailStatistic', params, 'get')
}
