<?php

namespace App\Http\Middleware;

use App\Models\AppSession;
use Closure;

class EnsureCompanyClient
{
    public function handle($request, Closure $next)
    {
        $patient = $request->route()->parameter('patient');
        if(!$patient) abort(403);

        if(!count($patient->companyClients)){
            $params = [
                'patient' => $patient,
                'popupmode' => request()->input('popupmode')
            ];
            return redirect()->to(route('patients.view.show-set-company-client', $params));
        }

        return $next($request);
    }
}
