# Copyright (c) 2020, JINGROW
# See license.txt
from __future__ import annotations

from unittest.mock import Mock, patch

import jingrow
from jingrow.model.naming import make_autoname
from jingrow.tests.utils import JingrowTestCase
from moto import mock_aws

from jcloud.jcloud.pagetype.agent_job.test_agent_job import fake_agent_job
from jcloud.jcloud.pagetype.jcloud_settings.test_jcloud_settings import (
	create_test_jcloud_settings,
)
from jcloud.jcloud.pagetype.proxy_server.proxy_server import ProxyServer
from jcloud.jcloud.pagetype.root_domain.root_domain import RootDomain
from jcloud.jcloud.pagetype.server.server import BaseServer
from jcloud.jcloud.pagetype.virtual_machine.test_virtual_machine import create_test_virtual_machine
from jcloud.utils.test import foreground_enqueue_pg


@patch.object(BaseServer, "after_insert", new=Mock())
@patch.object(ProxyServer, "validate_domains", new=Mock())
def create_test_proxy_server(
	hostname: str = "n",
	domain: str = "fc.dev",
	domains: list[dict[str, str]] | None = None,
	cluster: str = "Default",
	is_primary: bool = True,
) -> ProxyServer:
	"""Create test Proxy Server pg"""
	if domains is None:
		domains = [{"domain": "fc.dev"}]
	create_test_jcloud_settings()
	server = jingrow.get_pg(
		{
			"pagetype": "Proxy Server",
			"status": "Active",
			"ip": jingrow.mock("ipv4"),
			"private_ip": jingrow.mock("ipv4_private"),
			"hostname": make_autoname(hostname + ".######"),
			"cluster": cluster,
			"domain": domain,
			"domains": domains,
			"is_primary": is_primary,
			"virtual_machine": create_test_virtual_machine().name,
		}
	).insert(ignore_if_duplicate=True)
	server.reload()
	return server


@patch(
	"jcloud.jcloud.pagetype.proxy_server.proxy_server.jingrow.enqueue_pg",
	foreground_enqueue_pg,
)
@patch("jcloud.jcloud.pagetype.proxy_server.proxy_server.Ansible", new=Mock())
class TestProxyServer(JingrowTestCase):
	@fake_agent_job("Reload NGINX Job")
	@mock_aws
	@patch.object(
		RootDomain,
		"update_dns_records_for_sites",
		wraps=RootDomain.update_dns_records_for_sites,
		autospec=True,
	)
	def test_sites_dns_updated_on_failover(self, update_dns_records_for_sites):
		from jcloud.jcloud.pagetype.server.test_server import create_test_server
		from jcloud.jcloud.pagetype.site.test_site import create_test_site

		proxy1 = create_test_proxy_server()
		proxy2 = create_test_proxy_server(is_primary=False)

		root_domain: RootDomain = jingrow.get_pg("Root Domain", proxy1.domain)
		root_domain.boto3_client.create_hosted_zone(
			Name=proxy1.domain,
			CallerReference="1",
			HostedZoneConfig={"Comment": "Test", "PrivateZone": False},
		)

		server = create_test_server(proxy1.name)
		site1 = create_test_site(server=server.name)
		create_test_site()  # another proxy; unrelated

		proxy2.db_set("primary", proxy1.name)
		proxy2.db_set("is_replication_setup", 1)
		proxy2.trigger_failover()
		update_dns_records_for_sites.assert_called_once_with(root_domain, [site1.name], proxy2.name)
		proxy2.reload()
		proxy1.reload()
		self.assertTrue(proxy2.is_primary)
		self.assertFalse(proxy1.is_primary)
		self.assertEqual(proxy2.status, "Active")
		self.assertEqual(proxy1.status, "Active")
