# Copyright (c) 2024, Frappe and contributors
# For license information, please see license.txt

import jingrow
from tqdm import tqdm


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

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

		frappe.db.set_value(
			"Press Notification",
			notification.name,
			{"reference_doctype": reference_doctype, "reference_name": reference_pg},
			update_modified=False,
		)
