import { Job } from 'bullmq';
import { Logger } from '@nestjs/common';
import {
    updateCourseLectureStats,
    updateSectionLectureStats
} from '@server/models/lecture/utils';
import { ObjectType } from '@nice/common';
import {
    updateCourseEnrollmentStats,
    updateCourseReviewStats
} from '@server/models/course/utils';
import { QueueJobType } from '../types';
const logger = new Logger('QueueWorker');
export default async function processJob(job: Job<any, any, QueueJobType>) {
    try {
        if (job.name === QueueJobType.UPDATE_STATS) {
            const { sectionId, courseId, type } = job.data;
            // 处理 section 统计
            if (sectionId) {
                await updateSectionLectureStats(sectionId);
                logger.debug(`Updated section stats for sectionId: ${sectionId}`);
            }
            // 如果没有 courseId，提前返回
            if (!courseId) {
                return;
            }
            // 处理 course 相关统计
            switch (type) {
                case ObjectType.LECTURE:
                    await updateCourseLectureStats(courseId);
                    break;
                case ObjectType.ENROLLMENT:
                    await updateCourseEnrollmentStats(courseId);
                    break;
                case ObjectType.POST:
                    await updateCourseReviewStats(courseId);
                    break;
                default:
                    logger.warn(`Unknown update stats type: ${type}`);
            }

            logger.debug(`Updated course stats for courseId: ${courseId}, type: ${type}`);
        }


    } catch (error: any) {
        logger.error(`Error processing stats update job: ${error.message}`, error.stack);
    }
}