<?php

namespace App\Livewire\MistakeBook;

use App\Services\MistakeBookService;
use Filament\Widgets\StatsOverviewWidget as BaseStatsOverviewWidget;

class MistakeStatsOverview extends BaseStatsOverviewWidget
{
    public ?string $studentId;

    protected static ?int $sort = 0;

    public function mount(?string $studentId = null): void
    {
        $this->studentId = $studentId;
    }

    protected function getCards(): array
    {
        if (!$this->studentId) {
            return [];
        }

        $service = app(MistakeBookService::class);
        $summary = $service->summarize($this->studentId);

        return [
            \Filament\Widgets\StatsOverviewWidget\Stat::make('总错题数', $summary['total'] ?? 0)
                ->description('累计错题数量')
                ->descriptionIcon('heroicon-m-arrow-trending-up')
                ->chart([7, 3, 4, 5, 6, 3, 5, 3])
                ->color('primary'),
            \Filament\Widgets\StatsOverviewWidget\Stat::make('本周新增', $summary['this_week'] ?? 0)
                ->description('本周新增错题')
                ->descriptionIcon('heroicon-m-arrow-trending-up')
                ->chart([4, 5, 6, 3, 4, 5, 6])
                ->color('success'),
            \Filament\Widgets\StatsOverviewWidget\Stat::make('待复习', $summary['pending'] ?? 0)
                ->description('需要复习的错题')
                ->descriptionIcon('heroicon-m-exclamation-triangle')
                ->chart([7, 3, 4, 5, 6, 3, 5])
                ->color('warning'),
            \Filament\Widgets\StatsOverviewWidget\Stat::make('掌握率', ($summary['mastery_rate'] ?? 0) . '%')
                ->description('已掌握错题比例')
                ->descriptionIcon('heroicon-m-check-circle')
                ->chart([7, 3, 4, 5, 6, 3, 5])
                ->color($summary['mastery_rate'] >= 80 ? 'success' : ($summary['mastery_rate'] >= 50 ? 'warning' : 'danger')),
        ];
    }
}
