# Copyright (c) 2015, JINGROW and Contributors
# License: MIT. See LICENSE

"""build query for doclistview and return results"""

import json
from functools import lru_cache

from sql_metadata import Parser

import jingrow
import jingrow.permissions
from jingrow import _
from jingrow.core.pagetype.access_log.access_log import make_access_log
from jingrow.model import child_table_fields, default_fields, get_permitted_fields, optional_fields
from jingrow.model.base_page import get_controller
from jingrow.model.db_query import DatabaseQuery
from jingrow.model.utils import is_virtual_pagetype
from jingrow.utils import add_user_info, cint, format_duration
from jingrow.utils.data import sbool

DISALLOWED_PARAMS = ("cmd", "data", "ignore_permissions", "view", "user", "csrf_token", "join")


@jingrow.whitelist()
@jingrow.read_only()
def get():
	args = get_form_params()
	# If virtual pagetype, get data from controller get_list method
	if is_virtual_pagetype(args.pagetype):
		controller = get_controller(args.pagetype)
		data = compress(controller.get_list(args))
	else:
		data = compress(execute(**args), args=args)
	return data


@jingrow.whitelist()
@jingrow.read_only()
def get_list():
	args = get_form_params()

	if is_virtual_pagetype(args.pagetype):
		controller = get_controller(args.pagetype)
		data = controller.get_list(args)
	else:
		# uncompressed (refactored from jingrow.model.db_query.get_list)
		data = execute(**args)

	return data


@jingrow.whitelist()
@jingrow.read_only()
def get_count() -> int:
	args = get_form_params()

	if is_virtual_pagetype(args.pagetype):
		controller = get_controller(args.pagetype)
		count = controller.get_count(args)
	else:
		args.distinct = sbool(args.distinct)
		distinct = "distinct " if args.distinct else ""
		args.limit = cint(args.limit)
		fieldname = f"{distinct}`tab{args.pagetype}`.name"
		args.order_by = None

		if args.limit:
			args.fields = [fieldname]
			partial_query = execute(**args, run=0)
			count = jingrow.db.sql(f"""select count(*) from ( {partial_query} ) p""")[0][0]
		else:
			args.fields = [f"count({fieldname}) as total_count"]
			count = execute(**args)[0].get("total_count")

	return count


def execute(pagetype, *args, **kwargs):
	return DatabaseQuery(pagetype).execute(*args, **kwargs)


def get_form_params():
	"""parse GET request parameters."""
	data = jingrow._dict(jingrow.local.form_dict)
	clean_params(data)
	validate_args(data)
	return data


def validate_args(data):
	parse_json(data)
	setup_group_by(data)

	validate_fields(data)
	if data.filters:
		validate_filters(data, data.filters)
	if data.or_filters:
		validate_filters(data, data.or_filters)

	data.strict = None

	return data


def validate_fields(data):
	wildcard = update_wildcard_field_param(data)

	for field in list(data.fields or []):
		fieldname = extract_fieldnames(field)[0]
		if not fieldname:
			raise_invalid_field(fieldname)

		if is_standard(fieldname):
			continue

		meta, df = get_meta_and_pagefield(fieldname, data)

		if not df:
			if wildcard:
				continue
			else:
				raise_invalid_field(fieldname)

		# remove the field from the query if the report hide flag is set and current view is Report
		if df.report_hide and data.view == "Report":
			data.fields.remove(field)
			continue

		if df.fieldname in [_df.fieldname for _df in meta.get_high_permlevel_fields()]:
			if df.get("permlevel") not in meta.get_permlevel_access(parenttype=data.pagetype):
				data.fields.remove(field)


