<?php


if (!function_exists('setLog')) {
    function setLog($level, $event, $user_id = 0)
    {
        $activity_model = new \App\Models\Sistema\ActivityModel();
        $request = \Config\Services::request();
        $ip = $request->getIPAddress();
        $agent = $request->getUserAgent();

        $currentAgent = identifyAgent($agent);

        $activity_model->save([
            'user_id' => $user_id,
            'level' => $level,
            'event' => $event,
            'ip' => $ip,
            'os' => $agent->getPlatform(),
            'browser' => $currentAgent,
            'detail' => $agent
        ]);
    }
}


if (!function_exists('identifyAgent')) {
    function identifyAgent($agent)
    {
        if ($agent->isBrowser()) {
            return $agent->getBrowser() . ' ' . $agent->getVersion();
        }
        if ($agent->isRobot()) {
            return $agent->getRobot();
        }
        if ($agent->isMobile()) {
            return $agent->getMobile();
        }
        return 'Unidentified User Agent';
    }
}
