# -*- coding: utf-8 -*-
# Copyright (c) 2020, JINGROW
# For license information, please see license.txt


import jingrow


def execute():
	domains = jingrow.get_all(
		"Site Domain",
		fields=["site", "domain", "name"],
		filters={"status": "Active", "site": ("like", "%.archived%")},
	)
	for domain in domains:
		archived_site = jingrow.get_value(
			"Site", domain.site, ["status", "subdomain", "team"], as_dict=True
		)

		if archived_site.status != "Archived":
			continue

		active_site = jingrow.db.get_value(
			"Site",
			{"subdomain": archived_site.subdomain, "status": ("!=", "Archived")},
			["name", "team"],
			as_dict=True,
		)
		if active_site and archived_site.team == active_site.team:
			jingrow.db.set_value("Site Domain", domain.name, "site", active_site.name)