def validate_filters(data, filters):
	if isinstance(filters, list):
		# filters as list
		for condition in filters:
			if len(condition) == 3:
				# [fieldname, condition, value]
				fieldname = condition[0]
				if is_standard(fieldname):
					continue
				meta, df = get_meta_and_pagefield(fieldname, data)
				if not df:
					raise_invalid_field(condition[0])
			else:
				# [pagetype, fieldname, condition, value]
				fieldname = condition[1]
				if is_standard(fieldname):
					continue
				meta = jingrow.get_meta(condition[0])
				if not meta.get_field(fieldname):
					raise_invalid_field(fieldname)

	else:
		for fieldname in filters:
			if is_standard(fieldname):
				continue
			meta, df = get_meta_and_pagefield(fieldname, data)
			if not df:
				raise_invalid_field(fieldname)


def setup_group_by(data):
	"""Add columns for aggregated values e.g. count(name)"""
	if data.group_by and data.aggregate_function:
		if data.aggregate_function.lower() not in ("count", "sum", "avg"):
			jingrow.throw(_("Invalid aggregate function"))

		if jingrow.db.has_column(data.aggregate_on_pagetype, data.aggregate_on_field):
			data.fields.append(
				f"{data.aggregate_function}(`tab{data.aggregate_on_pagetype}`.`{data.aggregate_on_field}`) AS _aggregate_column"
			)
		else:
			raise_invalid_field(data.aggregate_on_field)

		data.pop("aggregate_on_pagetype")
		data.pop("aggregate_on_field")
		data.pop("aggregate_function")


def raise_invalid_field(fieldname):
	jingrow.throw(_("Field not permitted in query") + f": {fieldname}", jingrow.DataError)


def is_standard(fieldname):
	if "." in fieldname:
		fieldname = fieldname.split(".")[1].strip("`")
	return fieldname in default_fields or fieldname in optional_fields or fieldname in child_table_fields


@lru_cache
def extract_fieldnames(field):
	from jingrow.database.schema import SPECIAL_CHAR_PATTERN

	if not SPECIAL_CHAR_PATTERN.findall(field):
		return [field]

	columns = Parser(f"select {field} from _dummy").columns

	if not columns:
		f = field.lower()
		if ("count(" in f or "sum(" in f or "avg(" in f) and "*" in f:
			return ["*"]

	return columns


def get_meta_and_pagefield(fieldname, data):
	parenttype, fieldname = get_parenttype_and_fieldname(fieldname, data)
	meta = jingrow.get_meta(parenttype)
	df = meta.get_field(fieldname)
	return meta, df


def update_wildcard_field_param(data):
	if (isinstance(data.fields, str) and data.fields == "*") or (
		isinstance(data.fields, list | tuple) and len(data.fields) == 1 and data.fields[0] == "*"
	):
		parent_type = data.parenttype or data.parent_pagetype
		data.fields = get_permitted_fields(data.pagetype, parenttype=parent_type, ignore_virtual=True)
		return True

	return False


def clean_params(data):
	for param in DISALLOWED_PARAMS:
		if param in data:
			del data[param]


def parse_json(data):
	if (filters := data.get("filters")) and isinstance(filters, str):
		data["filters"] = json.loads(filters)
	if (applied_filters := data.get("applied_filters")) and isinstance(applied_filters, str):
		data["applied_filters"] = json.loads(applied_filters)
	if (or_filters := data.get("or_filters")) and isinstance(or_filters, str):
		data["or_filters"] = json.loads(or_filters)
	if (fields := data.get("fields")) and isinstance(fields, str):
		data["fields"] = ["*"] if fields == "*" else json.loads(fields)
	if isinstance(data.get("pagestatus"), str):
		data["pagestatus"] = json.loads(data["pagestatus"])
	if isinstance(data.get("save_user_settings"), str):
		data["save_user_settings"] = json.loads(data["save_user_settings"])
	else:
		data["save_user_settings"] = True


