<?php

namespace App\Filament\Widgets;

use App\Models\Module;
use App\Models\User;
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
use Filament\Widgets\StatsOverviewWidget\Stat;
use Spatie\Permission\Models\Role;

class AdminStatsOverview extends BaseWidget
{
    protected function getStats(): array
    {
        $user = auth()->user();
        $stats = [];

        if ($user && $user->can('users.view')) {
            $stats[] = Stat::make('Usuarios activos', User::query()->where('is_active', true)->count());
        }

        if ($user && $user->can('roles.view')) {
            $stats[] = Stat::make('Roles', Role::query()->count());
        }

        if ($user && $user->can('modules.view')) {
            $stats[] = Stat::make('Módulos', Module::query()->where('is_active', true)->count());
        }

        return $stats;
    }

    public static function canView(): bool
    {
        $user = auth()->user();

        return $user
            && ($user->can('users.view') || $user->can('roles.view') || $user->can('modules.view'));
    }
}
