<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\View;
use App\Models\Category;

class ViewServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        // Share $allCategories with every view (for nav/footer/mega-menu).
        // Uses 1-hour cache to avoid repeated DB queries.
        View::composer('*', function ($view) {
            if (!$view->offsetExists('allCategories')) {
                $view->with('allCategories', cache()->remember('all_nav_categories', 3600, function () {
                    return Category::orderBy('display_order')->orderBy('category_name')->get();
                }));
            }
        });
    }
}
