<?php

namespace App\Filament\Pages;

use App\Models\Pole;
use App\Models\Region;
use App\Models\Moughataa;
use App\Models\Commune;
use App\Models\Etablissement;
use App\Models\Annee;
use Filament\Forms\Components\Select;
use Filament\Schemas\Schema;
use Filament\Pages\Page;
use Filament\Forms\Contracts\HasForms;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Support\Icons\Heroicon;

class AnalyticsDashboard extends Page implements HasForms
{
    use InteractsWithForms;

    protected string $view = 'filament.pages.analytics-dashboard';

    protected static ?string $navigationLabel = 'Analytics Dashboard';
    protected static ?string $title = 'Analytics Dashboard';

    public ?array $data = [];

    public function mount(): void
    {
        $this->form->fill();
    }

    public function form(Schema $form): Schema
    {
        return $form
            ->schema([
                Select::make('pole_id')
                    ->label('Pôle')
                    ->options(function () {
                        return Pole::all()
                            ->filter(fn($pole) => !empty($pole->libellefr))
                            ->pluck('libellefr', 'id')
                            ->toArray();
                    })
                    ->searchable()
                    ->reactive()
                    ->afterStateUpdated(fn (callable $set) => $set('region_id', null))
                    ->placeholder('Tous les pôles'),

                Select::make('region_id')
                    ->label('Wilaya')
                    ->options(function (callable $get) {
                        $poleId = $get('pole_id');
                        $query = Region::query();
                        if ($poleId) {
                            $query = $query->where('pole_id', $poleId);
                        }
                        return $query->get()
                            ->filter(fn($region) => !empty($region->libellefr))
                            ->pluck('libellefr', 'id')
                            ->toArray();
                    })
                    ->searchable()
                    ->reactive()
                    ->afterStateUpdated(fn (callable $set) => $set('moughataa_id', null))
                    ->placeholder('Toutes les wilayas'),

                Select::make('moughataa_id')
                    ->label('Moughataa')
                    ->options(function (callable $get) {
                        $regionId = $get('region_id');
                        $query = Moughataa::query();
                        if ($regionId) {
                            $query = $query->where('region_id', $regionId);
                        }
                        return $query->get()
                            ->filter(fn($moughataa) => !empty($moughataa->libellefr))
                            ->pluck('libellefr', 'id')
                            ->toArray();
                    })
                    ->searchable()
                    ->reactive()
                    ->afterStateUpdated(fn (callable $set) => $set('commune_id', null))
                    ->placeholder('Toutes les moughataas'),

                Select::make('commune_id')
                    ->label('Commune')
                    ->options(function (callable $get) {
                        $moughataaId = $get('moughataa_id');
                        $query = Commune::query();
                        if ($moughataaId) {
                            $query = $query->where('moughataa_id', $moughataaId);
                        }
                        return $query->get()
                            ->filter(fn($commune) => !empty($commune->libellefr))
                            ->pluck('libellefr', 'id')
                            ->toArray();
                    })
                    ->searchable()
                    ->reactive()
                    ->afterStateUpdated(fn (callable $set) => $set('etablissement_id', null))
                    ->placeholder('Toutes les communes'),

                Select::make('etablissement_id')
                    ->label('Établissement')
                    ->options(function (callable $get) {
                        $communeId = $get('commune_id');
                        $query = Etablissement::query();
                        if ($communeId) {
                            $query = $query->where('commune_id', $communeId);
                        }
                        return $query->get()
                            ->filter(fn($etablissement) => !empty($etablissement->libellefr))
                            ->pluck('libellefr', 'id')
                            ->toArray();
                    })
                    ->searchable()
                    ->placeholder('Tous les établissements'),

                Select::make('annee_id')
                    ->label('Année scolaire')
                    ->options(function () {
                        return Annee::all()
                            ->filter(fn($annee) => !empty($annee->libellefr))
                            ->pluck('libellefr', 'id')
                            ->toArray();
                    })
                    ->searchable()
                    ->placeholder('Toutes les années'),
            ])
            ->statePath('data');
    }
}
