<?php

use App\Models\Role;

if(!function_exists('getRoles')){
    function getRoles($key = null): array|string
    {
        $roles = Role::NAMES;
        if($key && isset($roles[$key])){
            return $roles[$key];
        } else {
            return $roles;
        }
    }
}

if(!function_exists('hasRole')){
    function hasRole($roles, $user = null) : bool
    {
        if(!$user) $user = auth()->user();

        $roles = explode(',', $roles);
        return ($user && in_array($user->role, $roles));
    }
}

if(!function_exists('roleName')) {
    function roleName($role): string
    {
        return Role::NAMES[$role];
    }
}

// return current year
if(!function_exists('year')) {
    function year(): int
    {
        return (int)session('year', date('Y'));
    }
}
