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

import jingrow
from jingrow.model.page import Page

from jcloud.jcloud.pagetype.database_server.database_server import DatabaseServer


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

		datatype: DF.Literal["Int", "Float", "Str"]
		default_value: DF.Data | None
		pg_section: DF.Literal["server", "replication-and-binary-log", "innodb"]
		dynamic: DF.Check
		set_on_new_servers: DF.Check
		skippable: DF.Check
	# end: auto-generated types

	def get_default_value(self):
		if not (value := self.default_value):
			jingrow.throw("Default Value is required")
		match self.datatype:
			case "Int":
				return int(value)
			case "Float":
				return float(value)
		return value

	@jingrow.whitelist()
	def set_on_all_servers(self):
		value = self.get_default_value()
		servers = jingrow.get_all(
			"Database Server", {"status": "Active", "is_self_hosted": False}, pluck="name"
		)
		for server_name in servers:
			server: DatabaseServer = jingrow.get_pg("Database Server", server_name)
			server.add_mariadb_variable(self.name, f"value_{self.datatype.lower()}", value)

	@jingrow.whitelist()
	def set_on_server(self, server_name):
		value = self.get_default_value()
		server: DatabaseServer = jingrow.get_pg("Database Server", server_name)
		server.add_mariadb_variable(self.name, f"value_{self.datatype.lower()}", value)
