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

import jingrow
from tqdm import tqdm


def execute():
	notifications = jingrow.db.get_all(
		"Jcloud Notification", ["name", "document_type", "document_name"]
	)
	for notification in tqdm(notifications):
		if notification.document_type == "Agent Job":
			reference_pagetype = "Site"
			reference_pg = jingrow.db.get_value("Agent Job", notification.document_name, "site")
			if not reference_pg:
				reference_pagetype = "Server"
				reference_pg = jingrow.db.get_value(
					"Agent Job", notification.document_name, "server"
				)

		elif notification.document_type == "Deploy Candidate":
			reference_pagetype = "Release Group"
			reference_pg = jingrow.db.get_value(
				"Deploy Candidate", notification.document_name, "group"
			)

		jingrow.db.set_value(
			"Jcloud Notification",
			notification.name,
			{"reference_pagetype": reference_pagetype, "reference_name": reference_pg},
			update_modified=False,
		)
