<?php

namespace App\Filament\Widgets;

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

class StaffStats extends ChartWidget
{
    protected ?string $heading = 'Ratio Encadrement par Pôle';

    protected function getData(): array
    {
        $poles = Pole::all();
        
        return [
            'datasets' => [
                [
                    'label' => 'Inspecteurs',
                    'data' => $poles->map(function ($pole) {
                        return Inspecteur::where('pole_id', $pole->id)->count();
                    })->toArray(),
                    'backgroundColor' => 'rgba(0, 98, 51, 0.5)', // Mauritanian Green
                    'borderColor' => 'rgb(0, 98, 51)',
                ],
                [
                    '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(255, 205, 0, 0.5)', // Mauritanian Gold
                    'borderColor' => 'rgb(255, 205, 0)',
                ],
            ],
            'labels' => $poles->pluck('libellefr')->toArray(),
        ];
    }

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