// 学习行为分析相关接口
import { moduleRequest } from '@/utils/request'
import Mock from 'mockjs'

const request = moduleRequest(`/api/webapp/`)
import ForEach from "lodash-es/forEach";
// Mock 数据配置
Mock.setup({
	timeout: '200-600'
})

// 学院统计数据映射
const collegeStatsMap = {
	'': {
		totalCourses: 156,
		activeCourses: 142,
		courseAccessRate: 91.0,
		totalLogins: 12456,
		uniqueUsers: 3234,
		avgLoginPerUser: 3.85,
		totalOnlineTime: 2456,
		avgSessionTime: 45.2,
		peakOnlineUsers: 234
	},
	computer: {
		totalCourses: 45,
		activeCourses: 42,
		courseAccessRate: 93.3,
		totalLogins: 5234,
		uniqueUsers: 1234,
		avgLoginPerUser: 4.24,
		totalOnlineTime: 1234,
		avgSessionTime: 52.3,
		peakOnlineUsers: 156
	},
	business: {
		totalCourses: 38,
		activeCourses: 35,
		courseAccessRate: 92.1,
		totalLogins: 4567,
		uniqueUsers: 987,
		avgLoginPerUser: 4.63,
		totalOnlineTime: 987,
		avgSessionTime: 48.7,
		peakOnlineUsers: 123
	},
	art: {
		totalCourses: 28,
		activeCourses: 25,
		courseAccessRate: 89.3,
		totalLogins: 3123,
		uniqueUsers: 756,
		avgLoginPerUser: 4.13,
		totalOnlineTime: 756,
		avgSessionTime: 42.1,
		peakOnlineUsers: 89
	},
	science: {
		totalCourses: 35,
		activeCourses: 32,
		courseAccessRate: 91.4,
		totalLogins: 3890,
		uniqueUsers: 892,
		avgLoginPerUser: 4.36,
		totalOnlineTime: 892,
		avgSessionTime: 46.8,
		peakOnlineUsers: 112
	},
	engineering: {
		totalCourses: 42,
		activeCourses: 39,
		courseAccessRate: 92.9,
		totalLogins: 4123,
		uniqueUsers: 1045,
		avgLoginPerUser: 3.95,
		totalOnlineTime: 1045,
		avgSessionTime: 47.5,
		peakOnlineUsers: 134
	}
}

// 学员数据
const studentsData = [
	{
		id: 1,
		name: '张三',
		studentId: '2021001',
		college: '计算机学院',
		major: '计算机科学与技术',
		grade: '大三',
		overallProgress: 85.6,
		courses: [
			{
				id: 1,
				name: 'JavaScript程序设计',
				progress: 92,
				completedAssignments: 8,
				totalAssignments: 10,
				discussionParticipation: 15,
				questionCount: 5
			},
			{
				id: 2,
				name: '数据结构与算法',
				progress: 78,
				completedAssignments: 6,
				totalAssignments: 8,
				discussionParticipation: 12,
				questionCount: 3
			}
		]
	},
	{
		id: 2,
		name: '李四',
		studentId: '2021002',
		college: '商学院',
		major: '工商管理',
		grade: '大二',
		overallProgress: 72.3,
		courses: [
			{
				id: 3,
				name: '管理学原理',
				progress: 85,
				completedAssignments: 7,
				totalAssignments: 9,
				discussionParticipation: 8,
				questionCount: 2
			},
			{
				id: 4,
				name: '市场营销学',
				progress: 65,
				completedAssignments: 4,
				totalAssignments: 7,
				discussionParticipation: 6,
				questionCount: 1
			}
		]
	},
	{
		id: 3,
		name: '王五',
		studentId: '2021003',
		college: '艺术学院',
		major: '视觉传达设计',
		grade: '大一',
		overallProgress: 91.2,
		courses: [
			{
				id: 5,
				name: '设计基础',
				progress: 95,
				completedAssignments: 10,
				totalAssignments: 10,
				discussionParticipation: 20,
				questionCount: 8
			},
			{
				id: 6,
				name: '色彩构成',
				progress: 88,
				completedAssignments: 8,
				totalAssignments: 9,
				discussionParticipation: 15,
				questionCount: 6
			}
		]
	},
	{
		id: 4,
		name: '赵六',
		studentId: '2021004',
		college: '理学院',
		major: '数学与应用数学',
		grade: '大二',
		overallProgress: 79.4,
		courses: [
			{
				id: 7,
				name: '高等数学',
				progress: 82,
				completedAssignments: 9,
				totalAssignments: 12,
				discussionParticipation: 10,
				questionCount: 4
			},
			{
				id: 8,
				name: '线性代数',
				progress: 76,
				completedAssignments: 6,
				totalAssignments: 8,
				discussionParticipation: 8,
				questionCount: 2
			}
		]
	},
	{
		id: 5,
		name: '孙七',
		studentId: '2021005',
		college: '工程学院',
		major: '机械工程',
		grade: '大三',
		overallProgress: 88.1,
		courses: [
			{
				id: 9,
				name: '机械设计',
				progress: 90,
				completedAssignments: 11,
				totalAssignments: 12,
				discussionParticipation: 18,
				questionCount: 7
			},
			{
				id: 10,
				name: '工程力学',
				progress: 86,
				completedAssignments: 8,
				totalAssignments: 10,
				discussionParticipation: 14,
				questionCount: 5
			}
		]
	}
]