def get_parenttype_and_fieldname(field, data):
	if "." in field:
		parts = field.split(".")
		parenttype = parts[0]
		fieldname = parts[1]
		df = jingrow.get_meta(data.pagetype).get_field(parenttype)
		if not df and parenttype.startswith("tab"):
			# tabChild PageType.fieldname
			parenttype = parenttype[3:]
		else:
			# tablefield.fieldname
			parenttype = df.options
	else:
		parenttype = data.pagetype
		fieldname = field.strip("`")

	return parenttype, fieldname


def compress(data, args=None):
	"""separate keys and values"""
	from jingrow.desk.query_report import add_total_row

	user_info = {}

	if not data:
		return data
	if args is None:
		args = {}
	values = []
	keys = list(data[0])
	for row in data:
		values.append([row.get(key) for key in keys])

		# add user info for assignments (avatar)
		if row.get("_assign", ""):
			for user in json.loads(row._assign):
				add_user_info(user, user_info)

	if args.get("add_total_row"):
		meta = jingrow.get_meta(args.pagetype)
		values = add_total_row(values, keys, meta)

	return {"keys": keys, "values": values, "user_info": user_info}


@jingrow.whitelist()
def save_report(name, pagetype, report_settings):
	"""Save reports of type Report Builder from Report View"""

	if jingrow.db.exists("Report", name):
		report = jingrow.get_pg("Report", name)
		if report.is_standard == "Yes":
			jingrow.throw(_("Standard Reports cannot be edited"))

		if report.report_type != "Report Builder":
			jingrow.throw(_("Only reports of type Report Builder can be edited"))

		if report.owner != jingrow.session.user and not report.has_permission("write"):
			jingrow.throw(_("Insufficient Permissions for editing Report"), jingrow.PermissionError)
	else:
		report = jingrow.new_pg("Report")
		report.report_name = name
		report.ref_pagetype = pagetype

	report.report_type = "Report Builder"
	report.json = report_settings
	report.save(ignore_permissions=True)
	jingrow.msgprint(
		_("Report {0} saved").format(jingrow.bold(report.name)),
		indicator="green",
		alert=True,
	)
	return report.name


@jingrow.whitelist()
def delete_report(name):
	"""Delete reports of type Report Builder from Report View"""

	report = jingrow.get_pg("Report", name)
	if report.is_standard == "Yes":
		jingrow.throw(_("Standard Reports cannot be deleted"))

	if report.report_type != "Report Builder":
		jingrow.throw(_("Only reports of type Report Builder can be deleted"))

	if report.owner != jingrow.session.user and not report.has_permission("delete"):
		jingrow.throw(_("Insufficient Permissions for deleting Report"), jingrow.PermissionError)

	report.delete(ignore_permissions=True)
	jingrow.msgprint(
		_("Report {0} deleted").format(jingrow.bold(report.name)),
		indicator="green",
		alert=True,
	)


