<?php

namespace App\Filament\Pages;

use App\Models\OCRRecord;
use App\Services\ExamPaperService;
use App\Services\ChatGPTAnalysisService;
use App\Jobs\RegradeOCRSubmission;
use App\Filament\Traits\HasUserRole;
use Filament\Pages\Page;
use Filament\Notifications\Notification;
use Livewire\Attributes\Computed;
use Illuminate\Support\Facades\Log;

class OCRPaperGrading extends Page
{
    use HasUserRole;

    protected static ?string $navigationLabel = 'OCR智能阅卷';
    protected static ?string $title = 'OCR试卷智能阅卷系统';
    protected static ?string $slug = 'ocr-paper-grading';
    protected static ?int $navigationSort = 4;

    public static function getNavigationIcon(): ?string
    {
        return 'heroicon-o-document-duplicate';
    }

    public static function getNavigationGroup(): ?string
    {
        return '卷子生成管理';
    }

    protected string $view = 'filament.pages.ocr-paper-grading';

    // 选择相关
    public ?string $teacherId = null;
    public ?string $studentId = null;
    public ?string $selectedPaperId = null;

    // 处理状态
    public ?int $selectedRecordId = null;
    public ?OCRRecord $selectedRecord = null;

    // ChatGPT识别相关
    public bool $useChatGPT = false;

    // 获取识别模式文本
    public function getRecognitionModeText(): string
    {
        return $this->useChatGPT ? 'ChatGPT智能识别' : 'OCR识别 + AI判分';
    }

    public function mount(): void
    {
        $this->initializeUserRole();

        if ($this->isTeacher) {
            $teacherId = $this->getCurrentTeacherId();
            if ($teacherId) {
                $this->teacherId = $teacherId;
            }
        }
    }

    #[Computed]
    public function teachers(): array
    {
        return app(ExamPaperService::class)->getTeachers(
            $this->isTeacher ? $this->getCurrentTeacherId() : null
        );
    }

    #[Computed]
    public function students(): array
    {
        return app(ExamPaperService::class)->getStudents($this->teacherId);
    }

    #[Computed]
    public function studentPapers(): array
    {
        return app(ExamPaperService::class)->getStudentPapers($this->studentId);
    }

    #[Computed]
    public function selectedPaperQuestions(): array
    {
        return app(ExamPaperService::class)->getPaperQuestions($this->selectedPaperId);
    }

    public function updatedTeacherId($value): void
    {
        $this->studentId = null;
        $this->selectedPaperId = null;
    }

    public function updatedStudentId($value): void
    {
        $this->selectedPaperId = null;
    }

    /**
     * 查看OCR记录
     */
    public function viewRecord(int $recordId): void
    {
        $this->selectedRecordId = $recordId;
        $this->selectedRecord = OCRRecord::with('questions')->findOrFail($recordId);
    }

    /**
     * 手动重新判分
     */
    public function regrade(int $recordId): void
    {
        RegradeOCRSubmission::dispatch($recordId);

        Notification::make()
            ->title('已触发重新判分')
            ->body('请稍后刷新查看结果')
            ->success()
            ->send();
    }

    /**
     * 获取最近的OCR记录
     */
    public function getRecentRecords(): \Illuminate\Database\Eloquent\Collection
    {
        return OCRRecord::with('questions')
            ->orderBy('created_at', 'desc')
            ->limit(10)
            ->get();
    }

    /**
     * 切换识别模式
     */
    public function updatedUseChatGPT($value): void
    {
        if ($value) {
            Notification::make()
                ->title('ChatGPT识别模式')
                ->body('将使用ChatGPT进行试卷智能分析，无需OCR识别')
                ->info()
                ->send();
        }
    }
}