// Mock 接口定义
// Mock.mock(/\/api\/webapp\/analysis\/colleges/, 'get', {
// 	code: 200,
// 	message: '获取成功',
// 	data: [
// 		{ id: '', name: '全部学院' },
// 		{ id: 'computer', name: '计算机学院' },
// 		{ id: 'business', name: '商学院' },
// 		{ id: 'art', name: '艺术学院' },
// 		{ id: 'science', name: '理学院' },
// 		{ id: 'engineering', name: '工程学院' }
// 	]
// })

Mock.mock(/\/api\/webapp\/analysis\/college-stats/, 'get', (options) => {
	const params = new URLSearchParams(options.url.split('?')[1])
	const collegeId = params.get('collegeId') || ''

	return {
		code: 200,
		message: '获取成功',
		data: collegeStatsMap[collegeId] || collegeStatsMap['']
	}
})

Mock.mock(/\/api\/webapp\/analysis\/login-time-distribution/, 'get', (options) => {
	const params = new URLSearchParams(options.url.split('?')[1])
	const timeRange = parseInt(params.get('timeRange')) || 30
	const collegeId = params.get('collegeId') || ''

	// 基础时间段
	const hours = [
		'00:00',
		'02:00',
		'04:00',
		'06:00',
		'08:00',
		'10:00',
		'12:00',
		'14:00',
		'16:00',
		'18:00',
		'20:00',
		'22:00'
	]

	// 根据时间范围生成不同的数据模式
	let loginCounts = []

	switch (timeRange) {
		case 7: // 最近7天 - 数据相对较少，波动较大
			loginCounts = [12, 8, 3, 2, 45, 89, 67, 123, 156, 134, 178, 98]
			break
		case 30: // 最近30天 - 中等数据量
			loginCounts = [45, 23, 12, 8, 156, 234, 189, 267, 312, 298, 345, 289]
			break
		case 90: // 最近90天 - 数据量较大，趋势更明显
			loginCounts = [134, 89, 45, 23, 456, 678, 567, 789, 923, 867, 1023, 756]
			break
		case 365: // 最近一年 - 数据量最大，趋势稳定
			loginCounts = [567, 345, 234, 123, 1234, 1789, 1567, 2134, 2456, 2298, 2678, 1987]
			break
		default:
			loginCounts = [45, 23, 12, 8, 156, 234, 189, 267, 312, 298, 345, 289]
	}

	// 根据学院调整数据（不同学院有不同的活跃时段特点）
	if (collegeId) {
		const collegeMultipliers = {
			computer: [0.8, 0.7, 0.5, 0.4, 1.2, 1.3, 1.1, 1.4, 1.5, 1.3, 1.6, 1.2], // 计算机学院晚上更活跃
			business: [0.6, 0.5, 0.3, 0.2, 1.1, 1.4, 1.3, 1.2, 1.1, 1.0, 0.9, 0.8], // 商学院工作时间更活跃
			art: [1.2, 1.0, 0.8, 0.6, 0.9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.3], // 艺术学院夜猫子较多
			science: [0.7, 0.6, 0.4, 0.3, 1.0, 1.2, 1.1, 1.3, 1.2, 1.1, 1.0, 0.9], // 理学院相对规律
			engineering: [0.8, 0.7, 0.5, 0.4, 1.1, 1.3, 1.2, 1.4, 1.3, 1.2, 1.1, 1.0] // 工程学院
		}

		const multiplier = collegeMultipliers[collegeId]
		if (multiplier) {
			loginCounts = loginCounts.map((count, index) => Math.round(count * multiplier[index]))
		}
	}

	return {
		code: 200,
		message: '获取成功',
		data: {
			hours,
			loginCounts
		}
	}
})

