<?php

namespace App\Services;

use App\Models\StudentExercise;
use App\Models\MistakeRecord;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;

/**
 * 学生作答分析服务
 * 负责处理学生作答结果的保存、分析和掌握度更新
 */
class StudentAnswerAnalysisService
{
    public function __construct(
        private readonly LocalAIAnalysisService $aiAnalysisService,
        private readonly ExamAnswerAnalysisService $examAnswerAnalysisService
    ) {}

    /**
     * 保存作答记录
     */
    public function saveAnswerRecord(array $data): array
    {
        // 生成唯一记录ID
        $recordId = 'ans_' . Str::uuid()->toString();

        // 计算统计数据
        $answers = $data['answers'];
        $correctCount = 0;
        $wrongCount = 0;
        $totalScore = 0;
        $obtainedScore = 0;

        foreach ($answers as $answer) {
            $score = (float) ($answer['score'] ?? 0);
            $maxScore = (float) ($answer['max_score'] ?? $score);
            $totalScore += $maxScore;
            $obtainedScore += $score;

            if ($answer['is_correct']) {
                $correctCount++;
            } else {
                $wrongCount++;
            }
        }

        $accuracyRate = ($correctCount + $wrongCount) > 0
            ? round($correctCount / ($correctCount + $wrongCount), 4)
            : 0;

        // 保存到 student_exercises 表
        foreach ($answers as $answer) {
            StudentExercise::create([
                'student_id' => $data['student_id'],
                'question_id' => $answer['question_id'],
                'question_content' => json_encode([
                    'question_id' => $answer['question_id'],
                    'question_number' => $answer['question_number'] ?? null,
                    'paper_id' => $data['paper_id'],
                ]),
                'student_answer' => $answer['student_answer'] ?? '',
                'correct_answer' => $answer['correct_answer'] ?? '',
                'is_correct' => $answer['is_correct'],
                'submission_status' => 'completed',
                'kp_code' => $answer['knowledge_point'] ?? null,
                'difficulty_level' => 0.5, // 默认难度
                'time_spent_seconds' => 0, // 默认耗时
                'created_at' => $data['answer_time'] ?? now(),
                'updated_at' => now(),
            ]);

            // 保存错题记录
            if (!$answer['is_correct']) {
                $this->saveMistakeRecord($data, $answer);
            }
        }

        return [
            'record_id' => $recordId,
            'paper_id' => $data['paper_id'],
            'student_id' => $data['student_id'],
            'total_score' => $totalScore,
            'obtained_score' => $obtainedScore,
            'accuracy_rate' => $accuracyRate,
            'correct_count' => $correctCount,
            'wrong_count' => $wrongCount,
            'total_questions' => count($answers),
        ];
    }

    /**
     * 保存错题记录
     */
    private function saveMistakeRecord(array $data, array $answer): void
    {
        try {
            // 检查是否已存在相同的错题记录
            $existing = MistakeRecord::where('student_id', $data['student_id'])
                ->where('question_id', $answer['question_id'])
                ->first();

            if ($existing) {
                // 更新现有记录
                $existing->increment('review_count');
                $existing->update([
                    'student_answer' => $answer['student_answer'] ?? '',
                    'correct_answer' => $answer['correct_answer'] ?? '',
                    'updated_at' => now(),
                ]);
            } else {
                // 创建新记录
                MistakeRecord::create([
                    'student_id' => $data['student_id'],
                    'question_id' => $answer['question_id'],
                    'source' => MistakeRecord::SOURCE_EXAM,
                    'question_text' => json_encode([
                        'question_number' => $answer['question_number'] ?? null,
                        'paper_id' => $data['paper_id'],
                        'question_type' => $answer['question_type'] ?? null,
                    ]),
                    'student_answer' => $answer['student_answer'] ?? '',
                    'correct_answer' => $answer['correct_answer'] ?? '',
                    'knowledge_point' => $answer['knowledge_point'] ?? null,
                    'error_type' => $this->guessErrorType($answer),
                    'review_status' => MistakeRecord::REVIEW_STATUS_PENDING,
                    'review_count' => 0,
                    'force_review' => false,
                    'is_favorite' => false,
                    'in_retry_list' => false,
                    'difficulty' => 0.5,
                    'mastery_level' => 0.0,
                ]);
            }
        } catch (\Exception $e) {
            Log::warning('保存错题记录失败', [
                'student_id' => $data['student_id'],
                'question_id' => $answer['question_id'],
                'error' => $e->getMessage(),
            ]);
        }
    }

