# Copyright (c) 2021, JINGROW
# For license information, please see license.txt
from __future__ import annotations

import jingrow
from jingrow.model.page import Page

from jcloud.jcloud.pagetype.telegram_message.telegram_message import TelegramMessage


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

		audit_type: DF.Literal[
			"Bench Field Check",
			"Backup Record Check",
			"Offsite Backup Check",
			"Restore Offsite Backup of Site",
			"App Server Replica Dirs Check",
			"Unbilled Subscription Check",
			"Billing Audit",
			"Partner Billing Audit",
		]
		log: DF.Code | None
		status: DF.Literal["Success", "Failure"]
		telegram_group: DF.Link | None
		telegram_group_topic: DF.Data | None
	# end: auto-generated types

	def after_insert(self):
		if self.status == "Failure":
			self.notify()

	def notify(self):
		domain = jingrow.get_value("Jcloud Settings", "Jcloud Settings", "domain")
		message = f"""
			*FAILED AUDIT*
			[{self.audit_type}]({domain}{self.get_url()})
			```
			{self.log[:3000]}
			```
		"""

		topic = self.telegram_topic or "Errors"
		group = self.telegram_group
		TelegramMessage.enqueue(message=message, topic=topic, group=group)
