import { Request, Response } from 'express';
import { ExamTaskModel } from '../models/examTask';

export class UserQuizController {
  static async generateQuiz(req: Request, res: Response) {
    try {
      const { userId, subjectId, taskId } = req.body;

      if (!userId) {
        return res.status(400).json({
          success: false,
          message: '用户ID不能为空'
        });
      }

      if (taskId) {
        const result = await ExamTaskModel.generateQuizQuestions(taskId, userId);
        res.json({
          success: true,
          data: {
            questions: result.questions,
            totalScore: result.totalScore,
            timeLimit: result.timeLimitMinutes
          }
        });
        return;
      }

      if (!subjectId) {
        return res.status(400).json({
          success: false,
          message: 'subjectId或taskId必须提供其一'
        });
      }

      const { QuestionModel, ExamSubjectModel } = await import('../models');
      const subject = await ExamSubjectModel.findById(subjectId);
      if (!subject) {
        return res.status(404).json({
          success: false,
          message: '考试科目不存在'
        });
      }

      const questions: Awaited<ReturnType<typeof QuestionModel.getRandomQuestions>> = [];

      for (const [type, ratio] of Object.entries(subject.typeRatios)) {
        if (ratio <= 0) continue;
        const typeScore = Math.floor((ratio / 100) * subject.totalScore);
        const avgScore = 10;
        const count = Math.max(1, Math.round(typeScore / avgScore));

        const categories = Object.entries(subject.categoryRatios)
          .filter(([, r]) => r > 0)
          .map(([c]) => c);

        const qs = await QuestionModel.getRandomQuestions(type as any, count, categories);
        questions.push(...qs);
      }

      const totalScore = questions.reduce((sum, q) => sum + q.score, 0);

      res.json({
        success: true,
        data: {
          questions,
          totalScore,
          timeLimit: subject.timeLimitMinutes
        }
      });
    } catch (error: any) {
      res.status(500).json({
        success: false,
        message: error.message || '生成试卷失败'
      });
    }
  }
}