    /**
     * 猜测错误类型
     */
    private function guessErrorType(array $answer): string
    {
        // 根据题型和答案特征猜测错误类型
        $questionType = $answer['question_type'] ?? '';

        if ($questionType === 'choice') {
            return MistakeRecord::ERROR_TYPE_CARELESS;
        }

        if ($questionType === 'fill' || $questionType === 'answer') {
            // 检查是否部分正确
            if (isset($answer['step_scores']) && is_array($answer['step_scores'])) {
                $totalSteps = count($answer['step_scores']);
                $correctSteps = array_sum($answer['step_scores']);
                if ($correctSteps > 0 && $correctSteps < $totalSteps) {
                    return MistakeRecord::ERROR_TYPE_PROCEDURE ?? MistakeRecord::ERROR_TYPE_CALCULATION;
                }
            }
        }

        return MistakeRecord::ERROR_TYPE_OTHER;
    }

    /**
     * 保存分析结果
     */
    public function saveAnalysisResults(array $answerRecord, array $analysisData, array $questionAnalyses): void
    {
        try {
            // 生成分析ID
            $analysisId = 'analysis_' . Str::uuid()->toString();

            // 保存分析记录到 PostgreSQL
            DB::connection('pgsql')->table('answer_analysis_records')->insert([
                'analysis_id' => $analysisId,
                'exam_id' => $answerRecord['paper_id'],
                'student_id' => $answerRecord['student_id'],
                'ocr_record_id' => 0, // 如果是系统试卷，没有OCR记录
                'status' => 'completed',
                'analysis_results' => json_encode($analysisData),
                'completed_at' => now(),
                'created_at' => now(),
                'updated_at' => now(),
            ]);

            // 获取分析记录的ID
            $analysisRecordId = DB::connection('pgsql')
                ->table('answer_analysis_records')
                ->where('analysis_id', $analysisId)
                ->value('id');

            // 保存每道题的分析结果
            foreach ($questionAnalyses as $questionAnalysis) {
                DB::connection('pgsql')->table('question_analysis_results')->insert([
                    'analysis_record_id' => $analysisRecordId,
                    'question_id' => $questionAnalysis['question_id'],
                    'question_number' => $questionAnalysis['question_number'] ?? null,
                    'kp_code' => $questionAnalysis['kp_code'] ?? null,
                    'student_answer' => $questionAnalysis['student_answer'] ?? '',
                    'correct_answer' => $questionAnalysis['correct_answer'] ?? '',
                    'is_correct' => $questionAnalysis['is_correct'] ?? false,
                    'score_obtained' => $questionAnalysis['score_obtained'] ?? 0,
                    'max_score' => $questionAnalysis['max_score'] ?? 0,
                    'ai_analysis' => $questionAnalysis['ai_analysis'] ?? null,
                    'learning_suggestions' => json_encode($questionAnalysis['suggestions'] ?? []),
                    'created_at' => now(),
                    'updated_at' => now(),
                ]);

                // 更新掌握度
                if (!empty($questionAnalysis['kp_code'])) {
                    $this->updateMasteryForQuestion(
                        $answerRecord['student_id'],
                        $questionAnalysis['kp_code'],
                        $questionAnalysis['is_correct'],
                        $questionAnalysis['difficulty'] ?? 0.5
                    );
                }
            }

            Log::info('分析结果已保存', [
                'student_id' => $answerRecord['student_id'],
                'paper_id' => $answerRecord['paper_id'],
                'analysis_id' => $analysisId,
                'question_count' => count($questionAnalyses),
            ]);

        } catch (\Exception $e) {
            Log::error('保存分析结果失败', [
                'student_id' => $answerRecord['student_id'],
                'paper_id' => $answerRecord['paper_id'],
                'error' => $e->getMessage(),
            ]);
        }
    }

