<?php

namespace App\Livewire\MistakeBook;

use App\Models\MistakeRecord;
use Filament\Widgets\ChartWidget;

class MistakeTrendsChart extends ChartWidget
{
    public ?string $studentId;

    protected static ?int $sort = 1;

    protected int | string | array $columnSpan = 'full';

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

    protected function getData(): array
    {
        if (!$this->studentId) {
            return [
                'datasets' => [],
                'labels' => [],
            ];
        }

        // 获取最近30天的错题趋势数据
        $trends = $this->calculateTrends($this->studentId);

        return [
            'datasets' => [
                [
                    'label' => '新增错题',
                    'data' => $trends['daily'],
                    'backgroundColor' => 'rgba(239, 68, 68, 0.5)',
                    'borderColor' => 'rgb(239, 68, 68)',
                    'borderWidth' => 2,
                    'fill' => true,
                ],
                [
                    'label' => '复习完成',
                    'data' => $trends['reviewed'],
                    'backgroundColor' => 'rgba(34, 197, 94, 0.5)',
                    'borderColor' => 'rgb(34, 197, 94)',
                    'borderWidth' => 2,
                    'fill' => true,
                ],
            ],
            'labels' => $trends['labels'],
        ];
    }

    protected function getType(): string
    {
        return 'line';
    }

    protected function getOptions(): array
    {
        return [
            'responsive' => true,
            'maintainAspectRatio' => false,
            'plugins' => [
                'legend' => [
                    'display' => true,
                    'position' => 'top',
                ],
            ],
            'scales' => [
                'y' => [
                    'beginAtZero' => true,
                    'ticks' => [
                        'stepSize' => 1,
                    ],
                ],
            ],
        ];
    }

    private function calculateTrends(string $studentId): array
    {
        $days = collect(range(29, 0))->map(function ($day) use ($studentId) {
            $date = now()->subDays($day)->startOfDay();
            $endDate = (clone $date)->endOfDay();

            $created = MistakeRecord::forStudent($studentId)
                ->whereBetween('created_at', [$date, $endDate])
                ->count();

            $reviewed = MistakeRecord::forStudent($studentId)
                ->whereBetween('reviewed_at', [$date, $endDate])
                ->count();

            return [
                'date' => $date->format('m-d'),
                'created' => $created,
                'reviewed' => $reviewed,
            ];
        });

        return [
            'labels' => $days->pluck('date')->toArray(),
            'daily' => $days->pluck('created')->toArray(),
            'reviewed' => $days->pluck('reviewed')->toArray(),
        ];
    }
}
