<?php

namespace App\Providers;

use App\Models\Menu;
use App\View\Components\Circle;
use Auth;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
use Carbon\Carbon;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     */
    public function register(): void
    {
        //
    }

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        Blade::component('circle', Circle::class);

        View::composer('layouts.partials.sidebar', function ($view){
            $user = Auth::user();

            if($user){
                $menus = Menu::whereHas('roles', function ($query) use ($user){
                    $query->where('roles.id', $user->roles->pluck('id'));
                })
                ->with(['children' => function ($query) {
                    $query->whereHas('roles', function ($subQuery) {
                        $subQuery->where('role_menu.allow_show', 1);
                    });
                }])
                ->orderBy('sort_order', 'asc')
                ->get();
            }else{
                $menus = collect();
            }

            $view->with('menus', $menus);
        });
    }
}