@jingrow.whitelist()
@jingrow.read_only()
def export_query():
	"""export from report builder"""
	from jingrow.desk.utils import get_csv_bytes, pop_csv_params, provide_binary_file

	form_params = get_form_params()
	form_params["limit_page_length"] = None
	form_params["as_list"] = True
	pagetype = form_params.pop("pagetype")
	if isinstance(form_params["fields"], list):
		form_params["fields"].append("owner")
	elif isinstance(form_params["fields"], tuple):
		form_params["fields"] = form_params["fields"] + ("owner",)
	file_format_type = form_params.pop("file_format_type")
	title = form_params.pop("title", pagetype)
	csv_params = pop_csv_params(form_params)
	add_totals_row = 1 if form_params.pop("add_totals_row", None) == "1" else None
	translate_values = 1 if form_params.pop("translate_values", None) == "1" else None

	if selection := form_params.pop("selected_items", None):
		form_params["filters"] = {"name": ("in", json.loads(selection))}

	make_access_log(
		pagetype=pagetype,
		file_type=file_format_type,
		report_name=form_params.report_name,
		filters=form_params.filters,
	)

	db_query = DatabaseQuery(pagetype)
	ret = db_query.execute(**form_params)

	if not jingrow.permissions.can_export(pagetype):
		if jingrow.permissions.can_export(pagetype, is_owner=True):
			for row in ret:
				if row[-1] != jingrow.session.user:
					raise jingrow.PermissionError(
						_("You are not allowed to export {} pagetype").format(pagetype)
					)
		else:
			raise jingrow.PermissionError(_("You are not allowed to export {} pagetype").format(pagetype))

	if add_totals_row:
		ret = append_totals_row(ret)

	fields_info = get_field_info(db_query.fields, pagetype)

	labels = [info["label"] for info in fields_info]
	data = [[_("Sr"), *labels]]
	processed_data = []

	if jingrow.local.lang == "en" or not translate_values:
		data.extend([i + 1, *list(row)] for i, row in enumerate(ret))
	elif translate_values:
		translatable_fields = [field["translatable"] for field in fields_info]
		processed_data = []
		for i, row in enumerate(ret):
			processed_row = [i + 1] + [
				_(value) if translatable_fields[idx] else value for idx, value in enumerate(row)
			]
			processed_data.append(processed_row)
		data.extend(processed_data)

	data = handle_duration_fieldtype_values(pagetype, data, db_query.fields)

	if file_format_type == "CSV":
		from jingrow.utils.xlsxutils import handle_html

		file_extension = "csv"
		content = get_csv_bytes(
			[[handle_html(jingrow.as_unicode(v)) if isinstance(v, str) else v for v in r] for r in data],
			csv_params,
		)
	elif file_format_type == "Excel":
		from jingrow.utils.xlsxutils import make_xlsx

		file_extension = "xlsx"
		content = make_xlsx(data, pagetype).getvalue()

	provide_binary_file(title, file_extension, content)


def append_totals_row(data):
	if not data:
		return data
	data = list(data)
	totals = []
	totals.extend([""] * len(data[0]))

	for row in data:
		for i in range(len(row)):
			if isinstance(row[i], float | int):
				totals[i] = (totals[i] or 0) + row[i]

	if not isinstance(totals[0], int | float):
		totals[0] = "Total"

	data.append(totals)

	return data


def get_field_info(fields, pagetype):
	"""Get column names, labels, field types, and translatable properties based on column names."""

	field_info = []
	for key in fields:
		df = None
		try:
			parenttype, fieldname = parse_field(key)
		except ValueError:
			# handles aggregate functions
			parenttype = pagetype
			fieldname = key.split("(", 1)[0]
			fieldname = fieldname[0].upper() + fieldname[1:]

		parenttype = parenttype or pagetype

		if parenttype == pagetype and fieldname == "name":
			name = fieldname
			label = _("ID", context="Label of name column in report")
			fieldtype = "Data"
			translatable = True
		else:
			df = jingrow.get_meta(parenttype).get_field(fieldname)
			if df and df.fieldtype in ("Data", "Select", "Small Text", "Text"):
				name = df.name
				label = _(df.label)
				fieldtype = df.fieldtype
				translatable = getattr(df, "translatable", False)
			elif df and df.fieldtype == "Link" and jingrow.get_meta(df.options).translated_pagetype:
				name = df.name
				label = _(df.label)
				fieldtype = df.fieldtype
				translatable = True
			else:
				name = fieldname
				label = _(df.label) if df else _(fieldname)
				fieldtype = "Data"
				translatable = False

			if parenttype != pagetype:
				# If the column is from a child table, append the child pagetype.
				# For example, "Item Code (Sales Invoice Item)".
				label += f" ({ _(parenttype) })"

		field_info.append(
			{"name": name, "label": label, "fieldtype": fieldtype, "translatable": translatable}
		)

	return field_info


