<?php

namespace App\Http\Middleware;

use App\Enums\UserStatusEnum;
use App\Exceptions\CustomWebException;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class ClientAuthMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
     */
    public function handle(Request $request, Closure $next): Response
    {
        $user = auth('client')->user();

        throw_if(! $user?->email_verified_at && config('extra_service.system_config.email_verification.is_enabled'), new CustomWebException(__('You need to verify your email first.')));

        throw_if(! $user?->is_kyc_verified && config('extra_service.system_config.is_kyc_enabled.is_enabled'), new CustomWebException(__('you need to verify your kyc first.')));

        throw_if($user?->status == UserStatusEnum::BANNED, new CustomWebException(__('You are banned. Please contact admin.')));

        return $next($request);
    }
}
