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

import jingrow
from jingrow.model.page import Page
from jingrow.permissions import _pop_debug_log, has_permission


class PermissionInspector(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.types import DF

		pagename: DF.DynamicLink | None
		output: DF.Code | None
		permission_type: DF.Literal[
			"read",
			"write",
			"create",
			"delete",
			"submit",
			"cancel",
			"select",
			"amend",
			"print",
			"email",
			"report",
			"import",
			"export",
			"share",
		]
		ref_pagetype: DF.Link
		user: DF.Link
	# end: auto-generated types

	@jingrow.whitelist()
	def debug(self):
		if not (self.ref_pagetype and self.user):
			return

		result = has_permission(
			self.ref_pagetype, ptype=self.permission_type, pg=self.pagename, user=self.user, debug=True
		)

		self.output = "\n==============================\n".join(_pop_debug_log())
		self.output += "\n\n" + f"Ouput of has_permission: {result}"

	# None of these apply, overriden for sanity.
	def load_from_db(self):
		super(Page, self).__init__({"modified": None, "permission_type": "read"})

	def db_insert(self, *args, **kwargs):
		...

	def db_update(self):
		...

	@staticmethod
	def get_list(args):
		...

	@staticmethod
	def get_count(args):
		...

	@staticmethod
	def get_stats(args):
		...

	def delete(self):
		...