def handle_duration_fieldtype_values(pagetype, data, fields):
	for field in fields:
		try:
			parenttype, fieldname = parse_field(field)
		except ValueError:
			continue

		parenttype = parenttype or pagetype
		df = jingrow.get_meta(parenttype).get_field(fieldname)

		if df and df.fieldtype == "Duration":
			index = fields.index(field) + 1
			for i in range(1, len(data)):
				val_in_seconds = data[i][index]
				if val_in_seconds:
					duration_val = format_duration(val_in_seconds, df.hide_days)
					data[i][index] = duration_val
	return data


def parse_field(field: str) -> tuple[str | None, str]:
	"""Parse a field into parenttype and fieldname."""
	key = field.split(" as ", 1)[0]

	if key.startswith(("count(", "sum(", "avg(")):
		raise ValueError

	if "." in key:
		table, column = key.split(".", 2)[:2]
		return table[4:-1], column.strip("`")

	return None, key.strip("`")


@jingrow.whitelist()
def delete_items():
	"""delete selected items"""
	import json

	items = sorted(json.loads(jingrow.form_dict.get("items")), reverse=True)
	pagetype = jingrow.form_dict.get("pagetype")

	if len(items) > 10:
		jingrow.enqueue("jingrow.desk.reportview.delete_bulk", pagetype=pagetype, items=items)
	else:
		delete_bulk(pagetype, items)


def delete_bulk(pagetype, items):
	undeleted_items = []
	for i, d in enumerate(items):
		try:
			jingrow.delete_pg(pagetype, d)
			if len(items) >= 5:
				jingrow.publish_realtime(
					"progress",
					dict(
						progress=[i + 1, len(items)], title=_("Deleting {0}").format(pagetype), description=d
					),
					user=jingrow.session.user,
				)
			# Commit after successful deletion
			jingrow.db.commit()
		except Exception:
			# rollback if any record failed to delete
			# if not rollbacked, queries get committed on after_request method in app.py
			undeleted_items.append(d)
			jingrow.db.rollback()
	if undeleted_items and len(items) != len(undeleted_items):
		jingrow.clear_messages()
		delete_bulk(pagetype, undeleted_items)
	elif undeleted_items:
		jingrow.msgprint(
			_("Failed to delete {0} documents: {1}").format(len(undeleted_items), ", ".join(undeleted_items)),
			realtime=True,
			title=_("Bulk Operation Failed"),
		)
	else:
		jingrow.msgprint(
			_("Deleted all documents successfully"), realtime=True, title=_("Bulk Operation Successful")
		)


@jingrow.whitelist()
@jingrow.read_only()
def get_sidebar_stats(stats, pagetype, filters=None):
	if filters is None:
		filters = []

	if is_virtual_pagetype(pagetype):
		controller = get_controller(pagetype)
		args = {"stats": stats, "filters": filters}
		data = controller.get_stats(args)
	else:
		data = get_stats(stats, pagetype, filters)

	return {"stats": data}


@jingrow.whitelist()
@jingrow.read_only()
def get_stats(stats, pagetype, filters=None):
	"""get tag info"""
	import json

	if filters is None:
		filters = []
	columns = json.loads(stats)
	if filters:
		filters = json.loads(filters)
	results = {}

	try:
		db_columns = jingrow.db.get_table_columns(pagetype)
	except (jingrow.db.InternalError, jingrow.db.ProgrammingError):
		# raised when _user_tags column is added on the fly
		# raised if its a virtual pagetype
		db_columns = []

	for column in columns:
		if column not in db_columns:
			continue
		try:
			tag_count = jingrow.get_list(
				pagetype,
				fields=[column, "count(*)"],
				filters=[*filters, [column, "!=", ""]],
				group_by=column,
				as_list=True,
				distinct=1,
			)

			if column == "_user_tags":
				results[column] = scrub_user_tags(tag_count)
				no_tag_count = jingrow.get_list(
					pagetype,
					fields=[column, "count(*)"],
					filters=[*filters, [column, "in", ("", ",")]],
					as_list=True,
					group_by=column,
					order_by=column,
				)

				no_tag_count = no_tag_count[0][1] if no_tag_count else 0

				results[column].append([_("No Tags"), no_tag_count])
			else:
				results[column] = tag_count

		except jingrow.db.SQLError:
			pass
		except jingrow.db.InternalError:
			# raised when _user_tags column is added on the fly
			pass

	return results


