<?php

namespace App\Filament\Widgets;

use App\Models\Pole;
use App\Models\Region;
use App\Models\Etablissement;
use App\Models\Professeur;
use App\Models\Rapport;
use Filament\Widgets\Widget;

class MauritaniaMap extends Widget
{
    protected string $view = 'filament.widgets.mauritania-map';

    protected static ?string $heading = 'Carte géographique de la Mauritanie';

    public function getViewData(): array
    {
        $poles = Pole::all();
        
        $poles->each(function ($pole) {
            $pole->etablissements_count = Etablissement::whereHas('commune.moughataa.region', function ($query) use ($pole) {
                $query->where('pole_id', $pole->id);
            })->count();
            
            $pole->professeurs_count = Professeur::whereHas('etablissements.commune.moughataa.region', function ($query) use ($pole) {
                $query->where('pole_id', $pole->id);
            })->count();
            
            $pole->rapports_count = Rapport::whereHas('etablissement.commune.moughataa.region', function ($query) use ($pole) {
                $query->where('pole_id', $pole->id);
            })->count();
        });
        
        return [
            'poles' => $poles,
        ];
    }
}
