<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
use App\Http\Controllers\Mailgun\MailgunController;

class VerifyDomain
{
    /**
     * Handle an incoming request.
     *
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
     */
    public function handle(Request $request, Closure $next): Response
    {
        $companyId = getSelectedCompany();
       
        if(!$companyId){
            return false;
        }
    
        $company = get_company('id',$companyId);
        
         if(!$company){
            return false;
        }
        
        $domain = $company->domain;
        
        $controller = new MailgunController();
        
        $result = $controller->is_domain_verified($domain);
            if($result){
                return $next($request);
            }else{
                 return redirect()->route('showDomain',$domain);
            }
    }
}
