<?php

namespace plugin\admin\app\middleware;

use plugin\admin\api\Auth;
use ReflectionException;
use support\exception\BusinessException;
use Webman\Http\Request;
use Webman\Http\Response;
use Webman\MiddlewareInterface;

class Config implements MiddlewareInterface
{
    /**
     * @param Request $request
     * @param callable $handler
     * @return Response
     * @throws ReflectionException|BusinessException
     */
    public function process(Request $request, callable $handler): Response
    {
        $request->controller_name = get_controller_name();
        $request->action_name = get_action_name();
            
        $config = Config('site');
        $config['debug'] = config('app.debug');
        $config['controller'] = $request->controller_name;
        $config['action'] = $request->action_name;
        $config['moduleurl'] = admin_path();
        $config['admin_path'] = admin_path();
        $request->_view_vars = array_merge((array) $request->_view_vars,[
            'user'      =>  session('admin'),
            'config'    =>  $config
        ]);
        $IM = new \support\OpenImSdk\Client([
            'host' => 'http://127.0.0.1:10002', // OpenIM API地址
            'secret' => 'openIM123', // OpenIM密钥
        ]);
        $request->IM = $IM;
        return $handler($request);
    }
}
