# Copyright (c) 2024, Jingrow and Contributors
# See license.txt

import jingrow
from jingrow.model.naming import make_autoname
from jingrow.tests.utils import JingrowTestCase

from jcloude.jcloude.pagetype.team.test_team import create_test_team


class TestJcloudeRole(JingrowTestCase):
	def setUp(self):
		jingrow.set_user("Administrator")
		jingrow.db.delete("Jcloude Role")
		self.team_user = create_user("team@example.com")
		self.team = create_test_team(self.team_user.email)
		self.team.user = "Administrator"
		self.team_member = create_user("user123@example.com")
		self.team.append("team_members", {"user": self.team_member.name})
		self.team.save()
		self.external_team_member = create_user("external@example.com")
		self.admin_perm_role = create_permission_role(self.team.name)
		self.perm_role = create_permission_role(self.team.name)
		self.perm_role2 = create_permission_role(self.team.name)

	def tearDown(self):
		jingrow.set_user("Administrator")
		jingrow.delete_pg("Jcloude Role", self.perm_role.name, force=True)
		jingrow.delete_pg("Jcloude Role", self.perm_role2.name, force=True)
		jingrow.delete_pg("Team", self.team.name, force=True)
		jingrow.delete_pg("User", self.team_member.name, force=True)
		jingrow.delete_pg("User", self.team_user.name, force=True)
		jingrow.local._current_team = None

	@property
	def team_pg(self):
		return jingrow.get_pg("Team", self.team.name)

	def test_add_user(self):
		self.perm_role.add_user(self.team_member.name)
		perm_role_users = get_users(self.perm_role)
		perm_role_user_exists = any(
			self.team_member.name == perm_role_user.user for perm_role_user in perm_role_users
		)
		self.assertTrue(perm_role_user_exists)
		self.assertRaises(jingrow.ValidationError, self.perm_role.add_user, self.team_member.name)

	def test_remove_user(self):
		self.perm_role.add_user(self.team_member.name)
		self.perm_role.remove_user(self.team_member.name)
		perm_role_users = get_users(self.perm_role)
		perm_role_user_exists = any(
			self.team_member.name == perm_role_user.user for perm_role_user in perm_role_users
		)
		self.assertFalse(perm_role_user_exists)
		self.assertRaises(jingrow.ValidationError, self.perm_role.remove_user, self.team_member.name)

	def test_dont_allow_to_add_user_if_not_team_member(self):
		jingrow.set_user("Administrator")
		jingrow.local._current_team = self.team_pg

		role = create_permission_role(self.team.name)
		self.assertRaises(jingrow.exceptions.PermissionError, role.add_user, self.external_team_member.name)


# utils
def create_permission_role(team, allow_site_creation=0):
	pg = jingrow.new_pg("Jcloude Role")
	pg.title = make_autoname("Test-Role-.###")
	pg.team = team
	pg.allow_site_creation = allow_site_creation
	pg.save()

	return pg


def create_user(email):
	if jingrow.db.exists("User", email):
		return jingrow.get_pg("User", email)
	user = jingrow.new_pg("User")
	user.email = email
	user.first_name = email.split("@")[0]
	user.save()
	return user


def get_users(role):
	return role.users
