<?php
class DashboardController extends Controller
{
    public function index(Request $request): void
    {
        // Check must_change_password
        $user = Auth::user();
        if ($user && !empty($user['must_change_password'])) {
            $this->redirect('/change-password', ['alerts' => [['type' => 'warning', 'message' => 'You must change your password before continuing.']]]);
            return;
        }

        $widgets = ModuleRegistry::getWidgets();
        $filteredWidgets = [];
        foreach ($widgets as $widget) {
            if (isset($widget['permission']) && !Gate::can($widget['permission'])) {
                continue;
            }
            $filteredWidgets[] = $widget;
        }

        $this->view('dashboard/index', [
            'title' => lang('dashboard.title'),
            'breadcrumbs' => [['label' => lang('dashboard.title')]],
            'widgets' => $filteredWidgets,
            'user' => $user,
        ], 'app');
    }
}