<?php

namespace App\Filament\Widgets;

use Filament\Widgets\ChartWidget;
use Functional\Exercises\Models\Exercise;

/**
 * Répartition du catalogue d'exercices par partie du corps ciblée (doughnut).
 */
class ExercisesByBodyPartChart extends ChartWidget
{
    protected static ?int $sort = 4;

    protected int|string|array $columnSpan = 1;

    /** Palette "flamme" dégradée pour distinguer les parts sans dépendre de couleurs génériques. */
    private const PALETTE = [
        '#f59e0b', '#fb923c', '#f97316', '#ea580c', '#c2410c',
        '#fbbf24', '#fcd34d', '#fde68a', '#d97706', '#b45309',
    ];

    public function getHeading(): string
    {
        return 'Exercices par partie du corps';
    }

    protected function getData(): array
    {
        $countsByBodyPart = Exercise::query()
            ->whereNotNull('body_part')
            ->selectRaw('body_part, COUNT(*) as total')
            ->groupBy('body_part')
            ->orderByDesc('total')
            ->pluck('total', 'body_part');

        return [
            'datasets' => [
                [
                    'data' => $countsByBodyPart->values()->all(),
                    'backgroundColor' => self::PALETTE,
                ],
            ],
            'labels' => $countsByBodyPart->keys()->all(),
        ];
    }

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