<?php

namespace App\Http\Middleware;

use App\Models\AppSession;
use Closure;

class EnsureDefaultCompanyClient
{
    public function handle($request, Closure $next)
    {
        $patient = $request->route()->parameter('patient');
        if(!$patient) abort(403);
        
        $defaultCompanyClientUid = session()->get('DEFAULT_COMPANY_CLIENT_UID_'.@$patient->id);
        if(!$defaultCompanyClientUid){
            return redirect()->to(route('patients.view.show-set-default-company-client', $patient));
        }

        return $next($request);
    }
}
