<?php

namespace App\Jobs;

use App\Services\ExamPdfExportService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;

class GenerateAnalysisPdfJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public string $paperId;
    public string $studentId;
    public ?string $recordId;
    public int $maxAttempts = 3;

    public function __construct(string $paperId, string $studentId, ?string $recordId = null)
    {
        $this->paperId = $paperId;
        $this->studentId = $studentId;
        $this->recordId = $recordId;

        // 指定使用 pdf 队列，由独立的 pdf-worker 容器处理
        $this->onQueue('pdf');
    }

    public function handle(ExamPdfExportService $pdfExportService): void
    {
        try {
            Log::info('开始处理学情分析PDF生成队列任务', [
                'paper_id' => $this->paperId,
                'student_id' => $this->studentId,
                'record_id' => $this->recordId,
                'attempt' => $this->attempts(),
            ]);

            // 生成学情分析PDF
            $pdfUrl = $pdfExportService->generateAnalysisReportPdf(
                $this->paperId,
                $this->studentId,
                $this->recordId
            );

            if ($pdfUrl) {
                Log::info('学情分析PDF生成成功', [
                    'paper_id' => $this->paperId,
                    'student_id' => $this->studentId,
                    'record_id' => $this->recordId,
                    'pdf_url' => $pdfUrl,
                ]);
            } else {
                Log::error('学情分析PDF生成失败', [
                    'paper_id' => $this->paperId,
                    'student_id' => $this->studentId,
                    'record_id' => $this->recordId,
                ]);

                // 如果失败且还有重试次数，则重试
                if ($this->attempts() < $this->maxAttempts) {
                    Log::info('将在5秒后重试PDF生成', [
                        'paper_id' => $this->paperId,
                        'student_id' => $this->studentId,
                        'attempt' => $this->attempts(),
                    ]);
                    $this->release(5);
                    return;
                }
            }

        } catch (\Exception $e) {
            Log::error('学情分析PDF生成队列任务失败', [
                'paper_id' => $this->paperId,
                'student_id' => $this->studentId,
                'record_id' => $this->recordId,
                'error' => $e->getMessage(),
                'trace' => $e->getTraceAsString(),
            ]);

            // 如果是第一次失败且可能是临时错误，等待后重试
            if ($this->attempts() < $this->maxAttempts) {
                Log::info('检测到临时错误，将在10秒后重试', [
                    'paper_id' => $this->paperId,
                    'student_id' => $this->studentId,
                    'attempt' => $this->attempts(),
                ]);
                $this->release(10);
                return;
            }
        }
    }
}
