<?php

namespace App\Filament\Pages;

use App\Services\KnowledgeGraphService;
use App\Services\LearningAnalyticsService;
use Filament\Pages\Page;

class KnowledgeGraphVisualization extends Page
{
    protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-share';
    protected static string|\UnitEnum|null $navigationGroup = null;
    protected static ?int $navigationSort = 3;
    protected static bool $shouldRegisterNavigation = false;
    protected static ?string $navigationLabel = '知识图谱可视化';
    protected static ?string $title = '知识图谱可视化';
    protected string $view = 'filament.pages.knowledge-graph-visualization-simple';

    public array $graphData = [];
    public ?string $selectedStudentId = null;
    public array $studentMasteryData = [];

    public function mount(KnowledgeGraphService $service, LearningAnalyticsService $learningService): void
    {
        $this->graphData = $service->exportGraph();

        // 如果有选中的学生，获取掌握度数据
        if ($this->selectedStudentId) {
            $this->loadStudentMasteryData($learningService);
        }
    }

    public function updatedSelectedStudentId($value)
    {
        if ($value) {
            $learningService = app(LearningAnalyticsService::class);
            $this->loadStudentMasteryData($learningService);
        } else {
            $this->studentMasteryData = [];
        }
    }

    protected function loadStudentMasteryData(LearningAnalyticsService $learningService)
    {
        try {
            // 从MySQL查询学生掌握度数据
            $masteryRecords = \Illuminate\Support\Facades\DB::connection('remote_mysql')
                ->table('student_mastery')
                ->where('student_id', $this->selectedStudentId)
                ->select(['kp', 'mastery', 'stability'])
                ->get()
                ->toArray();

            $this->studentMasteryData = array_map(function ($record) {
                return [
                    'kp_code' => $record->kp,
                    'mastery' => (float) $record->mastery,
                    'stability' => (float) $record->stability
                ];
            }, $masteryRecords);
        } catch (\Exception $e) {
            \Illuminate\Support\Facades\Log::error('获取学生掌握度数据失败', [
                'student_id' => $this->selectedStudentId,
                'error' => $e->getMessage()
            ]);
            $this->studentMasteryData = [];
        }
    }

    public function getNodeMastery(string $kpCode): ?float
    {
        foreach ($this->studentMasteryData as $mastery) {
            if ($mastery['kp_code'] === $kpCode) {
                return $mastery['mastery'];
            }
        }
        return null;
    }

    public function getMasteryColor(?float $mastery): string
    {
        if ($mastery === null) {
            return '#d1d5db'; // gray-300 - 未学习
        }

        if ($mastery >= 0.9) return '#10b981'; // emerald-500 - 优秀
        if ($mastery >= 0.8) return '#34d399'; // emerald-400 - 良好
        if ($mastery >= 0.7) return '#fbbf24'; // amber-400 - 中等
        if ($mastery >= 0.6) return '#fb923c'; // orange-400 - 及格
        return '#ef4444'; // red-500 - 需提升
    }

    public function getMasteryLevel(?float $mastery): string
    {
        if ($mastery === null) return '未学习';
        if ($mastery >= 0.9) return '优秀';
        if ($mastery >= 0.8) return '良好';
        if ($mastery >= 0.7) return '中等';
        if ($mastery >= 0.6) return '及格';
        return '需提升';
    }

    public function getStudents(): array
    {
        try {
            return \Illuminate\Support\Facades\DB::connection('remote_mysql')
                ->table('students')
                ->select(['student_id', 'name'])
                ->limit(100)
                ->get()
                ->toArray();
        } catch (\Exception $e) {
            \Illuminate\Support\Facades\Log::error('获取学生列表失败', [
                'error' => $e->getMessage()
            ]);
            return [];
        }
    }
}