@jingrow.whitelist()
def get_filter_dashboard_data(stats, pagetype, filters=None):
	"""get tags info"""
	import json

	tags = json.loads(stats)
	filters = json.loads(filters or [])
	stats = {}

	columns = jingrow.db.get_table_columns(pagetype)
	for tag in tags:
		if tag["name"] not in columns:
			continue
		tagcount = []
		if tag["type"] not in ["Date", "Datetime"]:
			tagcount = jingrow.get_list(
				pagetype,
				fields=[tag["name"], "count(*)"],
				filters=[*filters, "ifnull(`%s`,'')!=''" % tag["name"]],
				group_by=tag["name"],
				as_list=True,
			)

		if tag["type"] not in [
			"Check",
			"Select",
			"Date",
			"Datetime",
			"Int",
			"Float",
			"Currency",
			"Percent",
		] and tag["name"] not in ["pagestatus"]:
			stats[tag["name"]] = list(tagcount)
			if stats[tag["name"]]:
				data = [
					"No Data",
					jingrow.get_list(
						pagetype,
						fields=[tag["name"], "count(*)"],
						filters=[*filters, "({0} = '' or {0} is null)".format(tag["name"])],
						as_list=True,
					)[0][1],
				]
				if data and data[1] != 0:
					stats[tag["name"]].append(data)
		else:
			stats[tag["name"]] = tagcount

	return stats


def scrub_user_tags(tagcount):
	"""rebuild tag list for tags"""
	rdict = {}
	tagdict = dict(tagcount)
	for t in tagdict:
		if not t:
			continue
		alltags = t.split(",")
		for tag in alltags:
			if tag:
				if tag not in rdict:
					rdict[tag] = 0

				rdict[tag] += tagdict[t]

	return [[tag, rdict[tag]] for tag in rdict]


# used in building query in queries.py
def get_match_cond(pagetype, as_condition=True):
	cond = DatabaseQuery(pagetype).build_match_conditions(as_condition=as_condition)
	if not as_condition:
		return cond

	return ((" and " + cond) if cond else "").replace("%", "%%")


def build_match_conditions(pagetype, user=None, as_condition=True):
	match_conditions = DatabaseQuery(pagetype, user=user).build_match_conditions(as_condition=as_condition)
	if as_condition:
		return match_conditions.replace("%", "%%")
	return match_conditions


def get_filters_cond(pagetype, filters, conditions, ignore_permissions=None, with_match_conditions=False):
	if isinstance(filters, str):
		filters = json.loads(filters)

	if filters:
		flt = filters
		if isinstance(filters, dict):
			filters = filters.items()
			flt = []
			for f in filters:
				if isinstance(f[1], str) and f[1][0] == "!":
					flt.append([pagetype, f[0], "!=", f[1][1:]])
				elif isinstance(f[1], list | tuple) and f[1][0].lower() in (
					"=",
					">",
					"<",
					">=",
					"<=",
					"!=",
					"like",
					"not like",
					"in",
					"not in",
					"between",
					"is",
				):
					flt.append([pagetype, f[0], f[1][0], f[1][1]])
				else:
					flt.append([pagetype, f[0], "=", f[1]])

		query = DatabaseQuery(pagetype)
		query.filters = flt
		query.conditions = conditions

		if with_match_conditions:
			query.build_match_conditions()

		query.build_filter_conditions(flt, conditions, ignore_permissions)

		cond = " and " + " and ".join(query.conditions)
	else:
		cond = ""
	return cond
