import boto3
import jingrow
from jingrow.core.utils import find
from jingrow.model.page import Page

from jcloud.utils import log_error


@jingrow.whitelist()
def create_dns_record(pg, record_name=None):
	"""Check if site needs dns records and creates one."""
	domain = jingrow.get_pg("Root Domain", pg.domain)
	is_standalone = jingrow.get_value("Server", pg.server, "is_standalone")
	if pg.cluster == domain.default_cluster and not is_standalone:
		return

	if is_standalone:
		_change_dns_record("UPSERT", domain, pg.server, record_name=record_name)
	else:
		proxy_server = jingrow.get_value("Server", pg.server, "proxy_server")
		_change_dns_record("UPSERT", domain, proxy_server, record_name=record_name)


def _change_dns_record(
	method: str, domain: Page, proxy_server: str, record_name: str = None
):
	"""
	Change dns record of site

	method: CREATE | DELETE | UPSERT
	"""
	try:
		client = boto3.client(
			"route53",
			aws_access_key_id=domain.aws_access_key_id,
			aws_secret_access_key=domain.get_password("aws_secret_access_key"),
		)
		zones = client.list_hosted_zones_by_name()["HostedZones"]
		hosted_zone = find(reversed(zones), lambda x: domain.name.endswith(x["Name"][:-1]))[
			"Id"
		]
		client.change_resource_record_sets(
			ChangeBatch={
				"Changes": [
					{
						"Action": method,
						"ResourceRecordSet": {
							"Name": record_name,
							"Type": "CNAME",
							"TTL": 600,
							"ResourceRecords": [{"Value": proxy_server}],
						},
					}
				]
			},
			HostedZoneId=hosted_zone,
		)
	except client.exceptions.InvalidChangeBatch as e:
		# If we're attempting to DELETE and record is not found, ignore the error
		# e.response["Error"]["Message"] looks like
		# [Tried to delete resource record set [name='xxx.jingrow.cloud.', type='CNAME'] but it was not found]
		if method == "DELETE" and "but it was not found" in e.response["Error"]["Message"]:
			return
		log_error(
			"Route 53 Record Creation Error",
			domain=domain.name,
			site=record_name,
			proxy_server=proxy_server,
		)
	except Exception:
		log_error(
			"Route 53 Record Creation Error",
			domain=domain.name,
			site=record_name,
			proxy_server=proxy_server,
		)