Mock.mock(/\/api\/webapp\/analysis\/course-heat/, 'get', (options) => {
	const params = new URLSearchParams(options.url.split('?')[1])
	const timeRange = parseInt(params.get('timeRange')) || 30
	const collegeId = params.get('collegeId') || ''

	// 不同学院的课程数据
	const collegeCoursesMap = {
		'': {
			courses: [
				'JavaScript程序设计',
				'数据结构与算法',
				'管理学原理',
				'设计基础',
				'市场营销学',
				'色彩构成',
				'Python数据分析',
				'React前端开发'
			],
			baseVisits: [1234, 1156, 987, 856, 789, 756, 678, 645]
		},
		computer: {
			courses: [
				'JavaScript程序设计',
				'数据结构与算法',
				'Python数据分析',
				'React前端开发',
				'Java程序设计',
				'数据库原理',
				'计算机网络',
				'操作系统'
			],
			baseVisits: [1456, 1234, 1123, 987, 856, 789, 678, 567]
		},
		business: {
			courses: [
				'管理学原理',
				'市场营销学',
				'财务管理',
				'人力资源管理',
				'战略管理',
				'组织行为学',
				'商务谈判',
				'创业管理'
			],
			baseVisits: [1345, 1234, 1098, 987, 876, 765, 654, 543]
		},
		art: {
			courses: ['设计基础', '色彩构成', '平面设计', 'UI设计', '插画设计', '品牌设计', '包装设计', '网页设计'],
			baseVisits: [1234, 1123, 1012, 901, 789, 678, 567, 456]
		},
		science: {
			courses: ['高等数学', '线性代数', '概率论与数理统计', '数学分析', '离散数学', '数值分析', '运筹学', '数学建模'],
			baseVisits: [1567, 1345, 1234, 1123, 1012, 901, 789, 678]
		},
		engineering: {
			courses: ['机械设计', '工程力学', '材料力学', '流体力学', '热力学', '控制工程', '机械制图', '工程材料'],
			baseVisits: [1456, 1345, 1234, 1123, 1012, 901, 789, 678]
		}
	}

	const courseData = collegeCoursesMap[collegeId] || collegeCoursesMap['']

	// 根据时间范围调整访问量
	let timeMultiplier = 1
	switch (timeRange) {
		case 7:
			timeMultiplier = 0.3
			break
		case 30:
			timeMultiplier = 1
			break
		case 90:
			timeMultiplier = 2.5
			break
		case 365:
			timeMultiplier = 8
			break
	}

	const visits = courseData.baseVisits.map((visit) => Math.round(visit * timeMultiplier * (0.8 + Math.random() * 0.4)))

	return {
		code: 200,
		message: '获取成功',
		data: {
			courses: courseData.courses,
			visits
		}
	}
})

