<?php

namespace App\Http\Middleware;

use App\Models\AppSession;
use Closure;

class RedirectAuthenticatedPro
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $sessionKey = $request->cookie('sessionKey');
        $appSession = AppSession::where('session_key', $sessionKey)->where('is_active', true)->first();

        if($appSession && $appSession->pro) {
            return redirect()->route('dashboard');
        }

        return $next($request);
    }
}
