<?php

namespace App\Http\Controllers;

use App\Services\ExamPdfExportService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class ExamAnalysisPdfController extends Controller
{
    public function show(Request $request, ExamPdfExportService $pdfExportService)
    {
        $paperId = $request->query('paperId');
        $studentId = $request->query('studentId');
        $recordId = $request->query('recordId'); // 可选的OCR记录ID

        if (!$paperId || !$studentId) {
            return response('paperId 和 studentId 不能为空', 400);
        }

        $pdfUrl = $pdfExportService->generateAnalysisReportPdf($paperId, $studentId, $recordId);
        if (!$pdfUrl) {
            Log::error('ExamAnalysisPdfController: 学情报告生成失败', [
                'paper_id' => $paperId,
                'student_id' => $studentId,
                'record_id' => $recordId,
            ]);
            return response('生成学情报告失败，请稍后重试', 500);
        }

        return redirect($pdfUrl);
    }
}
