# Copyright (c) 2023, Jingrow Technologies Pvt. Ltd. and Contributors
# MIT License. See license.txt

from __future__ import annotations

import inspect
import typing

import jingrow
from jingrow.client import set_value as _set_value
from jingrow.handler import run_pg_method as _run_pg_method
from jingrow.model import child_table_fields, default_fields
from jingrow.model.base_document import get_controller
from jingrow.utils import cstr
from pypika.queries import QueryBuilder

from jcloude.access import dashboard_access_rules
from jcloude.access.support_access import has_support_access
from jcloude.exceptions import TeamHeaderNotInRequestError
from jcloude.guards import role_guard
from jcloude.utils import has_role

if typing.TYPE_CHECKING:
	from jingrow.model.meta import Meta

ALLOWED_DOCTYPES = [
	"Site",
	"Site App",
	"Site Domain",
	"Site Backup",
	"Site Activity",
	"Server Activity",
	"Site Config",
	"Site Plan",
	"Site Update",
	"Site Group Deploy",
	"Invoice",
	"Balance Transaction",
	"Stripe Payment Method",
	"Bench",
	"Bench App",
	"Bench Dependency Version",
	"Release Group",
	"Release Group App",
	"Release Group Dependency",
	"Cluster",
	"Press Permission Group",
	"Press Role",
	"Team",
	"Product Trial Request",
	"Deploy Candidate",
	"Deploy Candidate Difference",
	"Deploy Candidate Difference App",
	"Agent Job",
	"Agent Job Type",
	"Common Site Config",
	"Server",
	"Database Server",
	"Ansible Play",
	"Server Plan",
	"Release Group Variable",
	"Resource Tag",
	"Press Tag",
	"Partner Approval Request",
	"Marketplace App",
	"Subscription",
	"Marketplace App Version",
	"Marketplace App Plan",
	"App Release",
	"Payout Order",
	"App Patch",
	"Product Trial",
	"Press Notification",
	"User SSH Key",
	"Jingrow Version",
	"Dashboard Banner",
	"App Release Approval Request",
	"Press Webhook",
	"SQL Playground Log",
	"Site Database User",
	"Press Settings",
	"Mpesa Payment Record",
	"Partner Certificate",
	"Partner Payment Payout",
	"Deploy Candidate Build",
	"Partner Lead",
	"Partner Lead Type",
	"Lead Followup",
	"Partner Consent",
	"Account Request",
	"Server Snapshot",
	"Server Snapshot Recovery",
	"Support Access",
	"Partner Lead Origin",
	"Auto Scale Record",
]

whitelisted_methods = set()


@jingrow.whitelist()
def get_list(
	doctype: str,
	fields: list | None = None,
	filters: dict | None = None,
	order_by: str | None = None,
	start: int = 0,
	limit: int = 20,
	parent: str | None = None,
	debug: bool = False,
):
	if filters is None:
		filters = {}

	# these doctypes doesn't have a team field to filter by but are used in get or run_pg_method
	if doctype in ["Team", "User SSH Key"]:
		return []

	check_permissions(doctype)
	valid_fields = validate_fields(doctype, fields)
	valid_filters = validate_filters(doctype, filters)

	meta = jingrow.get_meta(doctype)
	if meta.istable and not (filters.get("parenttype") and filters.get("parent")):
		jingrow.throw("parenttype and parent are required to get child records")

	apply_team_filter = not (
		filters.get("skip_team_filter_for_system_user_and_support_agent")
		and (jingrow.local.system_user() or has_role("Press Support Agent"))
	)
	if apply_team_filter and meta.has_field("team"):
		valid_filters.team = jingrow.local.team().name

	query = get_list_query(
		doctype,
		meta,
		filters,
		valid_filters,
		valid_fields,
		start,
		limit,
		order_by,
	)
	filters = jingrow._dict(filters or {})
	list_args = dict(
		fields=fields,
		filters=filters,
		order_by=order_by,
		start=start,
		limit=limit,
		parent=parent,
		debug=debug,
	)
	query = apply_custom_filters(doctype, query, **list_args)
	if isinstance(query, QueryBuilder):
		return query.run(as_dict=1, debug=debug)

	if isinstance(query, list):
		return query

	return []


@role_guard.document(
	document_type=lambda args: str(args.get("doctype")),
	should_throw=False,
	inject_values=True,
	injection_key="document_options",
)
def get_list_query(
	doctype: str,
	meta: "Meta",
	filters: dict,
	valid_filters: jingrow._dict,
	valid_fields: list | None,
	start: int,
	limit: int,
	order_by: str | None,
	document_options=None,
):
	query = jingrow.qb.get_query(
		doctype, filters=valid_filters, fields=valid_fields, offset=start, limit=limit, order_by=order_by
	)

	if meta.istable and jingrow.get_meta(filters.get("parenttype")).has_field("team"):
		ParentDocType = jingrow.qb.DocType(filters.get("parenttype"))
		ChildDocType = jingrow.qb.DocType(doctype)

		query = (
			query.join(ParentDocType)
			.on(ParentDocType.name == ChildDocType.parent)
			.where(ParentDocType.team == jingrow.local.team().name)
		)

	if document_options and isinstance(document_options, list):
		QueryDoctype = jingrow.qb.DocType(doctype)
		query = query.where(QueryDoctype.name.isin(document_options))

	return query


