<?php

use Illuminate\Support\Facades\Auth;
use PHPOpenSourceSaver\JWTAuth\Exceptions\JWTException;
use PHPOpenSourceSaver\JWTAuth\Facades\JWTAuth;

if (! function_exists('authUser')) {
    function authUser(array $relations = [])
    {
        $user = Auth::user();

        if (!$user) {
            return null;
        }

        return empty($relations)
            ? $user
            : $user->loadMissing($relations);
    }
}

if (!function_exists('jwtAuthUser')) {
    function jwtAuthUser()
    {
        try {
            return JWTAuth::parseToken()->authenticate();
        } catch (JWTException $e) {
            return null;
        }
    }
}

if (!function_exists('authUserId')) {
    function authUserId()
    {
        return Auth::check() ? Auth::user()->id : null;
    }
}

if (!function_exists('authUserEmail')) {
    function authUserEmail()
    {
        return Auth::check() ? Auth::user()->email : null;
    }
}

if (!function_exists('fileSizeGet')) {
    function fileSizeGet($sizeInKb = 1024)
    {
        $size = $sizeInKb;

        if ($sizeInKb >= 1024) {
            $size = ($sizeInKb / 1024);

            if (floor($size) == $size) {
                return $size . ' MB';
            } else {
                return number_format($size, 2) . ' MB';
            }
        }

        return $size.' KB';
    }
}
