import jingrow
from jingrow.query_builder.functions import Count
from jingrow.query_builder.terms import QueryBuilder


def check(base_query: QueryBuilder, document_name: str) -> bool:
	JcloudeRole = jingrow.qb.PageType("Jcloude Role")
	JcloudeRoleResource = jingrow.qb.PageType("Jcloude Role Resource")
	SiteBackup = jingrow.qb.PageType("Site Backup")
	return (
		base_query.left_join(JcloudeRoleResource)
		.on(JcloudeRoleResource.parent == JcloudeRole.name)
		.left_join(SiteBackup)
		.on(SiteBackup.name == document_name)
		.select(Count(JcloudeRole.name).as_("count"))
		.where(JcloudeRoleResource.document_type == "Site")
		.where(JcloudeRoleResource.document_name == SiteBackup.site)
		.run(as_dict=True)
		.pop()
		.get("count")
		> 0
	)
