<?php

namespace App\Services;

use App\Models\OCRRecord;
use App\Models\OCRQuestionResult;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class OCRService
{
    protected $ocrDriver;
    protected $learningAnalyticsService;

    public function __construct(LearningAnalyticsService $learningAnalyticsService)
    {
        $this->ocrDriver = \App\Services\OCR\OCRFactory::create();
        $this->learningAnalyticsService = $learningAnalyticsService;
    }

    /**
     * 上传卷子照片并创建OCR记录
     */
    public function uploadExamPaper(UploadedFile $image, string $studentId): OCRRecord
    {
        // 验证图片
        $this->validateImage($image);

        // 生成唯一ID
        $recordId = 'ocr_' . Str::uuid()->toString();
        $examId = 'exam_' . now()->format('YmdHis') . '_' . Str::random(8);

        // 获取图片信息
        $imageInfo = getimagesize($image->getPathName());
        $imageWidth = $imageInfo[0] ?? 0;
        $imageHeight = $imageInfo[1] ?? 0;
        $imageSize = filesize($image->getPathName());

        // 保存图片
        $extension = $image->getClientOriginalExtension();
        $filename = $recordId . '.' . $extension;
        $imagePath = 'uploads/ocr/' . $filename;

        Storage::disk('public')->put($imagePath, file_get_contents($image->getPathName()));

        // 创建OCR记录
        $ocrRecord = OCRRecord::create([
            'user_id' => $studentId,
            'file_path' => $imagePath,
            'paper_title' => $image->getClientOriginalName(),
            'status' => 'pending',
        ]);

        // 发送到OCR服务处理
        $this->dispatchToOcrService($ocrRecord);

        return $ocrRecord;
    }

    /**
     * 验证上传的图片
     */
    protected function validateImage(UploadedFile $image): void
    {
        $maxSize = config('ocr.upload.max_size', 10 * 1024 * 1024);
        $allowedTypes = config('ocr.upload.allowed_types', ['jpg', 'jpeg', 'png', 'webp']);

        if (!$image->isValid()) {
            throw new \Exception('文件上传失败');
        }

        if ($image->getSize() > $maxSize) {
            throw new \Exception('文件大小超出限制（' . ($maxSize / 1024 / 1024) . 'MB）');
        }

        $extension = strtolower($image->getClientOriginalExtension());
        if (!in_array($extension, $allowedTypes)) {
            throw new \Exception('不支持的文件类型，仅支持：' . implode(', ', $allowedTypes));
        }
    }

    /**
     * 发送到OCR服务处理
     */
    protected function dispatchToOcrService(OCRRecord $ocrRecord): void
    {
        try {
            // 检查图片路径是否存在
            if (empty($ocrRecord->image_path)) {
                throw new \Exception('OCR记录缺少图片路径，record_id: ' . $ocrRecord->id);
            }

            // 读取图片文件
            $imagePath = Storage::disk($this->getDisk())->path($ocrRecord->image_path);

            // 确保返回的是字符串路径
            if (empty($imagePath)) {
                throw new \Exception('无法获取图片路径: ' . $ocrRecord->image_path);
            }

            if (!file_exists($imagePath)) {
                throw new \Exception('图片文件不存在: ' . $imagePath);
            }

            // 更新状态为processing
            $ocrRecord->update(['status' => 'processing']);

            // Single API call with cutType: answer (returns both question and answer)
            \Log::info('OCR: Extracting questions and answers', ['record_id' => $ocrRecord->id]);
            $result = $this->ocrDriver->recognize($imagePath, [
                'cutType' => 'answer',
                'subject' => 'Math'
            ]);

            $items = $result['questions'] ?? [];
            \Log::info('OCR extraction complete', ['item_count' => count($items)]);

            // Step 2: Parse student answers from the answer_list data
            // Each item in answer_list contains the full question+answer text
            // The student's answer is typically the last letter (A/B/C/D) in the text
            \Log::info('Parsing student answers from question text');
            
            $parsedQuestions = [];
            
            foreach ($items as $item) {
                $questionNumber = $item['question_number'];
                $fullText = $item['content'] ?? '';
                $questionText = $fullText;
                $studentAnswer = '';
                
                // Smart parsing: extract the last single letter (A/B/C/D) as student answer
                // Pattern: "题目内容...选项D[学生答案]"
                // The student answer is usually the very last character if it's A/B/C/D
                if (preg_match('/([A-D])\s*$/u', $fullText, $matches)) {
                    $studentAnswer = $matches[1];
                    // Remove the answer from question text
                    $questionText = preg_replace('/\s*[A-D]\s*$/', '', $fullText);
                    
                    \Log::info('Extracted student answer', [
                        'question_number' => $questionNumber,
                        'answer' => $studentAnswer,
                        'original_text_length' => mb_strlen($fullText),
                        'cleaned_text_length' => mb_strlen($questionText)
                    ]);
                }
                
                $parsedQuestions[] = [
                    'question_number' => $questionNumber,
                    'content' => trim($questionText),
                    'student_answer' => $studentAnswer,
                    'confidence' => $item['confidence'] ?? 0.0,
                    'raw_data' => $item['raw_data'] ?? null
                ];
            }

            // 处理结果
            $this->processOcrResult($ocrRecord, [
                'questions' => $parsedQuestions,
                'raw' => $result
            ]);

        } catch (\Exception $e) {
            \Log::error('OCR服务调用失败', [
                'record_id' => $ocrRecord->id,
                'error' => $e->getMessage(),
            ]);

            // 标记为失败
            $ocrRecord->update([
                'status' => 'failed',
                'error_message' => 'OCR服务调用失败：' . $e->getMessage(),
            ]);
        }
    }

    /**
     * Match answers to questions by question number
     */
    protected function matchAnswersToQuestions(array $questions, array $answers): array
    {
        // Create a map of answers by question number
        $answerMap = [];
        foreach ($answers as $answer) {
            $questionNumber = $answer['question_number'] ?? null;
            if ($questionNumber) {
                $answerMap[$questionNumber] = $answer['content'] ?? '';
            }
        }

        // Match answers to questions
        $matched = [];
        foreach ($questions as $question) {
            $questionNumber = $question['question_number'];
            $matched[] = [
                'question_number' => $questionNumber,
                'content' => $question['content'],
                'student_answer' => $answerMap[$questionNumber] ?? '',
                'confidence' => $question['confidence'] ?? 0.0,
                'raw_data' => $question['raw_data'] ?? null
            ];
        }

        return $matched;
    }

    /**
     * 处理OCR结果
     */
    protected function processOcrResult(OCRRecord $ocrRecord, array $result): void
    {
        // Log the raw result for debugging
        \Log::info('OCR Result received', ['question_count' => count($result['questions'] ?? [])]);
        
        // Get matched questions from two-pass OCR
        $questions = $result['questions'] ?? [];

        // 使用 LaTeX 清理服务预处理所有公式
        $latexCleaner = app(\App\Services\LatexCleanerService::class);
        $questions = $latexCleaner->cleanArray($questions, ['content', 'student_answer']);
        
        \Log::info('LaTeX formulas cleaned', ['question_count' => count($questions)]);

        $processedCount = 0;

        foreach ($questions as $question) {
            // 再次确保清理（双重保险）
            $questionText = $latexCleaner->clean($question['content'] ?? '');
            $studentAnswer = $latexCleaner->clean($question['student_answer'] ?? '');
            
            // 验证清理后的内容
            $validation = $latexCleaner->validate($questionText);
            if (!$validation['valid']) {
                \Log::warning('LaTeX validation warnings', [
                    'question_number' => $question['question_number'],
                    'errors' => $validation['errors']
                ]);
            }
            
            OCRQuestionResult::create([
                'ocr_record_id' => $ocrRecord->id,
                'question_number' => $question['question_number'],
                'question_text' => $questionText,
                'student_answer' => $studentAnswer,
                'score_value' => 0, // Will be filled by AI grading
                'mark_detected' => null,
                'score_confidence' => $question['confidence'] ?? 0,
            ]);
            $processedCount++;
        }

        $ocrRecord->update([
            'status' => 'completed',
            'processed_at' => now(),
            'total_questions' => $processedCount,
            'processed_questions' => $processedCount,
            'confidence_avg' => collect($questions)->avg('confidence') ?? 0,
        ]);

        \Log::info('OCR processing complete', [
            'record_id' => $ocrRecord->id,
            'questions_processed' => $processedCount
        ]);

        // 不再自动提交分析,让用户在 OCR 详情页先生成题库题目
        // 用户需要在 ocr-record-view 页面手动点击"生成题库题目"和"提交分析"
        // if ($processedCount > 0) {
        //     $this->submitToAnalysis($ocrRecord, $questions);
        // }
    }

    /**
     * 提交到分析服务
     */
    protected function submitToAnalysis(OCRRecord $ocrRecord, array $questions): void
    {
        try {
            $analysisData = [
                'exam_id' => $ocrRecord->exam_id ?? ('ocr_' . $ocrRecord->id), // 使用 OCR 记录 ID 作为后备
                'student_id' => $ocrRecord->student_id,
                'ocr_record_id' => $ocrRecord->id,
                'teacher_name' => 'System', // 或者是上传者的名字
                'analysis_type' => 'mastery',
                'questions' => array_map(function($q) {
                    // 优先使用人工校准的答案
                    $studentAnswer = $q['student_answer'] ?? '';
                    if (isset($q['manual_answer']) && !empty($q['manual_answer'])) {
                        $studentAnswer = $q['manual_answer'];
                    }

                    return [
                        'question_id' => $q['question_number'], // 使用题号作为临时ID
                        'question_number' => (string)$q['question_number'],
                        'kp_code' => $q['kp_code'] ?? null,
                        'score_value' => $q['score_value'] ?? 0,
                        'student_answer' => $studentAnswer,
                        'ocr_confidence' => $q['confidence'] ?? 0,
                        'question_text' => $q['content'] ?? '', // 传递题目内容供AI分析
                        'teacher_validated' => $q['answer_verified'] ?? false,
                    ];
                }, $questions)
            ];

            $result = $this->learningAnalyticsService->submitOCRAnalysis($analysisData);

            if (isset($result['success']) && $result['success']) {
                $ocrRecord->update([
                    'ai_analyzed_at' => now(),
                    'ai_analysis_count' => ($ocrRecord->ai_analysis_count ?? 0) + 1
                ]);
            }

        } catch (\Exception $e) {
            \Log::error('Failed to submit to analysis service', [
                'record_id' => $ocrRecord->id,
                'error' => $e->getMessage()
            ]);
            // 不抛出异常，以免影响OCR流程的完成状态
        }
    }

    /**
     * 重新处理OCR记录
     */
    public function reprocess(OCRRecord $ocrRecord): bool
    {
        // 重置状态
        $ocrRecord->update([
            'status' => 'pending',
            'error_message' => null,
            'processed_at' => null,
            'total_questions' => 0,
            'processed_questions' => 0,
            'confidence_avg' => null,
        ]);

        // 删除旧的题目结果
        OCRQuestionResult::where('ocr_record_id', $ocrRecord->id)->delete();

        // 重新发送到OCR服务
        $this->dispatchToOcrService($ocrRecord);

        return true;
    }

    /**
     * 获取OCR记录的统计信息
     */
    public function getStatistics(): array
    {
        $total = OCRRecord::count();
        $pending = OCRRecord::where('status', 'pending')->count();
        $processing = OCRRecord::where('status', 'processing')->count();
        $completed = OCRRecord::where('status', 'completed')->count();
        $failed = OCRRecord::where('status', 'failed')->count();

        return [
            'total' => $total,
            'pending' => $pending,
            'processing' => $processing,
            'completed' => $completed,
            'failed' => $failed,
        ];
    }

    /**
     * 获取存储磁盘名称
     */
    protected function getDisk(): string
    {
        return 'public'; // OCR uploads are stored in public disk
    }
}