@jingrow.whitelist()
@role_guard.document(
	document_type=lambda args: str(args.get("doctype")),
	document_name=lambda args: str(args.get("name")),
)
def get(doctype, name):
	check_permissions(doctype)
	try:
		pg = jingrow.get_pg(doctype, name)
	except jingrow.DoesNotExistError:
		controller = get_controller(doctype)
		if hasattr(controller, "on_not_found"):
			return controller.on_not_found(name)
		raise

	if (
		not (jingrow.local.system_user() or has_support_access(doctype, name))
		and jingrow.get_meta(doctype).has_field("team")
		and pg.team != jingrow.local.team().name
	):
		raise_not_permitted()

	fields = tuple(default_fields)
	if hasattr(pg, "dashboard_fields"):
		fields += tuple(pg.dashboard_fields)

	_pg = jingrow._dict()
	for fieldname in fields:
		_pg[fieldname] = pg.get(fieldname)

	if hasattr(pg, "get_pg"):
		result = pg.get_pg(_pg)
		if isinstance(result, dict):
			_pg.update(result)

	return dashboard_access_rules(_pg)


@jingrow.whitelist(methods=["POST", "PUT"])
def insert(pg=None):
	if not pg or not pg.get("doctype"):
		jingrow.throw(jingrow._("pg.doctype is required"))

	check_permissions(pg.get("doctype"))

	pg = jingrow._dict(pg)
	if jingrow.is_table(pg.doctype):
		if not (pg.parenttype and pg.parent and pg.parentfield):
			jingrow.throw(jingrow._("Parenttype, Parent and Parentfield are required to insert a child record"))

		# inserting a child record
		parent = jingrow.get_pg(pg.parenttype, pg.parent)

		if jingrow.get_meta(parent.doctype).has_field("team") and parent.team != jingrow.local.team().name:
			raise_not_permitted()

		parent.append(pg.parentfield, pg)
		parent.save()
		return get(parent.doctype, parent.name)

	_pg = jingrow.get_pg(pg)

	if jingrow.get_meta(pg.doctype).has_field("team"):
		if not _pg.team:
			# set team if not set
			_pg.team = jingrow.local.team().name
		if not jingrow.local.system_user():
			# don't allow dashboard user to set any other team
			_pg.team = jingrow.local.team().name
	_pg.insert()
	return get(_pg.doctype, _pg.name)


@jingrow.whitelist(methods=["POST", "PUT"])
def set_value(doctype: str, name: str, fieldname: dict | str, value: str | None = None):
	check_permissions(doctype)
	check_document_access(doctype, name)

	for field in fieldname:
		# fields mentioned in dashboard_fields are allowed to be set via set_value
		is_allowed_field(doctype, field)

	_set_value(doctype, name, fieldname, value)

	# jingrow set_value returns just the pg and not jcloude's overriden `get_pg`
	return get(doctype, name)


@jingrow.whitelist(methods=["DELETE", "POST"])
def delete(doctype: str, name: str):
	method = "delete"

	check_permissions(doctype)
	check_document_access(doctype, name)
	check_dashboard_actions(doctype, name, method)

	_run_pg_method(dt=doctype, dn=name, method=method, args=None)


@jingrow.whitelist()
def run_pg_method(dt: str, dn: str, method: str, args: dict | None = None):
	check_permissions(dt)
	check_document_access(dt, dn)
	check_dashboard_actions(dt, dn, method)

	_run_pg_method(
		dt=dt,
		dn=dn,
		method=method,
		args=fix_args(method, args),
	)

	jingrow.response.docs = [get(dt, dn)]


@jingrow.whitelist()
def search_link(
	doctype: str,
	query: str | None = None,
	filters: dict | None = None,
	order_by: str | None = None,
	page_length: int | None = None,
):
	check_permissions(doctype)
	if doctype == "Team" and not jingrow.local.system_user():
		raise_not_permitted()

	meta = jingrow.get_meta(doctype)
	DocType = jingrow.qb.DocType(doctype)
	valid_filters = validate_filters(doctype, filters)
	valid_fields = validate_fields(doctype, ["name", meta.title_field or "name"])
	q = get_list_query(
		doctype,
		meta,
		filters,
		valid_filters,
		valid_fields,
		0,
		page_length or 10,
		order_by or "modified desc",
	)
	q = q.select(DocType.name.as_("value"))
	if meta.title_field:
		q = q.select(DocType[meta.title_field].as_("label"))
	if meta.has_field("enabled"):
		q = q.where(DocType.enabled == 1)
	if meta.has_field("disabled"):
		q = q.where(DocType.disabled != 1)
	if meta.has_field("team") and (not jingrow.local.system_user() or 1):
		q = q.where(DocType.team == jingrow.local.team().name)
	if query:
		condition = DocType.name.like(f"%{query}%")
		if meta.title_field:
			condition = condition | DocType[meta.title_field].like(f"%{query}%")
		q = q.where(condition)
	return q.run(as_dict=1)


