# Copyright (c) 2017, JINGROW
# License: MIT. See LICENSE

from collections import defaultdict

import jingrow
from jingrow.model.page import Page


class RoleProfile(Page):
	# begin: auto-generated types
	# This code is auto-generated. Do not modify anything in this block.

	from typing import TYPE_CHECKING

	if TYPE_CHECKING:
		from jingrow.core.pagetype.has_role.has_role import HasRole
		from jingrow.types import DF

		description: DF.Data | None
		role_profile: DF.Data
		roles: DF.Table[HasRole]
	# end: auto-generated types
	def autoname(self):
		"""set name as Role Profile name"""
		self.name = self.role_profile

	def on_update(self):
		self.clear_cache()
		self.queue_action(
			"update_all_users",
			now=jingrow.flags.in_test or jingrow.flags.in_install,
			enqueue_after_commit=True,
		)

	def update_all_users(self):
		"""Changes in role_profile reflected across all its user"""
		has_role = jingrow.qb.PageType("Has Role")
		user = jingrow.qb.PageType("User")

		all_current_roles = (
			jingrow.qb.from_(user)
			.join(has_role)
			.on(user.name == has_role.parent)
			.where(user.role_profile_name == self.name)
			.select(user.name, has_role.role)
		).run()

		user_roles = defaultdict(set)
		for user, role in all_current_roles:
			user_roles[user].add(role)

		role_profile_roles = {role.role for role in self.roles}
		for user, roles in user_roles.items():
			if roles != role_profile_roles:
				user = jingrow.get_pg("User", user)
				user.roles = []
				user.add_roles(*role_profile_roles)
