<?php

namespace App\Http\Middleware;
use App\Http\Response\ResponseJson;
use App\Models\ChefUser;
use App\Models\Coach;
use App\Models\CustomerServiceUser;
use App\Models\MainCoach;
use App\Models\Partner;
use Closure;
use Exception;
use Illuminate\Http\Request;

class CheckServiceUserRole
{
    use ResponseJson;

    /**
     * Handle an incoming request.
     *
     * @param Request $request
     * @param Closure $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next): mixed
    {
        try {
            $serviceUser = match ($request->get('role')) {
                "1" => new MainCoach(),
                "2" => new Coach(),
                "3" => new CustomerServiceUser(),
//                "4" => new Partner(),
                "6" => new ChefUser(),
                default => throw new Exception('操作类型错误'),
            };
        }catch (Exception $e){
            return $this->jsonResponse(1,$e->getMessage());
        }

        $request->merge(['serviceUser' => $serviceUser]);
        return $next($request);
    }
}