# Copyright (c) 2023, JINGROW
# For license information, please see license.txt

import jingrow
from jingrow.model.page import Page
from jingrow.query_builder.utils import PageType


class CustomHTMLBlock(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

		html: DF.Code | None
		private: DF.Check
		roles: DF.Table[HasRole]
		script: DF.Code | None
		style: DF.Code | None
	# end: auto-generated types
	pass


@jingrow.whitelist()
def get_custom_blocks_for_user(pagetype, txt, searchfield, start, page_len, filters):
	# return logged in users private blocks and all public blocks
	customHTMLBlock = PageType("Custom HTML Block")

	condition_query = jingrow.qb.from_(customHTMLBlock)

	return (
		condition_query.select(customHTMLBlock.name).where(
			(customHTMLBlock.private == 0)
			| ((customHTMLBlock.owner == jingrow.session.user) & (customHTMLBlock.private == 1))
		)
	).run()
