<?php

namespace App\Filament\Widgets;

use App\Models\Pole;
use App\Models\Etablissement;
use Filament\Widgets\ChartWidget;

class InfrastructureStats extends ChartWidget
{
    protected ?string $heading = 'Infrastructures par Pôle';

    protected function getData(): array
    {
        $poles = Pole::all();
        
        return [
            'datasets' => [
                [
                    'label' => 'Établissements',
                    'data' => $poles->map(function ($pole) {
                        return Etablissement::whereHas('commune.moughataa.region', function ($query) use ($pole) {
                            $query->where('pole_id', $pole->id);
                        })->count();
                    })->toArray(),
                    'backgroundColor' => 'rgba(59, 130, 246, 0.5)',
                    'borderColor' => 'rgb(59, 130, 246)',
                ],
            ],
            'labels' => $poles->pluck('libellefr')->toArray(),
        ];
    }

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