# Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors
# License: MIT. See LICENSE

import xhiveframework


def sendmail_to_system_managers(subject, content):
	xhiveframework.sendmail(recipients=get_system_managers(), subject=subject, content=content)


@xhiveframework.whitelist()
def get_contact_list(txt, page_length=20) -> list[dict]:
	"""Return email ids for a multiselect field."""
	from xhiveframework.contacts.doctype.contact.contact import get_full_name

	if cached_contacts := get_cached_contacts(txt):
		return cached_contacts[:page_length]

	fields = ["first_name", "middle_name", "last_name", "company_name"]
	contacts = xhiveframework.get_list(
		"Contact",
		fields=[*fields, "`tabContact Email`.email_id"],
		filters=[
			["Contact Email", "email_id", "is", "set"],
		],
		or_filters=[[field, "like", f"%{txt}%"] for field in fields]
		+ [["Contact Email", "email_id", "like", f"%{txt}%"]],
		limit_page_length=page_length,
	)

	# The multiselect field will store the `label` as the selected value.
	# The `value` is just used as a unique key to distinguish between the options.
	# https://lab.membtech.com/xhiveframework/xhiveframework15/blob/6c6a89bcdd9454060a1333e23b855d0505c9ebc2/xhiveframework/public/js/xhiveframework/form/controls/autocomplete.js#L29-L35
	result = [
		xhiveframework._dict(
			value=d.email_id,
			label=d.email_id,
			description=get_full_name(d.first_name, d.middle_name, d.last_name, d.company_name),
		)
		for d in contacts
	]

	update_contact_cache(result)

	return result


def get_system_managers():
	return xhiveframework.db.sql_list(
		"""select parent FROM `tabHas Role`
		WHERE role='System Manager'
		AND parent!='Administrator'
		AND parent IN (SELECT email FROM tabUser WHERE enabled=1)"""
	)


@xhiveframework.whitelist()
def relink(name, reference_doctype=None, reference_name=None):
	xhiveframework.db.sql(
		"""update
			`tabCommunication`
		set
			reference_doctype = %s,
			reference_name = %s,
			status = "Linked"
		where
			communication_type = "Communication" and
			name = %s""",
		(reference_doctype, reference_name, name),
	)


@xhiveframework.whitelist()
@xhiveframework.validate_and_sanitize_search_inputs
def get_communication_doctype(doctype, txt, searchfield, start, page_len, filters):
	user_perms = xhiveframework.utils.user.UserPermissions(xhiveframework.session.user)
	user_perms.build_permissions()
	can_read = user_perms.can_read
	from xhiveframework.modules import load_doctype_module

	com_doctypes = []
	if len(txt) < 2:
		for name in xhiveframework.get_hooks("communication_doctypes"):
			try:
				module = load_doctype_module(name, suffix="_dashboard")
				if hasattr(module, "get_data"):
					for i in module.get_data()["transactions"]:
						com_doctypes += i["items"]
			except ImportError:
				pass
	else:
		com_doctypes = [
			d[0] for d in xhiveframework.db.get_values("DocType", {"issingle": 0, "istable": 0, "hide_toolbar": 0})
		]

	return [[dt] for dt in com_doctypes if txt.lower().replace("%", "") in dt.lower() and dt in can_read]


def get_cached_contacts(txt):
	contacts = xhiveframework.cache.hget("contacts", xhiveframework.session.user) or []

	if not contacts:
		return

	if not txt:
		return contacts

	return [
		d
		for d in contacts
		if (d.value and ((d.value and txt in d.value) or (d.description and txt in d.description)))
	]


def update_contact_cache(contacts):
	cached_contacts = xhiveframework.cache.hget("contacts", xhiveframework.session.user) or []

	uncached_contacts = [d for d in contacts if d not in cached_contacts]
	cached_contacts.extend(uncached_contacts)

	xhiveframework.cache.hset("contacts", xhiveframework.session.user, cached_contacts)
