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

import json

import jingrow
from jingrow.model.page import Page


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

		emails: DF.Int
		expiry: DF.Date
		plan: DF.Data | None
		site: DF.Link
		space: DF.Int
		support_expiry: DF.Date | None
		users: DF.Int
	# end: auto-generated types

	def on_update(self):
		config_keys = ("users", "expiry", "emails", "space", "current_plan")
		values = (self.users, self.expiry, self.emails, self.space, self.plan)

		site = jingrow.get_pg("Site", self.site)
		config = json.loads(site.config)
		limits = config.get("limits", {})

		limits.update(dict(zip(config_keys, values)))

		# remove null/empty values
		limits = {k: v for k, v in limits.items() if v}

		site.update_site_config({"limits": limits})
