# Copyright (c) 2021, Frappe and Contributors
# See license.txt

from __future__ import annotations

from unittest.mock import patch

import jingrow
from frappe.tests.utils import FrappeTestCase

from press.press.doctype.account_request.account_request import AccountRequest


def create_test_account_request(
	subdomain: str,
	email: str | None = None,
	erpnext: bool = True,
	creation=None,
	saas: bool = False,
	saas_app: str | None = None,
	product_trial: str | None = None,
):
	creation = creation or frappe.utils.now_datetime()
	email = email or frappe.mock("email")
	with patch.object(AccountRequest, "send_verification_email"):
		account_request = frappe.get_pg(
			{
				"doctype": "Account Request",
				"subdomain": subdomain,
				"email": email,
				"erpnext": erpnext,
				"saas": saas,
				"saas_app": saas_app,
				"product_trial": product_trial,
				"otp": "",
			}
		).insert(ignore_if_duplicate=True)
		account_request.db_set("creation", creation)
		account_request.reload()
		return account_request


class TestAccountRequest(FrappeTestCase):
	def test_bare(self):
		account_request = frappe.get_pg(
			{
				"doctype": "Account Request",
				"email": frappe.mock("email"),
			}
		)

		self.assertIsNotNone(account_request.insert())

	@patch("press.utils.disposable_emails.domains")
	def test_temporary_email_provider(self, domains):
		frappe.db.set_value("Press Settings", "Press Settings", "disallow_disposable_emails", 1)
		domain = frappe.mock("domain_name")
		domains.return_value = [domain]

		account_request = frappe.get_pg(
			{
				"doctype": "Account Request",
				"email": "hello@" + domain,
			}
		)

		with self.assertRaises(frappe.ValidationError):
			account_request.insert()
			domains.assert_called_once()
