<?php

namespace App\Http\Middleware;

use Illuminate\Support\Facades\View;
use Closure;
use Modules\Banner\Enums\BannerActiveStatusEnum;
use Modules\Banner\Enums\BannerStatusEnum;
use Modules\Banner\Services\BannerService;
use Modules\Brand\Enums\BrandBoolEnum;
use Modules\Brand\Services\BrandService;
use Modules\Category\Enums\CategoryShowFooterEnum;
use Modules\Category\Services\CategoryService;
use Modules\City\Services\CityService;
use Modules\Offer\Enums\OfferStatusEnum;
use Modules\Offer\Services\OfferService;
use Modules\Page\Services\PageService;
use Modules\Type\Services\TypeService;
use Modules\User\Services\AccountService;
use Modules\User\Services\UserService;
use Modules\User\Enums\UserRoleEnum;

class SessionUser
{
    public function __construct(
        private AccountService $accountService,
        private OfferService $offerService,
        private CategoryService $categoryService,
        private TypeService $typeService,
        private BrandService $brandService,
        private BannerService $bannerService,
        private CityService $cityService,
        private UserService $userService,
        private PageService $pageService,
    ){}

    /**
     * Handle an incoming request.
     * Redirect user to admin login page if he has not the previliage to access the admin panel
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (auth()->user())
        {
            $role = UserRoleEnum::admins();
            if(in_array(auth()->user()->role, $role))
            {
                return redirect('admin');
            }
            if(auth()->user()->role == UserRoleEnum::STORE->value )
            {
                return redirect('store');
            }
            if (is_null(auth()->user()->address_id))
            {
                $this->userService->logout();
                return redirect(route('web.home'));
            }
        }
        View::composer('*', function ($view) {
            $account = $this->accountService->getAccount();
            $account = $account['user'];
            $view->with('account', $account);
            $tomorrow = date('d',strtotime('+ 1 days'));
            $tomorrow .= ' ' .__('web.monthes.'.date('m',strtotime('+ 1 days')));
            $tomorrow .= ' ' .date('Y',strtotime('+ 1 days'));
            $view->with('tomorrow', $tomorrow);
            $offerDaily = $this->offerService->get([
                'typeStatus' => OfferStatusEnum::DAY->value,
                'start_date' => date('Y-m-d')
            ])->first();
            $view->with('offerDaily', $offerDaily);
            $types = $this->typeService->get();
            $view->with('types', $types);
            $nav_brands = $this->brandService->get([
                'show_models' => BrandBoolEnum::TRUE->value
            ]);
            $view->with('nav_brands', $nav_brands);

            $model_banner = $this->bannerService->get([
                'typeStatus'=>BannerStatusEnum::MODELS->value,
                'type_id'=>$account->type_id
            ])->first();

            $footer = $this->pageService->getItemById(1);
            $view->with('footer', $footer);

            $view->with('model_banner', $model_banner);
            $footer_banner = $this->bannerService->get([
                'typeStatus'=>BannerStatusEnum::FOOTER->value,
                'type_id'=>$account->type_id
            ])->first();
            $view->with('footer_banner', $footer_banner);
            $bool_true = BannerActiveStatusEnum::ACTIVE->value;
            $view->with('bool_true', $bool_true);
            $nav_categories = $this->categoryService->get(['category_id'=>'main','type_id'=>$account->type_id,'orderBy'=>['key'=>'id','value'=>'ASC']]);
            $view->with('nav_categories', $nav_categories);
            $nav_sub_categories = $this->categoryService->get(['category_id'=>'main','type_id'=>$account->type_id,'orderBy'=>['key'=>'id','value'=>'ASC']]);
            $view->with('nav_sub_categories', $nav_sub_categories);
            $nav_cities = $this->cityService->get();
            $view->with('nav_cities', $nav_cities);
            $footer_categories = $this->categoryService->get([
                'show_footer'=>CategoryShowFooterEnum::TRUE->value,
                'category_id'=>'main',
                'type_id'=>$account->type_id
            ]);
            $view->with('footer_categories', $footer_categories);
            $footer_brands = $this->brandService->get();
            $view->with('footer_brands', $footer_brands);
        });
        return $next($request);
    }
}
