<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class TimeZone
{
    public function handle(Request $request, Closure $next)
    {

        if (auth()->check()) {
            \Illuminate\Support\Facades\App::setLocale(userLocal());

            $defaultTimezone = settings('timezone') ?? config('app.timezone', 'Asia/Dhaka');

            $timeZone = auth()->user()->time_zone ?? $defaultTimezone;

            config(['app.timezone' => $timeZone]);
            date_default_timezone_set($timeZone);
        }

        return $next($request);
    }
}