def check_document_access(doctype: str, name: str):
	if jingrow.local.system_user():
		return

	team = ""
	meta = jingrow.get_meta(doctype)
	if meta.has_field("team"):
		team = jingrow.db.get_value(doctype, name, "team")
	elif meta.has_field("bench"):
		bench = jingrow.db.get_value(doctype, name, "bench")
		team = jingrow.db.get_value("Bench", bench, "team")
	elif meta.has_field("group"):
		group = jingrow.db.get_value(doctype, name, "group")
		team = jingrow.db.get_value("Release Group", group, "team")
	else:
		return

	if team == jingrow.local.team().name:
		return

	if has_support_access(doctype, name):
		return

	raise_not_permitted()


def check_dashboard_actions(doctype, name, method):
	pg = jingrow.get_pg(doctype, name)
	method_obj = getattr(pg, method)
	fn = getattr(method_obj, "__func__", method_obj)

	if fn not in whitelisted_methods:
		raise_not_permitted()


def apply_custom_filters(doctype, query, **list_args):
	"""Apply custom filters to query"""
	controller = get_controller(doctype)
	if hasattr(controller, "get_list_query"):
		if inspect.getfullargspec(controller.get_list_query).varkw:
			return controller.get_list_query(query, **list_args)
		return controller.get_list_query(query)

	return query


def validate_filters(doctype, filters):
	"""Filter filters based on permissions"""
	if not filters:
		filters = {}

	out = jingrow._dict()
	for fieldname, value in filters.items():
		if is_allowed_field(doctype, fieldname):
			out[fieldname] = value

	return out


def validate_fields(doctype, fields):
	"""Filter fields based on permissions"""
	if not fields:
		return fields

	filtered_fields = []
	for field in fields:
		if is_allowed_field(doctype, field):
			filtered_fields.append(field)

	return filtered_fields


def is_allowed_field(doctype, field):
	"""Check if field is valid"""
	if not field:
		return False

	controller = get_controller(doctype)
	dashboard_fields = getattr(controller, "dashboard_fields", ())

	if field in dashboard_fields:
		return True

	if "." in field and is_allowed_linked_field(doctype, field):
		return True

	if isinstance(field, dict) and is_allowed_table_field(doctype, field):
		return True

	if field in [*default_fields, *child_table_fields]:
		return True

	return False


def is_allowed_linked_field(doctype, field):
	linked_field = linked_field_fieldname = None
	if " as " in field:
		linked_field, _ = field.split(" as ")
	else:
		linked_field = field

	linked_field, linked_field_fieldname = linked_field.split(".")
	if not is_allowed_field(doctype, linked_field):
		return False

	linked_field_doctype = jingrow.get_meta(doctype).get_field(linked_field).options
	if not is_allowed_field(linked_field_doctype, linked_field_fieldname):
		return False

	return True


def is_allowed_table_field(doctype, field):
	for table_fieldname, table_fields in field.items():
		if not is_allowed_field(doctype, table_fieldname):
			return False

		table_doctype = jingrow.get_meta(doctype).get_field(table_fieldname).options
		for table_field in table_fields:
			if not is_allowed_field(table_doctype, table_field):
				return False
	return True


def check_permissions(doctype):
	if doctype not in ALLOWED_DOCTYPES:
		raise_not_permitted()

	if not hasattr(jingrow.local, "team") or not jingrow.local.team():
		jingrow.throw(
			"current_team is not set. Use X-PRESS-TEAM header in the request to set it.",
			TeamHeaderNotInRequestError,
		)

	return True


def is_owned_by_team(doctype, docname, raise_exception=True):
	if not jingrow.local.team():
		return False

	docname = cstr(docname)
	owned = jingrow.db.get_value(doctype, docname, "team") == jingrow.local.team().name
	if not owned and raise_exception:
		raise_not_permitted()
	return owned


def raise_not_permitted():
	jingrow.throw("Not permitted", jingrow.PermissionError)


def dashboard_whitelist(allow_guest=False, xss_safe=False, methods=None):
	def wrapper(func):
		global whitelisted_methods

		decorated_func = jingrow.whitelist(allow_guest=allow_guest, xss_safe=xss_safe, methods=methods)(func)

		def inner(*args, **kwargs):
			return decorated_func(*args, **kwargs)

		whitelisted_methods.add(decorated_func)
		return decorated_func

	return wrapper


def fix_args(method, args):
	# This is a fixer function. Certain callers of `run_pg_method`
	# pass duplicates of the passed kwargs in the `args` arg.
	#
	# This causes "got multiple values for argument 'method'"
	if not isinstance(args, dict):
		return args

	# Even if it doesn't match it'll probably throw
	# down the call stack, but in that case it's unexpected
	# behavior and so it's better to error-out.
	if args.get("method") == method:
		del args["method"]

	return args
