<?php

namespace App\Providers;

use App\Enumerations\UserRole;

class AuthCheckProvider {
    public function checkLoggedIn() : bool {
        if (!auth()->check()) {
            return false;
        }

        $user = auth()->user();
        $tenant = app('tenant');
        if ($tenant->slug === 'lv') {
            return $user->active;
        }

        if ($user->user_role_main === UserRole::USER_ROLE_ADMIN) {
            return true;
        }

        return $user->active && $tenant->slug === $user->local_group;
    }

    public function getUserRole() : ?string {
        if (!$this->checkLoggedIn()) {
            return null;
        }

        if (app('tenant')->slug === 'lv') {
            return auth()->user()->user_role_main;
        }

        return auth()->user()->user_role_local_group;
    }
}