Mock.mock(/\/api\/webapp\/analysis\/college-course-details/, 'get', {
	code: 200,
	message: '获取成功',
	data: [
		{
			id: 1,
			name: '计算机学院',
			courseCount: 45,
			totalVisits: 5234,
			avgCompletionRate: 78.5,
			assignmentSubmissionRate: 85.2,
			dropoutRate: 10.2
		},
		{
			id: 2,
			name: '商学院',
			courseCount: 38,
			totalVisits: 4567,
			avgCompletionRate: 72.3,
			assignmentSubmissionRate: 79.8,
			dropoutRate: 12.5
		},
		{
			id: 3,
			name: '艺术学院',
			courseCount: 28,
			totalVisits: 3123,
			avgCompletionRate: 68.9,
			assignmentSubmissionRate: 76.4,
			dropoutRate: 15.2
		},
		{
			id: 4,
			name: '理学院',
			courseCount: 35,
			totalVisits: 3890,
			avgCompletionRate: 75.2,
			assignmentSubmissionRate: 82.1,
			dropoutRate: 13.8
		},
		{
			id: 5,
			name: '工程学院',
			courseCount: 42,
			totalVisits: 4123,
			avgCompletionRate: 76.8,
			assignmentSubmissionRate: 83.5,
			homeworkFinishRate: 11.7
		}
	]
})

Mock.mock(/\/api\/webapp\/analysis\/search-students/, 'get', (options) => {
	const params = new URLSearchParams(options.url.split('?')[1])
	const keyword = params.get('keyword') || ''

	let filteredStudents = studentsData
	if (keyword) {
		filteredStudents = studentsData.filter(
			(student) => student.name.includes(keyword) || student.studentId.includes(keyword)
		)
	}

	return {
		code: 200,
		message: '获取成功',
		data: filteredStudents
	}
})

// API 函数导出
export const getCollegeList = async (params) => {
	let res = await request('disk/studyBehavior/collegeCourseDetailProgress', params, 'get')
	console.log('什么呢1233', res)
	let list = []
	ForEach(res.records,(item) => {
		list.push({id : item.collegeId , name : item.collegeIdName})
	})


	let json = {
		code: 200,
		data: list
	}
	// {
	// 	"RESOURCE_TOTAL": 34,  总资源数
	// 	"TOTAL_STORAGE_SIZE": 20480, 总存储空间
	// 	"COLLECT_TOTAL": 9, 总收藏数
	// 	"WATCH_TOTAL": 471, 总观看数
	// 	"storageSize": 1726487824,
	// 	"totalStorageSize": 21474836480
	// }
	// totalResources: '0',
	// totalStorage: '0 TB',
	// totalCapacity: '10 TB',
	// usageRate: '0%',
	// totalViews: '0',
	// totalFavorites: '0'
	// 真实接口调用：
	// return request('/resource-library/summary', params, 'get')
	console.log('什么呢', json)
	return list
	// return request('analysis/colleges', '', 'get')
}

export const getCollegeStats = async (params) => {
	let res1 = await request('disk/studyBehavior/courseVisitProgress', params, 'get')
	let res2 = await request('disk/studyBehavior/userLoginProgress', params, 'get')
	let res3 = await request('disk/studyBehavior/watchDurationProgress', params, 'get')
		//
	// console.log('courseVisitProgress', res)
	let json = {
		code: 200,
		data : {...res1,...res2,...res3}
	}
	return json
}

export const getLoginTimeDistribution = async (params) => {
	let res = await request('disk/studyBehavior/loginFrameDistribution', params, 'get')
	let hours = []
	let loginCounts = []
	ForEach(res.records,(item) => {
		hours.push(item.createTime)
		loginCounts.push(item.COUNT)
	})


	let json = {
		code: 200,
		hours,
		loginCounts
	}
	return json


}

export const getCourseHeat = async (params) => {
	let res = await request('disk/studyBehavior/courseVisitHeatRank', params, 'get')
	let courses = []
	let visits = []
	ForEach(res.records,(item) => {
		courses.push(item.courseName)
		visits.push(item.watchCount)
	})


	let json = {
		code: 200,
		courses,
		visits
	}
	return json
}

export const getCollegeCourseDetails = async (params) => {
	let res = await request('/disk/studyBehavior/collegeCourseDetailProgress', params, 'get')
	console.log('1233123',res)
	let json = {
		code: 200,
		data : res.records
	}
	return json
}

export const searchStudents = async (params) => {


	let res = await request('disk/studyBehavior/studentAnalyse', params, 'get')

	console.log('1120',res)
	let json = {
		code: 200,
		data : res.records
	}
	return json
}
