from utility.privilege_manager import *
import Modules.Inventory.inventory_view as inventory_view
import persistence
from Model.isp_model import user_account

manager = Privilege_Manager()
privileges_users : set[ Privilege_Manager.Privilege_User] = set()


def init() :
    inventory_view.init()
    load_privilege_data()




def save_privilege_data() :
    None

def load_privilege_data() :
    with persistence.get_Session_Instance() as sess :
        users = sess.query(user_account)

        user : user_account
        for user in users :
            privileges = Privilege_Manager.Privilege_User(manager,user.privileges_data,user.is_super_admin,user.id)
            privileges_users.add(privileges)
            logger.debug("user privilege for account : " + user.nickname + " with id " + str(user.id) + " parsed")
            roles_string : list[str] = list()
            for role in privileges.get_roles() :
                roles_string.append(manager.get_role_domain(role).name + ' : ' + role.name)
            logger.debug("roles found : " + str(roles_string))


def get_privileges_from_user(user_id : int) :
    for user in privileges_users :
        if user.id == user_id :
            return user

    raise Exception("user privileges not found with id " + str(user_id))


#TODO flask blueprint for privilege management here
