# Copyright (c) 2015, Xhive Global Ltd. and Contributors

from unittest.mock import patch

import xhiveframework
from xhiveframework.tests.utils import XhiveFrameworkTestCase


class TestClient(XhiveFrameworkTestCase):
	def test_set_value(self):
		todo = xhiveframework.get_doc(dict(doctype="ToDo", description="test")).insert()
		xhiveframework.set_value("ToDo", todo.name, "description", "test 1")
		self.assertEqual(xhiveframework.get_value("ToDo", todo.name, "description"), "test 1")

		xhiveframework.set_value("ToDo", todo.name, {"description": "test 2"})
		self.assertEqual(xhiveframework.get_value("ToDo", todo.name, "description"), "test 2")

	def test_delete(self):
		from xhiveframework.client import delete
		from xhiveframework.desk.doctype.note.note import Note

		note = xhiveframework.get_doc(
			doctype="Note",
			title=xhiveframework.generate_hash(length=8),
			content="test",
			seen_by=[{"user": "Administrator"}],
		).insert()

		child_row_name = note.seen_by[0].name

		with patch.object(Note, "save") as save:
			delete("Note Seen By", child_row_name)
			save.assert_called()

		delete("Note", note.name)

		self.assertFalse(xhiveframework.db.exists("Note", note.name))
		self.assertRaises(xhiveframework.DoesNotExistError, delete, "Note", note.name)
		self.assertRaises(xhiveframework.DoesNotExistError, delete, "Note Seen By", child_row_name)

	def test_http_valid_method_access(self):
		from xhiveframework.client import delete
		from xhiveframework.handler import execute_cmd

		xhiveframework.set_user("Administrator")

		xhiveframework.local.request = xhiveframework._dict()
		xhiveframework.local.request.method = "POST"

		xhiveframework.local.form_dict = xhiveframework._dict(
			{"doc": dict(doctype="ToDo", description="Valid http method"), "cmd": "xhiveframework.client.save"}
		)
		todo = execute_cmd("xhiveframework.client.save")

		self.assertEqual(todo.get("description"), "Valid http method")

		delete("ToDo", todo.name)

	def test_http_invalid_method_access(self):
		from xhiveframework.handler import execute_cmd

		xhiveframework.set_user("Administrator")

		xhiveframework.local.request = xhiveframework._dict()
		xhiveframework.local.request.method = "GET"

		xhiveframework.local.form_dict = xhiveframework._dict(
			{"doc": dict(doctype="ToDo", description="Invalid http method"), "cmd": "xhiveframework.client.save"}
		)

		self.assertRaises(xhiveframework.PermissionError, execute_cmd, "xhiveframework.client.save")

	def test_run_doc_method(self):
		from xhiveframework.handler import execute_cmd

		if not xhiveframework.db.exists("Report", "Test Run Doc Method"):
			report = xhiveframework.get_doc(
				{
					"doctype": "Report",
					"ref_doctype": "User",
					"report_name": "Test Run Doc Method",
					"report_type": "Query Report",
					"is_standard": "No",
					"roles": [{"role": "System Manager"}],
				}
			).insert()
		else:
			report = xhiveframework.get_doc("Report", "Test Run Doc Method")

		xhiveframework.local.request = xhiveframework._dict()
		xhiveframework.local.request.method = "GET"

		# Whitelisted, works as expected
		xhiveframework.local.form_dict = xhiveframework._dict(
			{
				"dt": report.doctype,
				"dn": report.name,
				"method": "toggle_disable",
				"cmd": "run_doc_method",
				"args": 0,
			}
		)

		execute_cmd(xhiveframework.local.form_dict.cmd)

		# Not whitelisted, throws permission error
		xhiveframework.local.form_dict = xhiveframework._dict(
			{
				"dt": report.doctype,
				"dn": report.name,
				"method": "create_report_py",
				"cmd": "run_doc_method",
				"args": 0,
			}
		)

		self.assertRaises(xhiveframework.PermissionError, execute_cmd, xhiveframework.local.form_dict.cmd)

	def test_array_values_in_request_args(self):
		import requests

		from xhiveframework.auth import CookieManager, LoginManager

		xhiveframework.utils.set_request(path="/")
		xhiveframework.local.cookie_manager = CookieManager()
		xhiveframework.local.login_manager = LoginManager()
		xhiveframework.local.login_manager.login_as("Administrator")
		params = {
			"doctype": "DocType",
			"fields": ["name", "modified"],
			"sid": xhiveframework.session.sid,
		}
		headers = {
			"accept": "application/json",
			"content-type": "application/json",
		}
		url = (
			f"http://{xhiveframework.local.site}:{xhiveframework.conf.webserver_port}/api/method/xhiveframework.client.get_list"
		)
		res = requests.post(url, json=params, headers=headers)
		self.assertEqual(res.status_code, 200)
		data = res.json()
		first_item = data["message"][0]
		self.assertTrue("name" in first_item)
		self.assertTrue("modified" in first_item)
		xhiveframework.local.login_manager.logout()

	def test_client_get(self):
		from xhiveframework.client import get

		todo = xhiveframework.get_doc(doctype="ToDo", description="test").insert()
		filters = {"name": todo.name}
		filters_json = xhiveframework.as_json(filters)

		self.assertEqual(get("ToDo", filters=filters).description, "test")
		self.assertEqual(get("ToDo", filters=filters_json).description, "test")
		self.assertEqual(get("System Settings", "", "").doctype, "System Settings")
		self.assertEqual(get("ToDo", filters={}), get("ToDo", filters="{}"))
		todo.delete()

	def test_client_insert(self):
		from xhiveframework.client import insert

		def get_random_title():
			return f"test-{xhiveframework.generate_hash()}"

		# test insert dict
		doc = {"doctype": "Note", "title": get_random_title(), "content": "test"}
		note1 = insert(doc)
		self.assertTrue(note1)

		# test insert json
		doc["title"] = get_random_title()
		json_doc = xhiveframework.as_json(doc)
		note2 = insert(json_doc)
		self.assertTrue(note2)

		# test insert child doc without parent fields
		child_doc = {"doctype": "Note Seen By", "user": "Administrator"}
		with self.assertRaises(xhiveframework.ValidationError):
			insert(child_doc)

		# test insert child doc with parent fields
		child_doc = {
			"doctype": "Note Seen By",
			"user": "Administrator",
			"parenttype": "Note",
			"parent": note1.name,
			"parentfield": "seen_by",
		}
		note3 = insert(child_doc)
		self.assertTrue(note3)

		# cleanup
		xhiveframework.delete_doc("Note", note1.name)
		xhiveframework.delete_doc("Note", note2.name)

	def test_client_insert_many(self):
		from xhiveframework.client import insert, insert_many

		def get_random_title():
			return f"test-{xhiveframework.generate_hash(length=5)}"

		# insert a (parent) doc
		note1 = {"doctype": "Note", "title": get_random_title(), "content": "test"}
		note1 = insert(note1)

		doc_list = [
			{
				"doctype": "Note Seen By",
				"user": "Administrator",
				"parenttype": "Note",
				"parent": note1.name,
				"parentfield": "seen_by",
			},
			{
				"doctype": "Note Seen By",
				"user": "Administrator",
				"parenttype": "Note",
				"parent": note1.name,
				"parentfield": "seen_by",
			},
			{
				"doctype": "Note Seen By",
				"user": "Administrator",
				"parenttype": "Note",
				"parent": note1.name,
				"parentfield": "seen_by",
			},
			{"doctype": "Note", "title": "not-a-random-title", "content": "test"},
			{"doctype": "Note", "title": get_random_title(), "content": "test"},
			{"doctype": "Note", "title": get_random_title(), "content": "test"},
			{"doctype": "Note", "title": "another-note-title", "content": "test"},
		]

		# insert all docs
		docs = insert_many(doc_list)

		self.assertEqual(len(docs), 7)
		self.assertEqual(docs[3], "not-a-random-title")
		self.assertEqual(docs[6], "another-note-title")
		self.assertIn(note1.name, docs)

		# cleanup
		for doc in docs:
			xhiveframework.delete_doc("Note", doc)