    /**
     * 为单个题目更新掌握度
     */
    private function updateMasteryForQuestion(string $studentId, string $kpCode, bool $isCorrect, float $difficulty): void
    {
        try {
            // 获取当前掌握度
            $currentMastery = 0.5; // 默认值
            $existingMastery = DB::connection('pgsql')
                ->table('student_knowledge_mastery')
                ->where('student_id', $studentId)
                ->where('kp_code', $kpCode)
                ->first();

            if ($existingMastery) {
                $currentMastery = (float) $existingMastery->mastery_level;
            }

            // 使用AI分析服务更新掌握度
            $result = $this->aiAnalysisService->updateMastery(
                $studentId,
                $kpCode,
                $currentMastery,
                $isCorrect,
                $difficulty
            );

            Log::debug('掌握度已更新', [
                'student_id' => $studentId,
                'kp_code' => $kpCode,
                'old_mastery' => $result['old_mastery'],
                'new_mastery' => $result['new_mastery'],
                'change' => $result['change'],
            ]);

        } catch (\Exception $e) {
            Log::warning('更新掌握度失败', [
                'student_id' => $studentId,
                'kp_code' => $kpCode,
                'error' => $e->getMessage(),
            ]);
        }
    }

    /**
     * 创建掌握度快照
     */
    public function createMasterySnapshot(string $studentId, ?string $paperId = null, ?string $answerRecordId = null): ?array
    {
        // 使用AI分析服务创建快照
        return $this->aiAnalysisService->createMasterySnapshot($studentId, $paperId, $answerRecordId);
    }

    /**
     * 获取学生的学习历史
     */
    public function getStudentLearningHistory(string $studentId, int $limit = 10): array
    {
        try {
            $exercises = StudentExercise::where('student_id', $studentId)
                ->orderBy('created_at', 'desc')
                ->limit($limit)
                ->get()
                ->toArray();

            $mistakes = MistakeRecord::where('student_id', $studentId)
                ->orderBy('created_at', 'desc')
                ->limit($limit)
                ->get()
                ->toArray();

            // 使用AI分析服务获取掌握度数据
            $masteryData = $this->aiAnalysisService->getStudentMastery($studentId);

            // 获取掌握度快照历史
            $snapshots = DB::connection('pgsql')
                ->table('knowledge_point_mastery_snapshots')
                ->where('student_id', $studentId)
                ->orderBy('snapshot_time', 'desc')
                ->limit($limit)
                ->get()
                ->toArray();

            return [
                'exercises' => $exercises,
                'mistakes' => $mistakes,
                'mastery_data' => $masteryData['data'] ?? [],
                'mastery_snapshots' => $snapshots,
                'summary' => [
                    'total_exercises' => StudentExercise::where('student_id', $studentId)->count(),
                    'total_mistakes' => MistakeRecord::where('student_id', $studentId)->count(),
                    'mastery_snapshots_count' => count($snapshots),
                    'total_mastery_items' => count($masteryData['data'] ?? []),
                ],
            ];

        } catch (\Exception $e) {
            Log::error('获取学习历史失败', [
                'student_id' => $studentId,
                'error' => $e->getMessage(),
            ]);
            return [];
        }
    }

    /**
     * 使用增强版步骤级分析算法
     *
     * 这是基于《卷子分析思考.md》思路的增强版分析方法
     * 支持步骤级分析、知识点映射和智能出卷推荐
     *
     * @param array $examData 考试数据
     * @return array 分析结果
     */
    public function analyzeWithEnhancedSteps(array $examData): array
    {
        Log::info('StudentAnswerAnalysisService: 开始增强版步骤级分析', [
            'exam_id' => $examData['exam_id'] ?? 'unknown',
            'student_id' => $examData['student_id'] ?? 'unknown',
            'has_steps' => !empty(array_filter($examData['questions'] ?? [], fn($q) => !empty($q['steps'])))
        ]);

        try {
            // 使用增强的分析算法
            $result = $this->examAnswerAnalysisService->analyzeExamAnswers($examData);

            Log::info('StudentAnswerAnalysisService: 增强版步骤级分析完成', [
                'exam_id' => $examData['exam_id'],
                'student_id' => $examData['student_id'],
                'analyzed_knowledge_points' => count($result['knowledge_point_analysis'] ?? [])
            ]);

            return $result;

        } catch (\Exception $e) {
            Log::error('StudentAnswerAnalysisService: 增强版步骤级分析失败', [
                'exam_id' => $examData['exam_id'] ?? 'unknown',
                'student_id' => $examData['student_id'] ?? 'unknown',
                'error' => $e->getMessage()
            ]);

            throw new \Exception('增强版步骤级分析失败：' . $e->getMessage());
        }
    }
}
